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

Repository for ITP Fall 2015 Course
101 stars 39 forks source link

JSON grammar doesn't match rules object in ContextFree #23

Open shiffman opened 9 years ago

shiffman commented 9 years ago

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;