-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simple templating sample using lexer modes
- Loading branch information
Showing
15 changed files
with
639 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
} |
Oops, something went wrong.