Separate new lines trivia

This commit is contained in:
Alexander Luzgarev 2018-04-04 20:21:50 +02:00
parent b695f4961a
commit 4c2f37b000
7 changed files with 46 additions and 10 deletions

View File

@ -15,7 +15,7 @@ namespace Parser.Tests
[Test] [Test]
public void ParseSequenceOfIdentifiers() public void ParseSequenceOfIdentifiers()
{ {
var sut = CreateLexer("undefined is not a function"); var sut = CreateLexer("undefined is not\n a function");
var tokens = sut.ParseAll(); var tokens = sut.ParseAll();
Assert.AreEqual(6, tokens.Count); Assert.AreEqual(6, tokens.Count);
CollectionAssert.AreEqual( CollectionAssert.AreEqual(

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
namespace Lexer namespace Lexer
@ -7,6 +8,7 @@ namespace Lexer
{ {
private ITextWindow Window { get; } private ITextWindow Window { get; }
private Token LastToken { get; set; } private Token LastToken { get; set; }
private int TokensSinceNewLine { get; set; }
private PureTokenFactory PureTokenFactory { get; } private PureTokenFactory PureTokenFactory { get; }
public MLexer(ITextWindow window, PureTokenFactory pureTokenFactory) public MLexer(ITextWindow window, PureTokenFactory pureTokenFactory)
@ -67,16 +69,19 @@ namespace Lexer
break; break;
case '\r': case '\r':
case '\n': case '\n':
if (whiteSpaceCache.Length > 0)
{
triviaList.Add(new Trivia(TriviaType.Whitespace, whiteSpaceCache.ToString()));
}
whiteSpaceCache.Clear();
Window.ConsumeChar(); Window.ConsumeChar();
whiteSpaceCache.Append(character); triviaList.Add(new Trivia(TriviaType.NewLine, character.ToString()));
var whiteSpace = whiteSpaceCache.ToString();
triviaList.Add(new Trivia(TriviaType.Whitespace, whiteSpace));
if (isTrailing) if (isTrailing)
{ {
return triviaList; return triviaList;
} }
whiteSpaceCache.Clear();
break; break;
case '%': case '%':
if (whiteSpaceCache.Length > 0) if (whiteSpaceCache.Length > 0)
@ -595,6 +600,14 @@ namespace Lexer
var leadingTrivia = LexTrivia(false); var leadingTrivia = LexTrivia(false);
var token = LexTokenWithoutTrivia(leadingTrivia); var token = LexTokenWithoutTrivia(leadingTrivia);
var trailingTrivia = LexTrivia(true); var trailingTrivia = LexTrivia(true);
if (trailingTrivia.Where(t => t.Type == TriviaType.NewLine).Any())
{
TokensSinceNewLine = 0;
}
else
{
TokensSinceNewLine++;
}
var result = new Token(token, leadingTrivia, trailingTrivia); var result = new Token(token, leadingTrivia, trailingTrivia);
LastToken = result; LastToken = result;

View File

@ -12,11 +12,13 @@
private static readonly string[] PureTokenOfKind = private static readonly string[] PureTokenOfKind =
{ {
null, // None = 0, null, // None = 0,
null, // Identifier = 1, null, // EndOfFile = 1,
null, // NumberLiteral = 2, null, // Identifier = 2,
null, // StringLiteral = 3, null, // NumberLiteral = 3,
null, // DoubleQuotedStringLiteral = 4, null, // StringLiteral = 4,
null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, // DoubleQuotedStringLiteral = 5,
null, // UnquotedStringLiteral = 6
null, null, null, null, null, null, null, null, null, null, null, null, null,
"=", // Assignment = 20, "=", // Assignment = 20,
"==", // Equality = 21, "==", // Equality = 21,
"~=", // Inequality = 22, "~=", // Inequality = 22,

View File

@ -8,6 +8,7 @@
NumberLiteral = 3, NumberLiteral = 3,
StringLiteral = 4, StringLiteral = 4,
DoubleQuotedStringLiteral = 5, DoubleQuotedStringLiteral = 5,
UnquotedStringLiteral = 6,
Assignment = 20, Assignment = 20,
Equality = 21, Equality = 21,

View File

@ -3,6 +3,7 @@
public enum TriviaType public enum TriviaType
{ {
Whitespace, Whitespace,
NewLine,
Comment Comment
} }
} }

View File

@ -449,5 +449,6 @@ namespace Parser.Tests
Assert.IsInstanceOf<TryCatchStatementNode>(actual); Assert.IsInstanceOf<TryCatchStatementNode>(actual);
Assert.AreEqual(text, actual.FullText); Assert.AreEqual(text, actual.FullText);
} }
} }
} }

View File

@ -335,6 +335,24 @@ namespace Parser
} }
} }
public class UnquotedStringLiteralNode : ExpressionNode
{
public Token Token { get; }
public UnquotedStringLiteralNode(Token token) : base(null)
{
Token = token;
}
public override string FullText => Token.FullText;
public override IEnumerable<Token> ChildTokens
{
get { yield return Token; }
}
}
public class StatementNode : SyntaxNode public class StatementNode : SyntaxNode
{ {
public TokenNode SemicolonOrComma { get; set; } public TokenNode SemicolonOrComma { get; set; }