All files / egg-parser/src build-ast.js

95.45% Statements 21/22
100% Branches 6/6
66.66% Functions 4/6
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                          37x                         1x                                     287x 184x 184x 184x 184x 184x 184x     103x           103x 98x     5x 7x 7x           5x                       110x 103x             1x              
/**
 * 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
};