@graphistry/falcor-path-syntax
Version:
Parser for Falcor Path Syntax
59 lines (46 loc) • 1.4 kB
JavaScript
var Tokenizer = require('./tokenizer');
var head = require('./parse-tree/head');
var RoutedTokens = require('./RoutedTokens');
var parser = function parser(string, extendedRules) {
return head(new Tokenizer(string, extendedRules));
};
module.exports = parser;
// Constructs the paths from paths / pathValues that have strings.
// If it does not have a string, just moves the value into the return
// results.
parser.fromPathsOrPathValues = function(paths, ext) {
if (!paths) {
return [];
}
var out = [];
for (var i = 0, len = paths.length; i < len; i++) {
// Is the path a string
if (typeof paths[i] === 'string') {
out[i] = parser(paths[i], ext);
}
// is the path a path value with a string value.
else if (typeof paths[i].path === 'string') {
out[i] = {
path: parser(paths[i].path, ext), value: paths[i].value
};
}
// just copy it over.
else {
out[i] = paths[i];
}
}
return out;
};
// If the argument is a string, this with convert, else just return
// the path provided.
parser.fromPath = function(path, ext) {
if (!path) {
return [];
}
if (typeof path === 'string') {
return parser(path, ext);
}
return path;
};
// Potential routed tokens.
parser.RoutedTokens = RoutedTokens;