From 48da65d08ef31273afc4de79d099bac21c252d49 Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 19 Apr 2010 20:35:55 +0200 Subject: [PATCH] PEG.buildParser now accepts grammars only in string format. --- lib/compiler.js | 28 +++++++--------------------- test/compiler-test.js | 10 +--------- 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index be60080..9dab87c 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -11,32 +11,18 @@ function nop() {} /* * Generates a parser from a specified grammar and start rule and returns it. * - * The grammar may be either an object or a string. If it is an object, it - * must contain AST of the parsing expressions (i.e. instances of |PEG.Grammar.* - * classes| for the grammar rules in its properties. If it is a string, it is - * parsed using |PEG.grammarParser| to obtain the grammar AST and thus it must - * be in a format that this parser accepts (see the source code for details). + * The grammar must be a string in the format described by the metagramar in the + * metagrammar.pegjs file. The start rule may be unspecified, in which case + * "start" is used. * - * The start rule may be unspecified, in which case "start" is used. - * - * Throws |PEG.Grammar.GrammarError| if the grammar definition is not object nor - * string or if it contains an error. Note that not all errors are detected - * during the generation and some may protrude to the generated parser and cause - * its malfunction. + * Throws |PEG.Grammar.GrammarError| if the grammar contains an error. Note that + * not all errors are detected during the generation and some may protrude to + * the generated parser and cause its malfunction. */ PEG.buildParser = function(grammar, startRule) { startRule = startRule || "start"; - switch (typeof(grammar)) { - case "object": - var ast = grammar; - break; - case "string": - var ast = PEG.grammarParser.parse(grammar); - break; - default: - throw new PEG.Grammar.GrammarError("Grammar must be object or string."); - } + var ast = PEG.grammarParser.parse(grammar); for (var key in ast) { ast[key].checkReferencedRulesExist(ast); diff --git a/test/compiler-test.js b/test/compiler-test.js index 8c7f3b4..b481a79 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -169,17 +169,9 @@ test("generateUniqueIdentifier", function() { module("PEG"); -test("buildParser reports invalid grammar object", function() { - throws( - function() { PEG.buildParser(42); }, - PEG.Grammar.GrammarError, - { message: "Grammar must be object or string." } - ); -}); - test("buildParser reports missing start rule", function() { throws( - function() { PEG.buildParser({}); }, + function() { PEG.buildParser('notStart: "abcd"'); }, PEG.Grammar.GrammarError, { message: "Missing \"start\" rule." } );