Skip to content

Commit

Permalink
Merge pull request #56 from furesoft/fix/symbols_being_shared_from_ot…
Browse files Browse the repository at this point in the history
…her_parsers

fix: Symbols beeing shared cross multiple parsers
  • Loading branch information
furesoft authored Nov 1, 2024
2 parents 9dbc099 + 9fa8d6e commit 04b70f0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
14 changes: 0 additions & 14 deletions Source/Silverfly/LexerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions Source/Silverfly/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Token> _read = [];
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -114,14 +114,14 @@ private void AddLexerSymbols<TParselet>(Lexer lexer, Dictionary<Symbol, TParsele
{
if (!lexer.IsPunctuator(parselet.Key.Name) && !lexer.IsSpecialToken(parselet.Key.Name))
{
_lexerConfig.AddSymbol(parselet.Key.Name);
lexer.Config.AddSymbol(parselet.Key.Name);
}

if (parselet is ISymbolDiscovery sd)
{
_ = sd.GetSymbols().All(s =>
{
_lexerConfig.AddSymbol(s.Name);
lexer.Config.AddSymbol(s.Name);
return true;
});
}
Expand Down Expand Up @@ -266,7 +266,7 @@ private void EnsureSymbolIsRegistered(Symbol expected)
{
if (!Lexer.IsPunctuator(expected.Name))
{
_lexerConfig.AddSymbol(expected.Name);
Lexer.Config.AddSymbol(expected.Name);
}
}

Expand All @@ -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;
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions Source/TestProject/TestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

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)
Expand Down
9 changes: 8 additions & 1 deletion Source/TestProject/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using Silverfly;
using Silverfly.Helpers;
using Silverfly.Testing;
Expand Down Expand Up @@ -258,6 +258,13 @@ 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()
{
Expand Down

0 comments on commit 04b70f0

Please sign in to comment.