Ejemplo n.º 1
0
        /// <summary>
        /// Execute the input stream
        /// </summary>
        /// <param name="input">Input stream to be executed</param>
        public void InterpretAntlrStream(ANTLRStringStream input)
        {
            KermitLexer        lexer  = new KermitLexer(input);
            TokenRewriteStream tokens = new TokenRewriteStream(lexer);

            _parser.TokenStream = tokens;

            AstParserRuleReturnScope <KermitAST, CommonToken> ret;

            try
            {
                ret = _parser.program();
                CommitableScope g = GlobalScope as CommitableScope;
                g?.CommitScope();
            }
            catch (Exception e) when(e is ParserException || e is PartialStatement)
            {
                CommitableScope g = GlobalScope as CommitableScope;

                g?.RevertScope();
                throw;
            }

            if (_parser.NumberOfSyntaxErrors == 0)
            {
                _root = ret.Tree;

                try
                {
                    Block(_root);
                    CommitableScope g = GlobalScope as CommitableScope;
                    g?.CommitScope();
                }
                catch (InterpreterException e)
                {
                    if (e.CallStack != null)
                    {
                        Listener.Error("CallStack:");
                        foreach (string call in e.CallStack)
                        {
                            Listener.Error(" - " + call);
                        }
                    }
                    Listener.Error(e);
                    Stack.Clear(); // Clear the function stack after an error
                }
            }
            else // We shouldn't reach this condition never
            {
                //throw new InterpreterException($"{_parser.NumberOfSyntaxErrors} syntax errors");
                Listener.Error($"{_parser.NumberOfSyntaxErrors} syntax errors");
            }
        }
Ejemplo n.º 2
0
        public static void Loop2()
        {
            bool         exit   = false;
            string       input  = "";
            KermitParser parser = new KermitParser(null);

            //parser.TreeAdaptor = new KermitAdaptor();
            while (!exit)
            {
                if (input == string.Empty)
                {
                    Console.Write("> ");
                }
                else
                {
                    Console.Write(".. ");
                }
                input += Console.ReadLine() + "\n";
                ANTLRStringStream  stream = new ANTLRStringStream(input);
                KermitLexer        lexer  = new KermitLexer(stream);
                TokenRewriteStream tokens = new TokenRewriteStream(lexer);
                parser.TokenStream = tokens;
                try
                {
                    AstParserRuleReturnScope <KermitAST, CommonToken> result = parser.program();
                    var tree             = (CommonTree)result.Tree;
                    DotTreeGenerator gen = new DotTreeGenerator();
                    Console.WriteLine("{0}", gen.ToDot(tree));
                    input = "";

                    //Console.WriteLine(globalScope.ToString());
                }
                catch (PartialStatement)
                {
                }
                catch (ParserException e)
                {
                }
            }
        }