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); } }
private ExpressionNode Instantiation() { ExpressionNode itsAST = null; // in case there is a syntactic error SourceCodePosition itsPos = _currentToken.SourcePosition; Accept(Token.TokenType.New); if (Array.Exists(_typeFirstSet, e => e == _currentToken.Type && e != Token.TokenType.Identifier)) { TypeNode itsType = ValueType(); ExpressionNode itsSize = ArrayExpression(); ArrayTypeNode itsArray = new ArrayTypeNode(itsType, itsSize, itsPos); itsAST = new InstantiationNode(itsArray, itsSize, itsPos); } else { IdentifierNode itsName = new IdentifierNode(_currentToken); Accept(Token.TokenType.Identifier); ClassTypeNode itsType = new ClassTypeNode(itsName, _currentToken.SourcePosition); if (_currentToken.Type == Token.TokenType.LeftBracket) { ExpressionNode itsSize = ArrayExpression(); ArrayTypeNode itsArray = new ArrayTypeNode(itsType, itsSize, itsPos); itsAST = new InstantiationNode(itsArray, itsSize, itsPos); } else if (_currentToken.Type == Token.TokenType.LeftParen) { itsAST = new InstantiationNode(itsType, Call(itsName), itsPos); } else { Error("Expected leftBracket or leftParen"); } } return(itsAST); }
private TypeNode Type() { TypeNode itsAST = null; // in case there is a syntactic error SourceCodePosition itsPos = _currentToken.SourcePosition; if (Array.Exists(_typeFirstSet, e => e == _currentToken.Type && e != Token.TokenType.Identifier)) { itsAST = ValueType(); if (_currentToken.Type == Token.TokenType.LeftBracket) { itsAST = ArrayType(itsAST); } } else if (_currentToken.Type == Token.TokenType.Identifier) { IdentifierNode itsName = new IdentifierNode(_currentToken); itsAST = new ClassTypeNode(itsName, itsPos); Accept(Token.TokenType.Identifier); if (_currentToken.Type == Token.TokenType.LeftBracket) { itsAST = ArrayType(itsAST); } } else { Error("Expected int, real, string, boolean, identifier, or array type"); } return(itsAST); }
public virtual void VisitClassTypeNode(ClassTypeNode node) { Visit(node.ClassKeywordNode); Visit(node.DispositionNode); Visit(node.OpenParenthesisNode); Visit(node.InheritanceListNode); Visit(node.CloseParenthesisNode); Visit(node.ContentListNode); Visit(node.EndKeywordNode); }
private static void ConvertClassMembers(CodeTypeDeclaration nameClass, ClassTypeNode classTypeNode) { foreach (var visibilityItem in classTypeNode.ContentListNode.Items) { foreach (var child in visibilityItem.ContentListNode.Items) { ConvertMethod(nameClass, (MethodHeadingNode)child); } } }
/*optional-class-member-declaration-list: | class-member-declaration optional-class-member-declaration-list | EPSILON */ private void optional_class_member_declaration_list(ref ClassTypeNode currentClassType) { printIfDebug("optional_class_member_declaration_list"); if (pass(encapsulationOptions, optionalModifiersOptions, typesOptions, voidOption)) { class_member_declaration(ref currentClassType); optional_class_member_declaration_list(ref currentClassType); } else { //EPSILON } }
/*class-member-declaration: | encapsulation-modifier class-member-declaration-options */ private void class_member_declaration(ref ClassTypeNode currentClassType) { printIfDebug("class_member_declaration"); var encapsulation = encapsulation_modifier(); if (pass(optionalModifiersOptions, typesOptions, voidOption)) { class_member_declaration_options(ref currentClassType, encapsulation); } else { throwError(optionalModifiersOptions.ToString() + " or " + typesOptions.ToString() + "or void expected"); } }
public Dictionary <string, FieldNode> getFieldsForClass(ClassTypeNode classType, string path) { Dictionary <string, FieldNode> my_fields = new Dictionary <string, FieldNode>(); foreach (var f in classType.Fields) { if (my_fields.ContainsKey(f.identifier.Name)) { Utils.ThrowError(path + ": The type '" + classType.Identifier.Name + "' already contains a definition for '" + f.identifier.Name + "'"); } my_fields[f.identifier.Name] = f; } return(my_fields); }
/*class-body: | '{' optional-class-member-declaration-list '}' */ private ClassTypeNode class_body(IdNode Identifier, Token classTypeToken) { printIfDebug("class_body"); if (!pass(TokenType.PUNT_CURLY_BRACKET_OPEN)) { throwError("'{' expected"); } consumeToken(); var newClassType = new ClassTypeNode(Identifier, classTypeToken); optional_class_member_declaration_list(ref newClassType); if (!pass(TokenType.PUNT_CURLY_BRACKET_CLOSE)) { throwError("'}' expected"); } consumeToken(); return(newClassType); }
public Dictionary <string, ConstructorNode> getConstructorsForClass(ClassTypeNode classType, string path) { Dictionary <string, ConstructorNode> ctrs = new Dictionary <string, ConstructorNode>(); foreach (var ct in classType.Constructors) { string ctrName = ct.identifier.Name + buildFixedParams(ct.parameters); if (ctrs.ContainsKey(ctrName)) { Utils.ThrowError(getDeclarationPathForType(classType) + ": Type '" + classType.Identifier.Name + "' already defines a member called '.ctor' with the same parameter types '" + ctrName + "'"); } ctrs[ctrName] = ct; } if (ctrs.Count == 0) { var token = new Token(TokenType.RW_PUBLIC, "public", 0, 0); ctrs[classType.Identifier.Name + "()"] = new ConstructorNode(classType.Identifier, null, null , new StatementBlockNode(), token); } return(ctrs); }
private ConstructorDeclaringNode ConstructorDeclaring() { ConstructorDeclaringNode itsAST; SourceCodePosition itsPos = _currentToken.SourcePosition; Accept(Token.TokenType.Constructor); IdentifierNode itsName = new IdentifierNode(_currentToken); TypeNode itsType = new ClassTypeNode(itsName, itsPos); Accept(Token.TokenType.Identifier); Accept(Token.TokenType.LeftParen); ParameterSequenceNode itsParams = Parameters(); Accept(Token.TokenType.RightParen); Accept(Token.TokenType.LeftBrace); DeclaringSequenceNode itsVars = VariableDeclarings(); ActionSequenceNode itsActions = ActionStatements(); Accept(Token.TokenType.RightBrace); itsAST = new ConstructorDeclaringNode(itsName, itsType, itsParams, itsVars, itsActions, itsPos); return(itsAST); }
public object Visit(ClassTypeNode n, object o) { return(null); }
private bool Property(NamespaceNode ns, ClassTypeNode cls) { Token name; if (Name(out name)) { if (cls.PropertyMap.ContainsKey(name)) { ErrorAndThrow(new DiagMsgEx(DiagCodeEx.DuplicatePropertyName, name.Value), name.TextSpan); } KeywordExpected(ParserConstants.AsKeyword); cls.PropertyMap.Add(name, LocalTypeExpected(ns, LocalTypeFlags.GlobalTypeRef | LocalTypeFlags.Nullable | LocalTypeFlags.List | LocalTypeFlags.Set | LocalTypeFlags.Map)); return true; } return false; }
private bool ClassType(NamespaceNode ns) { if (Keyword(ParserConstants.ClassKeyword)) { var name = NameExpected(); CheckDuplicateGlobalType(ns, name); Token abstractOrSealed; if (!Keyword(ParserConstants.AbstractKeyword, out abstractOrSealed)) { Keyword(ParserConstants.SealedKeyword, out abstractOrSealed); } var baseClassQName = default(QualifiableNameNode); if (Keyword(ParserConstants.ExtendsKeyword)) { baseClassQName = QualifiableNameExpected(); } List<KeyNode> keyList = null; if (Keyword(ParserConstants.KeyKeyword)) { keyList = new List<KeyNode>(); while (true) { var key = new KeyNode { NameExpected() }; while (true) { if (Token('.')) { key.Add(NameExpected()); } else { break; } } keyList.Add(key); if (!Token(',')) { break; } } } TokenExpected('{'); var cls = new ClassTypeNode(ns, name, abstractOrSealed, baseClassQName, keyList); while (Property(ns, cls)) ; TokenExpected('}'); ns.GlobalTypeMap.Add(name, cls); return true; } return false; }
public object Visit(ClassTypeNode n, object o) { Append(n.ClassName.Value); return(null); }
/* * ; 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(); // } }