Dont save data comments if disabled through options

This commit is contained in:
Deukhoofd 2018-11-27 12:35:20 +01:00
parent b7d01b02f1
commit 8b08aea404
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 29 additions and 15 deletions

View File

@ -56,7 +56,7 @@ namespace Upsilon.Evaluator
internal void Parse()
{
_parsed = Parser.Parser.Parse(_scriptString, Diagnostics);
_parsed = Parser.Parser.Parse(_scriptString, Diagnostics, Options.SaveDataComments);
}

View File

@ -7,19 +7,21 @@ namespace Upsilon.Parser
{
public class Lexer
{
private bool SaveComments { get; }
private readonly string _text;
private readonly Diagnostics _diagnostics;
private int _position;
private Lexer(string text, Diagnostics diagnostics)
private Lexer(string text, Diagnostics diagnostics, bool saveComments)
{
SaveComments = saveComments;
_text = text;
_diagnostics = diagnostics;
}
public static ImmutableArray<SyntaxToken> Lex(string text, Diagnostics diagnostics)
public static ImmutableArray<SyntaxToken> Lex(string text, Diagnostics diagnostics, bool saveComments)
{
var lexer = new Lexer(text, diagnostics);
var lexer = new Lexer(text, diagnostics, saveComments);
return lexer.Lex();
}
@ -38,6 +40,7 @@ namespace Upsilon.Parser
switch (next.Kind)
{
case SyntaxKind.Comment:
if (SaveComments)
_activeComments.Add(next.Value.ToString());
continue;
case SyntaxKind.FunctionKeyword:
@ -202,17 +205,21 @@ namespace Upsilon.Parser
{
_position++;
var start = _position;
var stringBuilder = new StringBuilder();
StringBuilder stringBuilder = null;
if (SaveComments)
{
stringBuilder = new StringBuilder();
if (Current != ' ')
stringBuilder.Append(Current);
}
while (Next != '\n' && Next != '\0')
{
stringBuilder.Append(Next);
stringBuilder?.Append(Next);
_position++;
}
var str = stringBuilder.ToString();
var str = stringBuilder?.ToString();
_position++;
return new SyntaxToken(SyntaxKind.Comment, start, str, str);
return new SyntaxToken(SyntaxKind.Comment, start, _position - start, str);
}
}
}

View File

@ -18,9 +18,9 @@ namespace Upsilon.Parser
_diagnostics = diagnostics;
}
public static BlockStatementSyntax Parse(string text, Diagnostics diagnostics)
public static BlockStatementSyntax Parse(string text, Diagnostics diagnostics, bool saveComments)
{
var tokens = Lexer.Lex(text, diagnostics);
var tokens = Lexer.Lex(text, diagnostics, saveComments);
return (BlockStatementSyntax) new Parser(tokens, diagnostics).ParseScriptSyntax();
}

View File

@ -12,6 +12,13 @@ namespace Upsilon.Parser
Value = value;
}
public SyntaxToken(SyntaxKind kind, int position, int length, object value)
{
Kind = kind;
Span = new TextSpan(position, length);
Value = value;
}
public override SyntaxKind Kind { get; }
public object Value { get; }
public string[] CommentData { get; set; }