Пример #1
0
        /*group-declaration:
         | class-declaration
         | interface-declaration
         | enum-declaration */
        private TypeNode group_declaration(EncapsulationNode encapMod)
        {
            printIfDebug("group_declaration");
            TypeNode typeDeclaration = null;

            if (pass(TokenType.RW_ABSTRACT, TokenType.RW_CLASS))
            {
                typeDeclaration = class_declaration();
            }
            else if (pass(TokenType.RW_INTERFACE))
            {
                typeDeclaration = interface_declaration();
            }
            else if (pass(TokenType.RW_ENUM))
            {
                typeDeclaration = enum_declaration();
            }
            else
            {
                throwError("group-declaration expected [class|interface|enum]");
            }
            if (typeDeclaration != null)
            {
                typeDeclaration.setEncapsulationMode(encapMod);
            }
            return(typeDeclaration);
        }
Пример #2
0
 public static bool isValidEncapsulation(EncapsulationNode encapsulation, TokenType encapsulationType)
 {
     if (encapsulation == null || (encapsulation.token == null && encapsulationType == TokenType.RW_PUBLIC))
     {
         return(true);
     }
     return(encapsulation.type == encapsulationType);
 }
Пример #3
0
 public static bool isValidEncapsulationForClass(EncapsulationNode encapsulation, TokenType encapsulationType)
 {
     if (encapsulation == null)
     {
         return(true);
     }
     return(encapsulation.type == encapsulationType);
 }
 /*variable-declarator-list-p:
  | ',' variable-declarator-list
  | EPSILON */
 private List <FieldNode> variable_declarator_list_p(TypeNode type, EncapsulationNode encapsulation, bool isStatic)
 {
     printIfDebug("variable_declarator_list_p");
     if (pass(TokenType.PUNT_COMMA))
     {
         consumeToken();
         return(variable_declarator_list(type, encapsulation, isStatic));
     }
     else
     {
         return(new List <FieldNode>());
     }
 }
Пример #5
0
 /*encapsulation-modifier:
  | "public"
  | "protected"
  | "private"
  | EPSILON */
 private EncapsulationNode encapsulation_modifier()
 {
     printIfDebug("encapsulation_modifier");
     if (pass(encapsulationOptions))
     {
         var encapMod = new EncapsulationNode(token.type, token);
         consumeToken();
         return(encapMod);
     }
     else
     {
         return(new EncapsulationNode(TokenType.RW_PRIVATE, null));
     }
 }
        /*field-declaration:
         | variable-assigner variable-declarator-list-p ';' */
        private List <FieldNode> field_declaration(TypeNode type, IdNode Identifier, EncapsulationNode encapsulation, bool isStatic)
        {
            printIfDebug("field_declaration");
            var assigner = variable_assigner();
            var newField = new FieldNode(Identifier, type, isStatic, encapsulation, assigner, Identifier.token);
            var fields   = variable_declarator_list_p(type, encapsulation, isStatic);

            fields.Insert(0, newField);
            if (!pass(TokenType.PUNT_END_STATEMENT_SEMICOLON))
            {
                throwError("; expected");
            }
            consumeToken();
            return(fields);
        }
Пример #7
0
        TypeDeclarationNode TypeDeclaration()
        {
            printDebug("Type Declaration");
            var encapsulation_mod = new EncapsulationNode();

            if (MatchAny(this.encapsulation_modifiers))
            {
                encapsulation_mod.token = currentToken;
                ConsumeToken();
            }
            var decl = GroupDeclaration();

            decl.encapsulation_modifier = encapsulation_mod;

            return(decl);
        }
        /*variable-declarator-list:
         | identifier variable-assigner variable-declarator-list-p */
        private List <FieldNode> variable_declarator_list(TypeNode type, EncapsulationNode encapsulation, bool isStatic)
        {
            printIfDebug("variable_declarator_list");
            if (!pass(TokenType.ID))
            {
                throwError("identifier expected");
            }
            var identifier = new IdNode(token.lexeme, token);

            consumeToken();
            var assigner = variable_assigner();
            var fields   = variable_declarator_list_p(type, encapsulation, isStatic);

            fields.Insert(0, new FieldNode(identifier, type, isStatic, encapsulation, assigner, identifier.token));
            return(fields);
        }
Пример #9
0
 public void setEncapsulationMode(EncapsulationNode encapMod)
 {
     this.encapsulation = encapMod;
 }
Пример #10
0
 public TypeNode()
 {
     encapsulation = null;
 }
        private void field_or_method_declaration(ref ClassTypeNode currentClassType, TypeNode type, EncapsulationNode encapsulation, MethodModifierNode modifier)
        {
            printIfDebug("field_or_method_declaration");
            if (!pass(TokenType.ID))
            {
                throwError("identifier expected");
            }
            var Identifier = new IdNode(token.lexeme, token);
            var identifierMethodOrFielToken = token;

            consumeToken();
            if (pass(TokenType.PUNT_PAREN_OPEN))
            {
                var newMethodDeclared = method_declaration(Identifier, type, identifierMethodOrFielToken);
                newMethodDeclared.setEncapsulation(encapsulation);
                if (modifier != null)
                {
                    newMethodDeclared.setModifier(modifier);
                }
                currentClassType.addMethod(newMethodDeclared);
            }
            else
            {
                var isStatic = (modifier != null)?modifier.token.type == TokenType.RW_STATIC:false;
                if (modifier != null && !isStatic)
                {
                    Utils.ThrowError("The modifier '" + modifier.token.lexeme
                                     + "' is not valid on fields. Try using a property instead.");
                }
                var fieldDeclarationList = field_declaration(type, Identifier, encapsulation, isStatic);
                currentClassType.addFields(fieldDeclarationList);
            }
        }
        /*
         * ; SEMANTIC: validar que el constructor no tenga tipo de retorno
         * class-member-declaration-options:
         | optional-modifier type-or-void identifier field-or-method-or-constructor */
        private void class_member_declaration_options(ref ClassTypeNode currentClassType, EncapsulationNode encapsulation)
        {
            printIfDebug("class_member_declaration_options");
            if (pass(optionalModifiersOptions)) //Field or Method
            {
                Token optionalModifierToken = token;
                var   methodModifier        = new MethodModifierNode(optionalModifierToken);
                consumeToken();
                if (!pass(typesOptions, voidOption))
                {
                    throwError("type-or-void expected");
                }
                var type = type_or_void();
                if (!pass(TokenType.ID))
                {
                    throwError("identifier expected");
                }
                if (optionalModifierToken.type == TokenType.RW_STATIC) //Field
                {
                    field_or_method_declaration(ref currentClassType, type, encapsulation, methodModifier);
                }
                else   //Method
                {
                    var Identifier            = new IdNode(token.lexeme, token);
                    var identifierMethodToken = token;
                    consumeToken();
                    var methodDeclared = method_declaration(Identifier, type, identifierMethodToken);
                    methodDeclared.setModifier(methodModifier);
                    methodDeclared.setEncapsulation(encapsulation);
                    currentClassType.addMethod(methodDeclared);
                }
            }
            else if (pass(typesOptions, voidOption)) //Field, Method or constructor
            {
                Token oldToken = token;
                var   type     = type_or_void();

                if (oldToken.type == TokenType.ID) //Field, Method or constructor
                {
                    if (pass(TokenType.ID))        //Field or Method
                    {
                        field_or_method_declaration(ref currentClassType, type, encapsulation, null);
                    }
                    else if (pass(TokenType.PUNT_PAREN_OPEN)) //Contructor
                    {
                        var Identifier             = new IdNode(oldToken.lexeme, token);
                        var contructoreDeclaration = constructor_declaration(Identifier);
                        contructoreDeclaration.setEncapsulation(encapsulation);
                        currentClassType.addContructor(contructoreDeclaration);
                    }
                }
                else  //Field or Method
                {
                    if (!pass(TokenType.ID))
                    {
                        throwError("identifier expected");
                    }
                    field_or_method_declaration(ref currentClassType, type, encapsulation, null);
                }
            }
            // else //Contructor
            // {
            //     constructor_declaration();
            // }
        }