From 021664d8aa75855762ce4bccdefaeafd7a24298a Mon Sep 17 00:00:00 2001 From: b3b00 Date: Mon, 14 Jun 2021 10:51:48 +0200 Subject: [PATCH] version 2.7.0.2 : short code attributes for generic lexer --- ParserTests/EBNFTests.cs | 1 + ParserTests/lexer/GenericLexerTests.cs | 59 +++++++++++++++++++++++++ sly/lexer/attributes/StringAttribute.cs | 2 +- sly/sly.csproj | 4 +- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/ParserTests/EBNFTests.cs b/ParserTests/EBNFTests.cs index 8ae6648c..d11b7991 100644 --- a/ParserTests/EBNFTests.cs +++ b/ParserTests/EBNFTests.cs @@ -15,6 +15,7 @@ using sly.parser.parser; using sly.parser.syntax.grammar; using Xunit; +using String = System.String; namespace ParserTests { diff --git a/ParserTests/lexer/GenericLexerTests.cs b/ParserTests/lexer/GenericLexerTests.cs index f943c916..3512330f 100644 --- a/ParserTests/lexer/GenericLexerTests.cs +++ b/ParserTests/lexer/GenericLexerTests.cs @@ -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, @@ -1104,6 +1142,27 @@ public void TestIndented2() Assert.Equal(2,unindents); } + [Fact] + public void TestGenericShortCode() + { + var build = LexerBuilder.BuildLexer(); + 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(LexerResult result) where T : struct { return result.Tokens.Aggregate(new StringBuilder(), (buf, token) => buf.Append(token.TokenID)).ToString(); diff --git a/sly/lexer/attributes/StringAttribute.cs b/sly/lexer/attributes/StringAttribute.cs index 4b220239..85d52a5a 100644 --- a/sly/lexer/attributes/StringAttribute.cs +++ b/sly/lexer/attributes/StringAttribute.cs @@ -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) { } diff --git a/sly/sly.csproj b/sly/sly.csproj index d31c86f5..3c78a7f6 100644 --- a/sly/sly.csproj +++ b/sly/sly.csproj @@ -6,11 +6,11 @@ $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb #LY is a parser generator halfway between parser combinators and parser generator like ANTLR b3b00 - 2.7.0.1 + 2.7.0.2 https://github.com/b3b00/sly https://github.com/b3b00/sly https://github.com/b3b00/sly/blob/master/LICENSE - 2.7.0.1 + 2.7.0.2 Library