Skip to content

Commit

Permalink
Fix high memory consumption (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Feb 14, 2025
1 parent a8e1d7f commit ccec591
Show file tree
Hide file tree
Showing 30 changed files with 408 additions and 363 deletions.
5 changes: 3 additions & 2 deletions src/Parlot/Fluent/Between.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public Between(Parser<A> before, Parser<T> parser, Parser<B> after)
ExpectedChars = seekable.ExpectedChars;
SkipWhitespace = seekable.SkipWhitespace;
}

Name = $"Between({before.Name},{parser.Name},{after.Name})";
}

public bool CanSeek { get; }
Expand Down Expand Up @@ -144,4 +142,7 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => Name ?? $"Between({_before},{_parser},{_after})";

}
3 changes: 2 additions & 1 deletion src/Parlot/Fluent/Capture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public sealed class Capture<T> : Parser<TextSpan>, ICompilable
public Capture(Parser<T> parser)
{
_parser = parser;
Name = $"{parser.Name} (Capture)";
}

public override bool Parse(ParseContext context, ref ParseResult<TextSpan> result)
Expand Down Expand Up @@ -87,4 +86,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parser} (Capture)";
}
11 changes: 3 additions & 8 deletions src/Parlot/Fluent/CharLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public sealed class CharLiteral : Parser<char>, ICompilable, ISeekable
public CharLiteral(char c)
{
Char = c;
ExpectedChars = [c];
Name = $"Char('{c}')";
ExpectedChars = new[] { c };
}

public char Char { get; }
Expand Down Expand Up @@ -45,12 +44,6 @@ public CompilationResult Compile(CompilationContext context)
{
var result = context.CreateCompilationResult<char>();

// if (context.Scanner.ReadChar(Char))
// {
// success = true;
// value = Char;
// }

result.Body.Add(
Expression.IfThen(
context.ReadChar(Char),
Expand All @@ -65,4 +58,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"Char('{Char}')";
}
27 changes: 25 additions & 2 deletions src/Parlot/Fluent/Deferred.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Parlot.Compilation;
using Parlot.Rewriting;
using System;

#if NET
using System.Linq;
#endif
Expand All @@ -19,7 +20,6 @@ public Parser<T>? Parser
set
{
_parser = value ?? throw new ArgumentNullException(nameof(value));
Name = $"{_parser.Name} (Deferred)";
}
}

Expand All @@ -31,7 +31,6 @@ public Parser<T>? Parser

public Deferred()
{
Name = "Deferred";
}

public Deferred(Func<Deferred<T>, Parser<T>> parser) : this()
Expand Down Expand Up @@ -149,4 +148,28 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

private bool _toString;

public override string ToString()
{
// Handle recursion

lock (this)
{
if (!_toString)
{
_toString = true;
var result = Name == null
? $"{Parser} (Deferred)"
: $"{Name} (Deferred)";
_toString = false;
return result;
}
else
{
return "(Deferred)";
}
}
}
}
4 changes: 2 additions & 2 deletions src/Parlot/Fluent/Discard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public Discard(Parser<T> parser, U value)
{
_parser = parser;
_value = value;

Name = $"{parser.Name} (Discard)";
}

public override bool Parse(ParseContext context, ref ParseResult<U> result)
Expand Down Expand Up @@ -60,4 +58,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parser} (Discard)";
}
4 changes: 2 additions & 2 deletions src/Parlot/Fluent/Else.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public Else(Parser<T> parser, T value)
ExpectedChars = seekable.ExpectedChars;
SkipWhitespace = seekable.SkipWhitespace;
}

Name = $"{parser.Name} (Else)";
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down Expand Up @@ -81,4 +79,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parser} (Else)";
}
3 changes: 2 additions & 1 deletion src/Parlot/Fluent/Eof.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public sealed class Eof<T> : Parser<T>, ICompilable
public Eof(Parser<T> parser)
{
_parser = parser;
Name = $"{parser.Name} (Eof)";
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down Expand Up @@ -62,4 +61,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parser} (Eof)";
}
12 changes: 6 additions & 6 deletions src/Parlot/Fluent/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public ElseError(Parser<T> parser, string message)
ExpectedChars = seekable.ExpectedChars;
SkipWhitespace = seekable.SkipWhitespace;
}

Name = $"{parser.Name} (ElseError)";
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down Expand Up @@ -90,6 +88,8 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parser} (ElseError)";
}

public sealed class Error<T> : Parser<T>, ICompilable
Expand All @@ -101,8 +101,6 @@ public Error(Parser<T> parser, string message)
{
_parser = parser ?? throw new ArgumentNullException(nameof(parser));
_message = message;

Name = $"{parser.Name} (Error)";
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down Expand Up @@ -149,6 +147,8 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parser} (Error)";
}

public sealed class Error<T, U> : Parser<U>, ICompilable, ISeekable
Expand All @@ -173,8 +173,6 @@ public Error(Parser<T> parser, string message)
ExpectedChars = seekable.ExpectedChars;
SkipWhitespace = seekable.SkipWhitespace;
}

Name = $"{parser.Name} (Error)";
}

public override bool Parse(ParseContext context, ref ParseResult<U> result)
Expand Down Expand Up @@ -222,4 +220,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parser} (Error)";
}
4 changes: 2 additions & 2 deletions src/Parlot/Fluent/If.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public If(Parser<T> parser, Func<C, S?, bool> predicate, S? state)
_predicate = predicate ?? throw new ArgumentNullException(nameof(predicate));
_state = state;
_parser = parser ?? throw new ArgumentNullException(nameof(parser));

Name = $"{parser.Name} (If)";
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down Expand Up @@ -107,4 +105,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parser} (If)";
}
4 changes: 2 additions & 2 deletions src/Parlot/Fluent/ListOfCharsLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public ListOfChars(string values, int minSize = 1, int maxSize = 0)
ExpectedChars = values.ToCharArray();
_minSize = minSize;
_maxSize = maxSize;

Name = $"AnyOf({values})";
}

public override bool Parse(ParseContext context, ref ParseResult<TextSpan> result)
Expand Down Expand Up @@ -77,5 +75,7 @@ public override bool Parse(ParseContext context, ref ParseResult<TextSpan> resul
context.ExitParser(this);
return true;
}

public override string ToString() => $"AnyOf([{string.Join(", ", ExpectedChars)}])";
}
#endif
4 changes: 2 additions & 2 deletions src/Parlot/Fluent/Not.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public sealed class Not<T> : Parser<T>, ICompilable
public Not(Parser<T> parser)
{
_parser = parser ?? throw new ArgumentNullException(nameof(parser));

Name = $"Not ({parser.Name}";
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down Expand Up @@ -71,4 +69,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"Not ({_parser})";
}
4 changes: 2 additions & 2 deletions src/Parlot/Fluent/OneOf.ABT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public OneOf(Parser<A> parserA, Parser<B> parserB)
{
_parserA = parserA ?? throw new ArgumentNullException(nameof(parserA));
_parserB = parserB ?? throw new ArgumentNullException(nameof(parserB));

Name = $"OneOf ({parserA.Name}, {parserB.Name})";
}

public override bool Parse(ParseContext context, ref ParseResult<T> result)
Expand Down Expand Up @@ -105,4 +103,6 @@ public CompilationResult Compile(CompilationContext context)

return result;
}

public override string ToString() => $"{_parserA} | {_parserB}";
}
Loading

0 comments on commit ccec591

Please sign in to comment.