File tree 1 file changed +54
-0
lines changed
1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ import {
2
+ anyOf ,
3
+ regex ,
4
+ sequenceOf ,
5
+ string ,
6
+ optional ,
7
+ whitespace ,
8
+ between ,
9
+ } from "rudus" ;
10
+
11
+ console . time ( ) ;
12
+
13
+ const betweenDoubleQuotes = between ( string ( '"' ) ) ;
14
+ const optionalWhitespace = optional ( whitespace ( ) ) ;
15
+
16
+ const declarationKeyword = regex ( / ^ v a r | l e t | c o n s t / ) ;
17
+ const word = regex ( / \w + / ) ; // word
18
+ const equalSign = string ( "=" ) ;
19
+
20
+ const literalBool = regex ( / t r u e | f a l s e / ) ;
21
+ const literalUndefined = regex ( / u n d e f i n e d / ) ;
22
+ const literalNull = regex ( / n u l l / ) ;
23
+ const literalNumber = regex ( / ^ ( \d + \. ) ? \d + $ | ^ 0 b [ 0 - 1 ] + ( n ) ? $ / ) ;
24
+ const literalString = betweenDoubleQuotes ( word ) ;
25
+
26
+ const variableValue = anyOf ( [
27
+ literalBool ,
28
+ literalString ,
29
+ literalNumber ,
30
+ literalUndefined ,
31
+ literalNull ,
32
+ ] ) ;
33
+
34
+ const variableParser = sequenceOf ( [
35
+ declarationKeyword . map (
36
+ state => `<span style="color:red">${ state . result } </span>` ,
37
+ ) ,
38
+ optionalWhitespace ,
39
+ word . map ( state => `<span style="color:green">${ state . result } </span>` ) ,
40
+ optionalWhitespace ,
41
+ equalSign . map ( state => `<span style="color:grey">${ state . result } </span>` ) ,
42
+ optionalWhitespace ,
43
+ variableValue . map ( state => `<span style="color:blue">${ state . result } </span>` ) ,
44
+ ] ) ;
45
+
46
+ const parserResult = variableParser . run ( "const isCool = null" ) ;
47
+
48
+ console . timeEnd ( ) ;
49
+
50
+ if ( parserResult . isError ) {
51
+ console . log ( parserResult . errorMessage ) ;
52
+ } else {
53
+ console . log ( parserResult . result ) ;
54
+ }
You can’t perform that action at this time.
0 commit comments