Jasmine: Change |oneRuleGrammar| parameter handling

This change makes code using |oneRuleGrammar| less verbose + prepares
for passing of the initializer (will be used by code added in the next
few commits).
redux
David Majda 12 years ago
parent 171d62fce4
commit a49674b05f

@ -22,10 +22,13 @@ describe("PEG.js grammar parser", function() {
alternatives: [literalAbcd, literalEfgh, literalIjkl] alternatives: [literalAbcd, literalEfgh, literalIjkl]
}; };
function oneRuleGrammar(displayName, expression) { function oneRuleGrammar(expression) {
var initializer = arguments.length > 1 ? arguments[1] : null,
displayName = arguments.length > 2 ? arguments[2] : null;
return { return {
type: "grammar", type: "grammar",
initializer: null, initializer: initializer,
rules: [ rules: [
{ {
type: "rule", type: "rule",
@ -39,7 +42,7 @@ describe("PEG.js grammar parser", function() {
} }
function actionGrammar(code) { function actionGrammar(code) {
return oneRuleGrammar(null, { return oneRuleGrammar({
type: "action", type: "action",
expression: literalAbcd, expression: literalAbcd,
code: code code: code
@ -47,13 +50,13 @@ describe("PEG.js grammar parser", function() {
} }
function ruleRefGrammar(name) { function ruleRefGrammar(name) {
return oneRuleGrammar(null, { type: "rule_ref", name: name }); return oneRuleGrammar({ type: "rule_ref", name: name });
} }
function literalGrammar(value) { function literalGrammar(value) {
var ignoreCase = arguments.length > 1 ? arguments[1] : false; var ignoreCase = arguments.length > 1 ? arguments[1] : false;
return oneRuleGrammar(null, { return oneRuleGrammar({
type: "literal", type: "literal",
value: value, value: value,
ignoreCase: ignoreCase ignoreCase: ignoreCase
@ -64,7 +67,7 @@ describe("PEG.js grammar parser", function() {
var inverted = arguments.length > 2 ? arguments[2] : false, var inverted = arguments.length > 2 ? arguments[2] : false,
ignoreCase = arguments.length > 3 ? arguments[3] : false; ignoreCase = arguments.length > 3 ? arguments[3] : false;
return oneRuleGrammar(null, { return oneRuleGrammar({
type: "class", type: "class",
inverted: inverted, inverted: inverted,
ignoreCase: ignoreCase, ignoreCase: ignoreCase,
@ -157,31 +160,31 @@ describe("PEG.js grammar parser", function() {
/* Canonical rule is "a: \"abcd\"". */ /* Canonical rule is "a: \"abcd\"". */
it("parses rule", function() { it("parses rule", function() {
expect('start = "abcd" / "efgh" / "ijkl"').toParseAs( expect('start = "abcd" / "efgh" / "ijkl"').toParseAs(
oneRuleGrammar(null, choiceOfLiterals) oneRuleGrammar(choiceOfLiterals)
); );
expect('start "start rule" = "abcd" / "efgh" / "ijkl"').toParseAs( expect('start "start rule" = "abcd" / "efgh" / "ijkl"').toParseAs(
oneRuleGrammar("start rule", choiceOfLiterals) oneRuleGrammar(choiceOfLiterals, null, "start rule")
); );
expect('start = "abcd" / "efgh" / "ijkl";').toParseAs( expect('start = "abcd" / "efgh" / "ijkl";').toParseAs(
oneRuleGrammar(null, choiceOfLiterals) oneRuleGrammar(choiceOfLiterals)
); );
}); });
/* Canonical expression is "\"abcd\" / \"efgh\" / \"ijkl\"". */ /* Canonical expression is "\"abcd\" / \"efgh\" / \"ijkl\"". */
it("parses expression", function() { it("parses expression", function() {
expect('start = "abcd" / "efgh" / "ijkl"').toParseAs( expect('start = "abcd" / "efgh" / "ijkl"').toParseAs(
oneRuleGrammar(null, choiceOfLiterals) oneRuleGrammar(choiceOfLiterals)
); );
}); });
/* Canonical choice is "\"abcd\" / \"efgh\" / \"ijkl\"". */ /* Canonical choice is "\"abcd\" / \"efgh\" / \"ijkl\"". */
it("parses choice", function() { it("parses choice", function() {
expect('start = "abcd" "efgh" "ijkl"').toParseAs( expect('start = "abcd" "efgh" "ijkl"').toParseAs(
oneRuleGrammar(null, sequenceOfLiterals) oneRuleGrammar(sequenceOfLiterals)
); );
expect( expect(
'start = "abcd" "efgh" "ijkl" / "abcd" "efgh" "ijkl" / "abcd" "efgh" "ijkl"' 'start = "abcd" "efgh" "ijkl" / "abcd" "efgh" "ijkl" / "abcd" "efgh" "ijkl"'
).toParseAs(oneRuleGrammar(null, { ).toParseAs(oneRuleGrammar({
type: "choice", type: "choice",
alternatives: [sequenceOfLiterals, sequenceOfLiterals, sequenceOfLiterals] alternatives: [sequenceOfLiterals, sequenceOfLiterals, sequenceOfLiterals]
})); }));
@ -190,21 +193,17 @@ describe("PEG.js grammar parser", function() {
/* Canonical sequence is "\"abcd\" \"efgh\" \"ijkl\"". */ /* Canonical sequence is "\"abcd\" \"efgh\" \"ijkl\"". */
it("parses sequence", function() { it("parses sequence", function() {
expect('start = { code }').toParseAs( expect('start = { code }').toParseAs(
oneRuleGrammar(null, { oneRuleGrammar({
type: "action", type: "action",
expression: sequenceEmpty, expression: sequenceEmpty,
code: " code " code: " code "
}) })
); );
expect('start = a:"abcd" { code }').toParseAs( expect('start = a:"abcd" { code }').toParseAs(
oneRuleGrammar(null, { oneRuleGrammar({ type: "action", expression: labeledAbcd, code: " code " })
type: "action",
expression: labeledAbcd,
code: " code "
})
); );
expect('start = a:"abcd" b:"efgh" c:"ijkl" { code }').toParseAs( expect('start = a:"abcd" b:"efgh" c:"ijkl" { code }').toParseAs(
oneRuleGrammar(null, { oneRuleGrammar({
type: "action", type: "action",
expression: sequenceOfLabeleds, expression: sequenceOfLabeleds,
code: " code " code: " code "
@ -212,57 +211,55 @@ describe("PEG.js grammar parser", function() {
); );
expect('start = ').toParseAs( expect('start = ').toParseAs(
oneRuleGrammar(null, sequenceEmpty) oneRuleGrammar(sequenceEmpty)
); );
expect('start = a:"abcd"').toParseAs( expect('start = a:"abcd"').toParseAs(
oneRuleGrammar(null, labeledAbcd) oneRuleGrammar(labeledAbcd)
); );
expect('start = a:"abcd" b:"efgh" c:"ijkl"').toParseAs( expect('start = a:"abcd" b:"efgh" c:"ijkl"').toParseAs(
oneRuleGrammar(null, sequenceOfLabeleds) oneRuleGrammar(sequenceOfLabeleds)
); );
}); });
/* Canonical labeled is "label:\"abcd\"". */ /* Canonical labeled is "label:\"abcd\"". */
it("parses labeled", function() { it("parses labeled", function() {
expect('start = label:!"abcd"').toParseAs(oneRuleGrammar(null, { expect('start = label:!"abcd"').toParseAs(oneRuleGrammar({
type: "labeled", type: "labeled",
label: "label", label: "label",
expression: simpleNotLiteral expression: simpleNotLiteral
})); }));
expect('start = !"abcd"' ).toParseAs( expect('start = !"abcd"' ).toParseAs(oneRuleGrammar(simpleNotLiteral));
oneRuleGrammar(null, simpleNotLiteral)
);
}); });
/* Canonical prefixed is "!\"abcd\"". */ /* Canonical prefixed is "!\"abcd\"". */
it("parses prefixed", function() { it("parses prefixed", function() {
expect('start = &{ code }').toParseAs(oneRuleGrammar(null, { expect('start = &{ code }').toParseAs(oneRuleGrammar({
type: "semantic_and", type: "semantic_and",
code: " code " code: " code "
})); }));
expect('start = &"abcd"?' ).toParseAs(oneRuleGrammar(null, { expect('start = &"abcd"?' ).toParseAs(oneRuleGrammar({
type: "simple_and", type: "simple_and",
expression: optionalLiteral expression: optionalLiteral
})); }));
expect('start = !{ code }').toParseAs(oneRuleGrammar(null, { expect('start = !{ code }').toParseAs(oneRuleGrammar({
type: "semantic_not", type: "semantic_not",
code: " code " code: " code "
})); }));
expect('start = !"abcd"?' ).toParseAs(oneRuleGrammar(null, { expect('start = !"abcd"?' ).toParseAs(oneRuleGrammar({
type: "simple_not", type: "simple_not",
expression: optionalLiteral expression: optionalLiteral
})); }));
expect('start = "abcd"?' ).toParseAs(oneRuleGrammar(null, optionalLiteral)); expect('start = "abcd"?' ).toParseAs(oneRuleGrammar(optionalLiteral));
}); });
/* Canonical suffixed is "\"abcd\"?". */ /* Canonical suffixed is "\"abcd\"?". */
it("parses suffixed", function() { it("parses suffixed", function() {
expect('start = "abcd"?').toParseAs(oneRuleGrammar(null, optionalLiteral)); expect('start = "abcd"?').toParseAs(oneRuleGrammar(optionalLiteral));
expect('start = "abcd"*').toParseAs(oneRuleGrammar(null, { expect('start = "abcd"*').toParseAs(oneRuleGrammar({
type: "zero_or_more", type: "zero_or_more",
expression: literalAbcd expression: literalAbcd
})); }));
expect('start = "abcd"+').toParseAs(oneRuleGrammar(null, { expect('start = "abcd"+').toParseAs(oneRuleGrammar({
type: "one_or_more", type: "one_or_more",
expression: literalAbcd expression: literalAbcd
})); }));
@ -273,7 +270,7 @@ describe("PEG.js grammar parser", function() {
it("parses primary", function() { it("parses primary", function() {
expect('start = a' ).toParseAs(ruleRefGrammar("a")); expect('start = a' ).toParseAs(ruleRefGrammar("a"));
expect('start = "abcd"' ).toParseAs(literalGrammar("abcd")); expect('start = "abcd"' ).toParseAs(literalGrammar("abcd"));
expect('start = .' ).toParseAs(oneRuleGrammar(null, { type: "any" })); expect('start = .' ).toParseAs(oneRuleGrammar({ type: "any" }));
expect('start = [a-d]' ).toParseAs(classGrammar([["a", "d"]], "[a-d]")); expect('start = [a-d]' ).toParseAs(classGrammar([["a", "d"]], "[a-d]"));
expect('start = ("abcd")').toParseAs(literalGrammar("abcd")); expect('start = ("abcd")').toParseAs(literalGrammar("abcd"));
}); });
@ -321,7 +318,7 @@ describe("PEG.js grammar parser", function() {
/* Canonical string is "\"abcd\"". */ /* Canonical string is "\"abcd\"". */
it("parses string", function() { it("parses string", function() {
var grammar = oneRuleGrammar("abcd", literalAbcd); var grammar = oneRuleGrammar(literalAbcd, null, "abcd");
expect('start "abcd" = "abcd"' ).toParseAs(grammar); expect('start "abcd" = "abcd"' ).toParseAs(grammar);
expect('start \'abcd\' = "abcd"').toParseAs(grammar); expect('start \'abcd\' = "abcd"').toParseAs(grammar);

Loading…
Cancel
Save