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
        } // func T

        private bool TokenTest(string sToken, params KeyValuePair <LuaToken, string>[] token)
        {
            using (LuaLexer l = new LuaLexer("test.lua", new StringReader(sToken)))
            {
                l.Next();

                for (int i = 0; i < token.Length; i++)
                {
                    Debug.Write(String.Format("Test: {0} = {1} ==>", l.Current.Typ, token[i].Key));
                    if (l.Current.Typ != token[i].Key)
                    {
                        Debug.WriteLine("tokens FAILED");
                        return(false);
                    }
                    else if (l.Current.Value != token[i].Value)
                    {
                        Debug.WriteLine("values '{0}' != '{1}'   FAILED", l.Current.Value, token[i].Value);
                        return(false);
                    }
                    Debug.WriteLine("OK");
                    l.Next();
                }
                if (l.Current.Typ != LuaToken.Eof)
                {
                    return(false);
                }
                return(true);
            }
        } // func TokenTest
Esempio n. 3
0
        }         // func GetTokenColor

        public void Scan(IConsoleReadLineScannerSource source)
        {
            if (source.LineCount == 0)
            {
                return;
            }

            var cmdLine = source[0];

            if (cmdLine.StartsWith(":"))             // command
            {
                var startOfArgs = cmdLine.IndexOf(' ');
                if (startOfArgs == -1)
                {
                    startOfArgs = cmdLine.Length;
                }
                source.AppendToken(0, 0, 0, startOfArgs, ConsoleColor.White);
                source.AppendToken(0, startOfArgs, 0, cmdLine.Length, ConsoleColor.Gray);
            }
            else
            {
                using (var lex = LuaLexer.Create("cmd.lua", source.TextReader, true, 0, 0, 0))
                {
                    lex.Next();
                    while (lex.Current.Typ != LuaToken.Eof)
                    {
                        source.AppendToken(lex.Current.Start.Line, lex.Current.Start.Col, lex.Current.End.Line, lex.Current.End.Col, GetTokenColor(lex.Current.Typ));

                        lex.Next();
                    }
                }
            }
        }         // proc Scan
Esempio n. 4
0
        private List <NewFolding> CreateNewFoldings(String text, ref List <MarkerPosition> markers)
        {
            List <NewFolding> newFoldings = null;

            try
            {
                using (var reader = new StringReader(text))
                {
                    var antlrInputStream = new AntlrInputStream(reader);
                    var lexer            = new LuaLexer(antlrInputStream);
                    var tokens           = new CommonTokenStream(lexer);
                    var parser           = new LuaParser(tokens)
                    {
                        BuildParseTree = true
                    };
                    parser.RemoveErrorListeners();
                    parser.AddErrorListener(new MyErrorListener(_textMarkerService, ref markers));
                    var tree    = parser.block();
                    var visitor = new LuaVisitor();
                    newFoldings = visitor.Visit(tree);
                    Interlocked.Exchange(ref _syntaxErrors, parser.NumberOfSyntaxErrors);
                }
            }
            catch (Exception e)
            {
                // MessageBox.Show(e.ToString(), "NodeMCU Studio 2015", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.Yes);
                //On error resume next
            }

            return(newFoldings ?? new List <NewFolding>());
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        }         // func CompileChunk

        /// <summary>Create a code delegate without executing it.</summary>
        /// <param name="code">Code of the delegate..</param>
        /// <param name="name">Name of the delegate</param>
        /// <param name="options">Options for the compile process.</param>
        /// <param name="args">Arguments for the code block.</param>
        /// <returns>Compiled chunk.</returns>
        public LuaChunk CompileChunk(string code, string name, LuaCompileOptions options, params KeyValuePair <string, Type>[] args)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            using (var lex = LuaLexer.Create(name, new StringReader(code)))
                return(CompileChunkCore(lex, options, args));
        }         // func CompileChunk
Esempio n. 7
0
 public LuaSource(string text)
 {
     parserSource = () =>
     {
         var stream = new AntlrInputStream(text);
         var lexar  = new LuaLexer(stream);
         var tokens = new CommonTokenStream(lexar);
         return(new LuaParser(tokens));
     };
 }
Esempio n. 8
0
        }         // prop Dispose

        #endregion

        #region -- Parser -----------------------------------------------------------------

        public NeoLuaChunk ParseChunk(string sText)
        {
            if (isChunkDirty)
            {
                using (LuaLexer l = new LuaLexer(GetFilePath(), new StringReader(sText ?? GetText())))
                    currentChunk = NeoLuaChunk.Parse(l);
                isChunkDirty = false;
            }
            return(currentChunk);
        }         // func ParseChunk
Esempio n. 9
0
        public LuaParser CreateParser(string code)
        {
            ICharStream stream = CharStreams.fromstring(code);
            var         lexer  = new LuaLexer(stream);

            lexer.AddErrorListener(new ThrowExceptionErrorListener());
            ITokenStream tokens = new CommonTokenStream(lexer);
            var          parser = new LuaParser(tokens);

            parser.BuildParseTree = true;
            parser.RemoveErrorListeners();
            parser.AddErrorListener(new ThrowExceptionErrorListener());
            return(parser);
        }
Esempio n. 10
0
        }         // func CompileChunk

        /// <summary>Creates a simple lua-lambda-expression without any environment.</summary>
        /// <param name="sName">Name of the delegate</param>
        /// <param name="sCode">Code of the delegate.</param>
        /// <param name="typeDelegate">Delegate type. <c>null</c> is allowed.</param>
        /// <param name="returnType">Return-Type of the delegate</param>
        /// <param name="arguments">Arguments of the delegate.</param>
        /// <returns></returns>
        public Delegate CreateLambda(string sName, string sCode, Type typeDelegate, Type returnType, params KeyValuePair <string, Type>[] arguments)
        {
            using (LuaLexer l = new LuaLexer(sName, new StringReader(sCode)))
            {
                LambdaExpression expr = Parser.ParseChunk(this, new LuaCompileOptions(), false, l, typeDelegate, returnType, arguments);

                if (lPrintExpressionTree)
                {
                    Console.WriteLine(Parser.ExpressionToString(expr));
                    Console.WriteLine(new string('=', 79));
                }

                return(expr.Compile());
            }
        }         // func CreateLambda
Esempio n. 11
0
        }         // func CompileChunkCore

        /// <summary>Creates a simple lua-lambda-expression without any environment.</summary>
        /// <param name="name">Name of the delegate</param>
        /// <param name="code">Code of the delegate.</param>
        /// <param name="delegateType">Delegate type. <c>null</c> is allowed.</param>
        /// <param name="returnType">Return-Type of the delegate</param>
        /// <param name="arguments">Arguments of the delegate.</param>
        /// <returns></returns>
        public Delegate CreateLambda(string name, string code, Type delegateType, Type returnType, params KeyValuePair <string, Type>[] arguments)
        {
            using (var l = LuaLexer.Create(name, new StringReader(code)))
            {
                var expr = Parser.ParseChunk(this, new LuaCompileOptions(), false, l, delegateType, returnType, arguments);

                if (printExpressionTree != null)
                {
                    printExpressionTree.WriteLine(Parser.ExpressionToString(expr));
                    printExpressionTree.WriteLine(new string('=', 79));
                }

                return(expr.Compile());
            }
        }         // func CreateLambda
Esempio n. 12
0
        public bool ProcessEsoGuildStoreSales(Action <EsoSale> onSaleFound,
                                              params EsoSaleFilter[] filters)
        {
            if (FilePath == null)
            {
                return(false);
            }

            // Set a default filter to not filter anything
            if (filters == null || filters.Length == 0)
            {
                filters    = new EsoSaleFilter[1];
                filters[0] = new EsoSaleFilter();
            }

            // Set up our custom Lua listener for extracting sale data
            var listener = new MMLuaListener();

            // Open the saved variables file for reading
            using (var stream = File.OpenRead(FilePath))
            {
                // Lua parser stuff
                var charStream  = new AntlrInputStream(stream);
                var lexer       = new LuaLexer(charStream);
                var tokenStream = new CommonTokenStream(lexer);
                var parser      = new LuaParser(tokenStream);
                listener.SaleFound += (sender, saleFoundArgs) =>
                {
                    // Make sure the sale matches at least one of the filters, then add it to the list
                    var sale = saleFoundArgs.Sale;
                    if (filters.Where(filter => string.IsNullOrEmpty(filter.GuildName) ||
                                      filter.GuildName.Equals(sale.GuildName,
                                                              StringComparison.CurrentCultureIgnoreCase))
                        .Any(filter => (filter.TimestampMinimum == null || sale.SaleTimestamp >= filter.TimestampMinimum)
                             &&
                             (filter.TimestampMaximum == null || sale.SaleTimestamp <= filter.TimestampMaximum)))
                    {
                        onSaleFound(sale);
                    }
                };
                parser.AddParseListener(listener);

                // Run the Lua parser on the saved variables file
                parser.exp();
            }

            return(true);
        }
Esempio n. 13
0
File: Lua.cs Progetto: tmkls/neolua
        }         // func CompileChunk

        internal LuaChunk CompileChunk(string sChunkName, LuaCompileOptions options, TextReader tr, IEnumerable <KeyValuePair <string, Type> > args)
        {
            if (String.IsNullOrEmpty(sChunkName))
            {
                throw new ArgumentNullException("chunkname");
            }
            if (options == null)
            {
                options = new LuaCompileOptions();
            }

            using (LuaLexer l = new LuaLexer(sChunkName, tr))
            {
                bool lRegisterMethods = options.DebugEngine != null && (options.DebugEngine.Level & LuaDebugLevel.RegisterMethods) == LuaDebugLevel.RegisterMethods;
                if (lRegisterMethods)
                {
                    BeginCompile();
                }
                try
                {
                    LambdaExpression expr = Parser.ParseChunk(this, options, true, l, null, typeof(LuaResult), args);

                    if (printExpressionTree != null)
                    {
                        printExpressionTree.WriteLine(Parser.ExpressionToString(expr));
                        printExpressionTree.WriteLine(new string('=', 79));
                    }

                    // compile the chunk
                    if (options.DebugEngine == null)
                    {
                        return(new LuaChunk(this, expr.Name, expr.Compile()));
                    }
                    else
                    {
                        return(options.DebugEngine.CreateChunk(this, expr));
                    }
                }
                finally
                {
                    if (lRegisterMethods)
                    {
                        EndCompile();
                    }
                }
            }
        }         // func CompileChunk
Esempio n. 14
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. 15
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. 16
0
 /// <summary>Create the lexer for the parser</summary>
 /// <param name="chunkName"></param>
 /// <param name="tr"></param>
 /// <returns></returns>
 public virtual ILuaLexer CreateLexer(string chunkName, TextReader tr)
 => LuaLexer.Create(chunkName, tr);
Esempio n. 17
0
        }         // func TokenTest

        private ILuaLexer CreateLuaLexer(string lines)
        => LuaLexer.Create("test.lua", new StringReader(lines));
Esempio n. 18
0
 /// <summary>Create a code delegate without executing it.</summary>
 /// <param name="fileName">File to parse.</param>
 /// <param name="options">Options for the compile process.</param>
 /// <param name="args">Arguments for the code block.</param>
 /// <returns>Compiled chunk.</returns>
 public LuaChunk CompileChunk(string fileName, LuaCompileOptions options, params KeyValuePair <string, Type>[] args)
 {
     using (var sr = new StreamReader(fileName))
         using (var lex = LuaLexer.Create(Path.GetFileName(fileName), sr, true))
             return(CompileChunkCore(lex, options, args));
 }         // func CompileChunk
Esempio n. 19
0
 private ILuaLexer CreateHtmlLexer(string lines)
 => LuaLexer.CreateHtml(new LuaCharLexer("test.lua", new StringReader(lines), LuaLexer.HtmlCharStreamLookAHead));