Exemple #1
0
        private static IKeyedVariableCollection ToVariableCollection(IEnumerable <string> parameterNames)
        {
            var result = new KeyedVariableCollection();

            foreach (var parameterName in parameterNames)
            {
                if (result.Contains(parameterName))
                {
                    Errors.ThrowParameterAlreadyDeclared(parameterName);
                }
                result.Add(parameterName);
            }
            return(result);
        }
Exemple #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;
        }
Exemple #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);
        }