2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2015-04-01 11:33:15 +02:00
|
|
|
var arrays = require("../utils/arrays"),
|
|
|
|
visitor = require("./visitor");
|
2014-05-08 11:48:56 +02:00
|
|
|
|
|
|
|
/* AST utilities. */
|
|
|
|
var asts = {
|
2014-05-08 14:46:11 +02:00
|
|
|
findRule: function(ast, name) {
|
2014-05-08 11:48:56 +02:00
|
|
|
return arrays.find(ast.rules, function(r) { return r.name === name; });
|
|
|
|
},
|
|
|
|
|
2014-05-08 14:46:11 +02:00
|
|
|
indexOfRule: function(ast, name) {
|
2014-05-08 11:48:56 +02:00
|
|
|
return arrays.indexOf(ast.rules, function(r) { return r.name === name; });
|
2015-04-01 11:33:15 +02:00
|
|
|
},
|
|
|
|
|
2015-07-31 13:48:46 +02:00
|
|
|
alwaysAdvancesOnSuccess: function(ast, node) {
|
|
|
|
function advancesTrue() { return true; }
|
|
|
|
function advancesFalse() { return false; }
|
2015-04-01 11:33:15 +02:00
|
|
|
|
2015-07-31 13:48:46 +02:00
|
|
|
function advancesExpression(node) {
|
|
|
|
return advances(node.expression);
|
2015-04-01 11:33:15 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 13:48:46 +02:00
|
|
|
var advances = visitor.build({
|
2015-08-03 16:56:57 +02:00
|
|
|
rule: advancesExpression,
|
|
|
|
named: advancesExpression,
|
2015-04-01 11:33:15 +02:00
|
|
|
|
|
|
|
choice: function(node) {
|
2015-07-31 13:48:46 +02:00
|
|
|
return arrays.every(node.alternatives, advances);
|
2015-04-01 11:33:15 +02:00
|
|
|
},
|
|
|
|
|
2015-07-31 13:48:46 +02:00
|
|
|
action: advancesExpression,
|
2015-04-01 11:33:15 +02:00
|
|
|
|
|
|
|
sequence: function(node) {
|
2015-07-31 13:48:46 +02:00
|
|
|
return arrays.some(node.elements, advances);
|
2015-04-01 11:33:15 +02:00
|
|
|
},
|
|
|
|
|
2015-07-31 13:48:46 +02:00
|
|
|
labeled: advancesExpression,
|
|
|
|
text: advancesExpression,
|
|
|
|
simple_and: advancesFalse,
|
|
|
|
simple_not: advancesFalse,
|
|
|
|
optional: advancesFalse,
|
|
|
|
zero_or_more: advancesFalse,
|
|
|
|
one_or_more: advancesExpression,
|
|
|
|
semantic_and: advancesFalse,
|
|
|
|
semantic_not: advancesFalse,
|
2015-04-01 11:33:15 +02:00
|
|
|
|
|
|
|
rule_ref: function(node) {
|
2015-07-31 13:48:46 +02:00
|
|
|
return advances(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-07-31 13:48:46 +02:00
|
|
|
"class": advancesTrue,
|
|
|
|
any: advancesTrue
|
2015-04-01 11:33:15 +02:00
|
|
|
});
|
|
|
|
|
2015-07-31 13:48:46 +02:00
|
|
|
return advances(node);
|
2014-05-08 11:48:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = asts;
|