Skip to content

Commit

Permalink
Use Parser.SkipWhiteSpace in SkipWhiteSpace (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored May 4, 2021
1 parent 74363cb commit a08477a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Parlot/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public static bool IsWhiteSpace(char ch)
}

public static bool IsWhiteSpaceOrNewLine(char ch)
=> (ch == '\n') || (ch == '\r') || (ch == '\v') || IsWhiteSpace(ch);
=> IsNewLine(ch) || IsWhiteSpace(ch);

public static bool IsNewLine(char ch)
=> (ch == '\n') || (ch == '\r') || (ch == '\v');

public static char ScanHexEscape(string text, int index, out int length)
{
Expand Down
1 change: 0 additions & 1 deletion src/Parlot/Fluent/ParseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public ParseContext(Scanner scanner, bool useNewLines = false)

/// <summary>
/// The parser that is used to parse whitespaces and comments.
/// This can also include comments.
/// </summary>
public Parser<TextSpan> WhiteSpaceParser { get; set;}

Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/SkipWhiteSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public CompilationResult Compile(CompilationContext context)
result.Body.Add(
Expression.Block(
parserCompileResult.Variables,
context.SkipWhiteSpace(),
context.ParserSkipWhiteSpace(),
Expression.Block(parserCompileResult.Body),
Expression.IfThenElse(
parserCompileResult.Success,
Expand Down
26 changes: 25 additions & 1 deletion test/Parlot.Tests/CompileTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Parlot.Fluent;
using System;
using System.Collections.Generic;
using Xunit;
using static Parlot.Fluent.Parsers;
Expand Down Expand Up @@ -541,5 +540,30 @@ public void SkipWhiteSpaceCompiledShouldResetPosition()
Assert.True(parser.TryParse(" ab", out var result1));
Assert.Equal(" ab", result1);
}

[Fact]
public void SkipWhiteSpaceCompiledShouldResponseParseContextUseNewLines()
{
// Default behavior, newlines are skipped like any other space. The grammar is not "New Line Aware"

Assert.True(
SkipWhiteSpace(Literals.Text("ab")).Compile()
.TryParse(new ParseContext(new Scanner(" \nab"), useNewLines: false),
out var _, out var _));

// Here newlines are not skipped

Assert.False(
SkipWhiteSpace(Literals.Text("ab")).Compile()
.TryParse(new ParseContext(new Scanner(" \nab"), useNewLines: true),
out var _, out var _));

// Here newlines are not skipped, and the grammar reads them explicitly

Assert.True(
SkipWhiteSpace(Literals.WhiteSpace(includeNewLines: true).SkipAnd(Literals.Text("ab"))).Compile()
.TryParse(new ParseContext(new Scanner(" \nab"), useNewLines: true),
out var _, out var _));
}
}
}
25 changes: 25 additions & 0 deletions test/Parlot.Tests/FluentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,5 +614,30 @@ public void SkipWhiteSpaceShouldResetPosition()
Assert.True(parser.TryParse(" ab", out var result1));
Assert.Equal(" ab", result1);
}

[Fact]
public void SkipWhiteSpaceShouldResponseParseContextUseNewLines()
{
// Default behavior, newlines are skipped like any other space. The grammar is not "New Line Aware"

Assert.True(
SkipWhiteSpace(Literals.Text("ab"))
.TryParse(new ParseContext(new Scanner(" \nab"), useNewLines: false),
out var _, out var _));

// Here newlines are not skipped

Assert.False(
SkipWhiteSpace(Literals.Text("ab"))
.TryParse(new ParseContext(new Scanner(" \nab"), useNewLines: true),
out var _, out var _));

// Here newlines are not skipped, and the grammar reads them explicitly

Assert.True(
SkipWhiteSpace(Literals.WhiteSpace(includeNewLines: true).SkipAnd(Literals.Text("ab")))
.TryParse(new ParseContext(new Scanner(" \nab"), useNewLines: true),
out var _, out var _));
}
}
}

0 comments on commit a08477a

Please sign in to comment.