Jasmine: Convert |removeProxyRules| compiler pass tests
parent
eaf2af8e7b
commit
8ef5f08c90
@ -0,0 +1,75 @@
|
|||||||
|
describe("compiler pass |removeProxyRules|", function() {
|
||||||
|
var pass = PEG.compiler.passes.removeProxyRules;
|
||||||
|
|
||||||
|
function proxyGrammar(rule) {
|
||||||
|
return [rule, 'proxy = proxied', 'proxied = "a"'].join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
function expressionDetails(details) {
|
||||||
|
return {
|
||||||
|
rules: [
|
||||||
|
{ name: "start", expression: details },
|
||||||
|
{ name: "proxied" }
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var simpleDetails = expressionDetails({ expression: { name: "proxied" } });
|
||||||
|
|
||||||
|
it("removes proxy rule from a rule", function() {
|
||||||
|
expect(pass).toChangeAST(proxyGrammar('start = proxy'), {
|
||||||
|
startRule: "proxied",
|
||||||
|
rules: [{ name: "proxied", expression: { type: "literal" } }]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from a choice", function() {
|
||||||
|
expect(pass).toChangeAST(
|
||||||
|
proxyGrammar('start = proxy / "a" / "b"'),
|
||||||
|
expressionDetails({ alternatives: [{ name: "proxied" }, {}, {}] })
|
||||||
|
);
|
||||||
|
expect(pass).toChangeAST(
|
||||||
|
proxyGrammar('start = "a" / "b" / proxy'),
|
||||||
|
expressionDetails({ alternatives: [{}, {}, { name: "proxied" }] })
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from a sequence", function() {
|
||||||
|
expect(pass).toChangeAST(
|
||||||
|
proxyGrammar('start = proxy "a" "b"'),
|
||||||
|
expressionDetails({ elements: [{ name: "proxied" }, {}, {}] })
|
||||||
|
);
|
||||||
|
expect(pass).toChangeAST(
|
||||||
|
proxyGrammar('start = "a" "b" proxy'),
|
||||||
|
expressionDetails({ elements: [{}, {}, { name: "proxied" }] })
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from a labeled", function() {
|
||||||
|
expect(pass).toChangeAST(proxyGrammar('start = label:proxy'), simpleDetails);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from a simple and", function() {
|
||||||
|
expect(pass).toChangeAST(proxyGrammar('start = &proxy'), simpleDetails);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from a simple not", function() {
|
||||||
|
expect(pass).toChangeAST(proxyGrammar('start = &proxy'), simpleDetails);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from an optional", function() {
|
||||||
|
expect(pass).toChangeAST(proxyGrammar('start = proxy?'), simpleDetails);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from a zero or more", function() {
|
||||||
|
expect(pass).toChangeAST(proxyGrammar('start = proxy*'), simpleDetails);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from a one or more", function() {
|
||||||
|
expect(pass).toChangeAST(proxyGrammar('start = proxy+'), simpleDetails);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes proxy rule from an action", function() {
|
||||||
|
expect(pass).toChangeAST(proxyGrammar('start = proxy { }'), simpleDetails);
|
||||||
|
});
|
||||||
|
});
|
@ -1,152 +0,0 @@
|
|||||||
(function() {
|
|
||||||
|
|
||||||
module("PEG.compiler.passes.removeProxyRules");
|
|
||||||
|
|
||||||
test("removes proxy rules", function() {
|
|
||||||
function simpleGrammar(rules, startRule) {
|
|
||||||
return {
|
|
||||||
type: "grammar",
|
|
||||||
initializer: null,
|
|
||||||
rules: rules,
|
|
||||||
startRule: startRule
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
var proxiedRule = {
|
|
||||||
type: "rule",
|
|
||||||
name: "proxied",
|
|
||||||
displayName: null,
|
|
||||||
expression: { type: "literal", value: "a", ignoreCase: false }
|
|
||||||
};
|
|
||||||
|
|
||||||
var proxiedRuleRef = {
|
|
||||||
type: "rule_ref",
|
|
||||||
name: "proxied"
|
|
||||||
};
|
|
||||||
|
|
||||||
function simpleGrammarWithStartAndProxied(startRuleExpression) {
|
|
||||||
return simpleGrammar(
|
|
||||||
[
|
|
||||||
{
|
|
||||||
type: "rule",
|
|
||||||
name: "start",
|
|
||||||
displayName: null,
|
|
||||||
expression: startRuleExpression
|
|
||||||
},
|
|
||||||
proxiedRule
|
|
||||||
],
|
|
||||||
"start"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
var cases = [
|
|
||||||
{
|
|
||||||
grammar: 'start = proxy; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammar([proxiedRule], "proxied")
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = proxy / "a" / "b"; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "choice",
|
|
||||||
alternatives: [
|
|
||||||
proxiedRuleRef,
|
|
||||||
{ type: "literal", value: "a", ignoreCase: false },
|
|
||||||
{ type: "literal", value: "b", ignoreCase: false }
|
|
||||||
]
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = "a" / "b" / proxy; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "choice",
|
|
||||||
alternatives: [
|
|
||||||
{ type: "literal", value: "a", ignoreCase: false },
|
|
||||||
{ type: "literal", value: "b", ignoreCase: false },
|
|
||||||
proxiedRuleRef
|
|
||||||
]
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = proxy "a" "b"; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "sequence",
|
|
||||||
elements: [
|
|
||||||
proxiedRuleRef,
|
|
||||||
{ type: "literal", value: "a", ignoreCase: false },
|
|
||||||
{ type: "literal", value: "b", ignoreCase: false }
|
|
||||||
]
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = "a" "b" proxy; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "sequence",
|
|
||||||
elements: [
|
|
||||||
{ type: "literal", value: "a", ignoreCase: false },
|
|
||||||
{ type: "literal", value: "b", ignoreCase: false },
|
|
||||||
proxiedRuleRef
|
|
||||||
]
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = label:proxy; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "labeled",
|
|
||||||
label: "label",
|
|
||||||
expression: proxiedRuleRef
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = &proxy; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "simple_and",
|
|
||||||
expression: proxiedRuleRef
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = !proxy; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "simple_not",
|
|
||||||
expression: proxiedRuleRef
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = proxy?; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "optional",
|
|
||||||
expression: proxiedRuleRef
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = proxy*; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "zero_or_more",
|
|
||||||
expression: proxiedRuleRef
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = proxy+; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "one_or_more",
|
|
||||||
expression: proxiedRuleRef
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
grammar: 'start = proxy { }; proxy = proxied; proxied = "a"',
|
|
||||||
ast: simpleGrammarWithStartAndProxied({
|
|
||||||
type: "action",
|
|
||||||
code: " ",
|
|
||||||
expression: proxiedRuleRef
|
|
||||||
})
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
for (var i = 0; i < cases.length; i++) {
|
|
||||||
var ast = PEG.parser.parse(cases[i].grammar);
|
|
||||||
PEG.compiler.passes.removeProxyRules(ast);
|
|
||||||
|
|
||||||
deepEqual(ast, cases[i].ast);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
})();
|
|
Loading…
Reference in New Issue