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

View File

@ -7,8 +7,15 @@ namespace Upsilon.Parser
{ {
public SyntaxToken(SyntaxKind kind, int position, string text, object value) public SyntaxToken(SyntaxKind kind, int position, string text, object value)
{ {
Kind = kind; Kind = kind;
Span = new TextSpan(position, text.Length); Span = new TextSpan(position, text.Length);
Value = value;
}
public SyntaxToken(SyntaxKind kind, int position, int length, object value)
{
Kind = kind;
Span = new TextSpan(position, length);
Value = value; Value = value;
} }