Esempio n. 1
0
        public static void Format(ICharStream stream, IFormatWriter writer, FormatOptions options)
        {
            var lexer  = new LuaLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new LuaParser(tokens);

            tokens.Fill();

            var comments = tokens.GetTokens().Where(t => t.Channel == LuaLexer.Hidden);
            var spaces   = tokens.GetTokens().Where(t => t.Channel == 2);

            parser.BuildParseTree = true;
            parser.TrimParseTree  = false;

            IRuleNode root = parser.chunk();

            var ctx = new FormatContext(root, comments, spaces, writer, options);

            RuleFormatter.Format(root, ctx);

            ctx.WriteComments(int.MaxValue);

            var allTokens = tokens.GetTokens();

            if (allTokens.Count > 0)
            {
                var lastToken = allTokens[allTokens.Count - 1];
                while (ctx.line <= lastToken.Line)
                {
                    ctx.WriteLineBreak();
                }
            }

            tokens.Release(0);
        }
Esempio n. 2
0
        public void ParseLua()
        {
            var inputStream       = new AntlrInputStream(@"
     do
       local t = {}
       t[f(1)] = g
       t[1] = 5         -- 1st exp
       t[2] = 2         -- 2nd exp
       t.x = 1          
       t[3] = f(x)      -- 3rd exp
       t[30] = 23
       t[4] = 45        -- 4th exp
       a = t
     end
");
            var lexer             = new LuaLexer(inputStream);
            var commonTokenStream = new CommonTokenStream(lexer);
            var parser            = new LuaParser(commonTokenStream);
            var visitor           = new CstBuilderForAntlr4(parser);

            visitor.Visit(parser.chunk());
            Console.WriteLine(visitor.FinishParsing());
        }
Esempio n. 3
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;
            }
        }
        public void ParseLua() {
            var inputStream = new AntlrInputStream(@"
     do
       local t = {}
       t[f(1)] = g
       t[1] = 5         -- 1st exp
       t[2] = 2         -- 2nd exp
       t.x = 1          
       t[3] = f(x)      -- 3rd exp
       t[30] = 23
       t[4] = 45        -- 4th exp
       a = t
     end
");
            var lexer = new LuaLexer(inputStream);
            var commonTokenStream = new CommonTokenStream(lexer);
            var parser = new LuaParser(commonTokenStream);
            var visitor = new CstBuilderForAntlr4(parser);
            visitor.Visit(parser.chunk());
            Console.WriteLine(visitor.FinishParsing());
        }