public virtual void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) { if (parent != null) { TreeAdaptor.ReplaceChildren(parent, startChildIndex, stopChildIndex, t); } }
public override object NextElement() { _it.MoveNext(); object t = _it.Current; //System.out.println("pulled "+adaptor.getType(t)); if (t == _it.up) { _level--; if (_level == 0 && _hasNilRoot) { _it.MoveNext(); return(_it.Current); // don't give last UP; get EOF } } else if (t == _it.down) { _level++; } if (_level == 0 && TreeAdaptor.IsNil(t)) { // if nil root, scarf nil, DOWN _hasNilRoot = true; _it.MoveNext(); t = _it.Current; // t is now DOWN, so get first real node next _level++; _it.MoveNext(); t = _it.Current; } return(t); }
public bool HasPositionInformation(object node) { IToken token = TreeAdaptor.GetToken(node); if (token == null) { return(false); } if (token.Line <= 0) { return(false); } return(true); }
/** <summary>For debugging; destructive: moves tree iterator to end.</summary> */ public virtual string ToTokenTypeString() { Reset(); StringBuilder buf = new StringBuilder(); object o = LT(1); int type = TreeAdaptor.GetType(o); while (type != TokenTypes.EndOfFile) { buf.Append(" "); buf.Append(type); Consume(); o = LT(1); type = TreeAdaptor.GetType(o); } return(buf.ToString()); }
public IASTNode NegateNode(IASTNode node) { // TODO - copy code from HqlParser.java switch (node.Type) { case OR: node.Type = AND; node.Text = "{and}"; NegateNode(node.GetChild(0)); NegateNode(node.GetChild(1)); return(node); case AND: node.Type = OR; node.Text = "{or}"; NegateNode(node.GetChild(0)); NegateNode(node.GetChild(1)); return(node); case EQ: node.Type = NE; node.Text = "{not}" + node.Text; return(node); // (NOT (EQ a b) ) => (NE a b) case NE: node.Type = EQ; node.Text = "{not}" + node.Text; return(node); // (NOT (NE a b) ) => (EQ a b) case GT: node.Type = LE; node.Text = "{not}" + node.Text; return(node); // (NOT (GT a b) ) => (LE a b) case LT: node.Type = GE; node.Text = "{not}" + node.Text; return(node); // (NOT (LT a b) ) => (GE a b) case GE: node.Type = LT; node.Text = "{not}" + node.Text; return(node); // (NOT (GE a b) ) => (LT a b) case LE: node.Type = GT; node.Text = "{not}" + node.Text; return(node); // (NOT (LE a b) ) => (GT a b) case LIKE: node.Type = NOT_LIKE; node.Text = "{not}" + node.Text; return(node); // (NOT (LIKE a b) ) => (NOT_LIKE a b) case NOT_LIKE: node.Type = LIKE; node.Text = "{not}" + node.Text; return(node); // (NOT (NOT_LIKE a b) ) => (LIKE a b) case IN: node.Type = NOT_IN; node.Text = "{not}" + node.Text; return(node); case NOT_IN: node.Type = IN; node.Text = "{not}" + node.Text; return(node); case IS_NULL: node.Type = IS_NOT_NULL; node.Text = "{not}" + node.Text; return(node); // (NOT (IS_NULL a b) ) => (IS_NOT_NULL a b) case IS_NOT_NULL: node.Type = IS_NULL; node.Text = "{not}" + node.Text; return(node); // (NOT (IS_NOT_NULL a b) ) => (IS_NULL a b) case BETWEEN: node.Type = NOT_BETWEEN; node.Text = "{not}" + node.Text; return(node); // (NOT (BETWEEN a b) ) => (NOT_BETWEEN a b) case NOT_BETWEEN: node.Type = BETWEEN; node.Text = "{not}" + node.Text; return(node); // (NOT (NOT_BETWEEN a b) ) => (BETWEEN a b) /* This can never happen because this rule will always eliminate the child NOT. * case NOT: * return x.getFirstChild(); // (NOT (NOT x) ) => (x) */ default: IASTNode not = (IASTNode)TreeAdaptor.Create(NOT, "not"); not.AddChild(node); return(not); } }
public virtual int LA(int i) { return(TreeAdaptor.GetType(LT(i))); }
public override bool IsEndOfFile(object o) { return(TreeAdaptor.GetType(o) == CharStreamConstants.EndOfFile); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Console.WriteLine("---------------"); Console.Error.WriteLine("test.cs"); string file_name = "test.cs"; CommonTokenStream tokens = CreateLexer <csLexer>(file_name); csParser p = new csParser(tokens); TreeAdaptor csAdaptor = new TreeAdaptor(); p.TreeAdaptor = csAdaptor; csParser.compilation_unit_return parser_rt; CommonTree tree = null; p.OnDisplayRecognitionError += (err => { Console.WriteLine(err._errorStr); }); parser_rt = p.compilation_unit(); #region Error Checking int count = 0; tree = (CommonTree)parser_rt.Tree; // Check if we didn't get an AST // This often happens if your grammar and tree grammar don't match if (tree == null) { if (tokens.Count > 0) { Console.Error.WriteLine("No Tree returned from parsing! (Your rule did not parse correctly)"); } else { // the file was empty, this is not an error. // Clear archive attribute File.SetAttributes(file_name, File.GetAttributes(file_name) & ~FileAttributes.Archive); ++count; } return; } #endregion if (p.NumberOfSyntaxErrors > 0) { List <string> strs = p.ToStrings(tokens.GetTokens()); foreach (var v in strs) { // Console.WriteLine(v); } } //Console.WriteLine(tree.ToStringTree()); // Get the AST stream CommonTreeNodeStream nodes = new CommonTreeNodeStream(csAdaptor, tree); // Add the tokens for DumpNodes, otherwise there are no token names to print out. nodes.TokenStream = tokens; Console.WriteLine(nodes.ToTokenTypeString()); // Dump the tree nodes if -n is passed on the command line. DumpNodes(nodes); //SymbolTable symtab = new SymbolTable(); // init symbol table //Def def = new Def(nodes, symtab); // create Def phase //def.Downup(tree); // Do pass 1 //System.Console.WriteLine("globals: \n" + symtab.globals + "\n\n"); //System.ConsoleColor restore = System.Console.ForegroundColor; //System.Console.ForegroundColor = ConsoleColor.Yellow; //System.Console.WriteLine("Ref:"); //System.Console.ForegroundColor = restore; //nodes.Reset(); // rewind AST node stream to root //Ref @ref = new Ref(nodes); // create Ref phase //@ref.Downup(tree); // Do pass 2 Application.Run(new Form1()); }