public static void doTreeAction(string f, AST t, string[] tokenNames) { if (t == null) return ; if (showTree) { BaseAST.setVerboseStringConversion(true, tokenNames); ASTFactory factory = new ASTFactory(); AST r = factory.create(0, "AST ROOT"); r.setFirstChild(t); ASTFrame frame = new ASTFrame("Java AST", r); frame.ShowDialog(); //frame.Visible = true; // System.out.println(t.toStringList()); } JavaTreeParser tparse = new JavaTreeParser(); try { tparse.compilationUnit(t); // System.out.println("successful walk of result AST for "+f); } catch (RecognitionException e) { Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); } }
public virtual void resetState() { traceDepth = 0; returnAST = null; retTree_ = null; inputState.reset(); }
protected internal virtual void match(AST t, int ttype) { //System.out.println("match("+ttype+"); cursor is "+t); if (t == null || t == ASTNULL || t.Type != ttype) { throw new MismatchedTokenException(getTokenNames(), t, ttype, false); } }
public AST child; // current child to which siblings are added /*Make sure that child is the last sibling */ public void advanceChildToEnd() { if (child != null) { while (child.getNextSibling() != null) { child = child.getNextSibling(); } } }
public ASTFrame(string title, AST rootAST) : this() { this.Text = title; JTreeASTPanel treePanel = new JTreeASTPanel(new TreeViewEventHandler(tree_AfterSelect), rootAST); this.Controls.Add(treePanel); treePanel.Location= new Point(5, 5); treePanel.Dock=DockStyle.Fill; treePanel.Anchor=AnchorStyles.Top|AnchorStyles.Left; }
public void visit(AST node) { // Flatten this level of the tree if it has no children bool flatten = /*true*/ false; AST node2; for (node2 = node; node2 != null; node2 = node2.getNextSibling()) { if (node2.getFirstChild() != null) { flatten = false; break; } } for (node2 = node; node2 != null; node2 = node2.getNextSibling()) { if (!flatten || node2 == node) { tabs(); } if (node2.getText() == null) { Console.Out.Write("nil"); } else { Console.Out.Write(node2.getText()); } Console.Out.Write(" [" + node2.Type + "] "); if (flatten) { Console.Out.Write(" "); } else { Console.Out.WriteLine(""); } if (node2.getFirstChild() != null) { level++; visit(node2.getFirstChild()); level--; } } if (flatten) { Console.Out.WriteLine(""); } }
// Expected token / not token public MismatchedTokenException(string[] tokenNames_, AST node_, int expecting_, bool matchNot) : base("Mismatched Token", "<AST>", -1, -1) { tokenNames = tokenNames_; node = node_; if (node_ == null) { tokenText = "<empty tree>"; } else { tokenText = node_.ToString(); } mismatchType = matchNot ? TokenTypeEnum.NotTokenType : TokenTypeEnum.TokenType; expecting = expecting_; }
/*Add a node to the end of the child list for this node */ public virtual void addChild(AST node) { if (node == null) return ; BaseAST t = this.down; if (t != null) { while (t.right != null) { t = t.right; } t.right = (BaseAST) node; } else { this.down = (BaseAST) node; } }
private void doWorkForFindAll(ArrayList v, AST target, bool partialMatch) { AST sibling; // Start walking sibling lists, looking for matches. //siblingWalk: for (sibling = this; sibling != null; sibling = sibling.getNextSibling()) { if ((partialMatch && sibling.EqualsTreePartial(target)) || (!partialMatch && sibling.EqualsTree(target))) { v.Add(sibling); } // regardless of match or not, check any children for matches if (sibling.getFirstChild() != null) { ((BaseAST) sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch); } } }
//throws RecognitionException public Object ifAtom(AST _t) { Object value=null; antlr.stringtemplate.language.StringTemplateAST ifAtom_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; try { // for error handling value=expr(_t); _t = retTree_; } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return value; }
// Expected BitSet / not BitSet public MismatchedTokenException(string[] tokenNames_, AST node_, BitSet set_, bool matchNot) : base("Mismatched Token", "<AST>", -1, -1) { tokenNames = tokenNames_; node = node_; if (node_ == null) { tokenText = "<empty tree>"; } else { tokenText = node_.ToString(); } mismatchType = matchNot ? TokenTypeEnum.NotSetType : TokenTypeEnum.SetType; bset = set_; }
/// <summary> /// Make an AST the root of current AST. /// </summary> /// <param name="currentAST"></param> /// <param name="root"></param> public virtual void makeASTRoot(ASTPair currentAST, AST root) { if (root != null) { // Add the current root as a child of new root root.addChild(currentAST.root); // The new current child is the last sibling of the old root currentAST.child = currentAST.root; currentAST.advanceChildToEnd(); // Set the new root currentAST.root = root; } }
/// <summary> /// Make a tree from a list of nodes. The first element in the /// array is the root. If the root is null, then the tree is /// a simple list not a tree. Handles null children nodes correctly. /// For example, build(a, b, null, c) yields tree (a b c). build(null,a,b) /// yields tree (nil a b). /// </summary> /// <param name="nodes">List of Nodes.</param> /// <returns>AST Node tree.</returns> public virtual AST make(AST[] nodes) { if (nodes == null || nodes.Length == 0) return null; AST root = nodes[0]; AST tail = null; if (root != null) { root.setFirstChild(null); // don't leave any old pointers set } // link in children; for (int i = 1; i < nodes.Length; i++) { if (nodes[i] == null) continue; // ignore null nodes if (root == null) { // Set the root and set it up for a flat list root = (tail = nodes[i]); } else if (tail == null) { root.setFirstChild(nodes[i]); tail = root.getFirstChild(); } else { tail.setNextSibling(nodes[i]); tail = tail.getNextSibling(); } // Chase tail to last sibling while (tail.getNextSibling() != null) { tail = tail.getNextSibling(); } } return root; }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node. Ignore it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public virtual AST dupTree(AST t) { AST result = dup(t); // make copy of root // copy all children of root. if (t != null) { result.setFirstChild(dupList(t.getFirstChild())); } return result; }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node and all of it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public virtual AST dupList(AST t) { AST result = dupTree(t); // if t == null, then result==null AST nt = result; while (t != null) { // for each sibling of the root t = t.getNextSibling(); nt.setNextSibling(dupTree(t)); // dup each subtree, building new tree nt = nt.getNextSibling(); } return result; }
//throws RecognitionException public void singleTemplateArg(AST _t, StringTemplate embedded, IDictionary argumentContext ) { antlr.stringtemplate.language.StringTemplateAST singleTemplateArg_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; Object e = null; try { // for error handling AST __t41 = _t; antlr.stringtemplate.language.StringTemplateAST tmp22_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,SINGLEVALUEARG); _t = _t.getFirstChild(); e=expr(_t); _t = retTree_; _t = __t41; _t = _t.getNextSibling(); if ( e!=null ) { String soleArgName = null; // find the sole defined formal argument for embedded bool error = false; IDictionary formalArgs = embedded.getFormalArguments(); if ( formalArgs!=null ) { ICollection argNames = formalArgs.Keys; if ( argNames.Count==1 ) { string[] argNamesArray = new string[argNames.Count]; argNames.CopyTo(argNamesArray,0); soleArgName = argNamesArray[0]; //System.out.println("sole formal arg of "+embedded.getName()+" is "+soleArgName); } else { error=true; } } else { error=true; } if ( error ) { self.error("template "+embedded.getName()+ " must have exactly one formal arg in template context "+ self.getEnclosingInstanceStackString()); } else { self.rawSetArgumentAttribute(embedded,argumentContext,soleArgName,e); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; }
/// <summary> /// Creates and initializes a new AST node using the specified AST Node instance. /// the new AST node is initialized with the specified Token type ID and string. /// The <see cref="System.Type"/> used for creating this new AST node is /// determined solely by <c>aNode</c>. /// The AST Node type must have a default/parameterless constructor. /// </summary> /// <param name="aNode">AST Node instance to be used for creating the new AST Node.</param> /// <returns>An initialized AST node object.</returns> public virtual AST create(AST aNode) { AST newNode; if (aNode == null) newNode = null; else { newNode = createFromNodeTypeObject(aNode.GetType()); newNode.initialize(aNode); } return newNode; }
//throws RecognitionException public String value(AST _t) { String s ; AST value_AST_in = (AST)_t; AST l = null; AST ql = null; AST v = null; String variable = null; s = null; try { // for error handling if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case LITERAL: { l = _t; match(_t,LITERAL); _t = _t.getNextSibling(); s = l.getText(); break; } case QLITERAL: { ql = _t; match(_t,QLITERAL); _t = _t.getNextSibling(); s = ql.getText(); break; } case VAR: { AST tmp18_AST_in = _t; match(_t,VAR); _t = _t.getNextSibling(); AST tmp19_AST_in = _t; match(_t,LPAREN); _t = _t.getNextSibling(); v = _t; match(_t,LITERAL); _t = _t.getNextSibling(); AST tmp20_AST_in = _t; match(_t,RPAREN); _t = _t.getNextSibling(); variable = v.getText(); s = (String) environment.get(variable); if (s == null) { throw new RecognitionException("unrecognized variable " + variable); } break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return s ; }
//throws RecognitionException public bool ifCondition(AST _t) { bool value=false; antlr.stringtemplate.language.StringTemplateAST ifCondition_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; Object a=null, b=null; try { // for error handling if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case APPLY: case MULTI_APPLY: case INCLUDE: case VALUE: case FUNCTION: case LIST: case PLUS: case DOT: case ID: case ANONYMOUS_TEMPLATE: case STRING: case INT: { a=ifAtom(_t); _t = retTree_; value = chunk.testAttributeTrue(a); break; } case NOT: { AST __t30 = _t; antlr.stringtemplate.language.StringTemplateAST tmp18_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,NOT); _t = _t.getFirstChild(); a=ifAtom(_t); _t = retTree_; _t = __t30; _t = _t.getNextSibling(); value = !chunk.testAttributeTrue(a); break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return value; }
//throws RecognitionException /** create a new list of expressions as a new multi-value attribute */ public Object list(AST _t) { Object value=null; antlr.stringtemplate.language.StringTemplateAST list_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; Object e = null; IList elements = new ArrayList(); value = new CatIterator(elements); try { // for error handling AST __t6 = _t; antlr.stringtemplate.language.StringTemplateAST tmp14_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,LIST); _t = _t.getFirstChild(); { // ( ... )+ int _cnt8=0; for (;;) { if (_t == null) _t = ASTNULL; if ((tokenSet_0_.member(_t.Type))) { e=expr(_t); _t = retTree_; if ( e!=null ) { e = ASTExpr.convertAnythingToIterator(e); elements.Add(e); } } else { if (_cnt8 >= 1) { goto _loop8_breakloop; } else { throw new NoViableAltException(_t);; } } _cnt8++; } _loop8_breakloop: ; } // ( ... )+ _t = __t6; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return value; }
public virtual void traceOut(string rname, AST t) { traceIndent(); Console.Out.WriteLine("< " + rname + "(" + ((t != null) ? t.ToString() : "null") + ")" + ((inputState.guessing > 0) ? " [guessing]" : "")); traceDepth--; }
//throws RecognitionException public void template(AST _t, ArrayList templatesToApply ) { antlr.stringtemplate.language.StringTemplateAST template_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; antlr.stringtemplate.language.StringTemplateAST t = null; antlr.stringtemplate.language.StringTemplateAST args = null; antlr.stringtemplate.language.StringTemplateAST anon = null; antlr.stringtemplate.language.StringTemplateAST args2 = null; IDictionary argumentContext = null; Object n = null; try { // for error handling AST __t26 = _t; antlr.stringtemplate.language.StringTemplateAST tmp15_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,TEMPLATE); _t = _t.getFirstChild(); { if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case ID: { t = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,ID); _t = _t.getNextSibling(); args = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; if (null == _t) throw new MismatchedTokenException(); _t = _t.getNextSibling(); String templateName = t.getText(); StringTemplateGroup group = self.getGroup(); StringTemplate embedded = group.getEmbeddedInstanceOf(self, templateName); if ( embedded!=null ) { embedded.setArgumentsAST(args); templatesToApply.Add(embedded); } break; } case ANONYMOUS_TEMPLATE: { anon = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,ANONYMOUS_TEMPLATE); _t = _t.getNextSibling(); StringTemplate anonymous = anon.getStringTemplate(); templatesToApply.Add(anonymous); break; } case VALUE: { AST __t28 = _t; antlr.stringtemplate.language.StringTemplateAST tmp16_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,VALUE); _t = _t.getFirstChild(); n=expr(_t); _t = retTree_; args2 = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; if (null == _t) throw new MismatchedTokenException(); _t = _t.getNextSibling(); StringTemplate embedded = null; if ( n!=null ) { String templateName = n.ToString(); StringTemplateGroup group = self.getGroup(); embedded = group.getEmbeddedInstanceOf(self, templateName); if ( embedded!=null ) { embedded.setArgumentsAST(args2); templatesToApply.Add(embedded); } } _t = __t28; _t = _t.getNextSibling(); break; } default: { throw new NoViableAltException(_t); } } } _t = __t26; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; }
public ASTExpr(StringTemplate enclosingTemplate, AST exprTree, IDictionary options) : base(enclosingTemplate) { this.exprTree = exprTree; this.options = options; }
/// <summary> /// Returns a copy of the specified AST Node instance. The copy is obtained by /// using the <see cref="ICloneable"/> method Clone(). /// </summary> /// <param name="t">AST Node to copy.</param> /// <returns>An AST Node (or null if <c>t</c> is null).</returns> public virtual AST dup(AST t) { // The Java version is implemented using code like this: if (t == null) return null; AST dup_edNode = createFromNodeTypeObject(t.GetType()); dup_edNode.initialize(t); return dup_edNode; //return (AST)((t == null) ? null : t.Clone()); }
//throws RecognitionException public String literal(AST _t) { String s ; AST literal_AST_in = (AST)_t; AST l = null; AST ql = null; s = null; try { // for error handling if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case LITERAL: { l = _t; match(_t,LITERAL); _t = _t.getNextSibling(); s = l.getText(); break; } case QLITERAL: { ql = _t; match(_t,QLITERAL); _t = _t.getNextSibling(); s = ql.getText(); break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return s ; }
//throws RecognitionException public Object singleFunctionArg(AST _t) { Object value=null; antlr.stringtemplate.language.StringTemplateAST singleFunctionArg_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; try { // for error handling AST __t24 = _t; antlr.stringtemplate.language.StringTemplateAST tmp17_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,SINGLEVALUEARG); _t = _t.getFirstChild(); value=expr(_t); _t = retTree_; _t = __t24; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return value; }
//throws RecognitionException public boolean expression(AST _t) { boolean e ; AST expression_AST_in = (AST)_t; boolean a, b; String l, r; e = false; try { // for error handling if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case AND: { AST __t43 = _t; AST tmp21_AST_in = _t; match(_t,AND); _t = _t.getFirstChild(); a=expression(_t); _t = retTree_; b=expression(_t); _t = retTree_; e = a && b; _t = __t43; _t = _t.getNextSibling(); break; } case OR: { AST __t44 = _t; AST tmp22_AST_in = _t; match(_t,OR); _t = _t.getFirstChild(); a=expression(_t); _t = retTree_; b=expression(_t); _t = retTree_; e = a || b; _t = __t44; _t = _t.getNextSibling(); break; } case EQUAL: { AST __t45 = _t; AST tmp23_AST_in = _t; match(_t,EQUAL); _t = _t.getFirstChild(); l=value(_t); _t = retTree_; r=value(_t); _t = retTree_; e = l.equals(r); _t = __t45; _t = _t.getNextSibling(); break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return e ; }
/*Make sure current lookahead symbol matches the given set * Throw an exception upon mismatch, which is catch by either the * error handler or by the syntactic predicate. */ public virtual void match(AST t, BitSet b) { if (t == null || t == ASTNULL || !b.member(t.Type)) { throw new MismatchedTokenException(getTokenNames(), t, b, false); } }
private void reset() { root = null; child = null; }
/// <summary> /// Add a child to the current AST /// </summary> /// <param name="currentAST">The AST to add a child to</param> /// <param name="child">The child AST to be added</param> public virtual void addASTChild(ASTPair currentAST, AST child) { if (child != null) { if (currentAST.root == null) { // Make new child the current root currentAST.root = child; } else { if (currentAST.child == null) { // Add new child to current root currentAST.root.setFirstChild(child); } else { currentAST.child.setNextSibling(child); } } // Make new child the current child currentAST.child = child; currentAST.advanceChildToEnd(); } }