//////////////////////////////////////////////////////////////////////////
        private bool ReadInclude(String IncPath)
        {
            try
            {
                using (StreamReader sr = new StreamReader(IncPath))
                {
                    ScriptTokenizer Tokenizer = new ScriptTokenizer(sr, ScanIncludes);
                    Tokenizer.Filename = IncPath;

                    Tokenizer.GetTokens();

                    //Tokens.RemoveAt(Tokens.Count - 1);
                    //Tokens.RemoveAt(Tokens.Count - 1);

                    foreach (ScriptToken Token in Tokenizer.Tokens)
                    {
                        Token.StartLine = CurrentLine;
                        Token.EndLine = CurrentLine;
                        Token.StartCol = 0;
                        Token.EndCol = 0;
                        Tokens.Add(Token);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        //////////////////////////////////////////////////////////////////////////
        private bool Parse()
        {
            // cleanup
            GlobalScope = new ScriptScope();
            Scopes = new List<ScriptScope>();
            Functions = new List<ScriptFunction>();
            EventHandlers = new List<ScriptEventHandler>();

            _Tokenizer = new ScriptTokenizer(Reader);
            if (!_Tokenizer.GetTokens()) return false;

            ScriptScope CurrentScope = GlobalScope;

            bool ReadingBlock = false;
            int BlockNest = 0;

            List<ScriptToken>.Enumerator TokenEnum = _Tokenizer.Tokens.GetEnumerator();
            while(TokenEnum.MoveNext())
            {
                ScriptToken Token = TokenEnum.Current;
                if (Token.IsComment) continue;

                // functions
                if (Token.IsKeyword && (Token.Value == "function" || Token.Value == "method" || Token.Value == "external"))
                {
                    ScriptScope Scope = ParseFunction(ref TokenEnum);
                    if(Scope != null)
                    {
                        if(ReadingBlock)
                        {
                            CurrentScope.EndLine = Token.StartLine;
                            CurrentScope.EndCol = Token.StartCol;
                        }

                        CurrentScope = Scope;
                        Scopes.Add(Scope);
                        ReadingBlock = true;
                        BlockNest = 0;
                    }
                }

                // event handles
                else if(Token.IsKeyword && Token.Value=="on")
                {
                    ScriptScope Scope = ParseEventHandler(ref TokenEnum);
                    if (Scope != null)
                    {
                        if (ReadingBlock)
                        {
                            CurrentScope.EndLine = Token.StartLine;
                            CurrentScope.EndCol = Token.StartCol;
                        }

                        CurrentScope = Scope;
                        Scopes.Add(Scope);
                        ReadingBlock = true;
                        BlockNest = 0;
                    }
                }

                // variables
                else if(Token.IsKeyword && (Token.Value=="var" || Token.Value=="global" || Token.Value=="const"))
                {
                    ScriptVariable Var = ParseVariable(ref TokenEnum, false);
                    if(Var!=null && Var.Name!="")
                    {
                        if(ReadingBlock && BlockNest>0)	CurrentScope.Variables.Add(Var);
                        else GlobalScope.Variables.Add(Var);
                    }
                }

                // curly braces
                else if(Token.IsOperator)
                {
                    if (Token.Value == "{" && ReadingBlock) BlockNest++;
                    else if(Token.Value=="}" && ReadingBlock)
                    {
                        BlockNest--;
                        if (BlockNest <= 0)
                        {
                            ReadingBlock = false;
                            CurrentScope.EndLine = Token.EndLine;
                            CurrentScope.EndCol = Token.EndCol;
                        }
                    }
                    else if(Token.Value==";" && ReadingBlock && BlockNest==0)
                    {
                        ReadingBlock = false;
                        CurrentScope.EndLine = Token.EndLine;
                        CurrentScope.EndCol = Token.EndCol;
                    }

                }
            }

            return true;
        }
        //////////////////////////////////////////////////////////////////////////
        private void ScanScript(string ScriptFile)
        {
            try
            {
                FileStream fs = new FileStream(ScriptFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                ScriptTokenizer Tokenizer = new ScriptTokenizer(fs, false);
                Tokenizer.GetTokens();

                for (int i = 0; i < Tokenizer.Tokens.Count; i++)
                {
                    ScriptToken PrevToken = i > 0 ? Tokenizer.Tokens[i - 1] : null;
                    ScriptToken Token = Tokenizer.Tokens[i];

                    // some quick checks
                    if (!Token.IsString) continue;
                    if (PrevToken != null)
                    {
                        if ((PrevToken.IsKeyword && PrevToken.Value == "on") ||
                            (PrevToken.IsKeyword && PrevToken.Value == "extern") ||
                            (PrevToken.IsIdentifier && PrevToken.Value == "#include"))
                        {
                            continue;
                        }
                    }

                    string Str = Token.Value.Substring(1, Token.Value.Length - 2);
                    if (Str == string.Empty) continue;

                    Strings.Add(new StringLocation(Str, ScriptFile, Token.StartLine, StringLocation.StringType.ScriptFile));
                }
            }
            catch
            {
                AddLog(LogSeverity.Error, "Error parsing script '" + ScriptFile + "'");
            }
        }
Пример #4
0
        //////////////////////////////////////////////////////////////////////////
        private bool Parse()
        {
            // cleanup
            GlobalScope   = new ScriptScope();
            Scopes        = new List <ScriptScope>();
            Functions     = new List <ScriptFunction>();
            EventHandlers = new List <ScriptEventHandler>();

            _Tokenizer = new ScriptTokenizer(Reader);
            if (!_Tokenizer.GetTokens())
            {
                return(false);
            }

            ScriptScope CurrentScope = GlobalScope;

            bool ReadingBlock = false;
            int  BlockNest    = 0;

            List <ScriptToken> .Enumerator TokenEnum = _Tokenizer.Tokens.GetEnumerator();
            while (TokenEnum.MoveNext())
            {
                ScriptToken Token = TokenEnum.Current;
                if (Token.IsComment)
                {
                    continue;
                }

                // functions
                if (Token.IsKeyword && (Token.Value == "function" || Token.Value == "method" || Token.Value == "external"))
                {
                    ScriptScope Scope = ParseFunction(ref TokenEnum);
                    if (Scope != null)
                    {
                        if (ReadingBlock)
                        {
                            CurrentScope.EndLine = Token.StartLine;
                            CurrentScope.EndCol  = Token.StartCol;
                        }

                        CurrentScope = Scope;
                        Scopes.Add(Scope);
                        ReadingBlock = true;
                        BlockNest    = 0;
                    }
                }

                // event handles
                else if (Token.IsKeyword && Token.Value == "on")
                {
                    ScriptScope Scope = ParseEventHandler(ref TokenEnum);
                    if (Scope != null)
                    {
                        if (ReadingBlock)
                        {
                            CurrentScope.EndLine = Token.StartLine;
                            CurrentScope.EndCol  = Token.StartCol;
                        }

                        CurrentScope = Scope;
                        Scopes.Add(Scope);
                        ReadingBlock = true;
                        BlockNest    = 0;
                    }
                }

                // variables
                else if (Token.IsKeyword && (Token.Value == "var" || Token.Value == "global" || Token.Value == "const"))
                {
                    ScriptVariable Var = ParseVariable(ref TokenEnum, false);
                    if (Var != null && Var.Name != "")
                    {
                        if (ReadingBlock && BlockNest > 0)
                        {
                            CurrentScope.Variables.Add(Var);
                        }
                        else
                        {
                            GlobalScope.Variables.Add(Var);
                        }
                    }
                }

                // curly braces
                else if (Token.IsOperator)
                {
                    if (Token.Value == "{" && ReadingBlock)
                    {
                        BlockNest++;
                    }
                    else if (Token.Value == "}" && ReadingBlock)
                    {
                        BlockNest--;
                        if (BlockNest <= 0)
                        {
                            ReadingBlock         = false;
                            CurrentScope.EndLine = Token.EndLine;
                            CurrentScope.EndCol  = Token.EndCol;
                        }
                    }
                    else if (Token.Value == ";" && ReadingBlock && BlockNest == 0)
                    {
                        ReadingBlock         = false;
                        CurrentScope.EndLine = Token.EndLine;
                        CurrentScope.EndCol  = Token.EndCol;
                    }
                }
            }

            return(true);
        }