Пример #1
0
        public static LuaSymbol GetSymbolForText(string text, SymbolType type)
        {
            switch (type)
            {
            case SymbolType.Symbol:
                return(GetLuaSymbolFor(text));

            case SymbolType.Keyword:
                return(GetLuaKeywordFor(text));

            case SymbolType.None:
                LuaSymbol ls = GetLuaSymbolFor(text);
                if (ls != null)
                {
                    return(ls);
                }
                ls = GetLuaKeywordFor(text);
                if (ls != null)
                {
                    return(ls);
                }
                break;
            }

            return(null);
        }
Пример #2
0
        private void btnParse_Click(object sender, EventArgs e)
        {
            LuaTable lt = new LuaTable("");
            // first check to see if a name is being provided
            int i = tbLogLine.Text.IndexOf('{');

            if (i > 0)
            {
                string name = tbLogLine.Text.Substring(0, i).Trim();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    if (name.EndsWith("="))
                    {
                        lt.Text = name.Substring(0, name.Length - 1).Trim();
                    }
                    else
                    {
                        lt.Text = name;
                    }
                }
            }
            else if (i < 0)
            {
                Close();
                return;
            }

            string toprocess = tbLogLine.Text.Substring(i + 1, tbLogLine.Text.LastIndexOf('}') - i - 1);

            i = 0;
            while (i > -1)
            {
                if (i == 0)
                {
                    i = -1;
                }
                int    se      = toprocess.IndexOf(',', i + 1);
                string segment = se > -1 ? toprocess.Substring(i + 1, se - i - 1) : toprocess.Substring(i + 1);

                // do stuff
                int    eq   = segment.IndexOf('=');
                string name = segment.Substring(0, eq);
                string val  = segment.Substring(eq + 1);
                if (val.StartsWith("function:"))
                {
                    lt.Functions.Add(new LuaFunction(name));
                }
                else
                {
                    lt.Variables.Add(new LuaSymbol(name, SymbolType.Variable));
                }

                i = se;
            }

            ParsedSymbol = lt;
            Close();
        }
Пример #3
0
        static LuaSymbol ProcessWord(string w)
        {
            // this is where we would sort out exactly what word is, i.e. keyword, global table, table, variable, function, whatever
            LuaSymbol ls = Lua.GetLuaKeywordFor(w);

            if (ls == null)
            {
                ls = new LuaSymbol(w, SymbolType.Variable);
            }

            return(ls);
        }
Пример #4
0
        public static LuaScript FromSourceLines(string[] lines)
        {
            LuaScript script = new LuaScript();

            LuaSymbol currentsymbol = null;
            bool      literalstring = false;
            bool      commenting    = false;

            foreach (string line in lines)
            {
                List <LuaSymbol> symline = new List <LuaSymbol>();
                string           cur     = "";
                for (int i = 0; i < line.Length; i++)
                {
                    char c = line[i];

                    if (literalstring || commenting)
                    {
                        int j = line.IndexOf("]]", i);
                        if (j > -1)
                        {
                            symline.Add(new LuaSymbol(line.Substring(i, j - i), literalstring ? SymbolType.LiteralStringText : SymbolType.CommentText));
                            symline.Add(Lua.GetLuaSymbolFor("]]"));
                            i             = j;
                            literalstring = commenting = false;
                            continue;
                        }
                        else
                        {
                            symline.Add(new LuaSymbol(line, literalstring ? SymbolType.LiteralStringText : SymbolType.CommentText));
                            break;
                        }
                    }
                    else if (currentsymbol != null)
                    {
                        cur += c;
                        LuaSymbol ls = Lua.GetLuaSymbolFor(cur);
                        if (ls != null)
                        {
                            currentsymbol = ls;
                            continue;
                        }

                        if (currentsymbol.Type == SymbolType.CommentSingle)
                        {
                            symline.Add(new LuaSymbol(currentsymbol.Text, SymbolType.CommentSingle));
                            symline.Add(new LuaSymbol(line.Substring(i), SymbolType.CommentText));
                            currentsymbol = null;
                            break;
                        }
                        else if (currentsymbol.Type == SymbolType.CommentBlockStart)
                        {
                            symline.Add(currentsymbol);
                            currentsymbol = null;
                            int j = line.IndexOf("]]", i);
                            if (j > -1)
                            {
                                symline.Add(new LuaSymbol(line.Substring(i, j - i), SymbolType.CommentText));
                                symline.Add(Lua.GetLuaSymbolFor("]]"));
                                i   = j + 1;
                                cur = "";
                                continue;
                            }
                            else
                            {
                                symline.Add(new LuaSymbol(line.Substring(i), SymbolType.CommentText));
                                commenting = true;
                                break;
                            }
                        }
                        else if (currentsymbol.Type == SymbolType.LiteralString)
                        {
                            symline.Add(currentsymbol);
                            if (currentsymbol.Text == "[[")
                            {
                                currentsymbol = null;
                                cur           = "";
                                int j = line.IndexOf("]]", i);
                                if (j > -1)
                                {
                                    symline.Add(new LuaSymbol(line.Substring(i, j - i), SymbolType.LiteralStringText));
                                    symline.Add(Lua.GetLuaSymbolFor("]]"));
                                    i = j + 1;
                                    continue;
                                }
                                else
                                {
                                    symline.Add(new LuaSymbol(line.Substring(i), SymbolType.LiteralStringText));
                                    literalstring = true;
                                    break;
                                }
                            }
                            else
                            {
                                int j = line.IndexOf(currentsymbol.Text, i);
                                symline.Add(new LuaSymbol(j > -1 ? line.Substring(i, j - i) : line.Substring(i), SymbolType.LiteralStringText));
                                symline.Add(currentsymbol);
                                currentsymbol = null;
                                cur           = "";
                                if (i < j)
                                {
                                    i = j;
                                    continue;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        else if (currentsymbol.Type == SymbolType.Symbol)
                        {
                            symline.Add(currentsymbol);
                            currentsymbol = null;
                            cur           = "";
                            i--;
                            continue;
                        }
                    }
                    else if (Lua.IsNumber(c))
                    {
                        int j = i + 1;
                        if ((j < line.Length) && (line[j] == 'x'))
                        {
                            j++;
                        }
                        for (; j < line.Length; j++)
                        {
                            if (!Lua.IsNumber(line[j]) && (line[j] != '.'))
                            {
                                symline.Add(new LuaSymbol(line.Substring(i, j - i), SymbolType.LiteralNumber));
                                i = j - 1;
                                break;
                            }
                        }
                        if (i == j - 1)
                        {
                            continue;
                        }
                        symline.Add(new LuaSymbol(line.Substring(i), SymbolType.LiteralNumber));
                        break;
                    }
                    else if (Lua.IsSpacer(c))
                    {
                        int j = i + 1;
                        for (; j < line.Length; j++)
                        {
                            if (!Lua.IsSpacer(line[j]))
                            {
                                symline.Add(new LuaSymbol(line.Substring(i, j - i), SymbolType.None));
                                i = j - 1;
                                break;
                            }
                        }
                        if (i == j - 1)
                        {
                            continue;
                        }
                        symline.Add(new LuaSymbol(line.Substring(i), SymbolType.None));
                        continue;
                    }
                    else
                    {
                        cur           = c.ToString();
                        currentsymbol = Lua.GetLuaSymbolFor(cur);
                        if (currentsymbol != null)
                        {
                            continue;
                        }
                        bool done = false;
                        for (int j = i + 1; j < line.Length; j++)
                        {
                            if (!Lua.IsSpacer(line[j]) && !Lua.IsNumber(line[j]))
                            {
                                currentsymbol = Lua.GetLuaSymbolFor(line[j].ToString());
                                if (currentsymbol == null)
                                {
                                    continue;
                                }
                                else
                                {
                                    cur = line[j].ToString();
                                }
                            }

                            symline.Add(ProcessWord(line.Substring(i, j - i)));
                            if (currentsymbol != null)
                            {
                                i = j;
                            }
                            else
                            {
                                i = j - 1;
                            }
                            done = true;
                            break;
                        }

                        if (!done)
                        {
                            symline.Add(ProcessWord(line.Substring(i)));
                            break;
                        }
                    }
                }

                if (currentsymbol != null)
                {
                    symline.Add(currentsymbol);
                    currentsymbol = null;
                }

                script.Symbols.Add(symline);
            }

            return(script);
        }
 public TextSymbol(LuaSymbol ls)
 {
     Symbol = ls;
 }