Skip to content

Commit

Permalink
Merge pull request #502 from b3b00/bugfix/#501
Browse files Browse the repository at this point in the history
fix #501
  • Loading branch information
b3b00 authored Nov 8, 2024
2 parents 2106c87 + 4e11e26 commit 2b76b32
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
41 changes: 31 additions & 10 deletions src/sly/lexer/GenericLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,6 @@ private FSMLexer<GenericToken> SetLexerMode(Token<IN> token, Stack<FSMLexer<Gene
LexerFsm = SubLexersFsm[token.Position.Mode ?? ModeAttribute.DefaultLexerMode];
lexersStack.Push(LexerFsm);
}
else
{
LexerFsm = SubLexersFsm[token.Position.Mode ?? ModeAttribute.DefaultLexerMode];
}
}

return LexerFsm;
Expand Down Expand Up @@ -1230,14 +1226,22 @@ public Token<IN> Transcode(FSMMatch<GenericToken> match)
var newPush = match.NewPosition.IsPush;
var newPop = match.NewPosition.IsPop;
if (inTok != null
&& derivedTokens.TryGetValue(inTok.TokenID, out var derivations)
&& derivations.TryGetValue(inTok.Value, out var derivation))
&& derivedTokens.TryGetValue(inTok.TokenID, out var derivations))
{
newPop = derivation.isPop;
newPush = derivation.isPush;
newMode = derivation.mode;
if (derivations.TryGetValue(inTok.Value, out var derivation))
{
newPop = derivation.isPop;
newPush = derivation.isPush;
newMode = derivation.mode;
}
else
{
newPop = false;
newPush = false;
newMode = null;
}
}

tok.IsComment = inTok.IsComment;
tok.IsEmpty = inTok.IsEmpty;
tok.SpanValue = inTok.SpanValue;
Expand Down Expand Up @@ -1272,5 +1276,22 @@ public string ToGraphViz()
{
return TempLexerFsm.ToGraphViz();
}

public IList<string> GetSubLexers()
{
return SubLexersFsm.Keys.ToList();
}

public string ToGraphViz(string subLexerName)
{
if (SubLexersFsm.TryGetValue(subLexerName, out var subLexer))
{
return subLexer.ToGraphViz();
}

return null;
}


}
}
2 changes: 1 addition & 1 deletion src/sly/lexer/LexerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ private static BuildResult<ILexer<IN>> BuildGenericLexer<IN>(IDictionary<IN, Lis
{
lexer.FSMBuilder.Push(lexeme.Pushtarget);
}

if (lexeme.IsPop)
{
lexer.FSMBuilder.Pop();
Expand Down
34 changes: 34 additions & 0 deletions tests/ParserTests/Issue501/Issue501Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NFluent;
using sly.lexer;
using sly.parser;
using Xunit;

namespace ParserTests.Issue501;

public class Issue501Tests
{
[Fact]
public void TestIssue501()
{
var build = LexerBuilder.BuildLexer<Issue501Token>();
Check.That(build).IsOk();
Check.That(build.Result).IsNotNull();
Check.That(build.Result).IsInstanceOf<GenericLexer<Issue501Token>>();
var lexer = build.Result as GenericLexer<Issue501Token>;
var source = @"test = 3";
var lexed = lexer.Tokenize(source);
Check.That(lexed).IsOkLexing();
var tokens = lexed.Tokens;
Check.That(tokens).IsNotNull();
var mainTokens = tokens.MainTokens();
Check.That(mainTokens).CountIs(4);
Check.That(mainTokens.Last().IsEOS).IsTrue();
Check.That(mainTokens.Take(3).Select(x => x.TokenID)).IsEquivalentTo(new List<Issue501Token>()
{ Issue501Token.Identifier, Issue501Token.Assign, Issue501Token.Number });

}
}
24 changes: 24 additions & 0 deletions tests/ParserTests/Issue501/issue501Token.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using sly.lexer;

namespace ParserTests.Issue501;

public enum Issue501Token
{
[Keyword("import")]
[Push("importMode")]
ImportKeyword,
[UpTo(";")]
[Mode("importMode")]
ImportContent,
[Sugar(";")]
[Mode("importMode")]
[Pop]
EndImport,

[AlphaNumDashId]
Identifier,
[Int]
Number,
[Sugar("=")]
Assign
}

0 comments on commit 2b76b32

Please sign in to comment.