Support for comments

This commit is contained in:
Deukhoofd 2018-11-26 13:42:50 +01:00
parent 2ee8170f74
commit e02eb39753
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 35 additions and 6 deletions

View File

@ -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);
}
}
}

View File

@ -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()

View File

@ -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,
}
}