Moved parser javascript into single block

This commit is contained in:
Stefan Zermatten
2019-03-27 11:25:34 +02:00
parent ee788c952a
commit 484b73d836
2 changed files with 62 additions and 44 deletions

View File

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

View File

@@ -1,29 +1,37 @@
@{% @{%
const moo = require("moo"); const moo = require("moo");
const lexer = moo.compile({ const lexer = moo.compile({
number: /[0-9]+(?:\.[0-9]+)?/, number: /[0-9]+(?:\.[0-9]+)?/,
string: /'.*?'|".*?"/, string: {
name: {match: /[a-zA-Z]+\w*?/, type: moo.keywords({ match: /'.*?'|".*?"/,
'keywords': ['if', 'else', 'd'], value: s => s.slice(1, -1),
})}, },
space: {match: /\s+/, lineBreaks: true}, name: {
separators: [',', '.'], match: /[a-zA-Z]+\w*?/,
multiplicativeOperator: ['*', '/'], type: moo.keywords({
exponentOperator: ['^'], 'keywords': ['if', 'else', 'd'],
additiveOperator: ['+', '-'], }),
unaryOperator: ['-'], },
andOperator: ['&', '&&'], space: {
orOperator: ['|', '||'], match: /\s+/,
equalityOperator: ['=', '==', '===', '!=', '!=='], lineBreaks: true,
relationalOperator: ['>', '<', '>=', '<='], },
brackets: ['(', ')', '{', '}'], separators: [',', '.'],
}); multiplicativeOperator: ['*', '/'],
%} exponentOperator: ['^'],
additiveOperator: ['+', '-'],
unaryOperator: ['-'],
andOperator: ['&', '&&'],
orOperator: ['|', '||'],
stringDelimiters: ['\"', '\''],
equalityOperator: ['=', '==', '===', '!=', '!=='],
relationalOperator: ['>', '<', '>=', '<='],
brackets: ['(', ')', '{', '}'],
});
@{% function nuller() { return null; } %} function nuller() { return null; }
@{%
class OperatorNode { class OperatorNode {
constructor({left, right, operator, fn}) { constructor({left, right, operator, fn}) {
this.left = left; this.left = left;
@@ -49,8 +57,8 @@
class ConstantNode { class ConstantNode {
constructor(value, type){ constructor(value, type){
this.value = value;
this.type = type; this.type = type;
this.value = value;
} }
} }
%} %}