AST refactoring 6/6: Get rid of the |Grammar| namespace

redux
David Majda 14 years ago
parent b4bf49443a
commit 76ed63c86e

@ -14,8 +14,8 @@
* "start" is used. * "start" is used.
* *
* Throws |PEG.grammarParser.SyntaxError| if the grammar contains a syntax error * Throws |PEG.grammarParser.SyntaxError| if the grammar contains a syntax error
* or |PEG.Grammar.GrammarError| if it contains a semantic error. Note that not * or |PEG.GrammarError| if it contains a semantic error. Note that not all
* all errors are detected during the generation and some may protrude to the * errors are detected during the generation and some may protrude to the
* generated parser and cause its malfunction. * generated parser and cause its malfunction.
*/ */
PEG.buildParser = function(grammar, startRule) { PEG.buildParser = function(grammar, startRule) {
@ -27,6 +27,17 @@ PEG.buildParser = function(grammar, startRule) {
); );
}; };
/* ===== PEG.GrammarError ===== */
/* Thrown when the grammar contains an error. */
PEG.GrammarError = function(message) {
this.name = "PEG.GrammarError";
this.message = message;
};
PEG.GrammarError.prototype = Error.prototype;
/* ===== PEG.ArrayUtils ===== */ /* ===== PEG.ArrayUtils ===== */
/* Array manipulation utility functions. */ /* Array manipulation utility functions. */
@ -136,23 +147,6 @@ PEG.RegExpUtils = {
} }
}; };
/* ===== PEG.Grammar ===== */
/* Namespace with grammar AST nodes. */
PEG.Grammar = {};
/* ===== PEG.Grammar.GrammarError ===== */
/* Thrown when the grammar contains an error. */
PEG.Grammar.GrammarError = function(message) {
this.name = "PEG.Grammar.GrammarError";
this.message = message;
};
PEG.Grammar.GrammarError.prototype = Error.prototype;
/* ===== PEG.Compiler ===== */ /* ===== PEG.Compiler ===== */
PEG.Compiler = { PEG.Compiler = {
@ -261,8 +255,8 @@ PEG.Compiler = {
* Checks made on the grammar AST before compilation. Each check is a function * Checks made on the grammar AST before compilation. Each check is a function
* that is passed the AST and start rule and does not return anything. If the * that is passed the AST and start rule and does not return anything. If the
* check passes, the function does not do anything special, otherwise it * check passes, the function does not do anything special, otherwise it
* throws |PEG.Grammar.GrammarError|. The checks are run in sequence in order * throws |PEG.GrammarError|. The checks are run in sequence in order of their
* of their definition. * definition.
*/ */
_checks: [ _checks: [
/* Checks that all referenced rules exist. */ /* Checks that all referenced rules exist. */
@ -291,7 +285,7 @@ PEG.Compiler = {
rule_ref: rule_ref:
function(node) { function(node) {
if (ast[node.name] === undefined) { if (ast[node.name] === undefined) {
throw new PEG.Grammar.GrammarError( throw new PEG.GrammarError(
"Referenced rule \"" + node.name + "\" does not exist." "Referenced rule \"" + node.name + "\" does not exist."
); );
} }
@ -312,7 +306,7 @@ PEG.Compiler = {
/* Checks that the start rule is defined. */ /* Checks that the start rule is defined. */
function(ast, startRule) { function(ast, startRule) {
if (ast[startRule] === undefined) { if (ast[startRule] === undefined) {
throw new PEG.Grammar.GrammarError( throw new PEG.GrammarError(
"Missing \"" + startRule + "\" rule." "Missing \"" + startRule + "\" rule."
); );
} }
@ -356,7 +350,7 @@ PEG.Compiler = {
rule_ref: rule_ref:
function(node, appliedRules) { function(node, appliedRules) {
if (PEG.ArrayUtils.contains(appliedRules, node.name)) { if (PEG.ArrayUtils.contains(appliedRules, node.name)) {
throw new PEG.Grammar.GrammarError( throw new PEG.GrammarError(
"Left recursion detected for rule \"" + node.name + "\"." "Left recursion detected for rule \"" + node.name + "\"."
); );
} }
@ -771,9 +765,9 @@ PEG.Compiler = {
/* /*
* Generates a parser from a specified grammar AST and start rule. Throws * Generates a parser from a specified grammar AST and start rule. Throws
* |PEG.Grammar.GrammarError| if the AST contains a semantic error. Note that * |PEG.GrammarError| if the AST contains a semantic error. Note that not all
* not all errors are detected during the generation and some may protrude to * errors are detected during the generation and some may protrude to the
* the generated parser and cause its malfunction. * generated parser and cause its malfunction.
*/ */
compileParser: function(ast, startRule) { compileParser: function(ast, startRule) {
/* /*

@ -194,7 +194,7 @@ test("buildParser reports syntax errors in the grammar", function() {
test("buildParser reports missing start rule", function() { test("buildParser reports missing start rule", function() {
throws( throws(
function() { PEG.buildParser('notStart: "abcd"'); }, function() { PEG.buildParser('notStart: "abcd"'); },
PEG.Grammar.GrammarError, PEG.GrammarError,
{ message: "Missing \"start\" rule." } { message: "Missing \"start\" rule." }
); );
}); });
@ -217,7 +217,7 @@ test("buildParser reports missing referenced rules", function() {
PEG.ArrayUtils.each(grammars, function(grammar) { PEG.ArrayUtils.each(grammars, function(grammar) {
throws( throws(
function() { PEG.buildParser(grammar); }, function() { PEG.buildParser(grammar); },
PEG.Grammar.GrammarError, PEG.GrammarError,
{ message: "Referenced rule \"missing\" does not exist." } { message: "Referenced rule \"missing\" does not exist." }
); );
}); });
@ -244,7 +244,7 @@ test("buildParser reports left recursion", function() {
PEG.ArrayUtils.each(grammars, function(grammar) { PEG.ArrayUtils.each(grammars, function(grammar) {
throws( throws(
function() { PEG.buildParser(grammar); }, function() { PEG.buildParser(grammar); },
PEG.Grammar.GrammarError, PEG.GrammarError,
{ message: "Left recursion detected for rule \"start\"." } { message: "Left recursion detected for rule \"start\"." }
); );
}); });

@ -16,7 +16,6 @@ global.grammarParserDoesNotParse = function(input) {
module("Grammar Parser"); module("Grammar Parser");
with (PEG.Grammar) {
function rule(name, displayName, expression) { function rule(name, displayName, expression) {
return { return {
type: "rule", type: "rule",
@ -515,6 +514,5 @@ with (PEG.Grammar) {
grammarParserParses('start:\u205F"abcd"', simpleGrammar); grammarParserParses('start:\u205F"abcd"', simpleGrammar);
grammarParserParses('start:\u3000"abcd"', simpleGrammar); grammarParserParses('start:\u3000"abcd"', simpleGrammar);
}); });
}
})(); })();

Loading…
Cancel
Save