Better comments

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

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Text; using System.Text;
using Upsilon.Text; using Upsilon.Text;
@ -34,6 +35,8 @@ namespace Upsilon.Parser
var next = LexNext(); var next = LexNext();
if (next.Kind != SyntaxKind.WhiteSpace) if (next.Kind != SyntaxKind.WhiteSpace)
{ {
if (next.Kind == SyntaxKind.Comment)
continue;
array.Add(next); array.Add(next);
if (next.Kind == SyntaxKind.EndOfFile) if (next.Kind == SyntaxKind.EndOfFile)
break; break;
@ -186,6 +189,7 @@ namespace Upsilon.Parser
private SyntaxToken LexComments() private SyntaxToken LexComments()
{ {
_position++;
var start = _position; var start = _position;
var stringBuilder = new StringBuilder(); var stringBuilder = new StringBuilder();
if (Current != ' ') if (Current != ' ')
@ -195,8 +199,8 @@ namespace Upsilon.Parser
stringBuilder.Append(Next); stringBuilder.Append(Next);
_position++; _position++;
} }
var str = stringBuilder.ToString(); var str = stringBuilder.ToString();
_position++;
return new SyntaxToken(SyntaxKind.Comment, start, str, str); return new SyntaxToken(SyntaxKind.Comment, start, str, str);
} }
} }

View File

@ -27,18 +27,13 @@ namespace Upsilon.Parser
private SyntaxToken Current => Get(0); private SyntaxToken Current => Get(0);
private SyntaxToken Next => Get(1); private SyntaxToken Next => Get(1);
private SyntaxToken Get(int offset, bool allowComment = false) private SyntaxToken Get(int offset)
{ {
if (_position + offset >= _tokens.Length) if (_position + offset >= _tokens.Length)
return new SyntaxToken(SyntaxKind.EndOfFile, _position + offset, "\0", null); return new SyntaxToken(SyntaxKind.EndOfFile, _position + offset, "\0", null);
else else
{ {
var token = _tokens[_position + offset]; return _tokens[_position + offset];
if (token.Kind == SyntaxKind.Comment && !allowComment)
{
return Get(offset + 1);
}
return token;
} }
} }

View File

@ -0,0 +1,28 @@
using Upsilon.Evaluator;
using Xunit;
namespace UpsilonTests.GeneralTests
{
public class CommentTests : TestClass
{
public CommentTests(StaticScriptFixture fix) : base(fix)
{
}
[Fact]
public void BasicComment()
{
const string input = @"
-- test comment
return 100
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(100, result);
}
}
}