public DefinedMethod(ParseInfo parseInfo, Scope scope, DeltinScriptParser.Define_methodContext context)
            : base(parseInfo, scope, context.name.Text, new Location(parseInfo.Script.Uri, DocRange.GetRange(context.name)))
        {
            this.context = context;

            // Check if recursion is enabled.
            IsRecursive = context.RECURSIVE() != null;

            // Get the type.
            ReturnType = CodeType.GetCodeTypeFromContext(parseInfo, context.code_type());

            // Get the access level.
            AccessLevel = context.accessor().GetAccessLevel();

            // Setup the parameters and parse the block.
            SetupParameters(context.setParameters());

            if (context.block() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected block.", DocRange.GetRange(context.name));
            }

            scope.AddMethod(this, parseInfo.Script.Diagnostics, DocRange.GetRange(context.name));

            // Add the hover info.
            parseInfo.Script.AddHover(DocRange.GetRange(context.name), GetLabel(true));
        }
        public ForeachAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.ForeachContext foreachContext)
        {
            Scope varScope = scope.Child();

            ForeachVar = new Var(foreachContext.name.Text, new Location(parseInfo.Script.Uri, DocRange.GetRange(foreachContext.name)), parseInfo);
            ForeachVar.VariableType = VariableType.ElementReference;
            ForeachVar.CodeType     = CodeType.GetCodeTypeFromContext(parseInfo, foreachContext.code_type());
            ForeachVar.Finalize(varScope);

            // Get the array that will be iterated on. Syntax error if it is missing.
            if (foreachContext.expr() != null)
            {
                Array = DeltinScript.GetExpression(parseInfo, scope, foreachContext.expr());
            }
            else
            {
                parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(foreachContext.IN()));
            }

            // Get the foreach block. Syntax error if it is missing.
            if (foreachContext.block() != null)
            {
                Block = new BlockAction(parseInfo, varScope, foreachContext.block());
                // Get the path info.
                Path = new PathInfo(Block, DocRange.GetRange(foreachContext.FOREACH()), false);
            }
            else
            {
                parseInfo.Script.Diagnostics.Error("Expected block.", DocRange.GetRange(foreachContext.RIGHT_PAREN()));
            }
        }
        public TypeConvertAction(ParseInfo parseInfo, Scope scope, TypeCast typeConvert)
        {
            // Get the expression. Syntax error if there is none.
            Expression = parseInfo.GetExpression(scope, typeConvert.Expression);

            // Get the type. Syntax error if there is none.
            ConvertingTo = CodeType.GetCodeTypeFromContext(parseInfo, typeConvert.Type);
        }
Exemple #4
0
        public MacroVar GetMacro(Scope objectScope, Scope staticScope, MacroVarDeclaration macroContext)
        {
            // Get the return type.
            CodeType returnType = CodeType.GetCodeTypeFromContext(this, macroContext.Type);

            MacroVar newMacro = new MacroVar(this, objectScope, staticScope, macroContext, returnType);

            TranslateInfo.ApplyBlock((IApplyBlock)newMacro);
            return(newMacro);
        }
Exemple #5
0
        /// <summary>Creates a macro from a Define_macroContext.</summary>
        /// <param name="objectScope">The scope of the macro if there is no static attribute.</param>
        /// <param name="staticScope">The scope of the macro if there is a static attribute.</param>
        /// <param name="macroContext">The context of the macro.</param>
        /// <returns>A DefinedMacro if the macro has parameters, a MacroVar if there are no parameters.</returns>
        public DefinedMacro GetMacro(Scope objectScope, Scope staticScope, MacroFunctionContext macroContext)
        {
            // Get the return type.
            CodeType returnType = CodeType.GetCodeTypeFromContext(this, macroContext.Type);

            DefinedMacro newMacro = new DefinedMacro(this, objectScope, staticScope, macroContext, returnType);

            TranslateInfo.ApplyBlock((IApplyBlock)newMacro);
            return(newMacro);
        }
        protected override void GetCodeType()
        {
            var      context = _contextHandler.GetCodeType();
            CodeType type    = CodeType.GetCodeTypeFromContext(_parseInfo, context);

            if (type != null && type.Constant() == TypeSettable.Constant)
            {
                _diagnostics.Error($"Constant types cannot be used in subroutine parameters.", DocRange.GetRange(context));
            }

            _varInfo.Type = type;
        }
Exemple #7
0
        protected virtual void GetCodeType()
        {
            if (_contextHandler.GetCodeType() == null)
            {
                return;
            }

            // Get the type.
            CodeType type = CodeType.GetCodeTypeFromContext(_parseInfo, _contextHandler.GetCodeType());

            ApplyCodeType(type);
        }
Exemple #8
0
        protected virtual void GetCodeType()
        {
            // Get the type.
            CodeType type = CodeType.GetCodeTypeFromContext(_parseInfo, _contextHandler.GetCodeType());

            if (type != null && type.Constant() != TypeSettable.Normal)
            {
                _varInfo.IsWorkshopReference = true;
            }

            _varInfo.Type = type;
        }
Exemple #9
0
        protected virtual void GetCodeType()
        {
            if (_contextHandler.GetCodeType() == null)
            {
                return;
            }

            // Get the type.
            CodeType type = CodeType.GetCodeTypeFromContext(_parseInfo, _contextHandler.GetCodeType());

            if (type != null && type.IsConstant())
            {
                _varInfo.IsWorkshopReference = true;
            }

            _varInfo.Type = type;
        }
        public static IScopeable GetMacro(ParseInfo parseInfo, Scope objectScope, Scope staticScope, DeltinScriptParser.Define_macroContext macroContext)
        {
            // If the ; is missing, syntax error.
            if (macroContext.STATEMENT_END() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected ;", DocRange.GetRange((object)macroContext.TERNARY_ELSE() ?? (object)macroContext.name ?? (object)macroContext).end.ToRange());
            }

            // If the : is missing, syntax error.
            if (macroContext.TERNARY_ELSE() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected :", DocRange.GetRange(macroContext).end.ToRange());
            }
            else
            {
                // Get the expression that will be parsed.
                if (macroContext.expr() == null)
                {
                    parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(macroContext.TERNARY_ELSE()));
                }
            }

            // Get the return type.
            CodeType returnType = CodeType.GetCodeTypeFromContext(parseInfo, macroContext.code_type());

            IScopeable newMacro;

            if (macroContext.LEFT_PAREN() != null || macroContext.RIGHT_PAREN() != null)
            {
                newMacro = new DefinedMacro(parseInfo, objectScope, staticScope, macroContext, returnType);
            }
            else
            {
                newMacro = new MacroVar(parseInfo, objectScope, staticScope, macroContext, returnType);
            }

            parseInfo.TranslateInfo.ApplyBlock((IApplyBlock)newMacro);
            return(newMacro);
        }
        public TypeConvertAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.TypeconvertContext typeConvert)
        {
            // Get the expression. Syntax error if there is none.
            if (typeConvert.expr() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(typeConvert.GREATER_THAN()));
            }
            else
            {
                Expression = DeltinScript.GetExpression(parseInfo, scope, typeConvert.expr());
            }

            // Get the type. Syntax error if there is none.
            if (typeConvert.code_type() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected type name.", DocRange.GetRange(typeConvert.LESS_THAN()));
            }
            else
            {
                ConvertingTo = CodeType.GetCodeTypeFromContext(parseInfo, typeConvert.code_type());
            }
        }
Exemple #12
0
        public DefinedMethod(ParseInfo parseInfo, Scope objectScope, Scope staticScope, DeltinScriptParser.Define_methodContext context, CodeType containingType)
            : base(parseInfo, context.name.Text, new Location(parseInfo.Script.Uri, DocRange.GetRange(context.name)))
        {
            this.context = context;
            Attributes.ContainingType = containingType;

            DocRange nameRange = DocRange.GetRange(context.name);

            // Get the attributes.
            GetAttributes();

            SetupScope(Static ? staticScope : objectScope);
            methodScope.MethodContainer = true;
            BlockScope = methodScope.Child();

            // Get the type.
            if (context.VOID() == null)
            {
                doesReturnValue = true;
                ReturnType      = CodeType.GetCodeTypeFromContext(parseInfo, context.code_type());
            }

            // Setup the parameters and parse the block.
            if (!IsSubroutine)
            {
                SetupParameters(context.setParameters(), false);
            }
            else
            {
                subroutineDefaultGlobal = context.PLAYER() == null;
                Attributes.Parallelable = true;
                parseInfo.TranslateInfo.AddSubroutine(this);

                // Subroutines should not have parameters.
                SetupParameters(context.setParameters(), true);
            }

            // Override attribute.
            if (Attributes.Override)
            {
                IMethod overriding = objectScope.GetMethodOverload(this);

                // No method with the name and parameters found.
                if (overriding == null)
                {
                    parseInfo.Script.Diagnostics.Error("Could not find a method to override.", nameRange);
                }
                else if (!overriding.Attributes.IsOverrideable)
                {
                    parseInfo.Script.Diagnostics.Error("The specified method is not marked as virtual.", nameRange);
                }
                else
                {
                    overriding.Attributes.AddOverride(this);
                }

                if (overriding != null && overriding.DefinedAt != null)
                {
                    // Make the override keyword go to the base method.
                    parseInfo.Script.AddDefinitionLink(
                        attributes.First(at => at.Type == MethodAttributeType.Override).Range,
                        overriding.DefinedAt
                        );

                    if (!Attributes.Recursive)
                    {
                        Attributes.Recursive = overriding.Attributes.Recursive;
                    }
                }
            }

            if (Attributes.IsOverrideable && AccessLevel == AccessLevel.Private)
            {
                parseInfo.Script.Diagnostics.Error("A method marked as virtual or abstract must have the protection level 'public' or 'protected'.", nameRange);
            }

            // Syntax error if the block is missing.
            if (context.block() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected block.", nameRange);
            }

            // Add to the scope. Check for conflicts if the method is not overriding.
            containingScope.AddMethod(this, parseInfo.Script.Diagnostics, nameRange, !Attributes.Override);

            // Add the hover info.
            parseInfo.Script.AddHover(DocRange.GetRange(context.name), GetLabel(true));

            if (Attributes.IsOverrideable)
            {
                parseInfo.Script.AddCodeLensRange(new ImplementsCodeLensRange(this, parseInfo.Script, CodeLensSourceType.Function, nameRange));
            }

            parseInfo.TranslateInfo.ApplyBlock(this);
        }
Exemple #13
0
        public static Var CreateVarFromContext(VariableDefineType defineType, ParseInfo parseInfo, DeltinScriptParser.DefineContext context)
        {
            Var newVar;

            if (context.name != null)
            {
                newVar = new Var(context.name.Text, new Location(parseInfo.Script.Uri, DocRange.GetRange(context.name)), parseInfo);
                parseInfo.TranslateInfo.AddSymbolLink(newVar, new Location(parseInfo.Script.Uri, DocRange.GetRange(context.name)));
            }
            else
            {
                newVar = new Var(null, new Location(parseInfo.Script.Uri, DocRange.GetRange(context)), parseInfo);
            }

            newVar.context = context;
            newVar.InExtendedCollection = context.NOT() != null;
            newVar.DefineType           = defineType;

            // Check if global/player.
            if (defineType == VariableDefineType.RuleLevel)
            {
                if (context.GLOBAL() != null)
                {
                    newVar.VariableType = VariableType.Global;
                }
                else if (context.PLAYER() != null)
                {
                    newVar.VariableType = VariableType.Player;
                }
                else
                {
                    parseInfo.Script.Diagnostics.Error("Expected the globalvar/playervar attribute.", DocRange.GetRange(context));
                }
            }
            else
            {
                if (context.GLOBAL() != null)
                {
                    parseInfo.Script.Diagnostics.Error("The globalvar attribute is only allowed on variables defined at the rule level.", DocRange.GetRange(context.GLOBAL()));
                }
                if (context.PLAYER() != null)
                {
                    parseInfo.Script.Diagnostics.Error("The playervar attribute is only allowed on variables defined at the rule level.", DocRange.GetRange(context.PLAYER()));
                }
            }

            // Get the ID
            if (context.id != null)
            {
                if (defineType != VariableDefineType.RuleLevel)
                {
                    parseInfo.Script.Diagnostics.Error("Only defined variables at the rule level can be assigned an ID.", DocRange.GetRange(context.id));
                }
                else
                {
                    newVar.ID = int.Parse(context.id.GetText());
                    parseInfo.TranslateInfo.VarCollection.Reserve(newVar.ID, newVar.VariableType == VariableType.Global, parseInfo.Script.Diagnostics, DocRange.GetRange(context.id));
                }
            }

            if (defineType == VariableDefineType.InClass)
            {
                // Get the accessor
                newVar.AccessLevel = AccessLevel.Private;
                if (context.accessor() != null)
                {
                    newVar.AccessLevel = context.accessor().GetAccessLevel();
                }
                // Get the static attribute.
                newVar.Static = context.STATIC() != null;

                // Syntax error if the variable has '!'.
                if (!newVar.Static && newVar.InExtendedCollection)
                {
                    parseInfo.Script.Diagnostics.Error("Non-static type variables can not be placed in the extended collection.", DocRange.GetRange(context.NOT()));
                }
            }
            else
            {
                // Syntax error if class only attributes is used somewhere else.
                if (context.accessor() != null)
                {
                    parseInfo.Script.Diagnostics.Error("Only defined variables in classes can have an accessor.", DocRange.GetRange(context.accessor()));
                }
                if (context.STATIC() != null)
                {
                    parseInfo.Script.Diagnostics.Error("Only defined variables in classes can be static.", DocRange.GetRange(context.STATIC()));
                }
            }

            // If the type is InClass or RuleLevel, set WholeContext to true.
            // WholeContext's value for parameters don't matter since parameters are defined at the start anyway.
            newVar.WholeContext = defineType == VariableDefineType.InClass || defineType == VariableDefineType.RuleLevel;

            // Get the type.
            newVar.CodeType = CodeType.GetCodeTypeFromContext(parseInfo, context.code_type());

            if (newVar.CodeType != null && newVar.CodeType.Constant() != TypeSettable.Normal)
            {
                newVar.VariableType = VariableType.ElementReference;
            }

            // Syntax error if there is an '=' but no expression.
            if (context.EQUALS() != null && context.expr() == null)
            {
                parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(context).end.ToRange());
            }

            return(newVar);
        }
        public DefinedMethod(ParseInfo parseInfo, Scope objectScope, Scope staticScope, FunctionContext context, CodeType containingType)
            : base(parseInfo, context.Identifier.Text, new Location(parseInfo.Script.Uri, context.Identifier.Range))
        {
            this.Context = context;

            Attributes.ContainingType = containingType;

            DocRange nameRange = context.Identifier.Range;

            // Get the attributes.
            MethodAttributeAppender attributeResult = new MethodAttributeAppender(Attributes);
            MethodAttributesGetter  attributeGetter = new MethodAttributesGetter(context, attributeResult);

            attributeGetter.GetAttributes(parseInfo.Script.Diagnostics);

            // Copy attribute results
            Static         = attributeResult.Static;
            IsSubroutine   = attributeResult.IsSubroutine;
            SubroutineName = attributeResult.SubroutineName;
            AccessLevel    = attributeResult.AccessLevel;

            // Setup scope.
            SetupScope(Static ? staticScope : objectScope);
            methodScope.MethodContainer = true;

            // Get the type.
            if (!context.Type.IsVoid)
            {
                DoesReturnValue = true;
                CodeType        = CodeType.GetCodeTypeFromContext(parseInfo, context.Type);
            }

            // Setup the parameters and parse the block.
            if (!IsSubroutine)
            {
                SetupParameters(context.Parameters, false);
            }
            else
            {
                SubroutineDefaultGlobal = context.PlayerVar == null;
                Attributes.Parallelable = true;

                // Subroutines should not have parameters.
                SetupParameters(context.Parameters, true);
            }

            // Override attribute.
            if (Attributes.Override)
            {
                IMethod overriding = objectScope.GetMethodOverload(this);
                Attributes.Overriding = overriding;

                // No method with the name and parameters found.
                if (overriding == null)
                {
                    parseInfo.Script.Diagnostics.Error("Could not find a method to override.", nameRange);
                }
                else if (!overriding.Attributes.IsOverrideable)
                {
                    parseInfo.Script.Diagnostics.Error("The specified method is not marked as virtual.", nameRange);
                }
                else
                {
                    overriding.Attributes.AddOverride(this);
                }

                if (overriding != null && overriding.DefinedAt != null)
                {
                    // Make the override keyword go to the base method.
                    parseInfo.Script.AddDefinitionLink(
                        attributeGetter.ObtainedAttributes.First(at => at.Type == MethodAttributeType.Override).Range,
                        overriding.DefinedAt
                        );

                    if (!Attributes.Recursive)
                    {
                        Attributes.Recursive = overriding.Attributes.Recursive;
                    }
                }
            }

            if (Attributes.IsOverrideable && AccessLevel == AccessLevel.Private)
            {
                parseInfo.Script.Diagnostics.Error("A method marked as virtual or abstract must have the protection level 'public' or 'protected'.", nameRange);
            }

            // Add to the scope. Check for conflicts if the method is not overriding.
            containingScope.AddMethod(this, parseInfo.Script.Diagnostics, nameRange, !Attributes.Override);

            // Add the hover info.
            parseInfo.Script.AddHover(nameRange, GetLabel(true));

            if (Attributes.IsOverrideable)
            {
                parseInfo.Script.AddCodeLensRange(new ImplementsCodeLensRange(this, parseInfo.Script, CodeLensSourceType.Function, nameRange));
            }

            parseInfo.TranslateInfo.ApplyBlock(this);
        }