Skip to content

Commit

Permalink
version 2.7.0.2 : short code attributes for generic lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Jun 14, 2021
1 parent 3c4ec07 commit 021664d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions ParserTests/EBNFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using sly.parser.parser;
using sly.parser.syntax.grammar;
using Xunit;
using String = System.String;

namespace ParserTests
{
Expand Down
59 changes: 59 additions & 0 deletions ParserTests/lexer/GenericLexerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@
namespace ParserTests.lexer
{

public enum GenericShortAttributes
{
[Double] DOUBLE = 1,

// integer
[Int] INT = 3,

[AlphaId] IDENTIFIER = 4,

// the + operator
[Sugar("+")] PLUS = 5,

// the ++ operator
[Sugar("++")]
INCREMENT = 6,

// the - operator
[Sugar("-")] MINUS = 7,

// the * operator
[Sugar("*")] TIMES = 8,

// the / operator
[Sugar("/")] DIVIDE = 9,

// a left paranthesis (
[Sugar("(")] LPAREN = 10,

// a right paranthesis )
[Sugar(")")] RPAREN = 11,

[String("'","\\")]
STRING = 12,

[Keyword("hello")]
HELLO = 13
}

public enum Issue210Token
{
EOF = 0,
Expand Down Expand Up @@ -1104,6 +1142,27 @@ public void TestIndented2()
Assert.Equal(2,unindents);
}

[Fact]
public void TestGenericShortCode()
{
var build = LexerBuilder.BuildLexer<GenericShortAttributes>();
Assert.True(build.IsOk);
Assert.NotNull(build.Result);
var lexer = build.Result;
var lexResult = lexer.Tokenize(@"1 + 2 + a + b * 8.3 hello / 'b\'jour'");

Assert.True(lexResult.IsOk);
var tokens = lexResult.Tokens;
Assert.Equal(13,tokens.Count);
Assert.Equal(GenericShortAttributes.INT,tokens[0].TokenID);
Assert.Equal(GenericShortAttributes.PLUS,tokens[3].TokenID);
Assert.Equal(GenericShortAttributes.IDENTIFIER,tokens[4].TokenID);
Assert.Equal(GenericShortAttributes.DOUBLE,tokens[8].TokenID);
Assert.Equal(GenericShortAttributes.HELLO,tokens[9].TokenID);
Assert.Equal(GenericShortAttributes.STRING,tokens[11].TokenID);

}

private static string ToTokens<T>(LexerResult<T> result) where T : struct
{
return result.Tokens.Aggregate(new StringBuilder(), (buf, token) => buf.Append(token.TokenID)).ToString();
Expand Down
2 changes: 1 addition & 1 deletion sly/lexer/attributes/StringAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace sly.lexer
{
public class StringAttribute : LexemeAttribute
{
public StringAttribute(string delimiter, string escape) : base(GenericToken.String, delimiter, escape)
public StringAttribute(string delimiter = null, string escape = null) : base(GenericToken.String, delimiter, escape)
{

}
Expand Down
4 changes: 2 additions & 2 deletions sly/sly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Description>#LY is a parser generator halfway between parser combinators and parser generator like ANTLR</Description>
<Authors>b3b00</Authors>
<version>2.7.0.1</version>
<version>2.7.0.2</version>
<PackageProjectUrl>https://github.com/b3b00/sly</PackageProjectUrl>
<RepositoryUrl>https://github.com/b3b00/sly</RepositoryUrl>
<PackageLicenseUrl>https://github.com/b3b00/sly/blob/master/LICENSE</PackageLicenseUrl>
<PackageVersion>2.7.0.1</PackageVersion>
<PackageVersion>2.7.0.2</PackageVersion>
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject />
Expand Down

0 comments on commit 021664d

Please sign in to comment.