コード例 #1
0
ファイル: Main.cs プロジェクト: vikas753/examples-v3
        public static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                string fullpath;
                if ( Path.IsPathRooted(args[0]) )
                    fullpath = args[0];
                else
                    fullpath = Path.Combine(Environment.CurrentDirectory, args[0]);

                Console.Out.WriteLine("Processing file: {0}", fullpath);
                ICharStream input = new ANTLRFileStream(fullpath);
                SimpleCLexer lex = new SimpleCLexer(input);
                CommonTokenStream tokens = new CommonTokenStream(lex);
                SimpleCParser parser = new SimpleCParser(tokens);
                SimpleCParser.program_return r = parser.program();
                Console.Out.WriteLine("tree="+((ITree)r.Tree).ToStringTree());

                if ( parser.NumberOfSyntaxErrors > 0 ) {
                    // don't tree parse if has errors
                    return;
                }

                CommonTreeNodeStream nodes = new CommonTreeNodeStream((ITree)r.Tree);
                nodes.TokenStream = tokens;
                SimpleCWalker walker = new SimpleCWalker(nodes);

                walker.program();
            }
            else
                Console.Error.WriteLine("Usage: SimpleC <input-file>");
        }
コード例 #2
0
        public static string Compile(string input)
        {
            input = input.Replace("\r", "");
            ANTLRStringStream Input = new ANTLRStringStream(input);
            SugarCppLexer lexer = new SugarCppLexer(Input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            SugarCppParser parser = new SugarCppParser(tokens);

            AstParserRuleReturnScope<CommonTree, IToken> t = parser.root();
            CommonTree ct = (CommonTree)t.Tree;

            if (parser.errors.Count() > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var error in parser.errors)
                {
                    sb.Append(error);
                    sb.Append("\n");
                }
                throw new Exception(sb.ToString());
            }

            CommonTreeNodeStream nodes = new CommonTreeNodeStream(ct);
            SugarWalker walker = new SugarWalker(nodes);

            Root ast = walker.root();

            TargetCpp target_cpp = new TargetCpp();

            return ast.Accept(target_cpp).Render();
        }
コード例 #3
0
ファイル: TreeRewriter.cs プロジェクト: sebasjm/antlr
        public virtual object ApplyOnce( object t, Func<TreeRuleReturnScope> whichRule )
        {
            if ( t == null )
                return null;

            try
            {
                // share TreeParser object but not parsing-related state
                state = new RecognizerSharedState();
                input = new CommonTreeNodeStream( originalAdaptor, t );
                ( (CommonTreeNodeStream)input ).TokenStream = originalTokenStream;
                BacktrackingLevel = 1;
                TreeRuleReturnScope r = whichRule();
                BacktrackingLevel = 0;
                if ( Failed() )
                    return t;
                if (showTransformations &&
                    r != null && !t.Equals(r.Tree) && r.Tree != null)
                {
                    ReportTransformation(t, r.Tree);
                }
                if (r != null && r.Tree != null)
                    return r.Tree;
                else
                    return t;
            }
            catch ( RecognitionException )
            {
            }
            return t;
        }
コード例 #4
0
ファイル: TreeRewriter.cs プロジェクト: ksmyth/antlr
        public virtual object ApplyOnce( object t, System.Func<TreeRuleReturnScope> whichRule )
        {
            if ( t == null )
                return null;

            try
            {
                // share TreeParser object but not parsing-related state
                state = new RecognizerSharedState();
                input = new CommonTreeNodeStream( originalAdaptor, t );
                ( (CommonTreeNodeStream)input ).TokenStream = originalTokenStream;
                BacktrackingLevel = 1;
                TreeRuleReturnScope r = whichRule();
                BacktrackingLevel = 0;
                if ( Failed )
                    return t;
                if ( r != null && !t.Equals( r.Tree ) && r.Tree != null )
                { // show any transformations
                    Console.Out.WriteLine( ( (CommonTree)t ).ToStringTree() + " -> " +
                                       ( (CommonTree)r.Tree ).ToStringTree() );
                }
                if ( r != null && r.Tree != null )
                    return r.Tree;
                else
                    return t;
            }
            catch ( RecognitionException )
            {
            }
            return t;
        }
コード例 #5
0
ファイル: Main.cs プロジェクト: vikas753/examples-v3
        public static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                string fullpath;
                if ( Path.IsPathRooted(args[0]) )
                    fullpath = args[0];
                else
                    fullpath = Path.Combine(Environment.CurrentDirectory, args[0]);

                Console.Out.WriteLine("Processing file: {0}", fullpath);
                ICharStream input = new ANTLRFileStream(fullpath);
                LangLexer lex = new LangLexer(input);
                CommonTokenStream tokens = new CommonTokenStream(lex);
                LangParser parser = new LangParser(tokens);

                //LangParser.decl_return r = parser.decl();
                LangParser.start_return r = parser.start();
                Console.Out.WriteLine("tree: "+((ITree)r.Tree).ToStringTree());
                CommonTree r0 = ((CommonTree)r.Tree);
                CommonTreeNodeStream nodes = new CommonTreeNodeStream(r0);
                nodes.TokenStream = tokens;
                LangDumpDecl walker = new LangDumpDecl(nodes);
                walker.decl();
            }
            else
                Console.Error.WriteLine("Usage: TreeParser <input-file>");
        }
コード例 #6
0
ファイル: Unit.cs プロジェクト: brianex/osu-sgl
        public Boolean RunTest()
        {
            try
            {
                GlobalMemory.Clear();

                var sStream = new ANTLRStringStream(input);
                var lexer = new SGLLexer(sStream);

                var tStream = new CommonTokenStream(lexer);

                // Parsing
                var parser = new SGLParser(tStream);
                var t = (CommonTree) parser.main().Tree;

                // Printing tree
                Console.WriteLine("; " + t.ToStringTree());

                // TreeWalking
                var treeStream = new CommonTreeNodeStream(t);

                var tw = new SGLTreeWalker(treeStream, true);
                AbstractNode returned = tw.main();
                returned.Evaluate();

                if (debug)
                {
                    realOutput = GlobalMemory.Instance.DebugString;
                }
                else
                {
                    realOutput = GlobalMemory.Instance.StoryboardCode.ToString();
                }

                // comparison
                realOutput = realOutput.Trim();
                output.Trim();

                if (output.Equals(realOutput))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (CompilerException ce)
            {
                Console.WriteLine(ce.GetExceptionAsString());
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Es ist ein Fehler aufgetreten.");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return false;
            }
        }
コード例 #7
0
        public void ThatArraysCanHaveArrays()
        {
            var parser = Utilities.jsonParserFromString("[123,[],[654,888],345]");
            var tree = parser.array().Tree;

            var stream = new CommonTreeNodeStream(tree);
            var walker = new JsonTree(stream);
            var root = walker.array();
            Assert.AreEqual(4, root.Length);
            Assert.AreEqual(123d, root[0]);
            Assert.AreEqual(345d, root[3]);

            var a1 = root[1];
            var a2 = root[2];
            Assert.AreEqual(typeof(object[]), a1.GetType());
            Assert.AreEqual(typeof(object[]), a2.GetType());

            Assert.AreEqual(0, ((object[])a1).Length);
            Assert.AreEqual(2, ((object[])a2).Length);

            var o2 = (object[])a2;
            Assert.AreEqual(654d, o2[0]);
            Assert.AreEqual(888d, o2[1]);

        }
コード例 #8
0
ファイル: Unit.cs プロジェクト: peppy/sgl4cs
        public Boolean RunTest()
        {
            try
            {
            ANTLRStringStream sStream = new ANTLRStringStream(input);
            SGLLexer lexer = new SGLLexer(sStream);

            CommonTokenStream tStream = new CommonTokenStream(lexer);

            // Parsing
            SGLParser parser = new SGLParser(tStream);
            CommonTree t = (CommonTree)parser.compilationUnit().Tree;

            // Printing tree
            Console.WriteLine("; " + t.ToStringTree());

            // TreeWalking
            CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);

            SGLTreeWalker tw = new SGLTreeWalker(treeStream);
            SGLNode returned = tw.compilationUnit();
            returned.Evaluate();

            realOutput = tw.GetStoryboardCode().ToString();

            // comparison
            realOutput = realOutput.Trim();
            output.Trim();

            if (output.Equals(realOutput))
            {
                return true;
            }
            else
            {
                return false;
            }

            }
            catch (SGLCompilerException ce)
            {
                if (ce.ErrorType.Equals("Antlr.Parser"))
                {
                    Console.WriteLine("Error (wrong syntax) on " + ce.Message);
                }
                else
                {
                    Console.WriteLine("Error (" + ce.ErrorType + ") on line " + ce.Line + ": " + ce.Message);
                }
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Es ist ein Fehler aufgetreten.");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return false;
            }
        }
コード例 #9
0
        public static JsonTree jsonTreeFromString(string input)
        {
            var parser = jsonParserFromString("\"123\"");
            var tree = parser.@object().Tree;

            var stream = new CommonTreeNodeStream(tree);
            return new JsonTree(stream);
        }
コード例 #10
0
ファイル: Template.cs プロジェクト: claco/tt.net
        public string Process(string template, Dictionary<string, object> variables)
        {
            //This is a hack, but it removes the need for a step to separate html from tt
            //by treating it all as literals between tags
            Console.WriteLine("--------------------------------------------");
            Console.WriteLine(template);
            template = string.Concat("[%'", template.Replace("[%", "'^^][%").Replace("%]", "%][%'").Replace("'^^]", "'%]"), "'%]");
            template = template.Replace("[%''%]", "");
            Console.WriteLine("--------------------------------------------");
            Console.WriteLine(template);
            Console.WriteLine("--------------------------------------------");


            //CSharpTarget.stg contains string templates used for targeting C#
            //TextReader stgReader = new StreamReader(File.Open("CSharpGenerator.stg", FileMode.Open));
            TextReader stgReader = new StreamReader("CSharpGenerator.stg");
            StringTemplateGroup stg = new StringTemplateGroup(stgReader);

            //lex
            ICharStream input = new ANTLRStringStream(template);
            TemplateLexer lex = new TemplateLexer(input);

            //parse
            TemplateParser parse = new TemplateParser(new CommonTokenStream(lex));
            TemplateParser.document_return parseRet = parse.document();

            Console.WriteLine(( (CommonTree) parseRet.Tree).ToStringTree());

            //generate C#
            ITreeNodeStream nodeStream = new CommonTreeNodeStream(parseRet.Tree);
            CSharpGenerator gen = new CSharpGenerator(nodeStream);
            gen.TemplateLib = stg;
            CSharpGenerator.document_return ret = gen.document();

            //compile to executable and optionally run
            Console.WriteLine("--------------------------------------------");
            Console.WriteLine(ret.Template.ToString());
            Console.WriteLine("--------------------------------------------");


            // compile and run it
            var inMemory = true;
            Assembly assembly = Generate(ret.Template.ToString(), "", inMemory);
            if (inMemory)
            {
                //assembly.EntryPoint.Invoke(null, new object[] { new string[] { } });
                var o = assembly.CreateInstance("x");
                Object[] args = {this.Settings, variables};
                var result = o.GetType().InvokeMember("Render", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, o, args);

                return result.ToString();
            } 


            return "";
        }
コード例 #11
0
ファイル: Yaml.cs プロジェクト: tkellogg/enyaml
 internal static object Parse(string content)
 {
     var lexer = new YamlLexer(new ANTLRStringStream(content));
     var tokens = new CommonTokenStream(lexer);
     var parser = new YamlParser(tokens);
     var stream = new CommonTreeNodeStream(parser.value().Tree);
     stream.TokenStream = tokens;
     var tree = new YamlTree(stream);
     return tree.value();
 }
コード例 #12
0
ファイル: AbstractTest.cs プロジェクト: brianex/osu-sgl
        public Boolean Run()
        {
            // run lexer on input
            var lexer = new SGLLexer(new ANTLRStringStream(Input));
            var tokenStream = new CommonTokenStream(lexer);

            // run parser on tokens
            var parser = new SGLParser(tokenStream);
            var ast = (CommonTree) parser.main().Tree;

            // maybe test the tree
            if (testTree)
            {
                if (!ast.ToStringTree().Trim().Equals(ExpectedTree.Trim()))
                {
                    result += "Test " + name + "failed! Tree comparison failed!";
                    result += "Expected tree: " + ExpectedTree.Trim();
                    result += "Given tree: " + ast.ToStringTree().Trim();
                    return false;
                }
            }

            var astStream = new CommonTreeNodeStream(ast);

            // run walker on AST
            GlobalMemory.Clear();
            var treewalker = new SGLTreeWalker(astStream, true);
            treewalker.main().Evaluate();
            //String output = treewalker.GetStoryboardCode().ToString();
            if (testOutput)
            {
                String output = GlobalMemory.Instance.StoryboardCode.ToString();
                if (!output.Trim().Equals(ExpectedOutput.Trim()))
                {
                    result += "Test " + name + "failed! Output comparison failed!";
                    result += "Expected output: \r\n" + ExpectedOutput.Trim();
                    result += "Given output: \r\n" + output.Trim();
                    return false;
                }
            }

            if (testDebug)
            {
                String debug = GlobalMemory.Instance.StoryboardCode.ToString();
                if (!debug.Trim().Equals(ExpectedDebug.Trim()))
                {
                    result += "Test " + name + "failed! Debug comparison failed!";
                    result += "Expected debug: \r\n" + ExpectedDebug.Trim();
                    result += "Given debug: \r\n" + debug.Trim();
                    return false;
                }
            }
            return true;
        }
コード例 #13
0
ファイル: Parser.cs プロジェクト: DamianReeves/Functional
 public object ParseFile(string filename, TextReader reader)
 {
     var input = new ANTLRReaderStream(reader);
     var lexer = new FunctionalLexer(input);
     var tokenStream = new CommonTokenStream(lexer);
     var parser = new FunctionalParser(tokenStream);
     var tree = parser.prog().Tree;
     var nodes = new CommonTreeNodeStream(tree);
     var eval = new FunctionalTreeParser(nodes, parser.FunctionDefinitions);
     eval.prog();
     return default(BigInteger);
 }
コード例 #14
0
        public void ThatArraysCanBeEmpty()
        {
            var str = "[]";
            var parser = Utilities.jsonParserFromString(str);
            var tree = parser.array().Tree;

            var stream = new CommonTreeNodeStream(tree);
            var walker = new JsonTree(stream);
            var root = walker.array();
            Assert.AreEqual(0, root.Length);

        }
コード例 #15
0
ファイル: BonsaiParser.cs プロジェクト: eugen/Bonsai
        public static Sequence ParseString(string text)
        {
            ANTLRStringStream strStream = new ANTLRStringStream(text);
            BonsaiLexer lexer = new BonsaiLexer(strStream);
            CommonTokenStream tokStream = new CommonTokenStream(lexer);
            Generated.BonsaiParser parser = new Generated.BonsaiParser(tokStream);
            var returnValue = parser.program();
            CommonTree tree = (CommonTree)returnValue.Tree;
            CommonTreeNodeStream treeNodeStream = new CommonTreeNodeStream((CommonTree)returnValue.Tree);
            Generated.BonsaiTree treeWalker = new Generated.BonsaiTree(treeNodeStream);

            return new Sequence(treeWalker.program().result.Statements);
        }
コード例 #16
0
        public void ThatSingleAssignmentWorks()
        {
            var parser = Utilities.jsonParserFromString("\"key\":[1,2,3]");
            var tree = parser.pair().Tree;

            var stream = new CommonTreeNodeStream(tree);
            var walker = new JsonTree(stream);
            var root = walker.kvp();
            Assert.AreEqual("key", root.Key);
            Assert.AreEqual(typeof(object[]), root.Value.GetType());
            Assert.AreEqual(2d, ((object[])root.Value)[1]);

        }
コード例 #17
0
        public void ThatEmptyObjectParses()
        {
            var parser = Utilities.jsonParserFromString("{}");
            var tree = parser.@object().Tree;

            var stream = new CommonTreeNodeStream(tree);
            var walker = new JsonTree(stream);
            var root = walker.@object();

            Assert.AreEqual(typeof(KeyValuePair<string, object>[]), root.GetType());
            Assert.AreEqual(0, root.Length);

        }
コード例 #18
0
 private SourceEval createWalker(string file)
 {
     ANTLRFileStream stream = new ANTLRFileStream(file);
     SourceExprLexer lex = new SourceExprLexer(stream);
     CommonTokenStream tokens = new CommonTokenStream(lex);
     SourceExprParser parser = new SourceExprParser(tokens);
     SourceExprParser.prog_return r = parser.prog(_script);
     // WALK RESULTING TREE
     CommonTree t = (CommonTree)r.Tree; // get tree from parser
     // Create a tree node stream from resulting tree
     CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
     SourceEval walker = new SourceEval(nodes); // create a tree parser
     return walker;
 }
コード例 #19
0
ファイル: Parser.cs プロジェクト: adbrowne/abcm
        public static CFlatTree CreateParser(string input)
        {
            var antlrStringStream = new ANTLRStringStream(input);
            var lexter = new CFlatLexer(antlrStringStream);
            var tokens = new CommonTokenStream(lexter);
            var parser = new CFlatParser(tokens);

            var tree = parser.prog().Tree;

            var nodes = new CommonTreeNodeStream(tree);
            var walker = new CFlatTree(nodes, new ErrorSet());

            return walker;
        }
コード例 #20
0
        public void ThatArraysHasNumbers()
        {
            var parser = Utilities.jsonParserFromString("[123,234,345]");
            var tree = parser.array().Tree;

            var stream = new CommonTreeNodeStream(tree);
            var walker = new JsonTree(stream);
            var root = walker.array();

            Assert.AreEqual(123d, root[0]);
            Assert.AreEqual(234d, root[1]);
            Assert.AreEqual(345d, root[2]);

        }
コード例 #21
0
        public void ThatArraysCanHaveStrings()
        {
            var str = "[\"oeu\",\"uu\"]";
            var parser = Utilities.jsonParserFromString(str);
            var tree = parser.array().Tree;

            var stream = new CommonTreeNodeStream(tree);
            var walker = new JsonTree(stream);
            var root = walker.array();
            Assert.AreEqual(2, root.Length);
            Assert.AreEqual("oeu", root[0]);
            Assert.AreEqual("uu", root[1]);

        }
コード例 #22
0
        public Module Compile(string function)
        {
            var input = new ANTLRStringStream(function);
            var lexer = new NPortugolLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new NPortugolParser(tokens);

            var ast = parser.script();
            var tree = (CommonTree)ast.Tree;
            var nodes = new CommonTreeNodeStream(tree) { TokenStream = tokens };
            var walker = new NPortugolWalker(nodes);

            return walker.compile();
        } 
コード例 #23
0
ファイル: StackSteps.cs プロジェクト: AustinWise/CSC431
        public static InStep<Tuple<CommonTokenStream, CommonTree>> MakeClrExe()
        {
            return new InStep<Tuple<CommonTokenStream, CommonTree>>(t =>
            {
                CommonTreeNodeStream nodes = new CommonTreeNodeStream(t.Item2);
                nodes.TokenStream = t.Item1;
                StackGen tparser = new StackGen(nodes);
                tparser.TraceDestination = Console.Out;

                tparser.Program();

                if (tparser.NumberOfSyntaxErrors != 0)
                    throw new EvilException(EvilSystem.Clr, "CLR EXE tree walking errors.");
            });
        }
コード例 #24
0
ファイル: Npc.cs プロジェクト: ricardoborges/NPortugol
        public Bytecode Compilar(string script)
        {
            var input  = new ANTLRStringStream(script);
            var lexer  = new NPortugolLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new NPortugolParser(tokens);

            var ast = parser.script();

            var tree   = (CommonTree)ast.Tree;
            var nodes  = new CommonTreeNodeStream(tree) { TokenStream = tokens };
            var walker = new NPortugolWalker(nodes) { DebugInfo = DebugInfo };

            var asm = walker.script();

            return new Bytecode(asm, parser.Functions, walker.SourceMap);
        }
コード例 #25
0
		protected SqlString GenerateIdInsertSelect(IQueryable persister, string tableAlias, IASTNode whereClause)
		{
			var select = new SqlSelectBuilder(Factory);
			SelectFragment selectFragment = new SelectFragment(Factory.Dialect)
				.AddColumns(tableAlias, persister.IdentifierColumnNames, persister.IdentifierColumnNames);
			select.SetSelectClause(selectFragment.ToFragmentString().Substring(2));

			string rootTableName = persister.TableName;
			SqlString fromJoinFragment = persister.FromJoinFragment(tableAlias, true, false);
			select.SetFromClause(rootTableName + " " + tableAlias + fromJoinFragment);
			
			var whereJoinFragment = GetWhereJoinFragment(persister, tableAlias);

			SqlString userWhereClause = SqlString.Empty;
			if (whereClause.ChildCount != 0)
			{
				// If a where clause was specified in the update/delete query, use it to limit the
				// returned ids here...
				try
				{
					var nodes = new CommonTreeNodeStream(whereClause);
					var gen = new SqlGenerator(Factory, nodes);
					gen.whereClause();
					userWhereClause = gen.GetSQL().Substring(7);
				}
				catch (RecognitionException e)
				{
					throw new HibernateException("Unable to generate id select for DML operation", e);
				}
				if (whereJoinFragment.Length > 0)
				{
					whereJoinFragment.Append(" and ");
				}
			}

			select.SetWhereClause(whereJoinFragment + userWhereClause);

			var insert = new InsertSelect();
			if (Factory.Settings.IsCommentsEnabled)
			{
				insert.SetComment("insert-select for " + persister.EntityName + " ids");
			}
			insert.SetTableName(persister.TemporaryIdTableName);
			insert.SetSelect(select);
			return insert.ToSqlString();
		}
コード例 #26
0
        public virtual object ApplyOnce(object t, System.Func <IAstRuleReturnScope <TTree> > whichRule)
        {
            if (t == null)
            {
                return(null);
            }

            try
            {
                // share TreeParser object but not parsing-related state
                state = new RecognizerSharedState();
                input = new CommonTreeNodeStream(originalAdaptor, t);
                ((CommonTreeNodeStream)input).TokenStream = originalTokenStream;
                BacktrackingLevel = 1;
                IAstRuleReturnScope <TTree> r = whichRule();
                BacktrackingLevel = 0;
                if (Failed)
                {
                    return(t);
                }

                if (typeof(CommonTree).IsAssignableFrom(typeof(TTree)))
                {
                    if (r != null && !t.Equals(r.Tree) && r.Tree != null)
                    {
                        // show any transformations
                        Console.Out.WriteLine(((CommonTree)t).ToStringTree() + " -> " +
                                              ((CommonTree)(object)r.Tree).ToStringTree());
                    }
                }

                if (r != null && r.Tree != null)
                {
                    return(r.Tree);
                }
                else
                {
                    return(t);
                }
            }
            catch (RecognitionException)
            {
            }
            return(t);
        }
コード例 #27
0
ファイル: TreeFilter.cs プロジェクト: biddyweb/azfone-ios
        public virtual void ApplyOnce( object t, Action whichRule )
        {
            if ( t == null )
                return;

            try
            {
                // share TreeParser object but not parsing-related state
                state = new RecognizerSharedState();
                input = new CommonTreeNodeStream( originalAdaptor, t );
                ( (CommonTreeNodeStream)input ).TokenStream = originalTokenStream;
                BacktrackingLevel = 1;
                whichRule();
                BacktrackingLevel = 0;
            }
            catch ( RecognitionException )
            {
            }
        }
コード例 #28
0
 public BaseAST Parse(string code)
 {
     var codeStream = new ANTLRStringStream(code);
     var lexer = new EvaluationGrammarLexer(codeStream);
     if (lexer.NumberOfSyntaxErrors > 0)
     {
         return null;
     }
     var tokenStream = new CommonTokenStream(lexer);
     var parser = new EvaluationGrammarParser(tokenStream);
     var parseTree = (CommonTree)parser.program().Tree;
     if (parser.NumberOfSyntaxErrors > 0)
     {
         return null;
     }
     var nodes = new CommonTreeNodeStream(parseTree);
     var astGenerator = new ASTGenerator(nodes);
     return astGenerator.getAST();
 }
コード例 #29
0
ファイル: AntlrUtils.cs プロジェクト: exaphaser/cs2j
        public static void DumpNodesFlat(CommonTreeNodeStream nodes, string title)
        {
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine(title);
            //object o_prev = string.Empty;
            object o = nodes.NextElement();
            while (!nodes.IsEndOfFile(o))
            {
                if (o.ToString() == "DOWN")
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("{");
                    Console.ForegroundColor = ConsoleColor.Magenta;
                }
                else if (o.ToString() == "UP")
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write(" }");
                    Console.ForegroundColor = ConsoleColor.Magenta;
                }
//                 if (o.ToString() == "DOWN")
//                 {
//                     if (o_prev.ToString() != "UP" && o_prev.ToString() != "DOWN")
//                         Console.Write("  {0}{}", o_prev);
//                 }
//                 else if (o.ToString() == "UP")
//                 {
//                     if (o_prev.ToString() != "UP" && o_prev.ToString() != "DOWN")
//                         Console.Write("  {0} ]", o_prev);
//                 }
                else
                {
                    Console.Write(" {0}", o.ToString());
                }


                o = nodes.NextElement();
            }
//             if (o_prev.ToString() != "UP" && o_prev.ToString() != "DOWN")
//                 Console.Write(" {0}", o_prev.ToString());
            Console.WriteLine();
            Console.ResetColor();
        }
コード例 #30
0
        public static string FormatType(ITree typeTree)
        {
            if (typeTree == null)
                return "<null>";

            try
            {
                CommonTreeNodeStream input = new CommonTreeNodeStream(typeTree);
                GoTypeFormatter formatter = new GoTypeFormatter(input);
                return formatter.type();
            }
            catch (Exception e)
            {
                if (ErrorHandler.IsCriticalException(e))
                    throw;

                return "?";
            }
        }
コード例 #31
0
ファイル: Emitter.cs プロジェクト: keithharvey/Script-Parser
        static string createRewriter(string file, Script script)
        {
            ANTLRFileStream stream = new ANTLRFileStream(file);
            SourceExprLexer lex = new SourceExprLexer(stream);
            // create a buffer of tokens pulled from the lexer
            // Must use TokenRewriteStream not CommonTokenStream!
            TokenRewriteStream tokens = new TokenRewriteStream(lex);
            SourceExprParser parser = new SourceExprParser(tokens);
            SourceExprParser.prog_return r = parser.prog(script);

            // WALK TREE AND REWRITE TOKEN BUFFER
            CommonTree t = (CommonTree)r.Tree; // get tree from parser
            // create a stream of tree nodes from AST built by parser
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
            // tell it where it can find the token objects
            nodes.TokenStream = tokens;
            //SourceRewrite rewriter = new SourceRewrite(nodes);
            //rewriter.prog(script);
            return tokens.ToString(); // return tweaked token buffer
        }
コード例 #32
0
ファイル: TreeRewriter.cs プロジェクト: ymf1/webgrease
        public virtual object ApplyOnce(object t, System.Func <IAstRuleReturnScope> whichRule)
        {
            if (t == null)
            {
                return(null);
            }

            try
            {
                // share TreeParser object but not parsing-related state
                state = new RecognizerSharedState();
                input = new CommonTreeNodeStream(originalAdaptor, t);
                ((CommonTreeNodeStream)input).TokenStream = originalTokenStream;
                BacktrackingLevel = 1;
                IAstRuleReturnScope r = whichRule();
                BacktrackingLevel = 0;
                if (Failed)
                {
                    return(t);
                }

                if (showTransformations && r != null && !t.Equals(r.Tree) && r.Tree != null)
                {
                    ReportTransformation(t, r.Tree);
                }

                if (r != null && r.Tree != null)
                {
                    return(r.Tree);
                }
                else
                {
                    return(t);
                }
            }
            catch (RecognitionException)
            {
            }

            return(t);
        }
コード例 #33
0
        public virtual void ApplyOnce(object t, Action whichRule)
        {
            if (t == null)
            {
                return;
            }

            try
            {
                // share TreeParser object but not parsing-related state
                state = new RecognizerSharedState();
                input = new CommonTreeNodeStream(originalAdaptor, t);
                ((CommonTreeNodeStream)input).TokenStream = originalTokenStream;
                BacktrackingLevel = 1;
                whichRule();
                BacktrackingLevel = 0;
            }
            catch (RecognitionException)
            {
            }
        }
コード例 #34
0
 internal CommonTreeNodeStreamEnumerator(CommonTreeNodeStream nodeStream)
 {
     _nodeStream = nodeStream;
     Reset();
 }
コード例 #35
0
        /// <summary>
        /// Expensive to compute; recursively walk tree to find size;
        /// include navigation nodes and EOF.  Reuse functionality
        /// in CommonTreeNodeStream as we only really use this
        /// for testing.
        /// </summary>
        public virtual int Size()
        {
            CommonTreeNodeStream s = new CommonTreeNodeStream(root);

            return(s.Size());
        }