From 864b51d3bdfe1ae7638ad4c6928b837b4f12cc8e Mon Sep 17 00:00:00 2001 From: Chris Anders Date: Fri, 1 Nov 2024 12:26:46 +0100 Subject: [PATCH 1/2] fix: Symbols beeing shared cross multiple parsers --- Source/Silverfly/LexerConfig.cs | 14 -------------- Source/Silverfly/Parser.cs | 14 +++++++------- Source/TestProject/TestParser.cs | 13 +++++++++++++ Source/TestProject/Tests.cs | 8 ++++++++ 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/Source/Silverfly/LexerConfig.cs b/Source/Silverfly/LexerConfig.cs index a86c3d8..43438e9 100644 --- a/Source/Silverfly/LexerConfig.cs +++ b/Source/Silverfly/LexerConfig.cs @@ -21,20 +21,6 @@ public class LexerConfig public bool IgnoreCasing { get; set; } public StringComparison Casing => IgnoreCasing ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; - public LexerConfig() - { - // Register all of the Symbols that are explicit punctuators. - foreach (var type in PredefinedSymbols.Pool) - { - var punctuator = type.Punctuator(); - - if (punctuator != "\0") - { - Symbols.Add(punctuator, type); - } - } - } - // sort punctuators longest -> smallest to make it possible to use symbols with more than one character internal void OrderSymbols() diff --git a/Source/Silverfly/Parser.cs b/Source/Silverfly/Parser.cs index ed1fc40..6693535 100644 --- a/Source/Silverfly/Parser.cs +++ b/Source/Silverfly/Parser.cs @@ -13,7 +13,6 @@ namespace Silverfly; public abstract partial class Parser { public readonly Lexer Lexer; - private readonly LexerConfig _lexerConfig = new(); public readonly ParserDefinition ParserDefinition = new(); public ParserOptions Options = new(true, true); private readonly List _read = []; @@ -71,9 +70,10 @@ public TranslationUnit Parse(string source, string filename = "synthetic.dsl") public Parser() { - InitLexer(_lexerConfig); + var lexerConfig = new LexerConfig(); + InitLexer(lexerConfig); - Lexer = new Lexer(_lexerConfig); + Lexer = new Lexer(lexerConfig); InitParser(ParserDefinition); @@ -114,14 +114,14 @@ private void AddLexerSymbols(Lexer lexer, Dictionary { - _lexerConfig.AddSymbol(s.Name); + lexer.Config.AddSymbol(s.Name); return true; }); } @@ -266,7 +266,7 @@ private void EnsureSymbolIsRegistered(Symbol expected) { if (!Lexer.IsPunctuator(expected.Name)) { - _lexerConfig.AddSymbol(expected.Name); + Lexer.Config.AddSymbol(expected.Name); } } @@ -288,7 +288,7 @@ public bool IsMatch(params Symbol[] expected) private bool CompareToken(Token token, Symbol expected) { - return token.Type.Name.AsSpan().CompareTo(expected.Name.AsSpan(), _lexerConfig.Casing) == 0; + return token.Type.Name.AsSpan().CompareTo(expected.Name.AsSpan(), Lexer.Config.Casing) == 0; } /// diff --git a/Source/TestProject/TestParser.cs b/Source/TestProject/TestParser.cs index 7ee5545..4d7ebcf 100644 --- a/Source/TestProject/TestParser.cs +++ b/Source/TestProject/TestParser.cs @@ -6,6 +6,19 @@ namespace TestProject; +public class SecondParser : Parser +{ + protected override void InitLexer(LexerConfig lexer) + { + lexer.AddSymbols("blub", "bob"); + } + + protected override void InitParser(ParserDefinition def) + { + + } +} + public class TestParser : Parser { protected override void InitParser(ParserDefinition def) diff --git a/Source/TestProject/Tests.cs b/Source/TestProject/Tests.cs index f5ba38c..39c21ed 100644 --- a/Source/TestProject/Tests.cs +++ b/Source/TestProject/Tests.cs @@ -258,6 +258,14 @@ public Task GenericTypeName_Should_Pass() return Verify(result, Settings); } + [Test] + public void Multiple_Parsers_Should_Pass() + { + var p1 = new TestParser(); + var p2 = new SecondParser(); + + } + [Test] public Task GenericMultipleTypeName_Should_Pass() { From 9fa8d6ed83928bd35b66bf74dddcf744b23cd02d Mon Sep 17 00:00:00 2001 From: codefactor-io Date: Fri, 1 Nov 2024 11:27:09 +0000 Subject: [PATCH 2/2] [CodeFactor] Apply fixes to commit 864b51d --- Source/TestProject/TestParser.cs | 1 - Source/TestProject/Tests.cs | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Source/TestProject/TestParser.cs b/Source/TestProject/TestParser.cs index 4d7ebcf..35074ce 100644 --- a/Source/TestProject/TestParser.cs +++ b/Source/TestProject/TestParser.cs @@ -15,7 +15,6 @@ protected override void InitLexer(LexerConfig lexer) protected override void InitParser(ParserDefinition def) { - } } diff --git a/Source/TestProject/Tests.cs b/Source/TestProject/Tests.cs index 39c21ed..6615204 100644 --- a/Source/TestProject/Tests.cs +++ b/Source/TestProject/Tests.cs @@ -1,4 +1,4 @@ -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using Silverfly; using Silverfly.Helpers; using Silverfly.Testing; @@ -263,7 +263,6 @@ public void Multiple_Parsers_Should_Pass() { var p1 = new TestParser(); var p2 = new SecondParser(); - } [Test]