From e02eb39753851453d12ec24a180a5e86b8ba0c0b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 26 Nov 2018 13:42:50 +0100 Subject: [PATCH] Support for comments --- Upsilon/Parser/Lexer.cs | 22 +++++++++++++++++++++- Upsilon/Parser/Parser.cs | 16 ++++++++++++---- Upsilon/Parser/SyntaxKind.cs | 3 ++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Upsilon/Parser/Lexer.cs b/Upsilon/Parser/Lexer.cs index 6d33a5e..6d4f2ce 100644 --- a/Upsilon/Parser/Lexer.cs +++ b/Upsilon/Parser/Lexer.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Immutable; using System.Text; using Upsilon.Text; @@ -57,6 +56,11 @@ namespace Upsilon.Parser case '+': return new SyntaxToken(SyntaxKind.Plus, _position, "+", null); case '-': + if (Next == '-') + { + _position++; + return LexComments(); + } return new SyntaxToken(SyntaxKind.Minus, _position, "-", null); case '*': return new SyntaxToken(SyntaxKind.Star, _position, "*", null); @@ -179,5 +183,21 @@ namespace Upsilon.Parser } return new SyntaxToken(kind, start, str, null); } + + private SyntaxToken LexComments() + { + var start = _position; + var stringBuilder = new StringBuilder(); + if (Current != ' ') + stringBuilder.Append(Current); + while (Next != '\n' && Next != '\0') + { + stringBuilder.Append(Next); + _position++; + } + + var str = stringBuilder.ToString(); + return new SyntaxToken(SyntaxKind.Comment, start, str, str); + } } } \ No newline at end of file diff --git a/Upsilon/Parser/Parser.cs b/Upsilon/Parser/Parser.cs index d0a4282..c5a3627 100644 --- a/Upsilon/Parser/Parser.cs +++ b/Upsilon/Parser/Parser.cs @@ -27,11 +27,19 @@ namespace Upsilon.Parser private SyntaxToken Current => Get(0); private SyntaxToken Next => Get(1); - private SyntaxToken Get(int offset) + private SyntaxToken Get(int offset, bool allowComment = false) { - return _position + offset >= _tokens.Length - ? new SyntaxToken(SyntaxKind.EndOfFile, _position + offset, "\0", null) - : _tokens[_position + offset]; + if (_position + offset >= _tokens.Length) + return new SyntaxToken(SyntaxKind.EndOfFile, _position + offset, "\0", null); + else + { + var token = _tokens[_position + offset]; + if (token.Kind == SyntaxKind.Comment && !allowComment) + { + return Get(offset + 1); + } + return token; + } } private SyntaxToken NextToken() diff --git a/Upsilon/Parser/SyntaxKind.cs b/Upsilon/Parser/SyntaxKind.cs index 0241344..a4dac1b 100644 --- a/Upsilon/Parser/SyntaxKind.cs +++ b/Upsilon/Parser/SyntaxKind.cs @@ -7,6 +7,7 @@ namespace Upsilon.Parser EndOfFile, WhiteSpace, BadToken, + Comment, // tokens Number, @@ -80,6 +81,6 @@ namespace Upsilon.Parser TableAssignmentStatement, NumericForStatement, BreakStatement, - GenericForStatement + GenericForStatement, } } \ No newline at end of file