Exemple #1
0
 public static IParseTree ReconstructTreeAux(Parser grammar, Lexer lexer, IParseTree ast_tree, ParserRuleContext parent)
 {
     if (ast_tree == null)
     {
         return(null);
     }
     // Pre order visit.
     if (ast_tree as AstParserParser.NodeContext != null)
     {
         // Convert.
         var v  = ast_tree as AstParserParser.NodeContext;
         var id = v.GetChild(1).GetText();
         if (id == "TOKEN")
         {
             var type_attr = v.GetChild(2);
             var type_str  = type_attr.GetChild(2).GetText();
             type_str = type_str.Substring(1, type_str.Length - 2);
             var type      = Int32.Parse(type_str);
             var text_attr = v.GetChild(3);
             var text_str  = text_attr.GetChild(2).GetText();
             text_str = text_str.Substring(1, text_str.Length - 2);
             var text = text_str;
             var sym  = new CommonToken(type, text);
             var x    = new TerminalNodeImpl(sym);
             if (parent != null)
             {
                 parent.AddChild(x);
             }
             return(x);
         }
         else
         {
             // Look up "<id>Context" in grammar.
             id = id + "Context";
             var u = grammar.GetType().GetNestedTypes().Where(t =>
             {
                 if (t.IsClass && t.Name.ToLower() == id.ToLower())
                 {
                     return(true);
                 }
                 return(false);
             });
             var      w     = u.FirstOrDefault();
             object[] parms = new object[2];
             parms[0] = parent;
             parms[1] = 0;
             var x = (ParserRuleContext)Activator.CreateInstance(w, parms);
             if (parent != null)
             {
                 parent.AddChild(x);
             }
             for (int i = 0; i < ast_tree.ChildCount; ++i)
             {
                 var c  = ast_tree.GetChild(i);
                 var eq = ReconstructTreeAux(grammar, lexer, c, x);
             }
             return(x);
         }
     }
     else if (ast_tree as AstParserParser.AttrContext != null)
     {
         return(null);
     }
     else
     {
         var tni = ast_tree as TerminalNodeImpl;
         var sym = tni.Symbol;
         var pp  = sym.GetType().FullName;
         return(null);
     }
 }
 private void AddCtx(ParserRuleContext ctx, string s)
 {
     ctx.AddChild(new CommonToken(0, s));
 }