Esempio n. 1
0
        public IEnumerable<Function> Parse(string source)
        {
            _funcs = new List<Function>();

            _parser = new TokenParser.Parser(source);
            _parser.ReturnWhiteSpace = false;
            _parser.ReturnComments = false;

            while (!_parser.EndOfFile)
            {
                var startPos = _parser.Position;
                var checkForFunctionDef = true;

                if (!_parser.Peek()) break;
                if (_parser.TokenType == TokenParser.TokenType.Word)
                {
                    switch (_parser.TokenText)
                    {
                        case "for":
                        case "if":
                        case "select":
                        case "switch":
                        case "while":
                            // Functions will never be defined inside the conditions for these control statements.
                            while (_parser.Read())
                            {
                                if (_parser.TokenType == TokenParser.TokenType.Operator &&
                                    (_parser.TokenText == "{" || _parser.TokenText == ";" || _parser.TokenText == ":"))
                                {
                                    break;
                                }
                            }
                            checkForFunctionDef = false;
                            break;
                    }
                }

                if (checkForFunctionDef)
                {
                    if (!ProcessDefine() && !ProcessFunction())
                    {
                        // Unable to find any recognizable pattern, so just skip the next token.
                        _parser.Read();
                    }
                }
            }

            // Expand the function line ranges to fill in the function bodies.
            Function lastFunc = null;
            foreach (var func in _funcs)
            {
                if (lastFunc != null)
                {
                    lastFunc.EndLine = func.StartLine > lastFunc.StartLine ? func.StartLine - 1 : lastFunc.StartLine;
                }
                lastFunc = func;
            }

            if (lastFunc != null) lastFunc.EndLine = source.Count(c => c == '\n') + 1;

            return _funcs;
        }