Exemplo n.º 1
0
        /// <summary>
        ///     Construct an <see cref="LSLEventHandlerNode" /> with the given parameter list and code body.
        /// </summary>
        /// <param name="name">The event handler name.</param>
        /// <param name="parameterList">The parameter list.</param>
        /// <param name="code">The code body.</param>
        /// <exception cref="ArgumentNullException">
        ///     if <paramref name="name" /> or <paramref name="parameterList" /> or
        ///     <paramref name="code" /> is <c>null</c>.
        /// </exception>
        /// <exception cref="LSLInvalidSymbolNameException"><paramref name="name" /> contained characters not allowed in an LSL ID token.</exception>
        public LSLEventHandlerNode(string name, LSLParameterListNode parameterList, LSLCodeScopeNode code)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (parameterList == null)
            {
                throw new ArgumentNullException("parameterList");
            }
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }

            if (!LSLTokenTools.IDRegexAnchored.IsMatch(name))
            {
                throw new LSLInvalidSymbolNameException("name provided contained characters not allowed in an LSL ID token.");
            }

            ParameterList        = parameterList;
            ParameterList.Parent = this;

            Name = name;

            Code               = code;
            Code.Parent        = this;
            Code.CodeScopeType = LSLCodeScopeType.EventHandler;
        }
Exemplo n.º 2
0
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="parameterList" /> or <paramref name="code" /> is
        ///     <c>null</c>.
        /// </exception>
        internal LSLEventHandlerNode(LSLParser.EventHandlerContext context, LSLParameterListNode parameterList,
                                     LSLCodeScopeNode code)
        {
            if (parameterList == null)
            {
                throw new ArgumentNullException("parameterList");
            }

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

            Name = context.handler_name.Text;

            Code               = code;
            Code.Parent        = this;
            Code.CodeScopeType = LSLCodeScopeType.EventHandler;

            ParameterList        = parameterList;
            ParameterList.Parent = this;


            SourceRange     = new LSLSourceCodeRange(context);
            SourceRangeName = new LSLSourceCodeRange(context.handler_name);

            SourceRangesAvailable = true;
        }
Exemplo n.º 3
0
        public override bool VisitFunctionDeclaration(LSLParser.FunctionDeclarationContext context)
        {
            if (context == null || Utility.AnyNull(context.function_name, context.code))
            {
                throw LSLCodeValidatorInternalException.VisitContextInvalidState("VisitFunctionDeclaration", true);
            }

            if (_validatorStrategies.LibraryDataProvider.LibraryFunctionExist(context.function_name.Text))
            {
                GenSyntaxError().RedefinedStandardLibraryFunction(
                    new LSLSourceCodeRange(context.function_name), context.function_name.Text,
                    _validatorStrategies.LibraryDataProvider.GetLibraryFunctionSignatures(
                        context.function_name.Text));

                return(false);
            }


            if (_scopingManager.FunctionIsPreDefined(context.function_name.Text))
            {
                GenSyntaxError().RedefinedFunction(
                    new LSLSourceCodeRange(context.function_name),
                    _scopingManager.ResolveFunctionPreDefine(context.function_name.Text));

                return(false);
            }


            var parameterListNode = LSLParameterListNode.BuildFromParserContext(
                context.parameters,
                LSLParameterListType.FunctionParameters,
                _validatorStrategies);


            if (parameterListNode.HasErrors)
            {
                return(false);
            }

            var returnType = LSLType.Void;

            if (context.return_type != null)
            {
                returnType =
                    LSLTypeTools.FromLSLTypeName(context.return_type.Text);
            }


            var func = new LSLPreDefinedFunctionSignature(returnType,
                                                          context.function_name.Text,
                                                          parameterListNode);

            _scopingManager.PreDefineFunction(func);


            base.VisitFunctionDeclaration(context);

            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Construct an <see cref="LSLPreDefinedFunctionSignature" /> from an <see cref="LSLType" /> representing the return
        ///     type, a function name and an <see cref="LSLParameterListNode" />
        ///     from an LSL Syntax tree.
        /// </summary>
        /// <param name="returnType">The return type of the function signature.</param>
        /// <param name="name">The name of the function.</param>
        /// <param name="parameters">
        ///     The <see cref="LSLParameterListNode" /> from an LSL syntax tree that represents the function
        ///     signatures parameters.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="parameters"/> is <see langword="null" />.</exception>
        /// <exception cref="LSLInvalidSymbolNameException">
        ///     Thrown if the function name does not follow LSL symbol naming conventions.
        /// </exception>
        public LSLPreDefinedFunctionSignature(LSLType returnType, string name, LSLParameterListNode parameters)
            : base(returnType, name)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            foreach (var param in parameters)
            {
                AddParameter(param.CreateSignature());
            }

            ParameterListNode = parameters;
        }
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="parameterList" /> or <paramref name="code" />
        ///     is <c>null</c>.
        /// </exception>
        internal LSLFunctionDeclarationNode(LSLParser.FunctionDeclarationContext context,
                                            LSLParameterListNode parameterList, LSLCodeScopeNode code)
        {
            if (parameterList == null)
            {
                throw new ArgumentNullException("parameterList");
            }

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

            if (context.return_type != null)
            {
                ReturnTypeName        = context.return_type.Text;
                ReturnType            = LSLTypeTools.FromLSLTypeName(ReturnTypeName);
                SourceRangeReturnType = new LSLSourceCodeRange(context.return_type);
            }
            else
            {
                ReturnTypeName = "";
                ReturnType     = LSLType.Void;
            }

            Name = context.function_name.Text;


            ParameterList        = parameterList;
            ParameterList.Parent = this;

            Code               = code;
            Code.Parent        = this;
            Code.CodeScopeType = LSLCodeScopeType.Function;

            SourceRange     = new LSLSourceCodeRange(context);
            SourceRangeName = new LSLSourceCodeRange(context.function_name);

            SourceRangesAvailable = true;
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Create an <see cref="LSLParameterListNode" /> by cloning from another.
        /// </summary>
        /// <param name="other">The other node to clone from.</param>
        /// <exception cref="ArgumentNullException"><paramref name="other" /> is <c>null</c>.</exception>
        private LSLParameterListNode(LSLParameterListNode other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }


            SourceRangesAvailable = other.SourceRangesAvailable;

            if (SourceRangesAvailable)
            {
                SourceRange           = other.SourceRange;
                _sourceRangeCommaList = other.SourceRangeCommaList.ToGenericArray();
            }

            foreach (var param in other._parameters.Values)
            {
                Add(param.Clone());
            }

            HasErrors = other.HasErrors;
        }
        /// <summary>
        ///     Construct an <see cref="LSLFunctionDeclarationNode" /> with the given return type, parameter list, and code body.
        /// </summary>
        /// <param name="returnType">The return type of the function.</param>
        /// <param name="functionName">The name of the function.</param>
        /// <param name="parameterList">The parameter list node representing the list of parameter definitions for the function.</param>
        /// <param name="code">The code scope that makes up the function's code body.</param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="functionName" /> or <paramref name="parameterList" /> or <paramref name="code" /> is <c>null</c>.
        /// </exception>
        /// <exception cref="LSLInvalidSymbolNameException">
        ///     if <paramref name="functionName" /> contains invalid characters for an LSL ID
        ///     token.
        /// </exception>
        public LSLFunctionDeclarationNode(LSLType returnType, string functionName,
                                          LSLParameterListNode parameterList, LSLCodeScopeNode code)
        {
            if (parameterList == null)
            {
                throw new ArgumentNullException("parameterList");
            }
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }
            if (functionName == null)
            {
                throw new ArgumentNullException("functionName");
            }

            if (!LSLTokenTools.IDRegexAnchored.IsMatch(functionName))
            {
                throw new LSLInvalidSymbolNameException(
                          "functionName provided contained characters not allowed in an LSL ID token.");
            }

            Name = functionName;


            ReturnTypeName = returnType == LSLType.Void ? "" : LSLTypeTools.ToLSLTypeName(returnType);
            ReturnType     = returnType;


            ParameterList        = parameterList;
            ParameterList.Parent = this;

            Code               = code;
            Code.Parent        = this;
            Code.CodeScopeType = LSLCodeScopeType.Function;
        }
Exemplo n.º 8
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);
        }
 /// <summary>
 ///     Construct an  <see cref="LSLParsedEventHandlerSignature" /> from an event handler name and a
 ///     <see cref="LSLParameterListNode" /> from
 ///     an LSL Syntax tree.
 /// </summary>
 /// <param name="name">The name of the event handler.</param>
 /// <param name="parameters">
 ///     The <see cref="LSLParameterListNode" /> from the syntax tree that represents the event
 ///     handlers parsed parameters.
 /// </param>
 public LSLParsedEventHandlerSignature(string name, LSLParameterListNode parameters) :
     base(name, parameters.Parameters.Select(x => new LSLParameterSignature(x.Type, x.Name, false)))
 {
     //TODO validate parameters
     ParameterListNode = parameters;
 }