AST refactoring 6/6: Get rid of the |Grammar| namespace
This commit is contained in:
parent
b4bf49443a
commit
76ed63c86e
|
@ -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) {
|
||||
|
@ -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. */
|
||||
|
@ -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 = {
|
||||
|
@ -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. */
|
||||
|
@ -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."
|
||||
);
|
||||
}
|
||||
|
@ -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."
|
||||
);
|
||||
}
|
||||
|
@ -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 + "\"."
|
||||
);
|
||||
}
|
||||
|
@ -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) {
|
||||
/*
|
||||
|
|
|
@ -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." }
|
||||
);
|
||||
});
|
||||
|
@ -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." }
|
||||
);
|
||||
});
|
||||
|
@ -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\"." }
|
||||
);
|
||||
});
|
||||
|
|
|
@ -16,7 +16,6 @@ global.grammarParserDoesNotParse = function(input) {
|
|||
|
||||
module("Grammar Parser");
|
||||
|
||||
with (PEG.Grammar) {
|
||||
function rule(name, displayName, expression) {
|
||||
return {
|
||||
type: "rule",
|
||||
|
@ -515,6 +514,5 @@ with (PEG.Grammar) {
|
|||
grammarParserParses('start:\u205F"abcd"', simpleGrammar);
|
||||
grammarParserParses('start:\u3000"abcd"', simpleGrammar);
|
||||
});
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
Loading…
Reference in a new issue