@@ -30,6 +30,10 @@ Install from the NuGet gallery GUI or with the Package Manager Console using the
3030
3131``` Install-Package sly ```
3232
33+ or with dotnet core
34+
35+ ``` dotnet add package sly ```
36+
3337
3438## Lexer ##
3539
@@ -41,79 +45,81 @@ It could be improved in the future.
4145
4246### configuration ###
4347
44- To configure a lexer 2 items has to be done :
48+ The full lexer configuration is done in an ``` enum ``` :
4549
50+ The ``` enum ``` is listing all the possible tokens (no special constraint here except public visibility)
51+
52+ Each ``` enum ``` value has a ``` [Lexeme] ``` attribute to mark it has a lexeme. The lexeme attribute takes 3 parameters:
53+ -
54+ ``` c#
55+ string regex
56+ ``` : a regular expression that captures the lexeme
57+ - ```boolean isSkippable ``` (optional , default is ```false ```): a boolean , true if the lexeme must be ignored ( whitespace for example )
58+ - ```boolean isLineending ``` (optionanl , default is ```false ```) : true if the lexeme matches a line end (to allow line counting while lexing ).
4659
47- - an ``` enum ``` listing all the possible tokens (no special constraint here except public visibility)
48- - a method with special attribute ``` [LexerConfigurationAttribute] ``` that associates tokens from the above enum with matching regex.
49-
50- the configuration method takes a ``` Lexer<T> ``` (where T is the tokens ``` enum ``` parameters and returns the same lexer after having added (token,regex) associations.
5160
5261The lexer can be used apart from the parser . It provides a method that returns an ```IEnumerable < Token < T >> ``` (where T is the tokens ```enum ```) from a ```string ```
5362
63+
64+
5465```c #
5566 IList < Token < T >> tokens = Lexer .Tokenize (source ).ToList <Token <T >>();
5667```
5768
58- ### full example, for a mathematical parser ###
5969
60- #### the tokens enum ####
70+ You can also build only a lexer using :
6171
6272``` c#
73+ ILexer < ExpressionToken > lexer = LexerBuilder .BuildLexer <ExpressionToken >();
74+ var tokens = lexer .Tokenize (source ).ToList ();
75+ ```
76+
77+ ### full example, for a mathematical parser ###
6378
79+ ``` c#
6480public enum ExpressionToken
6581 {
82+ // float number
83+ [Lexeme (" [0-9]+\\ .[0-9]+" )]
84+ DOUBLE = 1 ,
6685
67- INT = 2 , // integer
68-
69- DOUBLE = 3 , // float number
70-
71- PLUS = 4 , // the + operator
86+ // integer
87+ [Lexeme (" [0-9]+" )]
88+ INT = 3 ,
7289
73- MINUS = 5 , // the - operator
90+ // the + operator
91+ [Lexeme (" \\ +" )]
92+ PLUS = 4 ,
7493
75- TIMES = 6 , // the * operator
94+ // the - operator
95+ [Lexeme (" \\ -" )]
96+ MINUS = 5 ,
7697
77- DIVIDE = 7 , // the / operator
98+ // the * operator
99+ [Lexeme (" \\ *" )]
100+ TIMES = 6 ,
78101
79- LPAREN = 8 , // a left paranthesis (
102+ // the / operator
103+ [Lexeme (" \\ /" )]
104+ DIVIDE = 7 ,
80105
81- RPAREN = 9 ,// a right paranthesis )
106+ // a left paranthesis (
107+ [Lexeme (" \\ (" )]
108+ LPAREN = 8 ,
82109
83- WS = 12 , // a whitespace
110+ // a right paranthesis )
111+ [Lexeme (" \\ )" )]
112+ RPAREN = 9 ,
84113
85- EOL = 14 // an end of line
114+ // a whitespace
115+ [Lexeme (" [ \\ t]+" ,true )]
116+ WS = 12 ,
86117
118+ [Lexeme (" [\\ n\\ r]+" , true , true )]
119+ EOL = 14
87120 }
88121```
89122
90- #### regular expressions
91-
92- ``` c#
93-
94- [LexerConfiguration ]
95- public Lexer < ExpressionToken > BuildExpressionLexer (Lexer < ExpressionToken > lexer = null )
96- {
97- if (lexer == null )
98- {
99- lexer = new Lexer <ExpressionToken >();
100- }
101-
102- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .DOUBLE , " [0-9]+\\ .[0-9]+" ));
103- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .INT , " [0-9]+" ));
104- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .PLUS , " \\ +" ));
105- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .MINUS , " \\ -" ));
106- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .TIMES , " \\ *" ));
107- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .DIVIDE , " \\ /" ));
108-
109- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .LPAREN , " \\ (" ));
110- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .RPAREN , " \\ )" ));
111-
112- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .WS , " [ \\ t]+" , true ));
113- lexer .AddDefinition (new TokenDefinition <ExpressionToken >(ExpressionToken .EOL , " [\\ n\\ r]+" , true , true ));
114- return lexer ;
115- }
116- ```
117123
118124
119125## Parser ##
0 commit comments