Programming-from-A-to-Z / A2Z-F15

Repository for ITP Fall 2015 Course

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON grammar doesn't match rules object in ContextFree

shiffman opened this issue · comments

In week 8 cf.js examples with JSON, it would be nice to be able to avoid this extra step to parse the JSON file into the rules object.

  // Look at the JSON object
  for (var rule in grammar) {
    // Get the expansions and split them
    var expansions = grammar[rule];
    for (var j = 0; j < expansions.length; j++) {
      // Now split up each expansion into its own array
      // Internally, the CF object is using arrays of arrays
      // It's awkward to look at it that in the JSON file so split by whitespace
      var tokens = expansions[j].split(/\s+/);
      // Add the rule
      cfree.addRule(rule, tokens);
    }
  }

and just say:

  cfree.rules = grammar;