diff --git a/parser/grammar.js b/parser/grammar.js index 4b1e8dd9..ea992829 100644 --- a/parser/grammar.js +++ b/parser/grammar.js @@ -3,28 +3,38 @@ (function () { function id(x) { return x[0]; } - const moo = require("moo"); + const moo = require("moo"); - const lexer = moo.compile({ - number: /[0-9]+(?:\.[0-9]+)?/, - string: /'.*?'|".*?"/, - name: {match: /[a-zA-Z]+\w*?/, type: moo.keywords({ - 'keywords': ['if', 'else', 'd'], - })}, - space: {match: /\s+/, lineBreaks: true}, - separators: [',', '.'], - multiplicativeOperator: ['*', '/'], - exponentOperator: ['^'], - additiveOperator: ['+', '-'], - unaryOperator: ['-'], - andOperator: ['&', '&&'], - orOperator: ['|', '||'], - equalityOperator: ['=', '==', '===', '!=', '!=='], - relationalOperator: ['>', '<', '>=', '<='], - brackets: ['(', ')', '{', '}'], - }); + const lexer = moo.compile({ + number: /[0-9]+(?:\.[0-9]+)?/, + string: { + match: /'.*?'|".*?"/, + value: s => s.slice(1, -1), + }, + name: { + match: /[a-zA-Z]+\w*?/, + type: moo.keywords({ + 'keywords': ['if', 'else', 'd'], + }), + }, + space: { + match: /\s+/, + lineBreaks: true, + }, + separators: [',', '.'], + multiplicativeOperator: ['*', '/'], + exponentOperator: ['^'], + additiveOperator: ['+', '-'], + unaryOperator: ['-'], + andOperator: ['&', '&&'], + orOperator: ['|', '||'], + stringDelimiters: ['\"', '\''], + equalityOperator: ['=', '==', '===', '!=', '!=='], + relationalOperator: ['>', '<', '>=', '<='], + brackets: ['(', ')', '{', '}'], + }); - function nuller() { return null; } + function nuller() { return null; } class OperatorNode { constructor({left, right, operator, fn}) { @@ -51,8 +61,8 @@ function id(x) { return x[0]; } class ConstantNode { constructor(value, type){ - this.value = value; this.type = type; + this.value = value; } } var grammar = { diff --git a/parser/grammar.ne b/parser/grammar.ne index 5b8e9459..cca3fa88 100644 --- a/parser/grammar.ne +++ b/parser/grammar.ne @@ -1,29 +1,37 @@ @{% - const moo = require("moo"); + const moo = require("moo"); - const lexer = moo.compile({ - number: /[0-9]+(?:\.[0-9]+)?/, - string: /'.*?'|".*?"/, - name: {match: /[a-zA-Z]+\w*?/, type: moo.keywords({ - 'keywords': ['if', 'else', 'd'], - })}, - space: {match: /\s+/, lineBreaks: true}, - separators: [',', '.'], - multiplicativeOperator: ['*', '/'], - exponentOperator: ['^'], - additiveOperator: ['+', '-'], - unaryOperator: ['-'], - andOperator: ['&', '&&'], - orOperator: ['|', '||'], - equalityOperator: ['=', '==', '===', '!=', '!=='], - relationalOperator: ['>', '<', '>=', '<='], - brackets: ['(', ')', '{', '}'], - }); -%} + const lexer = moo.compile({ + number: /[0-9]+(?:\.[0-9]+)?/, + string: { + match: /'.*?'|".*?"/, + value: s => s.slice(1, -1), + }, + name: { + match: /[a-zA-Z]+\w*?/, + type: moo.keywords({ + 'keywords': ['if', 'else', 'd'], + }), + }, + space: { + match: /\s+/, + lineBreaks: true, + }, + separators: [',', '.'], + multiplicativeOperator: ['*', '/'], + exponentOperator: ['^'], + additiveOperator: ['+', '-'], + unaryOperator: ['-'], + andOperator: ['&', '&&'], + orOperator: ['|', '||'], + stringDelimiters: ['\"', '\''], + equalityOperator: ['=', '==', '===', '!=', '!=='], + relationalOperator: ['>', '<', '>=', '<='], + brackets: ['(', ')', '{', '}'], + }); -@{% function nuller() { return null; } %} + function nuller() { return null; } -@{% class OperatorNode { constructor({left, right, operator, fn}) { this.left = left; @@ -49,8 +57,8 @@ class ConstantNode { constructor(value, type){ - this.value = value; this.type = type; + this.value = value; } } %}