From 8b08aea40428ffd7413158d87a340c96f193eb3b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 27 Nov 2018 12:35:20 +0100 Subject: [PATCH] Dont save data comments if disabled through options --- Upsilon/Evaluator/Script.cs | 2 +- Upsilon/Parser/Lexer.cs | 27 +++++++++++++++++---------- Upsilon/Parser/Parser.cs | 4 ++-- Upsilon/Parser/SyntaxToken.cs | 11 +++++++++-- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Upsilon/Evaluator/Script.cs b/Upsilon/Evaluator/Script.cs index 85a25b9..3a77812 100644 --- a/Upsilon/Evaluator/Script.cs +++ b/Upsilon/Evaluator/Script.cs @@ -56,7 +56,7 @@ namespace Upsilon.Evaluator internal void Parse() { - _parsed = Parser.Parser.Parse(_scriptString, Diagnostics); + _parsed = Parser.Parser.Parse(_scriptString, Diagnostics, Options.SaveDataComments); } diff --git a/Upsilon/Parser/Lexer.cs b/Upsilon/Parser/Lexer.cs index be612ed..6f2f5d3 100644 --- a/Upsilon/Parser/Lexer.cs +++ b/Upsilon/Parser/Lexer.cs @@ -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 Lex(string text, Diagnostics diagnostics) + public static ImmutableArray Lex(string text, Diagnostics diagnostics, bool saveComments) { - var lexer = new Lexer(text, diagnostics); + var lexer = new Lexer(text, diagnostics, saveComments); return lexer.Lex(); } @@ -38,7 +40,8 @@ namespace Upsilon.Parser switch (next.Kind) { case SyntaxKind.Comment: - _activeComments.Add(next.Value.ToString()); + if (SaveComments) + _activeComments.Add(next.Value.ToString()); continue; case SyntaxKind.FunctionKeyword: case SyntaxKind.LocalKeyword: @@ -202,17 +205,21 @@ namespace Upsilon.Parser { _position++; var start = _position; - var stringBuilder = new StringBuilder(); - if (Current != ' ') - stringBuilder.Append(Current); + 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); } } } \ No newline at end of file diff --git a/Upsilon/Parser/Parser.cs b/Upsilon/Parser/Parser.cs index bbec5c1..f1d0391 100644 --- a/Upsilon/Parser/Parser.cs +++ b/Upsilon/Parser/Parser.cs @@ -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(); } diff --git a/Upsilon/Parser/SyntaxToken.cs b/Upsilon/Parser/SyntaxToken.cs index 8501f0f..d34817d 100644 --- a/Upsilon/Parser/SyntaxToken.cs +++ b/Upsilon/Parser/SyntaxToken.cs @@ -7,8 +7,15 @@ namespace Upsilon.Parser { public SyntaxToken(SyntaxKind kind, int position, string text, object value) { - Kind = kind; - Span = new TextSpan(position, text.Length); + Kind = kind; + 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; }