Skip to content

Commit

Permalink
Fix CommentParser eol issue sprache#184
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-hassan committed Dec 14, 2021
1 parent 9d1721b commit 86db56d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Sprache/CommentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Parser<string> SingleLineComment
throw new ParseException("Field 'Single' is null; single-line comments not allowed.");

return from first in Parse.String(Single)
from rest in Parse.CharExcept(NewLine).Many().Text()
from rest in Parse.CharExcept(new[] {'\r', '\n' }).Many().Text()
select rest;
}
private set { }
Expand Down
35 changes: 35 additions & 0 deletions test/Sprache.Tests/CommentParserTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Sprache.Tests
{
public class CommentParserTest
{
[Fact]
public void Check_single_line_comment_for_both_eol_windows_linux()
{
var parser = new CommentParser();
var expected = "this is a line comment";
var result = parser.SingleLineComment.Parse("//this is a line comment\r\nint i=5;");
Assert.Equal(result, expected);
result = parser.SingleLineComment.Parse("//this is a line comment\nint i=5;");
Assert.Equal(result, expected);

}
[Fact]
public void Check_any_comment_for_both_eol_windows_linux()
{
var parser = new CommentParser();
var expected = "this is a line comment";
var result = parser.AnyComment.Parse("//this is a line comment\r\nint i=5;");
Assert.Equal(result, expected);
result = parser.AnyComment.Parse("//this is a line comment\nint i=5;");
Assert.Equal(result, expected);

}
}
}

0 comments on commit 86db56d

Please sign in to comment.