2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-09-13 16:08:45 +02:00
|
|
|
let visitor = require("./visitor");
|
2014-05-08 11:48:56 +02:00
|
|
|
|
|
|
|
/* AST utilities. */
|
2016-09-08 16:04:36 +02:00
|
|
|
let asts = {
|
2014-05-08 14:46:11 +02:00
|
|
|
findRule: function(ast, name) {
|
2016-09-13 16:08:45 +02:00
|
|
|
for (let i = 0; i < ast.rules.length; i++) {
|
|
|
|
if (ast.rules[i].name === name) {
|
|
|
|
return ast.rules[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
2014-05-08 11:48:56 +02:00
|
|
|
},
|
|
|
|
|
2014-05-08 14:46:11 +02:00
|
|
|
indexOfRule: function(ast, name) {
|
2016-09-13 16:08:45 +02:00
|
|
|
for (let i = 0; i < ast.rules.length; i++) {
|
|
|
|
if (ast.rules[i].name === name) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2015-04-01 11:33:15 +02:00
|
|
|
},
|
|
|
|
|
2015-09-04 17:35:37 +02:00
|
|
|
alwaysConsumesOnSuccess: function(ast, node) {
|
|
|
|
function consumesTrue() { return true; }
|
|
|
|
function consumesFalse() { return false; }
|
2015-04-01 11:33:15 +02:00
|
|
|
|
2015-09-04 17:35:37 +02:00
|
|
|
function consumesExpression(node) {
|
|
|
|
return consumes(node.expression);
|
2015-04-01 11:33:15 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 16:04:36 +02:00
|
|
|
let consumes = visitor.build({
|
2015-09-04 17:35:37 +02:00
|
|
|
rule: consumesExpression,
|
|
|
|
named: consumesExpression,
|
2015-04-01 11:33:15 +02:00
|
|
|
|
|
|
|
choice: function(node) {
|
2016-08-19 17:18:43 +02:00
|
|
|
return node.alternatives.every(consumes);
|
2015-04-01 11:33:15 +02:00
|
|
|
},
|
|
|
|
|
2015-09-04 17:35:37 +02:00
|
|
|
action: consumesExpression,
|
2015-04-01 11:33:15 +02:00
|
|
|
|
|
|
|
sequence: function(node) {
|
2016-08-19 17:20:40 +02:00
|
|
|
return node.elements.some(consumes);
|
2015-04-01 11:33:15 +02:00
|
|
|
},
|
|
|
|
|
2015-09-04 17:35:37 +02:00
|
|
|
labeled: consumesExpression,
|
|
|
|
text: consumesExpression,
|
|
|
|
simple_and: consumesFalse,
|
|
|
|
simple_not: consumesFalse,
|
|
|
|
optional: consumesFalse,
|
|
|
|
zero_or_more: consumesFalse,
|
|
|
|
one_or_more: consumesExpression,
|
2016-03-11 16:18:29 +01:00
|
|
|
group: consumesExpression,
|
2015-09-04 17:35:37 +02:00
|
|
|
semantic_and: consumesFalse,
|
|
|
|
semantic_not: consumesFalse,
|
2015-04-01 11:33:15 +02:00
|
|
|
|
|
|
|
rule_ref: function(node) {
|
2015-09-04 17:35:37 +02:00
|
|
|
return consumes(asts.findRule(ast, node.name));
|
2015-04-01 11:33:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
literal: function(node) {
|
2015-07-31 13:48:46 +02:00
|
|
|
return node.value !== "";
|
2015-04-01 11:33:15 +02:00
|
|
|
},
|
|
|
|
|
2015-09-04 17:35:37 +02:00
|
|
|
"class": consumesTrue,
|
|
|
|
any: consumesTrue
|
2015-04-01 11:33:15 +02:00
|
|
|
});
|
|
|
|
|
2015-09-04 17:35:37 +02:00
|
|
|
return consumes(node);
|
2014-05-08 11:48:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = asts;
|