Parse multi-line comments
This commit is contained in:
parent
578d590bed
commit
e54c01a243
@ -234,5 +234,22 @@ namespace Lexer.Tests
|
|||||||
Assert.AreEqual(2, tokens.Count);
|
Assert.AreEqual(2, tokens.Count);
|
||||||
Assert.AreEqual(TokenKind.NumberLiteral, tokens[0].Kind);
|
Assert.AreEqual(TokenKind.NumberLiteral, tokens[0].Kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase("%{\nabc\n%}", true)]
|
||||||
|
[TestCase("%{ a\nabc\n%}", false)]
|
||||||
|
[TestCase("if %{\nabc\n%}", false)]
|
||||||
|
public void ParseMultilineComments(string text, bool isMultiline)
|
||||||
|
{
|
||||||
|
var sut = CreateLexer(text);
|
||||||
|
var tokens = sut.ParseAll();
|
||||||
|
if (isMultiline)
|
||||||
|
{
|
||||||
|
Assert.AreEqual(1, tokens.Count);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Less(1, tokens.Count);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -27,6 +27,10 @@ namespace Lexer
|
|||||||
|
|
||||||
private Trivia LexComment()
|
private Trivia LexComment()
|
||||||
{
|
{
|
||||||
|
if (TokensSinceNewLine == 0 && Window.PeekChar(1) == '{')
|
||||||
|
{
|
||||||
|
return LexMultilineComment();
|
||||||
|
}
|
||||||
var n = 1;
|
var n = 1;
|
||||||
while (!IsEolOrEof(Window.PeekChar(n)))
|
while (!IsEolOrEof(Window.PeekChar(n)))
|
||||||
{
|
{
|
||||||
@ -36,6 +40,52 @@ namespace Lexer
|
|||||||
return new Trivia(TriviaType.Comment, Window.GetAndConsumeChars(n));
|
return new Trivia(TriviaType.Comment, Window.GetAndConsumeChars(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Trivia LexMultilineComment()
|
||||||
|
{
|
||||||
|
var n = 2;
|
||||||
|
var metPercentSign = false;
|
||||||
|
var atFirstLine = true;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
var c = Window.PeekChar(n);
|
||||||
|
if (c == '\0')
|
||||||
|
{
|
||||||
|
throw new ParsingException($"Unexpected end of file while parsing multi-line comment.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == '\n')
|
||||||
|
{
|
||||||
|
atFirstLine = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (atFirstLine && !IsWhitespace(c)) // this is a one-line comment
|
||||||
|
{
|
||||||
|
while (!IsEolOrEof(Window.PeekChar(n)))
|
||||||
|
{
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Trivia(TriviaType.Comment, Window.GetAndConsumeChars(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (metPercentSign && c == '}')
|
||||||
|
{
|
||||||
|
return new Trivia(TriviaType.MultiLineComment, Window.GetAndConsumeChars(n+1));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c == '%')
|
||||||
|
{
|
||||||
|
metPercentSign = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
metPercentSign = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private List<Trivia> LexCommentAfterDotDotDot()
|
private List<Trivia> LexCommentAfterDotDotDot()
|
||||||
{
|
{
|
||||||
var n = 0;
|
var n = 0;
|
||||||
|
@ -52,8 +52,19 @@
|
|||||||
|
|
||||||
public void ConsumeChars(int n)
|
public void ConsumeChars(int n)
|
||||||
{
|
{
|
||||||
|
for (var i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
if (PeekChar(i) == '\n' || PeekChar(i) == '\r')
|
||||||
|
{
|
||||||
|
_position.Line++;
|
||||||
|
_position.Column = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_position.Column++;
|
||||||
|
}
|
||||||
|
}
|
||||||
Offset += n;
|
Offset += n;
|
||||||
_position.Column += n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public char GetAndConsumeChar()
|
public char GetAndConsumeChar()
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
{
|
{
|
||||||
Whitespace,
|
Whitespace,
|
||||||
NewLine,
|
NewLine,
|
||||||
Comment
|
Comment,
|
||||||
|
MultiLineComment
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user