Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #501 #502

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading