esther-medina-quintero-parser-nearley
Version:
100 lines (87 loc) • 2.06 kB
JavaScript
/**
* The ASTs of the Egg Lang
* @external Grammar
* @see {@link https://ull-esit-pl-2122.github.io/temas/syntax-analysis/ast.html#gramatica-informal-de-los-arboles-del-parser-de-egg}
*/
/**
* A function that builds a number value
* @param {Token} token The token to be built
* @return {Object} The number value
*/
function buildNumberValue([token]) {
// console.log(token)
return {
type: "value",
value: token.value,
raw: token.text,
};
}
/**
* A function that builds a string value
* @param {Token} token The token to be built
* @return {Object} The string value
*/
function buildStringValue([token]) {
return {
type: "value",
value: token.value.replace(/^"|"$/g, ""),
raw: token.text,
};
}
function buildArrayValue([token]) {
}
/**
* A function that builds a word applies
* @param {Array} word The word to be built
* @param {Array} applies The applies to be built
* @return {Object} The word applies
*/
function buildWordApplies([word, applies]) {
if (applies == null) {
word.type = "word";
word.name = word.value;
delete(word.value);
delete(word.text);
delete(word.toString);
return word;
}
let ast = {
type: "apply",
operator: word,
args: applies[0],
}
if (applies.length == 1) {
return ast;
}
for (let i = 1; i < applies.length; i++) {
let oldAst = ast;
ast = {
type: "apply",
operator: oldAst,
args: applies[i],
};
}
return ast;
}
/**
* A function that builds nested applies
* @param {Array} parentExp The parent expression to be built
* @param {Array} applies The applies to be built
* @return {Array} The nested applies
*/
function buildNestedApplies([parentExp, applies]) {
// console.log(parentExp);
// console.log(applies);
if (applies) return [parentExp].concat(applies);
return [parentExp];
}
function buildArrayApplies([_, applies]) {
return applies;
}
module.exports = {
buildNumberValue,
buildStringValue,
buildWordApplies,
buildArrayValue,
buildNestedApplies
};