Skip to content

Commit

Permalink
feat: add arrowFunctionExpression (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikossor committed Jul 25, 2022
1 parent 061b188 commit 1c5b210
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/parser/JavaScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import {
optional,
whitespace,
between,
lazy,
} from "rudus";
import { NodeType } from "../enums";
import { IParser } from "../Interfaces";

const betweenDoubleQuotes = between(string('"'));
const betweenParanthesis = between(string("("), string(")"));
const betweenBraces = between(string("{"), string("}"));

const optionalWhitespace = optional(whitespace()).map(state => [
NodeType.Whitespace,
state.result,
Expand All @@ -24,14 +28,26 @@ const optionalSemicolon = optional(string(";")).map(state => [
const declarationKeyword = regex(/^var|let|const/);
const word = regex(/\w+/); // word
const equalSign = string("=");
const bigArrow = 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 functionBody = lazy(() => literalNumber); // for testing purposes

const arrowFunctionExpression = sequenceOf([
betweenParanthesis(optional(word)),
optionalWhitespace,
bigArrow,
optionalWhitespace,
betweenBraces(optional(functionBody)),
]);

const variableValue = anyOf([
arrowFunctionExpression.map(state => [NodeType.CallExpression, state.result]),
literalBool.map(state => [NodeType.BooleanLiteral, state.result]),
literalString.map(state => [NodeType.StringLiteral, state.result]),
literalNumber.map(state => [NodeType.NumberLiteral, state.result]),
Expand Down

0 comments on commit 1c5b210

Please sign in to comment.