示例#1
0
        private void ValidateAttribute(FileDiagnostics diagnostics, MethodAttributeContext newAttribute)
        {
            // The attribute is not allowed.
            if (DisallowAttributes().Contains(newAttribute.Type))
            {
                diagnostics.Error("The '" + newAttribute.Type.ToString().ToLower() + "' attribute is not allowed.", newAttribute.Range);
            }
            else
            {
                // Virtual attribute on a static method (static attribute was first.)
                if (ResultAppender.IsStatic() && newAttribute.Type == MethodAttributeType.Virtual)
                {
                    diagnostics.Error("Static methods cannot be virtual.", newAttribute.Range);
                }

                // Static attribute on a virtual method (virtual attribute was first.)
                if (ResultAppender.IsVirtual() && newAttribute.Type == MethodAttributeType.Static)
                {
                    diagnostics.Error("Virtual methods cannot be static.", newAttribute.Range);
                }
            }
        }
        protected void Inherit(CodeType extend, FileDiagnostics diagnostics, DocRange range)
        {
            if (extend == null)
            {
                throw new ArgumentNullException(nameof(extend));
            }

            string errorMessage = null;

            if (!extend.CanBeExtended)
            {
                errorMessage = "Type '" + extend.Name + "' cannot be inherited.";
            }

            else if (extend == this)
            {
                errorMessage = "Cannot extend self.";
            }

            else if (extend.Implements(this))
            {
                errorMessage = $"The class {extend.Name} extends this class.";
            }

            if (errorMessage != null)
            {
                if (diagnostics == null || range == null)
                {
                    throw new Exception(errorMessage);
                }
                else
                {
                    diagnostics.Error(errorMessage, range);
                    return;
                }
            }

            Extends = extend;
        }
        public bool Apply(FileDiagnostics diagnostics, AttributeContext attribute)
        {
            if (_disallowedAttributes.Contains(attribute.Type))
            {
                diagnostics.Error("The '" + attribute.Type.ToString().ToLower() + "' attribute is not allowed.", attribute.Range);
                return(false);
            }

            // Apply the attribute.
            switch (attribute.Type)
            {
            // Accessors
            case AttributeType.Public: Accessor = AccessLevel.Public; break;

            case AttributeType.Protected: Accessor = AccessLevel.Protected; break;

            case AttributeType.Private: Accessor = AccessLevel.Private; break;

            // Apply static
            case AttributeType.Static: IsStatic = true; break;

            // Apply virtual
            case AttributeType.Virtual: IsVirtual = true; break;

            // Apply override
            case AttributeType.Override: IsOverride = true; break;

            // Apply Recursive
            case AttributeType.Recursive: IsRecursive = true; break;

            // Apply Variables

            default: throw new NotImplementedException();
            }

            return(true);
        }
示例#4
0
        public void Reject(FileDiagnostics diagnostics)
        {
            switch (Type)
            {
            // Accessors
            case AttributeType.Public:
            case AttributeType.Protected:
            case AttributeType.Private:
                diagnostics.Error("Accessor not valid here.", Range);
                break;

            // Workshop ID override
            case AttributeType.ID:
                diagnostics.Error($"Cannot override workshop variable ID here.", Range);
                break;

            // Extended collection
            case AttributeType.Ext:
                diagnostics.Error($"Cannot put variable in the extended collection.", Range);
                break;

            // Initial value
            case AttributeType.Initial:
                diagnostics.Error($"Variable cannot have an initial value.", Range);
                break;

            // Use attribute name
            case AttributeType.Static:
            case AttributeType.Globalvar:
            case AttributeType.Playervar:
            case AttributeType.Ref:
            default:
                diagnostics.Error($"'{Type.ToString().ToLower()}' attribute not valid here.", Range);
                break;
            }
        }
        /// <summary>
        /// Adds a method to the current scope.
        /// When handling methods added by the user, supply the diagnostics and range to show the syntax error at.
        /// When handling methods added internally, have the diagnostics and range parameters be null. An exception will be thrown instead if there is a syntax error.
        /// </summary>
        /// <param name="method">The method that will be added to the current scope. If the object reference is already in the direct scope, an exception will be thrown.</param>
        /// <param name="diagnostics">The file diagnostics to throw errors with. Should be null when adding methods internally.</param>
        /// <param name="range">The document range to throw errors at. Should be null when adding methods internally.</param>
        public void AddMethod(IMethod method, FileDiagnostics diagnostics, DocRange range, bool checkConflicts = true)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            if (checkConflicts && HasConflict(method))
            {
                string message = "A method with the same name and parameter types was already defined in this scope.";

                if (diagnostics != null && range != null)
                {
                    diagnostics.Error(message, range);
                    return;
                }
                else
                {
                    throw new Exception(message);
                }
            }

            AddNativeMethod(method);
        }
示例#6
0
        public Var GetVar()
        {
            _parseInfo   = _contextHandler.ParseInfo;
            _diagnostics = _parseInfo.Script.Diagnostics;
            _nameRange   = _contextHandler.GetNameRange();

            // Get the attributes.
            _attributes = _contextHandler.GetAttributes();

            // Stores all values in the enum AttributeType as a list.
            List <AttributeType> missingTypes = Enum.GetValues(typeof(AttributeType)).Cast <AttributeType>().ToList();

            // Filter missing attributes.
            foreach (VarBuilderAttribute attribute in _attributes)
            {
                missingTypes.Remove(attribute.Type);
            }

            // Check missing attributes.
            MissingAttribute(missingTypes.ToArray());

            // Check existing attributes.
            CheckAttributes();

            // Create the varinfo.
            _varInfo = new VarInfo(_contextHandler.GetName(), _contextHandler.GetDefineLocation(), _parseInfo);

            // Get the variable type.
            _typeRange = _contextHandler.GetTypeRange();
            GetCodeType();

            if (_varInfo.Type is Lambda.PortableLambdaType)
            {
                _varInfo.TokenType = TokenType.Function;
            }

            // Apply attributes.
            foreach (VarBuilderAttribute attribute in _attributes)
            {
                attribute.Apply(_varInfo);
            }

            Apply();

            // Set the variable and store types.
            if (_varInfo.IsWorkshopReference)
            {
                // If the variable is a workshop reference, set the variable type to ElementReference.
                _varInfo.VariableType = VariableType.ElementReference;
                _varInfo.StoreType    = StoreType.None;
            }
            // In extended collection.
            else if (_varInfo.InExtendedCollection)
            {
                _varInfo.StoreType = StoreType.Indexed;
            }
            // Full workshop variable.
            else
            {
                _varInfo.StoreType = StoreType.FullVariable;
            }

            TypeCheck();
            _varInfo.Recursive = IsRecursive();

            // Set the scope.
            var scope = OperationalScope();

            _varInfo.OperationalScope = scope;

            // Get the resulting variable.
            var result = new Var(_varInfo);

            // Add the variable to the operational scope.
            if (_contextHandler.CheckName())
            {
                scope.AddVariable(result, _diagnostics, _nameRange);
            }
            else
            {
                scope.CopyVariable(result);
            }

            return(result);
        }
        public VariableResolve(VariableResolveOptions options, IExpression expression, DocRange expressionRange, FileDiagnostics diagnostics)
        {
            // The expression is a variable.
            if (expression is CallVariableAction)
            {
                // Get the variable being set and the range.
                SetVariable   = (CallVariableAction)expression;
                VariableRange = expressionRange;
            }
            // The expression is an expression tree.
            else if (expression is ExpressionTree tree)
            {
                Tree = tree;
                if (tree.Completed)
                {
                    // If the resulting expression in the tree is not a variable.
                    if (tree.Result is CallVariableAction == false)
                    {
                        NotAVariableRange = tree.ExprContextTree.Last().GetRange();
                    }
                    else
                    {
                        // Get the variable and the range.
                        SetVariable   = (CallVariableAction)tree.Result;
                        VariableRange = tree.ExprContextTree.Last().GetRange();
                    }
                }
            }
            // The expression is not a variable.
            else if (expression != null)
            {
                NotAVariableRange = expressionRange;
            }

            // NotAVariableRange will not be null if the resulting expression is a variable.
            if (NotAVariableRange != null)
            {
                diagnostics.Error("Expected a variable.", NotAVariableRange);
            }

            // Make sure the variable can be set to.
            if (SetVariable != null)
            {
                // Check if the variable is settable.
                if (options.ShouldBeSettable && !SetVariable.Calling.Settable())
                {
                    diagnostics.Error($"The variable '{SetVariable.Calling.Name}' cannot be set to.", VariableRange);
                }

                // Check if the variable is a whole workshop variable.
                if (options.FullVariable)
                {
                    Var asVar = SetVariable.Calling as Var;
                    if (asVar == null || asVar.StoreType != StoreType.FullVariable)
                    {
                        diagnostics.Error($"The variable '{SetVariable.Calling.Name}' cannot be indexed.", VariableRange);
                    }
                }

                // Check for indexers.
                if (!options.CanBeIndexed && SetVariable.Index.Length != 0)
                {
                    diagnostics.Error($"The variable '{SetVariable.Calling.Name}' cannot be indexed.", VariableRange);
                }
            }

            DoesResolveToVariable = SetVariable != null;
        }
示例#8
0
 public FileImporter(FileDiagnostics diagnostics, Importer importer)
 {
     Diagnostics = diagnostics;
     Importer    = importer;
 }
示例#9
0
 public void OutputComment(FileDiagnostics diagnostics, DocRange range, string comment)
 {
     Comment = comment;
 }
示例#10
0
 public void Copy(FileDiagnostics diagnostics)
 {
     diagnostics.Error($"Multiple '{Type.ToString().ToLower()}' attributes.", Range);
 }
 /// <summary>Sets the related output comment. This assumes that the result is both an IExpression and an IStatement.</summary>
 public void OutputComment(FileDiagnostics diagnostics, DocRange range, string comment) => ((IStatement)Result).OutputComment(diagnostics, range, comment);
 public void OutputComment(FileDiagnostics diagnostics, DocRange range, string comment) => Result?.SetComment(comment);
示例#13
0
        public Var GetVar()
        {
            _parseInfo   = _contextHandler.ParseInfo;
            _diagnostics = _parseInfo.Script.Diagnostics;
            _nameRange   = _contextHandler.GetNameRange();

            // Get the attributes.
            _attributes = _contextHandler.GetAttributes();

            // Stores all values in the enum AttributeType as a list.
            List <AttributeType> missingTypes = Enum.GetValues(typeof(AttributeType)).Cast <AttributeType>().ToList();

            // Filter missing attributes.
            foreach (VarBuilderAttribute attribute in _attributes)
            {
                missingTypes.Remove(attribute.Type);
            }

            // Check missing attributes.
            MissingAttribute(missingTypes.ToArray());

            // Check existing attributes.
            CheckAttributes();

            // Create the varinfo.
            _varInfo = new VarInfo(_contextHandler.GetName(), _contextHandler.GetDefineLocation(), _parseInfo);

            // Get the variable type.
            _typeRange = _contextHandler.GetTypeRange();
            GetCodeType();

            // Apply attributes.
            foreach (VarBuilderAttribute attribute in _attributes)
            {
                attribute.Apply(_varInfo);
            }

            Apply();

            // Set the variable and store types.
            if (_varInfo.IsWorkshopReference)
            {
                // If the variable is a workshop reference, set the variable type to ElementReference.
                _varInfo.VariableType = VariableType.ElementReference;
                _varInfo.StoreType    = StoreType.None;
            }
            // In extended collection.
            else if (_varInfo.InExtendedCollection)
            {
                _varInfo.StoreType = StoreType.Indexed;
            }
            // Full workshop variable.
            else
            {
                _varInfo.StoreType = StoreType.FullVariable;
            }

            TypeCheck();

            return(new Var(_varInfo));
        }
        public void Add(FileDiagnostics fileDiagnostics)
        {
            ThrowIfFileIsAlreadyAdded(fileDiagnostics.Uri);

            diagnosticFiles.Add(fileDiagnostics);
        }
 public VariableResolveErrorHandler(FileDiagnostics diagnostics)
 {
     _diagnostics = diagnostics;
 }