Пример #1
0
 public Function ParseFunction(string functionName, IEnumerable <string> parameterNames)
 {
     Contract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(functionName), "functionName");
     Contract.Requires <ArgumentNullException>(parameterNames != null, "parameterNames");
     // ReSharper disable once UseObjectOrCollectionInitializer
     _currentFunction = new ParsingFunction(null, functionName, 1, ToVariableCollection(parameterNames), false);
     _currentFunction.FunctionBody = ParseFunctionBody();
     Contract.Assert(Lookahead.Type == TokenType.Unknown);
     return(_currentFunction.ToFunction());
 }
Пример #2
0
        public ParsingFunction(
            ParsingFunction outer,
            string name,
            int lineNo,
            IKeyedVariableCollection parameterNames,
            bool isDeclaration)
        {
            Contract.Requires(!(isDeclaration && string.IsNullOrEmpty(name)));
            Contract.Requires(parameterNames != null);

            Outer             = outer;
            _name             = name;
            _lineNo           = lineNo;
            _parameterNames   = parameterNames;
            _directives       = new HashSet <string>();
            DeclaredVariables = new KeyedVariableCollection();
            NestedFunctions   = new KeyedFunctionCollection();
            _isDeclaration    = isDeclaration;
        }
Пример #3
0
        private Function ParseFunction(bool isDeclaration)
        {
            Contract.Requires(_currentFunction != null);
            Contract.Ensures(_currentFunction != null);

            var startPosition = Lookahead.StartPosition;

            Match(TokenType.Function);

            string name = null;

            if (Lookahead.Type == TokenType.Ident)
            {
                name = Lookahead.Value;
                if (_currentFunction.Outer != null && _currentFunction.Outer.NestedFunctions.Contains(name))
                {
                    Errors.ThrowFunctionAlreadyDeclared(startPosition, name);
                }
                ReadNextToken();
            }

            var parameterNames = new KeyedVariableCollection();

            Match(TokenType.LParenthesis);
            if (Lookahead.Type != TokenType.RParenthesis)
            {
                if (Lookahead.Type != TokenType.Ident)
                {
                    Errors.ThrowUnmatchedToken(TokenType.Ident, Lookahead);
                }
                parameterNames.Add(Lookahead.Value);
                ReadNextToken();
                while (Lookahead.Type == TokenType.Comma)
                {
                    ReadNextToken();
                    if (Lookahead.Type != TokenType.Ident)
                    {
                        Errors.ThrowUnmatchedToken(TokenType.Ident, Lookahead);
                    }
                    var parameterName = Lookahead.Value;
                    if (parameterNames.Contains(parameterName))
                    {
                        Errors.ThrowParameterAlreadyDeclared(Lookahead.StartPosition, parameterName);
                    }
                    parameterNames.Add(parameterName);
                    ReadNextToken();
                }
            }
            Match(TokenType.RParenthesis);

            _currentFunction = new ParsingFunction(_currentFunction, name, startPosition.LineNo, parameterNames, isDeclaration);
            Match(TokenType.LCurlyBrace);
            _currentFunction.FunctionBody = ParseFunctionBody();
            Match(TokenType.RCurlyBrace);

            var result = _currentFunction.ToFunction();

            _currentFunction = _currentFunction.Outer;
            _currentFunction.NestedFunctions.Add(result);
            return(result);
        }