Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code gen templates for operators; assume root=null, kids=List<Token>adaptor.createChildList()

grammar fragment

code

A

Code Block
kids.add($A);
_localctx.tree = adaptor.create(root, kids); // A

A B

Code Block
kids.add($A);
kids.add($B);
_localctx.tree = adaptor.create(root, kids); // ^(nil A B)

A^ B

Code Block
root = root==null ? $A : adaptor.becomeRoot(root, $A, kids);
kids.add($ID);
_localctx.tree = adaptor.create(root, kids); // ^(A B)

A B^

Code Block
kids.add($A);
root = root==null ? $B : adaptor.becomeRoot(root, $B, kids);
_localctx.tree = adaptor.create(root, kids); // ^(B A)

A B^ C

Code Block
kids.add($A);
root = root==null ? $B : adaptor.becomeRoot(root, $B, kids);
kids.add($C);
_localctx.tree = adaptor.create(root, kids); // ^(B A C)

A B^ C^ D

Code Block
kids.add($A);
root = root==null ? $B : adaptor.becomeRoot(root, $B, kids);
root = root==null ? $C : adaptor.becomeRoot(root, $C, kids);
kids.add($D);
_localctx.tree = adaptor.create(root, kids); // ^(C ^(B A) D)

...

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

grammar fragment

code

A -> A

Code Block
A_it = A_list.iterator(); // iterator of tokens
kids.add(adaptor.create(A_it.next()));;    // let adaptor.add() create appropriate node
_localctx.tree = adaptor.create(rootnull, kids); // A

A -> A A

Code Block
A_it = A_list.iterator();
kids.add(adaptor.create(A_it.next()));
kids.add(adaptor.create(A_it.next())); // iterator keeps returning A nodes
_localctx.tree = adaptor.create(root, kids); // ^(nil A A)

A B + -> ^(A A B)

Code Block
A_it = A_list.iterator();
kids.add(A_it.next());
B_it = B_list.iterator();
kids.add(A_it.next()); // iterator keeps returning A nodes
_localctx.tree = adaptor.create(root, kids); // ^(nil A A)

A B -> ^(A B)

Code Block

root = adaptor.create(A_it.next());
kids.add(adaptor.create(B_it.next()$B));
_localctx.tree = adaptor.create(root, kids); // ^(nil A A)

A+ -> A*

Code Block
A_it = A_list.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*)

Code Block
A_it = A_list.iterator();
B_it = B_list.iterator();
root = adaptor.create(B_it.next());
while (A_it.hasNext()) {
    kids.add(adaptor.create(A_it.next()));
}
_localctx.tree = adaptor.create(root, kids); // ^(B A A ... A)

A+ -> ^(B A)*

Code Block
A_it = A_list.iterator();
B_it = B_list.iterator();
root_1 = null;
while (A_it.hasNext()) {
    root_1 = adaptor.create(B_itadaptor.next(create(B, "B")); // imaginary B root
    kids_1.add(adaptor.create(A_it.next()));
    kids.add( adaptor.create(root_1, kids_1) ); // ^(B A)
}
_localctx.tree = adaptor.create(root, kids); // ^(nil ^(B A) ^(B A) ...)