Exemplo n.º 1
0
        internal static DynamicExprExpression LoadDynamicExpr(Script script, SourceCode source)
        {
            AntlrErrorListener listener = new AntlrErrorListener(source);

            try
            {
                LuaParser parser = CreateParser(script, new AntlrInputStream(source.Code), source.SourceID, p => p.dynamicexp(), listener);

                ScriptLoadingContext lcontext = CreateLoadingContext(script, source);
                lcontext.IsDynamicExpression = true;
                lcontext.Anonymous           = true;

                DynamicExprExpression stat;

                using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.AstCreation))
                    stat = new DynamicExprExpression(parser.dynamicexp(), lcontext);

                return(stat);
            }
            catch (ParseCanceledException ex)
            {
                HandleParserError(ex, listener);
                throw;
            }
        }
Exemplo n.º 2
0
        internal static int LoadChunk(Script script, SourceCode source, ByteCode bytecode, Table globalContext)
        {
            AntlrErrorListener listener = new AntlrErrorListener(source);

            try
            {
                LuaParser parser = CreateParser(script, new AntlrInputStream(source.Code), source.SourceID, p => p.chunk(), listener);

                ScriptLoadingContext lcontext = CreateLoadingContext(script, source);
                ChunkStatement       stat;

                using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.AstCreation))
                    stat = new ChunkStatement(parser.chunk(), lcontext, globalContext);

                int beginIp = -1;

                //var srcref = new SourceRef(source.SourceID);

                using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.Compilation))
                    using (bytecode.EnterSource(null))
                    {
                        bytecode.Emit_Nop(string.Format("Begin chunk {0}", source.Name));
                        beginIp = bytecode.GetJumpPointForLastInstruction();
                        stat.Compile(bytecode);
                        bytecode.Emit_Nop(string.Format("End chunk {0}", source.Name));
                    }

                Debug_DumpByteCode(bytecode, source.SourceID);

                return(beginIp);
            }
            catch (ParseCanceledException ex)
            {
                HandleParserError(ex, listener);
                throw;
            }
        }
Exemplo n.º 3
0
        internal static int LoadFunction(Script script, SourceCode source, ByteCode bytecode, Table globalContext)
        {
            AntlrErrorListener listener = new AntlrErrorListener(source);

            try
            {
                LuaParser parser = CreateParser(script, new AntlrInputStream(source.Code), source.SourceID, p => p.singlefunc(), listener);

                ScriptLoadingContext         lcontext = CreateLoadingContext(script, source);
                FunctionDefinitionExpression fndef;

                using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.AstCreation))
                    fndef = new FunctionDefinitionExpression(parser.anonfunctiondef(), lcontext, false, globalContext);

                int beginIp = -1;

                // var srcref = new SourceRef(source.SourceID);

                using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.Compilation))
                    using (bytecode.EnterSource(null))
                    {
                        bytecode.Emit_Nop(string.Format("Begin function {0}", source.Name));
                        beginIp = fndef.CompileBody(bytecode, source.Name);
                        bytecode.Emit_Nop(string.Format("End function {0}", source.Name));

                        Debug_DumpByteCode(bytecode, source.SourceID);
                    }

                return(beginIp);
            }
            catch (ParseCanceledException ex)
            {
                HandleParserError(ex, listener);
                throw;
            }
        }
Exemplo n.º 4
0
        private static LuaParser CreateParser(Script script, ICharStream charStream, int sourceIdx, Func <LuaParser, IParseTree> dumper, AntlrErrorListener errorListener)
        {
            LuaLexer  lexer;
            LuaParser parser;

            using (script.PerformanceStats.StartStopwatch(Diagnostics.PerformanceCounter.Parsing))
            {
                lexer = new LuaLexer(charStream);
                lexer.RemoveErrorListeners();
                lexer.AddErrorListener(errorListener);

                parser = new LuaParser(new CommonTokenStream(lexer))
                {
                    ErrorHandler = new BailErrorStrategy(),
                };

                parser.Interpreter.PredictionMode = PredictionMode.Ll;
                parser.RemoveErrorListeners();
                parser.AddErrorListener(errorListener);
                //Debug_DumpAst(parser, sourceIdx, dumper);
            }


            return(parser);
        }
Exemplo n.º 5
0
        private static void HandleParserError(ParseCanceledException ex, AntlrErrorListener listener)
        {
            string msg = listener.Message ?? (string.Format("Unknown syntax error. <eof> expected ? : {0}", ex.Message));

            throw new SyntaxErrorException(msg);
        }