Skip to content

Commit

Permalink
It was a long time
Browse files Browse the repository at this point in the history
  • Loading branch information
Pliavi committed Jul 27, 2019
1 parent ed6221f commit e2b21cd
Show file tree
Hide file tree
Showing 17 changed files with 5,144 additions and 228 deletions.
11 changes: 11 additions & 0 deletions .babelrc
@@ -0,0 +1,11 @@
{
"presets": [
[ "@babel/preset-env" ,
{
"targets": {
"esmodules": true
}
}
]
]
}
18 changes: 9 additions & 9 deletions docs/diagrams/railroad.html
Expand Up @@ -23,15 +23,15 @@
{
"type": "Rule",
"name": "program",
"orgText": "function () {\n _this.MANY(function () {\n _this.SUBRULE(_this.function);\n });\n }",
"orgText": "() => {\n $.MANY(() => {\n $.SUBRULE($.declaration);\n });\n }",
"definition": [
{
"type": "Repetition",
"idx": 0,
"definition": [
{
"type": "NonTerminal",
"name": "function",
"name": "declaration",
"idx": 0
}
]
Expand All @@ -40,8 +40,8 @@
},
{
"type": "Rule",
"name": "function",
"orgText": "function () {\n _this.CONSUME(Tokens_1.IDENTIFIER);\n _this.CONSUME(Tokens_1.L_PAREN);\n _this.MANY_SEP({\n SEP: Tokens_1.COMMA,\n DEF: function () {\n _this.SUBRULE(_this.value, { ARGS: [true] });\n }\n });\n _this.CONSUME(Tokens_1.R_PAREN);\n _this.CONSUME(Tokens_1.RETURN_WORD);\n _this.SUBRULE(_this.expression);\n }",
"name": "declaration",
"orgText": "() => {\n $.CONSUME(_Tokens.IDENTIFIER, {\n LABEL: \"functionName\"\n });\n $.CONSUME(_Tokens.L_PAREN);\n $.MANY_SEP({\n SEP: _Tokens.COMMA,\n DEF: () => {\n $.SUBRULE($.value, {\n ARGS: [true],\n LABEL: \"params\"\n });\n }\n });\n $.CONSUME(_Tokens.R_PAREN);\n $.CONSUME(_Tokens.RETURN_WORD);\n $.SUBRULE($.expression, {\n LABEL: \"body\"\n });\n }",
"definition": [
{
"type": "Terminal",
Expand Down Expand Up @@ -99,7 +99,7 @@
{
"type": "Rule",
"name": "expression",
"orgText": "function () {\n _this.SUBRULE(_this.value);\n _this.OPTION(function () {\n _this.SUBRULE(_this.mathExpression);\n });\n }",
"orgText": "() => {\n $.SUBRULE($.value, {\n LABEL: \"left\"\n });\n $.OPTION(() => {\n $.SUBRULE($.mathExpression, {\n LABEL: \"right\"\n });\n });\n }",
"definition": [
{
"type": "NonTerminal",
Expand All @@ -122,7 +122,7 @@
{
"type": "Rule",
"name": "mathExpression",
"orgText": "function () {\n _this.SUBRULE(_this.binaryOperator);\n _this.SUBRULE(_this.expression);\n }",
"orgText": "() => {\n $.SUBRULE($.binaryOperator, {\n LABEL: \"operator\"\n });\n $.SUBRULE($.expression, {\n LABEL: \"right\"\n });\n }",
"definition": [
{
"type": "NonTerminal",
Expand All @@ -139,7 +139,7 @@
{
"type": "Rule",
"name": "value",
"orgText": "function (isFunctionPermited) {\n if (isFunctionPermited === void 0) { isFunctionPermited = false; }\n _this.OR([\n {\n GATE: isFunctionPermited,\n ALT: function () {\n _this.SUBRULE(_this.functionCall);\n }\n },\n {\n ALT: function () {\n _this.CONSUME(Tokens_1.NUMBER_LITERAL);\n }\n },\n {\n ALT: function () {\n _this.CONSUME(Tokens_1.STRING_LITERAL);\n }\n },\n {\n ALT: function () {\n _this.CONSUME(Tokens_1.TRUE);\n }\n },\n {\n ALT: function () {\n _this.CONSUME(Tokens_1.FALSE);\n }\n },\n {\n ALT: function () {\n _this.CONSUME(Tokens_1.NULL);\n }\n },\n {\n ALT: function () {\n _this.CONSUME2(Tokens_1.IDENTIFIER);\n }\n }\n ]);\n }",
"orgText": "function () {\n let isFunctionPermited = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;\n $.OR([{\n GATE: isFunctionPermited,\n ALT: () => {\n $.SUBRULE($.functionCall);\n }\n }, {\n ALT: () => {\n $.CONSUME(_Tokens.NUMBER_LITERAL);\n }\n }, {\n ALT: () => {\n $.CONSUME(_Tokens.STRING_LITERAL);\n }\n }, {\n ALT: () => {\n $.CONSUME(_Tokens.TRUE);\n }\n }, {\n ALT: () => {\n $.CONSUME(_Tokens.FALSE);\n }\n }, {\n ALT: () => {\n $.CONSUME(_Tokens.NULL);\n }\n }, {\n ALT: () => {\n $.CONSUME2(_Tokens.IDENTIFIER);\n }\n }]);\n }",
"definition": [
{
"type": "Alternation",
Expand Down Expand Up @@ -234,7 +234,7 @@
{
"type": "Rule",
"name": "binaryOperator",
"orgText": "function () {\n _this.OR([\n {\n ALT: function () {\n _this.CONSUME(Tokens_1.ADDITIVE_OPERATOR);\n }\n },\n {\n ALT: function () {\n _this.CONSUME(Tokens_1.MULTIPLICATIVE_OPERATOR);\n }\n }\n ]);\n }",
"orgText": "() => {\n $.OR([{\n ALT: () => {\n $.CONSUME(_Tokens.ADDITIVE_OPERATOR, {\n LABEL: \"operator\"\n });\n }\n }, {\n ALT: () => {\n $.CONSUME(_Tokens.MULTIPLICATIVE_OPERATOR, {\n LABEL: \"operator\"\n });\n }\n }]);\n }",
"definition": [
{
"type": "Alternation",
Expand Down Expand Up @@ -271,7 +271,7 @@
{
"type": "Rule",
"name": "functionCall",
"orgText": "function () {\n _this.CONSUME(Tokens_1.IDENTIFIER);\n _this.CONSUME(Tokens_1.L_PAREN);\n _this.MANY_SEP({\n SEP: Tokens_1.COMMA,\n DEF: function () {\n // console.log(this.LA(2));\n _this.SUBRULE(_this.expression);\n }\n });\n _this.CONSUME(Tokens_1.R_PAREN);\n }",
"orgText": "() => {\n $.CONSUME(_Tokens.IDENTIFIER);\n $.CONSUME(_Tokens.L_PAREN);\n $.MANY_SEP({\n SEP: _Tokens.COMMA,\n DEF: () => {\n $.SUBRULE($.expression, {\n LABEL: \"functionArguments\"\n });\n }\n });\n $.CONSUME(_Tokens.R_PAREN);\n }",
"definition": [
{
"type": "Terminal",
Expand Down
5 changes: 5 additions & 0 deletions nodemon.json
@@ -0,0 +1,5 @@
{
"watch": ["src"],
"ext": "js",
"exec": "babel-node ./src/main.js"
}
16 changes: 11 additions & 5 deletions package.json
@@ -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"
}
}
34 changes: 34 additions & 0 deletions src/BaseVisitor.js
@@ -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))
: []
}

}
144 changes: 144 additions & 0 deletions src/Parser.js
@@ -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();
}
}

0 comments on commit e2b21cd

Please sign in to comment.