Exemple #1
0
 public override void Visit(IdentifierSyntax pNode)
 {
     if (pNode.Type == SmallType.Undefined)
     {
         Compiler.ReportError(CompilerErrorType.UnknownType, pNode);
     }
     base.Visit(pNode);
 }
 protected override SyntaxNode VisitIdentifierSyntax(IdentifierSyntax pNode)
 {
     //TODO this isn't 100% because we could define a var with the same name as the constant
     if (_locals.IsVariableDefined(pNode.Value))
     {
         return(_locals.GetVariable(pNode.Value));
     }
     return(base.VisitIdentifierSyntax(pNode));
 }
        public override void VisitIdentifierSyntax(IdentifierSyntax syntax)
        {
            if (syntax.IdentifierName.Length > LanguageConstants.MaxIdentifierLength)
            {
                this.diagnostics.Add(DiagnosticBuilder.ForPosition(syntax.Identifier).IdentifierNameExceedsLimit());
            }

            base.VisitIdentifierSyntax(syntax);
        }
Exemple #4
0
        private Symbol?TryGetBannedFunction(IdentifierSyntax identifierSyntax)
        {
            if (BannedFunctions.TryGetValue(identifierSyntax.IdentifierName, out var banned))
            {
                return(banned.CreateSymbol(DiagnosticBuilder.ForPosition(identifierSyntax)));
            }

            return(null);
        }
Exemple #5
0
        public override SyntaxNode Visit(AssignmentStatementSyntax pNode)
        {
            IdentifierSyntax n = null;

            n = pNode.Identifier.Accept <IdentifierSyntax>(this);

            var v = pNode.Value.Accept <ExpressionSyntax>(this);

            return(SyntaxFactory.AssignmentStatement(n, v));
        }
Exemple #6
0
        public UserFieldDefinition(StructDeclaratorSyntax field, IdentifierSyntax identifier, string documentation, Scope scope)
            : base((field.Parent as StructDeclarationSyntax).TypeQualifier?.ToSyntaxTypes(), new TypeDefinition((field.Parent as StructDeclarationSyntax).TypeSyntax), identifier.Identifier, documentation, scope, identifier.Span)
        {
            List <ColoredString> arraySpecifiers = new List <ColoredString>();

            for (int i = 0; i < field.ArraySpecifiers.Count; i++)
            {
                arraySpecifiers.AddRange(field.ArraySpecifiers[i].ToColoredString());
            }

            this.ArraySpecifiers = arraySpecifiers;
        }
        public UserParameterDefinition(ParameterSyntax parameter, IdentifierSyntax identifier, string documentation, Scope scope)
            : base(parameter.TypeQualifier?.ToSyntaxTypes(), new TypeDefinition(parameter.TypeSyntax), identifier.Identifier, documentation, scope, identifier.Span)
        {
            List <ColoredString> arraySpecifiers = new List <ColoredString>();

            for (int i = 0; i < parameter.ArraySpecifiers.Count; i++)
            {
                arraySpecifiers.AddRange(parameter.ArraySpecifiers[i].ToColoredString());
            }

            this.ArraySpecifiers = arraySpecifiers;
        }
Exemple #8
0
        public void Emit(ILRunner pRunner, IdentifierSyntax pNode)
        {
            if (Parameter)
            {
                var pb = pRunner.GetParameter(Value, out short i);
                if (pb == null)
                {
                    throw new InvalidOperationException("Invalid var");
                }

                if (pNode.LoadAddress && !IsAddress)
                {
                    pRunner.Emitter.Emit(OpCodes.Ldarga_S, i);
                }
                else
                {
                    pRunner.Emitter.Emit(OpCodes.Ldarg, i);
                }
            }
            else if (Field)
            {
                var fd = Type.GetField(Value);
                if (fd == null)
                {
                    throw new InvalidOperationException("Invalid var");
                }

                pRunner.Emitter.Emit(OpCodes.Ldarg_0);
                pRunner.Emitter.Emit(OpCodes.Ldfld, fd.Type.ToSystemType());
            }
            else
            {
                if (!_created)
                {
                    _lb      = pRunner.CreateLocal(Value, Type);
                    _created = true;
                }
                if (_lb == null)
                {
                    throw new InvalidOperationException("Invalid var");
                }

                if (pNode.LoadAddress)
                {
                    pRunner.Emitter.Emit(OpCodes.Ldloca_S, _lb);
                }
                else
                {
                    pRunner.Emitter.Emit(OpCodes.Ldloc, _lb);
                }
            }
        }
Exemple #9
0
        protected override SyntaxBase ReplaceObjectSyntax(ObjectSyntax syntax)
        {
            var declaredType = semanticModel.GetDeclaredType(syntax);

            if (declaredType is not ObjectType objectType)
            {
                return(base.ReplaceObjectSyntax(syntax));
            }

            var newChildren = new List <SyntaxBase>();

            foreach (var child in syntax.Children)
            {
                if (child is ObjectPropertySyntax objectProperty &&
                    objectProperty.TryGetKeyText() is string propertyKey &&
                    !objectType.Properties.ContainsKey(propertyKey))
                {
                    var insensitivePropertyKey = objectType.Properties.Keys.FirstOrDefault(x => StringComparer.OrdinalIgnoreCase.Equals(x, propertyKey));
                    if (insensitivePropertyKey != null)
                    {
                        SyntaxBase newKeySyntax;
                        if (Regex.IsMatch(insensitivePropertyKey, "^[a-zA-Z][a-zA-Z0-9_]*$"))
                        {
                            newKeySyntax = new IdentifierSyntax(new Token(TokenType.Identifier, new TextSpan(0, 0), insensitivePropertyKey, Enumerable.Empty <SyntaxTrivia>(), Enumerable.Empty <SyntaxTrivia>()));
                        }
                        else
                        {
                            var stringToken = new Token(TokenType.StringComplete, new TextSpan(0, 0), StringUtils.EscapeBicepString(insensitivePropertyKey), Enumerable.Empty <SyntaxTrivia>(), Enumerable.Empty <SyntaxTrivia>());
                            newKeySyntax = new StringSyntax(stringToken.AsEnumerable(), Enumerable.Empty <SyntaxBase>(), insensitivePropertyKey.AsEnumerable());
                        }

                        newChildren.Add(new ObjectPropertySyntax(
                                            newKeySyntax,
                                            objectProperty.Colon,
                                            Rewrite(objectProperty.Value)));
                        continue;
                    }
                }

                newChildren.Add(Rewrite(child));
            }

            if (Enumerable.SequenceEqual(newChildren, syntax.Children))
            {
                return(base.ReplaceObjectSyntax(syntax));
            }

            return(new ObjectSyntax(
                       syntax.OpenBrace,
                       newChildren,
                       syntax.CloseBrace));
        }
        protected override void VisitIdentifierSyntax(IdentifierSyntax pNode)
        {
            if (CurrentType != null || !_unit.IsTypeDefined(Namespace, pNode.Value))
            {
                //Normal identifier, continue as usual
                if (!IsVariableDefined(pNode.Value, out SmallType type))
                {
                    if (CurrentType == null)
                    {
                        //Generate a slightly different error message if we are in a struct
                        //This can happen if we forget self
                        if (Struct != null)
                        {
                            CompilerErrors.IdentifierNotDeclaredSelf(pNode, pNode.Span);
                        }
                        else
                        {
                            CompilerErrors.IdentifierNotDeclared(pNode, pNode.Span);
                        }
                    }
                    else
                    {
                        CompilerErrors.IdentifierNotDeclared(CurrentType, pNode, pNode.Span);
                    }
                }
                else
                {
                    pNode.SetType(type);
                }
            }
            else
            {
                //Shared or enum value
                var result = _unit.FromString(Namespace, pNode.Value, out SmallType t);
                switch (result)
                {
                case Compiler.FindResult.Found:
                    pNode.SetType(t);
                    break;

                case Compiler.FindResult.IncorrectScope:
                    CompilerErrors.TypeNotInScope(pNode.Value, pNode.Span);
                    break;

                case Compiler.FindResult.NotFound:
                    CompilerErrors.UndeclaredType(pNode.Value, pNode.Span);
                    break;
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// This function assumes that the function name, passed in <paramref name="functionName"/>,
        /// has already been read and validated, and the next token is known to be "(".
        /// </summary>
        private bool TryParseFunctionCall(IdentifierSyntax functionName,
                                          [NotNullWhen(true)] out FunctionCallSyntax?callSyntax)
        {
            callSyntax = null;
            var callPosition = _lexer.LastPosition;

            // Read the parameter list: this also eats the open paren
            if (!TryParseParameterList(out var parameters))
            {
                return(false);
            }

            callSyntax = new FunctionCallSyntax(functionName, parameters, callPosition);
            return(true);
        }
Exemple #12
0
        public UserVariableDefinition(ConditionSyntax condition, IdentifierSyntax identifier, string documentation, DefinitionKind kind, Scope scope)
            : base(documentation, kind, scope, identifier.Span)
        {
            if (kind == DefinitionKind.LocalVariable)
            {
                this.Name = ColoredString.Create(identifier.Identifier, ColorType.LocalVariable);
            }
            else
            {
                this.Name = ColoredString.Create(identifier.Identifier, ColorType.GlobalVariable);
            }

            this.TypeQualifiers  = condition.TypeQualifier?.ToSyntaxTypes() ?? new List <SyntaxType>();
            this.Type            = new TypeDefinition(condition.TypeSyntax);
            this.ArraySpecifiers = new List <ColoredString>();
        }
Exemple #13
0
        public override SyntaxNode Visit(DeclarationStatementSyntax pNode)
        {
            var v = pNode.Value.Accept <ExpressionSyntax>(this);

            IdentifierSyntax n = null;

            using (new ContextValue(this, "InDeclaration", true))
            {
                using (new ContextValue(this, "LoadObject", pNode.Value.GetType() == typeof(ObjectInitializerExpressionSyntax)))
                {
                    n = pNode.Identifier.Accept <IdentifierSyntax>(this);
                }
            }

            return(SyntaxFactory.DeclarationStatement(n, v));
        }
Exemple #14
0
        public IEnumerable <Symbol> ResolveUnqualifiedFunction(IdentifierSyntax identifierSyntax, bool includeDecorators)
        {
            // attempt to find function in all imported namespaces
            var symbols = this.namespaceTypes.Values
                          .Select(type => includeDecorators
                    ? type.MethodResolver.TryGetSymbol(identifierSyntax) ?? type.DecoratorResolver.TryGetSymbol(identifierSyntax)
                    : type.MethodResolver.TryGetSymbol(identifierSyntax));

            foreach (var symbol in symbols)
            {
                // The caller is responsible for deduplicating this list and returning an error if there is ambiguity, so we simply return everything.
                if (symbol is not null)
                {
                    yield return(symbol);
                }
            }
        }
Exemple #15
0
        private IdentifierSyntax ParseQuery()
        {
            switch (Current.Type)
            {
            case TokenType.Length:
                Expect(TokenType.Length);
                Expect(TokenType.LeftParen);

                IdentifierSyntax i = ParseIdentifierWithFunctionCall(true);

                Expect(TokenType.RightParen);
                return(SyntaxFactory.LengthQueryExpression(i));

            default:
                return(null);
            }
        }
Exemple #16
0
        public void Variable_reference_must_refer_to_existent_variable()
        {
            var position    = new TextPosition(10, 3, 4);
            var syntax      = new IdentifierSyntax("a", position);
            var method      = new CompiledMethod("Test::Method");
            var builder     = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics = new TestingDiagnosticSink();
            var variableMap = new ScopedVariableMap();

            variableMap.PushScope();

            var localIndex = ExpressionCompiler.TryCompileExpression(
                syntax, SimpleType.Int32, method, builder, new TestingResolver(variableMap), diagnostics);

            Assert.That(localIndex, Is.EqualTo(-1));
            diagnostics.AssertDiagnosticAt(DiagnosticCode.VariableNotFound, position).WithActual("a");
        }
Exemple #17
0
        public void Variable_reference_must_have_correct_type()
        {
            var position    = new TextPosition(10, 3, 4);
            var syntax      = new IdentifierSyntax("a", position);
            var method      = new CompiledMethod("Test::Method");
            var builder     = new BasicBlockGraphBuilder().GetInitialBlockBuilder();
            var diagnostics = new TestingDiagnosticSink();

            var variableMap = new ScopedVariableMap();

            variableMap.PushScope();
            variableMap.TryAddVariable("a", 0);
            method.AddLocal(SimpleType.Bool, LocalFlags.None);

            var localIndex = ExpressionCompiler.TryCompileExpression(
                syntax, SimpleType.Int32, method, builder, new TestingResolver(variableMap), diagnostics);

            Assert.That(localIndex, Is.EqualTo(-1));
            diagnostics.AssertDiagnosticAt(DiagnosticCode.TypeMismatch, position)
            .WithActual("bool").WithExpected("int32");
        }
Exemple #18
0
        private Symbol LookupSymbolByName(IdentifierSyntax identifierSyntax, bool isFunctionCall)
        {
            // attempt to find name in the imported namespaces
            if (this.namespaces.TryGetValue(identifierSyntax.IdentifierName, out var namespaceSymbol))
            {
                // namespace symbol found
                return(namespaceSymbol);
            }

            // declarations must not have a namespace value, namespaces are used to fully qualify a function access.
            // There might be instances where a variable declaration for example uses the same name as one of the imported
            // functions, in this case to differentiate a variable declaration vs a function access we check the namespace value,
            // the former case must have an empty namespace value whereas the latter will have a namespace value.
            if (this.declarations.TryGetValue(identifierSyntax.IdentifierName, out var localSymbol))
            {
                // we found the symbol in the local namespace
                return(localSymbol);
            }

            // attempt to find function in all imported namespaces
            var foundSymbols = this.namespaces
                               .Select(kvp => allowedFlags.HasDecoratorFlag()
                    ? kvp.Value.Type.MethodResolver.TryGetSymbol(identifierSyntax) ?? kvp.Value.Type.DecoratorResolver.TryGetSymbol(identifierSyntax)
                    : kvp.Value.Type.MethodResolver.TryGetSymbol(identifierSyntax))
                               .Where(symbol => symbol != null)
                               .ToList();

            if (foundSymbols.Count > 1)
            {
                // ambiguous symbol
                return(new ErrorSymbol(DiagnosticBuilder.ForPosition(identifierSyntax).AmbiguousSymbolReference(identifierSyntax.IdentifierName, this.namespaces.Keys)));
            }

            var foundSymbol = foundSymbols.FirstOrDefault();

            return(isFunctionCall ?
                   SymbolValidator.ResolveUnqualifiedFunction(allowedFlags, foundSymbol, identifierSyntax, namespaces.Values) :
                   SymbolValidator.ResolveUnqualifiedSymbol(foundSymbol, identifierSyntax, namespaces.Values, declarations.Keys));
        }
        private void OnTextViewMouseHover(object sender, MouseHoverEventArgs e)
        {
            SnapshotPoint?point = this.textView.BufferGraph.MapDownToFirstMatch(new SnapshotPoint(this.textView.TextSnapshot, e.Position), PointTrackingMode.Positive, snapshot => this.subjectBuffers.Contains(snapshot.TextBuffer), PositionAffinity.Predecessor);

            if (point != null)
            {
                ITrackingPoint triggerPoint = point.Value.Snapshot.CreateTrackingPoint(point.Value.Position, PointTrackingMode.Positive);

                if (this.provider.QuickInfoBroker.IsQuickInfoActive(this.textView))
                {
                    return;
                }

                Snapshot snapshot = this.source.CurrentSnapshot;

                IdentifierSyntax identifier = this.source.Tree?.GetNodeFromPosition(snapshot, point.Value.Position) as IdentifierSyntax;

                if (identifier?.Definition != null)
                {
                    this.provider.QuickInfoBroker.TriggerQuickInfo(this.textView, triggerPoint, true);
                }
            }
        }
Exemple #20
0
        private bool TryReadAndValidateIdentifier([NotNullWhen(true)] out IdentifierSyntax?identifier,
                                                  bool allowReservedTypeNames)
        {
            if (_lexer.PeekTokenType() != TokenType.Identifier)
            {
                _diagnosticSink.Add(DiagnosticCode.ExpectedIdentifier, _lexer.Position, ReadTokenIntoString());
                identifier = null;
                return(false);
            }

            var token = ReadTokenIntoString();

            if (!NameParsing.IsValidFullName(token) ||
                (!allowReservedTypeNames && NameParsing.IsReservedTypeName(token)))
            {
                _diagnosticSink.Add(DiagnosticCode.InvalidIdentifier, _lexer.LastPosition, token);
                identifier = null;
                return(false);
            }

            identifier = new IdentifierSyntax(token, _lexer.LastPosition);
            return(true);
        }
Exemple #21
0
        private Symbol LookupGlobalSymbolByName(IdentifierSyntax identifierSyntax, bool isFunctionCall)
        {
            // attempt to find name in the built in namespaces. imported namespaces will be present in the declarations list as they create declared symbols.
            if (this.namespaceResolver.BuiltIns.TryGetValue(identifierSyntax.IdentifierName) is { } namespaceSymbol)
            {
                // namespace symbol found
                return(namespaceSymbol);
            }

            // declarations must not have a namespace value, namespaces are used to fully qualify a function access.
            // There might be instances where a variable declaration for example uses the same name as one of the imported
            // functions, in this case to differentiate a variable declaration vs a function access we check the namespace value,
            // the former case must have an empty namespace value whereas the latter will have a namespace value.
            if (this.declarations.TryGetValue(identifierSyntax.IdentifierName, out var globalSymbol))
            {
                // we found the symbol in the global namespace
                return(globalSymbol);
            }

            // attempt to find function in all imported namespaces
            var foundSymbols = namespaceResolver.ResolveUnqualifiedFunction(identifierSyntax, includeDecorators: allowedFlags.HasAnyDecoratorFlag());

            if (foundSymbols.Count() > 1)
            {
                var ambiguousNamespaces = foundSymbols.OfType <FunctionSymbol>().Select(x => x.DeclaringObject.Name);

                // ambiguous symbol
                return(new ErrorSymbol(DiagnosticBuilder.ForPosition(identifierSyntax).AmbiguousSymbolReference(identifierSyntax.IdentifierName, ambiguousNamespaces.ToImmutableSortedSet(StringComparer.Ordinal))));
            }

            var foundSymbol = foundSymbols.FirstOrDefault();

            return(isFunctionCall ?
                   SymbolValidator.ResolveUnqualifiedFunction(allowedFlags, foundSymbol, identifierSyntax, namespaceResolver) :
                   SymbolValidator.ResolveUnqualifiedSymbol(foundSymbol, identifierSyntax, namespaceResolver, declarations.Keys));
        }
        private FunctionExpression ConvertObject(ObjectSyntax syntax)
        {
            // need keys and values in one array of parameters
            var parameters = new LanguageExpression[syntax.Properties.Count() * 2];

            int index = 0;

            foreach (var propertySyntax in syntax.Properties)
            {
                parameters[index] = propertySyntax.Key switch
                {
                    IdentifierSyntax identifier => new JTokenExpression(identifier.IdentifierName),
                    StringSyntax @string => ConvertString(@string),
                    _ => throw new NotImplementedException($"Encountered an unexpected type '{propertySyntax.Key.GetType().Name}' when generating object's property name.")
                };
                index++;

                parameters[index] = ConvertExpression(propertySyntax.Value);
                index++;
            }

            // we are using the createObject() function as a proxy for an object literal
            return(GetCreateObjectExpression(parameters));
        }
Exemple #23
0
        private Symbol?LookupLocalSymbolByName(IdentifierSyntax identifierSyntax, bool isFunctionCall)
        {
            if (isFunctionCall)
            {
                // functions can't be local symbols
                return(null);
            }

            // iterating over a stack gives you the items in the same
            // order as if you popped each one but without modifying the stack
            foreach (var scope in activeScopes)
            {
                // resolve symbol against current scope
                // this binds to the innermost symbol even if there exists one at the parent scope
                var symbol = LookupLocalSymbolByName(scope, identifierSyntax);
                if (symbol != null)
                {
                    // found a symbol - return it
                    return(symbol);
                }
            }

            return(null);
        }
Exemple #24
0
 public UserMacroDefinition(DefinePreprocessorSyntax define, IdentifierSyntax identifier, string documentation, Scope scope)
     : base(identifier.Identifier, define.Arguments?.ToColoredString(), define.TokenString?.ToColoredString(), documentation, scope, identifier.Span)
 {
 }
Exemple #25
0
        public static string GetClassificationName(this SyntaxToken token)
        {
            IdentifierSyntax identifier = token as IdentifierSyntax;

            if (token.SyntaxType.IsPreprocessor())
            {
                return(GLSLConstants.PreprocessorKeyword);
            }

            if (identifier?.Definition?.Kind == DefinitionKind.Macro)
            {
                return(GLSLConstants.Macro);
            }

            if (token?.IsExcludedCode() ?? false)
            {
                return(GLSLConstants.ExcludedCode);
            }

            if (token.SyntaxType.IsPunctuation())
            {
                return(GLSLConstants.Punctuation);
            }

            if (token?.IsPreprocessorText() ?? false)
            {
                return(GLSLConstants.PreprocessorText);
            }

            if (token.SyntaxType.IsKeyword())
            {
                return(GLSLConstants.Keyword);
            }

            if (token.SyntaxType.IsNumber())
            {
                return(GLSLConstants.Number);
            }

            if (identifier?.Definition != null)
            {
                switch (identifier.Definition.Kind)
                {
                case DefinitionKind.Field:
                    return(GLSLConstants.Field);

                case DefinitionKind.Function:
                    return(GLSLConstants.Function);

                case DefinitionKind.GlobalVariable:
                    return(GLSLConstants.GlobalVariable);

                case DefinitionKind.LocalVariable:
                    return(GLSLConstants.LocalVariable);

                case DefinitionKind.Macro:
                    return(GLSLConstants.Macro);

                case DefinitionKind.Parameter:
                    return(GLSLConstants.Parameter);

                case DefinitionKind.TypeName:
                case DefinitionKind.InterfaceBlock:
                    return(GLSLConstants.TypeName);

                default:
                    return(GLSLConstants.Identifier);
                }
            }

            if (identifier?.Parent.SyntaxType == SyntaxType.FieldSelection)
            {
                return(GLSLConstants.Field);
            }

            if (token.SyntaxType == SyntaxType.IdentifierToken)
            {
                return(GLSLConstants.Identifier);
            }

            return(PredefinedClassificationTypeNames.FormalLanguage);
        }
		public virtual void PostWalkIdentifier(IdentifierSyntax identifierSyntax) { }
		// IdentifierSyntax
		public virtual bool WalkIdentifier(IdentifierSyntax identifierSyntax) { return DefaultWalk(identifierSyntax); }
Exemple #28
0
 public Symbol?TryGetSymbol(IdentifierSyntax identifierSyntax) => this.functionResolver.TryGetSymbol(identifierSyntax);
Exemple #29
0
 private Symbol LookupSymbolByName(IdentifierSyntax identifierSyntax, bool isFunctionCall) =>
 this.LookupLocalSymbolByName(identifierSyntax, isFunctionCall) ?? LookupGlobalSymbolByName(identifierSyntax, isFunctionCall);
Exemple #30
0
 protected DeclaredSymbol(ISymbolContext context, string name, SyntaxBase declaringSyntax, IdentifierSyntax nameSyntax)
     : base(name)
 {
     this.Context         = context;
     this.DeclaringSyntax = declaringSyntax;
     this.NameSyntax      = nameSyntax;
 }
Exemple #31
0
 public static ObjectPropertySyntax CreateProperty(IdentifierSyntax name, SyntaxBase value) => new ObjectPropertySyntax(name, CreateToken(TokenType.Colon), value, CreateNewLines());
Exemple #32
0
 private static ResourceSymbol?LookupResourceSymbolByName(ILanguageScope scope, IdentifierSyntax identifierSyntax) =>
 scope.Declarations
 .OfType <ResourceSymbol>()
 .FirstOrDefault(symbol => string.Equals(identifierSyntax.IdentifierName, symbol.Name, LanguageConstants.IdentifierComparison));