Skip to content

Commit

Permalink
simple templating sample using lexer modes
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Jun 29, 2022
1 parent a7ee5ad commit e752cee
Show file tree
Hide file tree
Showing 15 changed files with 639 additions and 21 deletions.
1 change: 1 addition & 0 deletions ParserTests/ParserTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ProjectReference Include="..\samples\indented\indented.csproj" />
<ProjectReference Include="..\samples\postProcessedLexerParser\postProcessedLexerParser.csproj" />
<ProjectReference Include="..\samples\SimpleExpressionParser\SimpleExpressionParser.csproj" />
<ProjectReference Include="..\samples\SimpleTemplate\SimpleTemplate.csproj" />
<ProjectReference Include="..\samples\while\while.csproj" />
<ProjectReference Include="..\samples\XML\XML.csproj" />
<ProjectReference Include="..\sly\sly.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion ParserTests/lexer/Issue210Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ FSMMatch<GenericToken> Callback(FSMMatch<GenericToken> match)


lexer.FSMBuilder.GoTo(GenericLexer<Issue210Token>.start)
.Transition('?')
.SafeTransition('?')
.Mark("qmark")
.ExceptTransition(new[] {'?'}) // moving to first char of a special
.Mark("in_qmark") // now we are really in a potential SPECIAL
Expand Down
39 changes: 39 additions & 0 deletions ParserTests/samples/TemplateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using SimpleTemplate;
using sly.parser;
using sly.parser.generator;
using Xunit;

namespace ParserTests.samples
{
public class TemplateTests
{

public Parser<TemplateLexer, string> GetParser()
{
var builder = new ParserBuilder<TemplateLexer, string>();
var instance = new TemplateParser();
var build = builder.BuildParser(instance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "template");
Assert.False(build.IsError);
Assert.NotNull(build.Result);
return build.Result;
}

[Fact]
public void BasicTemplateTest()
{
var parser = GetParser();
var source = @"hello-{=world=}-billy-{% if (a == 1.0) %}-bob-{%else%}-boubou-{%endif%}this is the end";

var context = new Dictionary<string, string>()
{
{ "world", "monde" },
{ "a", "1.0" }
};

var result = parser.ParseWithContext(source,context);
Assert.False(result.IsError);
Assert.Equal("hello-monde-billy--bob-this is the end",result.Result);
}
}
}
1 change: 1 addition & 0 deletions samples/ParserExample/ParserExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ProjectReference Include="..\..\sly\sly.csproj" />
<ProjectReference Include="..\postProcessedLexerParser\postProcessedLexerParser.csproj" />
<ProjectReference Include="..\SimpleExpressionParser\SimpleExpressionParser.csproj" />
<ProjectReference Include="..\SimpleTemplate\SimpleTemplate.csproj" />
<ProjectReference Include="..\while\while.csproj" />
<ProjectReference Include="..\XML\XML.csproj" />
</ItemGroup>
Expand Down
52 changes: 50 additions & 2 deletions samples/ParserExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using ParserTests.Issue239;
using ParserTests.lexer;
using simpleExpressionParser;
using SimpleTemplate;
using sly.lexer;
using sly.lexer.fsm;
using sly.parser;
Expand Down Expand Up @@ -995,10 +996,57 @@ public static void TestIndentedParserNeverEnding()
Assert.NotNull(ifthenelse.Else);
Assert.Equal(2,ifthenelse.Else.Statements.Count);
}

private static void TestTemplate()
{
var lexerResult = LexerBuilder.BuildLexer<TemplateLexer>();
Assert.False(lexerResult.IsError);
var genericLexer = lexerResult.Result as GenericLexer<TemplateLexer>;
var fsm = genericLexer.FSMBuilder.Fsm;
var fsmGraph = fsm.ToGraphViz();
Console.WriteLine(fsmGraph);



var source = @"hello - {= world =} - billy - {% if (a == 1.0) %} - bob - {%else%} - boubou - {%endif%}";

var tokens = genericLexer.Tokenize(source);
foreach (var token in tokens.Tokens.Tokens)
{
Console.WriteLine(token);
}

var builder = new ParserBuilder<TemplateLexer, string>();
var instance = new TemplateParser();
var build = builder.BuildParser(instance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "template");
if (build.IsOk)
{
var context = new Dictionary<string, string>()
{
{ "world", "monde" },
{ "a", "1.0" }
};
var r = build.Result.ParseWithContext(source, context);
if (r.IsOk)
{
Console.WriteLine(r.Result);
}
else
{
r.Errors.ForEach(x => Console.WriteLine(x.ErrorMessage));
}
}
else
{
build.Errors.ForEach(x => Console.WriteLine(x.Message));
}

}

private static void Main(string[] args)
{
testErrors();
TestTemplate();
// testErrors();
//TestContextualParser();
//TestTokenCallBacks();
//test104();
Expand Down Expand Up @@ -1038,7 +1086,7 @@ private static void Main(string[] args)
// TestChannels();
//TestIndentedParserNeverEnding();
//TestLexerModes();
TestXmlParser();
// TestXmlParser();
}

private static void testManySugar()
Expand Down
11 changes: 11 additions & 0 deletions samples/SimpleTemplate/SimpleTemplate.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\sly\sly.csproj" />
</ItemGroup>

</Project>
161 changes: 161 additions & 0 deletions samples/SimpleTemplate/TemplateLexer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System;
using sly.lexer;

namespace SimpleTemplate
{
public enum TemplateLexer
{

NOT_A_TOKEN = 0,

#region TEXT

[AllExcept("{%", "{=")]
// [AllExcept("{%")]
TEXT,

[Sugar("{%")] [Push("code")] OPEN_CODE,

[Sugar("{=")] [Push("value")] OPEN_VALUE,





#endregion

#region value

[AlphaId]
[Mode("value")]
[Mode("code")]
ID,

[Sugar("=}")]
[Mode("value")]
[Pop]
CLOSE_VALUE,

#endregion

#region code

[Sugar("%}")]
[Mode("code")]
[Pop]
CLOSE_CODE,

[Keyword("if")]
[Mode("code")]
IF,

[Keyword("endif")]
[Mode("code")]
ENDIF,

[Keyword("else")]
[Mode("code")]
ELSE,

#region literals

[String()]
[Mode("code")]
STRING,

// [Int()]
// [Mode("code")]
// INT,

[Double()]
[Mode("code")]
DOUBLE,

[Lexeme(GenericToken.KeyWord, "TRUE")]
[Lexeme(GenericToken.KeyWord, "true")]
[Mode("code")]
TRUE,

[Lexeme(GenericToken.KeyWord, "FALSE")]
[Lexeme(GenericToken.KeyWord, "false")]
[Mode("code")]
FALSE,



#endregion

#region operators 30 -> 49

[Sugar( ">")]
[Mode("code")]
GREATER = 30,

[Sugar( "<")]
[Mode("code")]
LESSER = 31,

[Sugar( "==")]
[Mode("code")]
EQUALS = 32,

[Sugar( "!=")]
[Mode("code")]
DIFFERENT = 33,

[Sugar( ".")]
[Mode("code")]
CONCAT = 34,

[Sugar( ":=")]
[Mode("code")]
ASSIGN = 35,

[Sugar( "+")]
[Mode("code")]
PLUS = 36,

[Sugar( "-")]
[Mode("code")]
MINUS = 37,


[Sugar( "*")]
[Mode("code")]
TIMES = 38,

[Sugar( "/")]
[Mode("code")]
DIVIDE = 39,


#endregion

#region sugar 100 -> 150

[Sugar("(")]
[Mode("code")]
OPEN_PAREN,

[Sugar(")")]
[Mode("code")]
CLOSE_PAREN,

[Lexeme(GenericToken.KeyWord, "NOT")] [Lexeme(GenericToken.KeyWord, "not")]
[Mode("code")]
NOT,

[Lexeme(GenericToken.KeyWord, "AND")] [Lexeme(GenericToken.KeyWord, "and")]
[Mode("code")]
AND,

[Lexeme(GenericToken.KeyWord, "OR")] [Lexeme(GenericToken.KeyWord, "or")]
[Mode("code")]
OR,

#endregion

#endregion

}
}
Loading

0 comments on commit e752cee

Please sign in to comment.