Skip to content

Commit

Permalink
refactor(parser): create lexer to hold lexing logic (#2)
Browse files Browse the repository at this point in the history
* refactor(parser): create lexer to hold lexing logic

Create separate class for lexing
Refactor language spec for variables and functions.
Add support for comments.

* refactor: add support for more function features

Add support for spread operators.
Add support for calling prototype functions.
  • Loading branch information
koladilip authored Nov 10, 2022
1 parent 0e7a4d4 commit 01b0c1a
Show file tree
Hide file tree
Showing 14 changed files with 8,100 additions and 910 deletions.
37 changes: 37 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Test Current File",
"program": "${relativeFile}",
"request": "launch",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"skipFiles": ["<node_internals>/**"],
"type": "node"
},
{
"name": "Test Extractor",
"program": "${workspaceFolder}/test/test_extractor.ts",
"request": "launch",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"skipFiles": ["<node_internals>/**"],
"type": "node"
},
{
"runtimeExecutable": "/usr/local/bin/node",
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${fileBasenameNoExtension}", "--config", "jest.config.ts"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
}
]
}
6,895 changes: 6,894 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"clean": "rm -rf build",
"lint:fix": "eslint . --fix",
"lint:check": "eslint . || exit 1",
"format": "prettier --write '**/*.ts' '**/*.json'",
"format": "prettier --write '**/*.ts' '**/*.js' '**/*.json'",
"prepare": "husky install"
},
"engines": {
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const VARS_PREFIX = '___';
export const DATA_PARAM_KEY = '___d';
export const BINDINGS_PARAM_KEY = '___b';
7 changes: 5 additions & 2 deletions src/engine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BINDINGS_PARAM_KEY, DATA_PARAM_KEY } from './constants';
import { JsonTemplateLexer } from './lexer';
import { JsonTemplateParser } from './parser';
import { JsonTemplateTranslator } from './translator';

Expand All @@ -7,9 +9,10 @@ export class JsonTemplateEngine {
this.fn = JsonTemplateEngine.compile(template);
}
private static compile(template: string) {
const parser = new JsonTemplateParser(template);
const lexer = new JsonTemplateLexer(template);
const parser = new JsonTemplateParser(lexer);
const translator = new JsonTemplateTranslator(parser.parse());
return new Function('data', 'bindings', translator.translate());
return new Function(DATA_PARAM_KEY, BINDINGS_PARAM_KEY, translator.translate());
}

evaluate(data: any, bindings: any = {}) {
Expand Down
4 changes: 2 additions & 2 deletions src/error.ts → src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export class JsonTemplateParserError extends Error {
export class JsonTemplateLexerError extends Error {
readonly column: number;
constructor(message: string, column: number) {
super(message);
this.column = column;
}
}

export class JsosTemplateEngineError extends Error {
export class JsosTemplateParserError extends Error {
constructor(message: string) {
super(message);
}
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './error';
export * from './constants';
export * from './engine';
export * from './errors';
export * from './lexer';
export * from './operators';
export * from './parser';
export * from './translator';
Expand Down
Loading

0 comments on commit 01b0c1a

Please sign in to comment.