Code generation templates

AST operators

Code gen templates for operators; assume root=nil. This is v3 compatible.

grammar fragment

code

A

_adaptor.addChild(_root0, _adaptor.create($A));
root = _adaptor.rulePostProcessing(root, kids); // A
_localctx.tree = root;

A B

_adaptor.addChild(_root0, _adaptor.create($A));
_adaptor.addChild(_root0, _adaptor.create($B));
root = _adaptor.rulePostProcessing(root, kids); // ^(nil A B)
_localctx.tree = root;

A^ B

_root0 = _adaptor.becomeRoot(_adaptor.create($A), _root0);
_adaptor.addChild(_root0, _adaptor.create($B));
root = _adaptor.rulePostProcessing(root, kids); // ^(A B)
_localctx.tree = root;

A B^

_adaptor.addChild(_root0, _adaptor.create($A));
_root0 = _adaptor.becomeRoot(_adaptor.create($B), _root0);
root = _adaptor.rulePostProcessing(root, kids); // ^(B A)
_localctx.tree = root;

A B^ C

_adaptor.addChild(_root0, _adaptor.create($A));
_root0 = _adaptor.becomeRoot(_adaptor.create($B), _root0);
_adaptor.addChild(_root0, _adaptor.create($C));
root = _adaptor.rulePostProcessing(root, kids);// ^(B A C)
_localctx.tree = root;

A B^ C^ D

_adaptor.addChild(_root0, _adaptor.create($A));
_root0 = _adaptor.becomeRoot(_adaptor.create($B), _root0);
_root0 = _adaptor.becomeRoot(_adaptor.create($C), _root0);
_adaptor.addChild(_root0, _adaptor.create($D));
root = _adaptor.rulePostProcessing(root, kids); // ^(C ^(B A) D)
_localctx.tree = root;

a^ b

_root0 = _adaptor.becomeRoot($a.tree, _root0);
_adaptor.addChild(_root0, $b.tree);
root = _adaptor.rulePostProcessing(root, kids); // ^(A B)
_localctx.tree = root;

Rewrites

Code gen templates for rewrite rules; assume root=_adaptor.nil(), kids is _adaptor.createChildList(). assume we can't distinguish statically single vs multi-valued elements from left of -> for now.

grammar fragment

code

A -> A

A_it = _track_A.iterator(); // iterator of tokens
kids.add(A_it.next());    // let _adaptor.add() create appropriate node
_localctx.tree = _adaptor.create(null, kids); // A

A -> A A

A_it = _track_A.iterator();
kids.add(A_it.next());
kids.add(A_it.next()); // iterator keeps returning A nodes
_localctx.tree = _adaptor.create(root, kids); // ^(nil A A)

A+ -> A A

A_it = _track_A.iterator();
kids.add(A_it.next());
kids.add(A_it.next()); // iterator keeps returning A nodes
_localctx.tree = _adaptor.create(root, kids); // ^(nil A A)

A B -> ^(A B)

root = A_it.next(); // don't create node yet
kids.add(_adaptor.create($B));
_localctx.tree = _adaptor.create(_adaptor.create(root), kids); // ^(A B)

A+ -> A*

A_it = _track_A.iterator();
while (A_it.hasNext()) {
    kids.add(_adaptor.create(A_it.next()));
}
_localctx.tree = _adaptor.create(root, kids); // ^(nil A A ... A)

B A+ -> ^(B A*)

A_it = _track_A.iterator();
B_it = _track_B.iterator();
root = B_it.next();
while (A_it.hasNext()) {
    kids.add(_adaptor.create(A_it.next()));
}
_localctx.tree = _adaptor.create(_adaptor.create(root), kids); // ^(B A A ... A)

A+ -> ^(B A)*

A_it = _track_A.iterator();
root_1 = null;
while (A_it.hasNext()) {
    root_1 = ; // imaginary B root
    kids_1.add(_adaptor.create(A_it.next()));
    kids.add( _adaptor.create(_adaptor.create(B, "B"), kids_1) ); // ^(B A)
}
_localctx.tree = _adaptor.create(root, kids); // ^(nil ^(B A) ^(B A) ...)