Esempio n. 1
0
        /// <summary>
        ///     Add a parameter definition node to this parameter list node.
        /// </summary>
        /// <param name="node">The parameter definition node to add.</param>
        /// <exception cref="ArgumentNullException">Thrown if the 'node' parameter is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">
        ///     If a parameter with the same name already exists.
        /// </exception>
        public void Add(LSLParameterNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (_parameters.ContainsKey(node.Name))
            {
                throw new ArgumentException(
                          "Parameter with the name \"{0}\" has already been added to the parameter list.");
            }

            node.Parent         = this;
            node.ParameterIndex = _parameters.Count;
            _parameters.Add(node.Name, node);
        }
Esempio n. 2
0
        /// <summary>
        ///     Builds a parameter list node directly from a parser context, checking for duplicates and reporting
        ///     duplicate parameter errors via a validator strategies object. <see cref="ILSLCodeValidatorStrategies" />.
        /// </summary>
        /// <param name="context">The context to build from</param>
        /// <param name="validatorStrategies">The validator strategies object to use for reporting errors or warnings</param>
        /// <param name="parameterListType">The parameter list type.</param>
        /// <returns>the created parameter list node</returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="context" /> or <paramref name="validatorStrategies" /> is
        ///     <c>null</c>.
        /// </exception>
        internal static LSLParameterListNode BuildFromParserContext(LSLParser.OptionalParameterListContext context,
                                                                    LSLParameterListType parameterListType, ILSLCodeValidatorStrategies validatorStrategies)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (validatorStrategies == null)
            {
                throw new ArgumentNullException("validatorStrategies");
            }


            var result = new LSLParameterListNode(context, parameterListType);

            var parameterList = context.parameterList();

            if (parameterList == null)
            {
                return(result);
            }

            var parameterNames = new HashSet <string>();

            var parameterIndex = 0;

            foreach (var comma in parameterList.children)
            {
                //'comma' for some reason will be an internal object
                //that cannot be accessed via cast when a COMMA token is encountered.
                //
                //However, Payload contains a CommonToken instance, which implements IToken.
                var token = comma.Payload as IToken;

                //when a parameter def is found, 'comma' will be the grammar defined
                //LSLParser.ParameterDefinitionContext type.
                var parameter = comma as LSLParser.ParameterDefinitionContext;

                if (token != null)
                {
                    result._sourceRangeCommaList.Add(new LSLSourceCodeRange(token));
                }
                else if (parameter != null)
                {
                    if (parameterNames.Contains(parameter.ID().GetText()))
                    {
                        var paramLocation = new LSLSourceCodeRange(parameter);

                        validatorStrategies.SyntaxErrorListener.ParameterNameRedefined(
                            paramLocation,
                            parameterListType,
                            LSLTypeTools.FromLSLTypeName(parameter.TYPE().GetText()),
                            parameter.ID().GetText());

                        result.HasErrors = true;

                        result._parameters.Clear();

                        return(result);
                    }


                    parameterNames.Add(parameter.ID().GetText());

                    var addition = new LSLParameterNode(parameter)
                    {
                        ParameterIndex = parameterIndex
                    };

                    result.Add(addition);

                    parameterIndex++;
                }
            }

            parameterNames.Clear();

            return(result);
        }