Skip to content

Commit

Permalink
AST refactoring 6/6: Get rid of the |Grammar| namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dmajda committed May 21, 2010
1 parent b4bf494 commit 76ed63c
Show file tree
Hide file tree
Showing 3 changed files with 496 additions and 504 deletions.
48 changes: 21 additions & 27 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* "start" is used.
*
* Throws |PEG.grammarParser.SyntaxError| if the grammar contains a syntax error
* or |PEG.Grammar.GrammarError| if it contains a semantic error. Note that not
* all errors are detected during the generation and some may protrude to the
* or |PEG.GrammarError| if it contains a semantic 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) {
Expand All @@ -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 ===== */

/* Array manipulation utility functions. */
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -261,8 +255,8 @@ PEG.Compiler = {
* 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
* check passes, the function does not do anything special, otherwise it
* throws |PEG.Grammar.GrammarError|. The checks are run in sequence in order
* of their definition.
* throws |PEG.GrammarError|. The checks are run in sequence in order of their
* definition.
*/
_checks: [
/* Checks that all referenced rules exist. */
Expand Down Expand Up @@ -291,7 +285,7 @@ PEG.Compiler = {
rule_ref:
function(node) {
if (ast[node.name] === undefined) {
throw new PEG.Grammar.GrammarError(
throw new PEG.GrammarError(
"Referenced rule \"" + node.name + "\" does not exist."
);
}
Expand All @@ -312,7 +306,7 @@ PEG.Compiler = {
/* Checks that the start rule is defined. */
function(ast, startRule) {
if (ast[startRule] === undefined) {
throw new PEG.Grammar.GrammarError(
throw new PEG.GrammarError(
"Missing \"" + startRule + "\" rule."
);
}
Expand Down Expand Up @@ -356,7 +350,7 @@ PEG.Compiler = {
rule_ref:
function(node, appliedRules) {
if (PEG.ArrayUtils.contains(appliedRules, node.name)) {
throw new PEG.Grammar.GrammarError(
throw new PEG.GrammarError(
"Left recursion detected for rule \"" + node.name + "\"."
);
}
Expand Down Expand Up @@ -771,9 +765,9 @@ PEG.Compiler = {

/*
* Generates a parser from a specified grammar AST and start rule. Throws
* |PEG.Grammar.GrammarError| if the AST contains a semantic error. Note that
* not all errors are detected during the generation and some may protrude to
* the generated parser and cause its malfunction.
* |PEG.GrammarError| if the AST contains a semantic error. Note that not all
* errors are detected during the generation and some may protrude to the
* generated parser and cause its malfunction.
*/
compileParser: function(ast, startRule) {
/*
Expand Down
6 changes: 3 additions & 3 deletions test/compiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ test("buildParser reports syntax errors in the grammar", function() {
test("buildParser reports missing start rule", function() {
throws(
function() { PEG.buildParser('notStart: "abcd"'); },
PEG.Grammar.GrammarError,
PEG.GrammarError,
{ message: "Missing \"start\" rule." }
);
});
Expand All @@ -217,7 +217,7 @@ test("buildParser reports missing referenced rules", function() {
PEG.ArrayUtils.each(grammars, function(grammar) {
throws(
function() { PEG.buildParser(grammar); },
PEG.Grammar.GrammarError,
PEG.GrammarError,
{ message: "Referenced rule \"missing\" does not exist." }
);
});
Expand All @@ -244,7 +244,7 @@ test("buildParser reports left recursion", function() {
PEG.ArrayUtils.each(grammars, function(grammar) {
throws(
function() { PEG.buildParser(grammar); },
PEG.Grammar.GrammarError,
PEG.GrammarError,
{ message: "Left recursion detected for rule \"start\"." }
);
});
Expand Down

0 comments on commit 76ed63c

Please sign in to comment.