Skip to content

Commit 396828b

Browse files
committed
feat: add new parser file to use rudus
#119
1 parent fe74c7a commit 396828b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/parser/JavaScript-rudus.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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(/^var|let|const/);
17+
const word = regex(/\w+/); // word
18+
const equalSign = string("=");
19+
20+
const literalBool = regex(/true|false/);
21+
const literalUndefined = regex(/undefined/);
22+
const literalNull = regex(/null/);
23+
const literalNumber = regex(/^(\d+\.)?\d+$|^0b[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+
}

0 commit comments

Comments
 (0)