示例#1
0
 public static ResolvedType FromClass(ClassLikeDefinition cd)
 {
     return(new ResolvedType()
     {
         CustomType = cd,
         Generics = EMPTY_GENERICS,
     });
 }
        public static TopLevelEntity ParseClassMember(
            ClassLikeDefinition classDef,
            Token className,
            ParserContext context,
            TokenStream tokens)
        {
            Token firstToken = tokens.Peek();
            Dictionary <string, Token> modifiers = ParseModifiers(context, tokens);

            if (tokens.IsNext("enum"))
            {
                return(ParseEnumDefinition(context, classDef, firstToken, modifiers, tokens));
            }

            if (tokens.IsNext("class"))
            {
                return(ParseClass(context, classDef, firstToken, modifiers, tokens));
            }

            if (tokens.IsNext("const"))
            {
                return(ParseConstDefinition(context, classDef, firstToken, modifiers, tokens));
            }

            CSharpType type = CSharpType.TryParse(tokens);

            if (tokens.IsNext("(") && type.SimpleTypeName == className.Value)
            {
                return(ParseClassConstructor(context, classDef, firstToken, modifiers, type, tokens));
            }

            Token memberName = tokens.PopWord();

            if (tokens.IsNext(";") || tokens.IsNext("="))
            {
                return(ParseClassField(context, classDef, firstToken, modifiers, type, memberName, tokens));
            }

            if (tokens.IsNext("[") && memberName.Value == "this")
            {
                return(ParseIndexProperty(context, classDef, firstToken, modifiers, type, tokens));
            }

            if (tokens.IsNext("{"))
            {
                return(ParseClassProperty(context, classDef, firstToken, modifiers, type, memberName, tokens));
            }

            if (tokens.IsNext("("))
            {
                return(ParseClassMethod(classDef, context, firstToken, modifiers, type, memberName, tokens));
            }

            throw new ParserException(tokens.Peek(), "Not implemented");
        }
        private static TopLevelEntity ParseIndexProperty(
            ParserContext context,
            ClassLikeDefinition parent,
            Token firstToken,
            Dictionary <string, Token> modifiers,
            CSharpType type,
            TokenStream tokens)
        {
            tokens.PopExpected("[");
            CSharpType indexType         = CSharpType.Parse(tokens);
            Token      indexVariableName = tokens.PopWord();

            tokens.PopExpected("]");
            tokens.PopExpected("{");

            PropertyDefinition indexProperty = new PropertyDefinition(firstToken, modifiers, type, null, parent);

            PropertyBody getter = null;
            PropertyBody setter = null;

            while (tokens.IsNext("get") || tokens.IsNext("set"))
            {
                Token getOrSetToken = tokens.Peek();
                bool  isGet         = getOrSetToken.Value == "get";
                if (!isGet && setter != null)
                {
                    tokens.PopExpected("}");                           // intentionally throw
                }
                if (isGet && getter != null)
                {
                    tokens.PopExpected("set"); //intentionally throw
                }
                tokens.Pop();                  // get/set already fetched with Peek() above.
                PropertyBody body = new PropertyBody(getOrSetToken, new Dictionary <string, Token>(), isGet, indexProperty);
                Executable[] code = ExecutableParser.ParseCodeBlock(context, tokens, body, true);
                body.Code = code;
                if (isGet)
                {
                    getter = body;
                }
                else
                {
                    setter = body;
                }
            }

            indexProperty.IndexType         = indexType;
            indexProperty.IndexVariableName = indexVariableName;

            indexProperty.Getter = getter;
            indexProperty.Setter = setter;

            return(indexProperty);
        }
        private static TopLevelEntity ParseClassProperty(
            ParserContext context,
            ClassLikeDefinition parent,
            Token firstToken,
            Dictionary <string, Token> modifiers,
            CSharpType type,
            Token propertyName,
            TokenStream tokens)
        {
            tokens.PopExpected("{");

            PropertyDefinition propertyDefinition = new PropertyDefinition(firstToken, modifiers, type, propertyName, parent);

            PropertyBody getter = null;
            PropertyBody setter = null;

            while (!tokens.IsNext("}") && (getter == null || setter == null))
            {
                Token bodyFirstToken = tokens.Peek();
                Dictionary <string, Token> bodyModifiers = ParseModifiers(context, tokens);
                if (tokens.IsNext("get") && getter == null)
                {
                    Token getToken = tokens.Pop();
                    getter = new PropertyBody(bodyFirstToken, bodyModifiers, true, propertyDefinition);
                    if (!tokens.PopIfPresent(";"))
                    {
                        getter.Code = ExecutableParser.ParseCodeBlock(context, tokens, getter, true);
                    }
                }
                else if (tokens.IsNext("set") && setter == null)
                {
                    Token setToken = tokens.Pop();
                    setter = new PropertyBody(bodyFirstToken, bodyModifiers, false, propertyDefinition);
                    if (!tokens.PopIfPresent(";"))
                    {
                        setter.Code = ExecutableParser.ParseCodeBlock(context, tokens, setter, true);
                    }
                }
                else if (getter == null)
                {
                    tokens.PopExpected("get"); // intentionally throw
                }
                else
                {
                    tokens.PopExpected("set"); // intentionally throw
                }
            }
            tokens.PopExpected("}");

            propertyDefinition.Getter = getter;
            propertyDefinition.Setter = setter;

            return(propertyDefinition);
        }
        private static TopLevelEntity ParseClassConstructor(
            ParserContext context,
            ClassLikeDefinition classDef,
            Token firstToken,
            Dictionary <string, Token> modifiers,
            CSharpType type,
            TokenStream tokens)
        {
            List <CSharpType> argTypes     = new List <CSharpType>();
            List <Token>      argNames     = new List <Token>();
            List <Token>      argModifiers = new List <Token>();

            ParseArgList(argTypes, argNames, argModifiers, tokens);

            ConstructorDefinition constructorDef = new ConstructorDefinition(
                firstToken,
                modifiers,
                argTypes,
                argNames,
                argModifiers,
                classDef);

            if (tokens.PopIfPresent(":"))
            {
                if (!tokens.IsNext("base") && !tokens.IsNext("this"))
                {
                    tokens.PopExpected("base"); // intentionally throw
                }
                constructorDef.BaseConstructorInvocation = tokens.Pop();

                tokens.PopExpected("(");
                List <Expression> baseConstructorArgs = new List <Expression>();
                while (!tokens.PopIfPresent(")"))
                {
                    if (baseConstructorArgs.Count > 0)
                    {
                        tokens.PopExpected(",");
                    }
                    baseConstructorArgs.Add(ExpressionParser.Parse(context, tokens, constructorDef));
                }
                constructorDef.BaseConstructorArgs = baseConstructorArgs.ToArray();
            }

            constructorDef.Code = ExecutableParser.ParseCodeBlock(context, tokens, constructorDef, true);

            return(constructorDef);
        }
        private static TopLevelEntity ParseClassField(
            ParserContext context,
            ClassLikeDefinition parent,
            Token firstToken,
            Dictionary <string, Token> modifiers,
            CSharpType type,
            Token fieldName,
            TokenStream tokens)
        {
            FieldDefinition fieldDef = new FieldDefinition(firstToken, modifiers, type, fieldName, null, parent);

            if (!tokens.PopIfPresent(";"))
            {
                tokens.PopExpected("=");
                Expression initialValue = ExpressionParser.Parse(context, tokens, fieldDef);
                tokens.PopExpected(";");
                fieldDef.InitialValue = initialValue;
            }
            return(fieldDef);
        }
示例#7
0
 private static void BuildAncestorLookupForClassImpl(string className, ParserContext parserContext, HashSet <string> lookup)
 {
     lookup.Add(className);
     if (FRAMEWORK_CLASSES_AND_PARENTS.ContainsKey(className))
     {
         foreach (string parent in FRAMEWORK_CLASSES_AND_PARENTS[className])
         {
             BuildAncestorLookupForClassImpl(parent, parserContext, lookup);
         }
     }
     else
     {
         TopLevelEntity tle = parserContext.DoLookup(className);
         if (tle is ClassLikeDefinition)
         {
             ClassLikeDefinition cd = (ClassLikeDefinition)tle;
             foreach (ResolvedType parentType in cd.ParentClasses)
             {
                 if (parentType.CustomType != null)
                 {
                     BuildAncestorLookupForClassImpl(parentType.CustomType.FullyQualifiedName, parserContext, lookup);
                 }
                 else if (parentType.FrameworkClass != null)
                 {
                     BuildAncestorLookupForClassImpl(parentType.FrameworkClass, parserContext, lookup);
                 }
                 else
                 {
                     throw new System.NotImplementedException();
                 }
             }
         }
         else
         {
             throw new System.NotImplementedException();
         }
     }
 }
        private static TopLevelEntity ParseClassMethod(
            ClassLikeDefinition classDef,
            ParserContext context,
            Token firstToken,
            Dictionary <string, Token> modifiers,
            CSharpType returnType,
            Token methodName,
            TokenStream tokens)
        {
            List <CSharpType> argTypes     = new List <CSharpType>();
            List <Token>      argNames     = new List <Token>();
            List <Token>      argModifiers = new List <Token>();

            ParseArgList(argTypes, argNames, argModifiers, tokens);

            MethodDefinition methodDef = new MethodDefinition(
                firstToken,
                modifiers,
                returnType,
                methodName,
                argNames,
                argTypes,
                argModifiers,
                classDef);

            if (methodDef.IsAbstract || classDef is InterfaceDefinition)
            {
                tokens.PopExpected(";");
            }
            else
            {
                methodDef.Code = ExecutableParser.ParseCodeBlock(context, tokens, methodDef, true);
            }

            return(methodDef);
        }
示例#9
0
        public override Expression ResolveTypes(ParserContext context, VariableScope varScope)
        {
            string name = this.Name.Value;

            if (name == "this" || name == "base")
            {
                if (name == "this")
                {
                    ClassLikeDefinition cd = this.Parent.ClassContainer;

                    return(new ThisKeyword(this.FirstToken, this.Parent)
                    {
                        ResolvedType = ResolvedType.FromClass(cd),
                    });
                }

                // TODO: base
                throw new System.NotImplementedException();
            }

            this.ResolvedType = varScope.GetVariableType(name);
            if (this.ResolvedType == null)
            {
                foreach (TopLevelEntity member in this.ClassContainer.GetMemberNonNull(name))
                {
                    if (member is PropertyDefinition || member is FieldDefinition || member is MethodDefinition)
                    {
                        VerifiedFieldReference vfr;
                        if (member.IsStatic)
                        {
                            ClassLikeDefinition  cd       = member.ClassContainer;
                            StaticClassReference classRef = new StaticClassReference(this.FirstToken, this.parent, member.ClassContainer);
                            vfr = new VerifiedFieldReference(this.FirstToken, this.parent, this.Name, classRef, ResolvedType.FromClass(cd));
                        }
                        else
                        {
                            ThisKeyword thisKeyword = new ThisKeyword(this.FirstToken, this.Parent);
                            thisKeyword.ResolvedType = ResolvedType.FromClass(this.Parent.ClassContainer);
                            vfr = new VerifiedFieldReference(this.FirstToken, this.parent, this.Name, thisKeyword, null);
                        }

                        if (member is PropertyDefinition)
                        {
                            vfr.Property     = (PropertyDefinition)member;
                            vfr.ResolvedType = vfr.Property.ResolvedType;
                        }
                        else if (member is FieldDefinition)
                        {
                            vfr.Field        = (FieldDefinition)member;
                            vfr.ResolvedType = vfr.Field.ResolvedType;
                        }
                        else if (member is MethodDefinition)
                        {
                            vfr.Method       = (MethodDefinition)member;
                            vfr.ResolvedType = vfr.Method.ResolvedReturnType;
                        }
                        else
                        {
                            throw new System.NotImplementedException();
                        }

                        return(vfr);
                    }
                    else
                    {
                        throw new System.NotImplementedException();
                    }
                }

                char c = this.Name.Value[0]; // lame optimization
                if (c >= 'A' && c <= 'Z')
                {
                    Expression newExpr = DotField.AttemptToResolveDotFieldChainIntoDirectReference(new Token[] { this.Name }, context, this);
                    if (newExpr != null)
                    {
                        return(newExpr);
                    }
                }

                throw new ParserException(this.FirstToken, "This variable is not declared.");
            }
            return(this);
        }
 public StaticClassReference(Token firstToken, TopLevelEntity parent, ClassLikeDefinition classDef)
     : base(firstToken, parent)
 {
     this.ClassDef     = classDef;
     this.ResolvedType = ResolvedType.FromClass(classDef);
 }