univac
Version:
Generate AST of lots of common programming languages using antlr4. JavaScript API and CLI tool.
41 lines • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function getSExpressionImpl() {
var Lexer = require('../grammar/sexpression/sexpressionLexer').sexpressionLexer;
var Parser = require('../grammar/sexpression/sexpressionParser').sexpressionParser;
return {
Lexer: Lexer,
Parser: Parser,
mainRule: 'sexpr',
mutate: mutate
};
}
exports.getSExpressionImpl = getSExpressionImpl;
function mutate(ast, impl) {
// in the original grammar there are three types, ListContext, ItemContext and AtomContext. ItemContext is only used as an intermediary so we will deleting it. Actually what we do is removing Item, and renaming atom to item. We verify that children is exactly one, has a parent, and its type and text are identical to its childAlso we rename the types to be shorter.
// So the grammar will be transformed format
// L => ([I]...), I=>L|A,
// L=>([I|A]...)
function f(n, p) {
var _a;
if (n.start === undefined) {
delete n.start;
}
if (n.stop === undefined) {
delete n.stop;
}
if (n.type === 'item') {
p = p || n.parent;
if (p) {
if (n.children.length) {
var ni = p.children.findIndex(function (i) { return i === n; });
(_a = p.children).splice.apply(_a, [ni, 1].concat(n.children));
}
}
}
n.children.forEach(function (c) { return f(c, n); });
return n;
}
return f(ast);
}
//# sourceMappingURL=sexpression.js.map