Skip to content

Commit

Permalink
feat: add new parser file to use rudus
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikossor committed Jul 10, 2022
1 parent fe74c7a commit 396828b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/parser/JavaScript-rudus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
anyOf,
regex,
sequenceOf,
string,
optional,
whitespace,
between,
} from "rudus";

console.time();

const betweenDoubleQuotes = between(string('"'));
const optionalWhitespace = optional(whitespace());

const declarationKeyword = regex(/^var|let|const/);
const word = regex(/\w+/); // word
const equalSign = string("=");

const literalBool = regex(/true|false/);
const literalUndefined = regex(/undefined/);
const literalNull = regex(/null/);
const literalNumber = regex(/^(\d+\.)?\d+$|^0b[0-1]+(n)?$/);
const literalString = betweenDoubleQuotes(word);

const variableValue = anyOf([
literalBool,
literalString,
literalNumber,
literalUndefined,
literalNull,
]);

const variableParser = sequenceOf([
declarationKeyword.map(
state => `<span style="color:red">${state.result}</span>`,
),
optionalWhitespace,
word.map(state => `<span style="color:green">${state.result}</span>`),
optionalWhitespace,
equalSign.map(state => `<span style="color:grey">${state.result}</span>`),
optionalWhitespace,
variableValue.map(state => `<span style="color:blue">${state.result}</span>`),
]);

const parserResult = variableParser.run("const isCool = null");

console.timeEnd();

if (parserResult.isError) {
console.log(parserResult.errorMessage);
} else {
console.log(parserResult.result);
}

0 comments on commit 396828b

Please sign in to comment.