Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
5,144 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"presets": [ | ||
[ "@babel/preset-env" , | ||
{ | ||
"targets": { | ||
"esmodules": true | ||
} | ||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"watch": ["src"], | ||
"ext": "js", | ||
"exec": "babel-node ./src/main.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
{ | ||
"name": "typescriptPRT", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"main": "main.js", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^12.0.10", | ||
"typescript": "^3.5.2", | ||
"ts-node": "^8.3.0" | ||
"@babel/node": "^7.4.5", | ||
"@babel/preset-env": "^7.4.5", | ||
"nodemon": "^1.19.1", | ||
"typescript": "^3.5.2" | ||
}, | ||
"scripts": { | ||
"dev": "ts-node ./src/main.ts" | ||
"start": "babel-node ./src/main.js", | ||
"dev": "nodemon" | ||
}, | ||
"dependencies": { | ||
"@babel/core": "^7.4.5", | ||
"chevrotain": "^4.8.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Parser from "./Parser"; | ||
|
||
const parserInstance = new Parser(); | ||
const BaseVisitor = parserInstance.getBaseCstVisitorConstructor(); | ||
|
||
export default class Visitor extends BaseVisitor { | ||
|
||
/** | ||
* @returns {string} | ||
*/ | ||
getImage(contextNode) { | ||
return contextNode[0].image | ||
} | ||
|
||
/** | ||
* @return {Array} | ||
*/ | ||
createOrPush(arr, id, value) { | ||
arr[id] = arr[id] ? arr[id] : [] | ||
arr[id].push(value) | ||
|
||
return arr; | ||
} | ||
|
||
/** | ||
* @return {Array} | ||
*/ | ||
visitAll(contextNode){ | ||
return contextNode | ||
? contextNode.map(node => this.visit(node)) | ||
: [] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { CstParser, IToken } from "chevrotain"; | ||
import { | ||
TRUE, | ||
FALSE, | ||
NULL, | ||
L_CURLY, | ||
R_CURLY, | ||
L_SQUARE, | ||
R_SQUARE, | ||
L_PAREN, | ||
R_PAREN, | ||
COMMA, | ||
COLON, | ||
STRING_LITERAL, | ||
NUMBER_LITERAL, | ||
UNARY_OPERATOR, | ||
ADDITIVE_OPERATOR, | ||
PLUS, | ||
MINUS, | ||
MULTIPLICATIVE_OPERATOR, | ||
ASTERISK, | ||
SLASH, | ||
EQUALITY_OPERATOR, | ||
EQUAL, | ||
FAT_ARROW, | ||
FUNCTION_WORD, | ||
RETURN_WORD, | ||
WHITE_SPACE, | ||
IDENTIFIER, | ||
ALL_TOKENS | ||
} from "./Tokens"; | ||
|
||
export default class PRTParser extends CstParser { | ||
constructor(input) { | ||
super(ALL_TOKENS); | ||
if (input) { | ||
this.input = input; | ||
} | ||
|
||
const $ = this; | ||
|
||
$.RULE("program", () => { | ||
$.MANY(() => { | ||
$.SUBRULE($.declaration); | ||
}); | ||
}); | ||
|
||
$.RULE("declaration", () => { | ||
$.CONSUME(IDENTIFIER, { LABEL: "functionName" }); | ||
$.CONSUME(L_PAREN); | ||
$.MANY_SEP({ | ||
SEP: COMMA, | ||
DEF: () => { | ||
$.SUBRULE($.value, { ARGS: [true], LABEL: "params" }); | ||
} | ||
}); | ||
$.CONSUME(R_PAREN); | ||
$.CONSUME(RETURN_WORD); | ||
$.SUBRULE($.expression, { LABEL: "body" }); | ||
}); | ||
|
||
$.RULE("expression", () => { | ||
$.SUBRULE($.value, { LABEL: "left" }); | ||
$.OPTION(() => { | ||
$.SUBRULE($.mathExpression, { LABEL: "right" }); | ||
}); | ||
}); | ||
|
||
$.RULE("mathExpression", () => { | ||
$.SUBRULE($.binaryOperator, { LABEL: "operator" }); | ||
$.SUBRULE($.expression, { LABEL: "right" }); | ||
}); | ||
|
||
$.RULE("value", (isFunctionPermited = false) => { | ||
$.OR([ | ||
{ | ||
GATE: isFunctionPermited, | ||
ALT: () => { | ||
$.SUBRULE($.functionCall); | ||
} | ||
}, | ||
{ | ||
ALT: () => { | ||
$.CONSUME(NUMBER_LITERAL); | ||
} | ||
}, | ||
{ | ||
ALT: () => { | ||
$.CONSUME(STRING_LITERAL); | ||
} | ||
}, | ||
{ | ||
ALT: () => { | ||
$.CONSUME(TRUE); | ||
} | ||
}, | ||
{ | ||
ALT: () => { | ||
$.CONSUME(FALSE); | ||
} | ||
}, | ||
{ | ||
ALT: () => { | ||
$.CONSUME(NULL); | ||
} | ||
}, | ||
{ | ||
ALT: () => { | ||
$.CONSUME2(IDENTIFIER); | ||
} | ||
} | ||
]); | ||
}); | ||
|
||
$.RULE("binaryOperator", () => { | ||
$.OR([ | ||
{ | ||
ALT: () => { | ||
$.CONSUME(ADDITIVE_OPERATOR, { LABEL: "operator"}); | ||
} | ||
}, | ||
{ | ||
ALT: () => { | ||
$.CONSUME(MULTIPLICATIVE_OPERATOR, { LABEL: "operator"}); | ||
} | ||
} | ||
]); | ||
}); | ||
|
||
$.RULE("functionCall", () => { | ||
$.CONSUME(IDENTIFIER); | ||
$.CONSUME(L_PAREN); | ||
$.MANY_SEP({ | ||
SEP: COMMA, | ||
DEF: () => { | ||
$.SUBRULE($.expression, { LABEL: "functionArguments" }); | ||
} | ||
}); | ||
$.CONSUME(R_PAREN); | ||
}); | ||
|
||
this.performSelfAnalysis(); | ||
} | ||
} |
Oops, something went wrong.