public static UnifiedClassDefinition CreateModule(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "module"); return UnifiedClassDefinition.Create( null, null, CreateSymbol(node.NthElement(0)), null, null, CreateScope(node.NthElement(1))); }
public static UnifiedIf CreateIf(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "if"); return UnifiedIf.Create( CreateExpresion(node.NthElement(0)), CreateBlock(node.NthElement(1)), CreateBlock(node.NthElement(2))); }
public static UnifiedExpression CreateDefn(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "defn"); return UnifiedFunctionDefinition.Create( null, null, null, null, CreateSymbol(node.NthElement(0)), CreateArgs(node.NthElement(1)), null, CreateScope(node.NthElement(2))); }
public static UnifiedEigenClassDefinition CreateSclass(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "sclass"); return UnifiedEigenClassDefinition.Create( null, null, null, null, UnifiedEigenConstrain.Create( CreateExpresion(node.NthElement(0))). ToSet<UnifiedTypeConstrain>(), CreateScope(node.NthElement(1))); }
public static UnifiedExpression CreateFor(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "for"); Contract.Assert( node.NthElement(1).Name() == "lasgn" || node.NthElement(1).Name() == "masgn"); return UnifiedForeach.Create( CreateExpresion(node.NthElement(0)), CreateExpresion(node.NthElement(1).FirstElement()), CreateBlock(node.NthElement(2))); }
public static UnifiedClassDefinition CreateClass(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "class"); var constNode = node.NthElement(1); var constrain = constNode.Name() != "nil" ? UnifiedExtendConstrain.Create( UnifiedType.Create(constNode.Value)) : null; return UnifiedClassDefinition.Create( null, null, CreateSymbol(node.NthElement(0)), null, constrain.ToSet<UnifiedTypeConstrain>(), CreateScope(node.NthElement(2))); }
public static UnifiedCall CreateCall(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "call"); // TODO: 演算子への変換 var receiver = CreateExpresion(node.NthElement(0)); var secondNode = node.NthElement(1); return UnifiedCall.Create( receiver != null ? UnifiedProperty.Create( ".", receiver, CreateExpresion(secondNode)) : CreateExpresion(secondNode), CreateArglist(node.NthElement(2))); }
public static UnifiedCall CreateIter(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "iter"); var call = CreateCall(node.NthElement(0)); var paramNode = node.NthElement(1); Contract.Assert( paramNode.Name() != "Fixnum" || paramNode.Value == "0"); var parameters = paramNode.Name() != "Fixnum" ? CreateLasgnOrMasgnOrNil( node.NthElement(1)) .Select(e => e.ToParameter()) .ToSet() : null; var block = CreateBlock(node.NthElement(2)); call.Proc = UnifiedProc.Create(parameters, block); return call; }
public static UnifiedExpression CreateCase(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "case"); return UnifiedSwitch.Create( CreateExpresion(node.NthElement(0)), node.Elements().Skip(1) .SelectMany(CreateWhenAndDefault) .ToSet() ); }
private static UnifiedExpression CreateDefs(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "defs"); var owner = CreateExpresion(node.NthElement(0)); var target = UnifiedFunctionDefinition.Create( null, null, null, null, CreateSymbol(node.NthElement(1)), CreateArgs(node.NthElement(2)), null, CreateScope(node.NthElement(3))); return UnifiedProperty.Create(".", owner, target); }
public static UnifiedProgram CreateCompilationUnit(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "compilationUnit"); /* * compilationUnit * : ( (annotations)? packageDeclaration )? (importDeclaration)* (typeDeclaration)* */ var program = UnifiedProgram.Create(UnifiedBlock.Create()); var expressions = program.Body; var first = node.FirstElementOrDefault(); if (first.SafeName() == "annotations") { var annotations = CreateAnnotations(node.FirstElement()); var packageDeclaration = CreatePackageDeclaration(node.NthElement(1)); packageDeclaration.Annotations = annotations; expressions.Add(packageDeclaration); expressions = packageDeclaration.Body; } else if (first.SafeName() == "packageDeclaration") { var packageDeclaration = CreatePackageDeclaration(first); expressions.Add(packageDeclaration); expressions = packageDeclaration.Body; } foreach (var e in node.Elements("importDeclaration")) { var importDeclaration = CreateImportDeclaration(e); expressions.Add(importDeclaration); } foreach (var e in node.Elements("typeDeclaration")) { var typeDeclaration = CreateTypeDeclaration(e); expressions.AddRange(typeDeclaration); } // Pick up comment nodes which are in last children of the root node program.Comments = node.Elements(Code2XmlConstants.CommentName) .Select(CreateComment) .ToSet(); return program; }
CreateNormalInterfaceDeclaration( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "normalInterfaceDeclaration"); /* * normalInterfaceDeclaration * : modifiers 'interface' IDENTIFIER (typeParameters)? ('extends' typeList)? interfaceBody */ var annotationsAndModifiers = CreateModifiers(node.Element("modifiers")); var name = UnifiedVariableIdentifier.Create( node.NthElement(2).Value); var typeParametersNode = node.Element("typeParameters"); var typeParameters = typeParametersNode != null ? CreateTypeParameters( typeParametersNode) : null; var typeListNode = node.Element("typeList"); var constrains = typeListNode != null ? UnifiedSet<UnifiedTypeConstrain>.Create( CreateTypeList(typeListNode).Select ( UnifiedExtendConstrain .Create)) : null; var body = CreateInterfaceBody(node.Element("interfaceBody")); return UnifiedInterfaceDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, name, typeParameters, constrains, body); }
public static UnifiedExpression CreateForstatement(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "forstatement"); /* * forstatement * // enhanced for loop * 'for' '(' variableModifiers type IDENTIFIER ':' expression ')' statement * // normal for loop * | 'for' '(' (forInit)? ';' (expression)? ';' (expressionList)? ')' statement */ if (node.NthElement(2).Name() == "variableModifiers") { var annotationsAndModifiers = CreateVariableModifiers( node.Element("variableModifiers")); return UnifiedForeach.Create( UnifiedVariableDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, CreateType(node.Element("type")), UnifiedVariableIdentifier.Create( node.Element("IDENTIFIER").Value) ).ToVariableDefinitionList(), CreateExpression(node.Element("expression")), CreateStatement(node.Element("statement")).ToBlock() ); } var forInit = node.HasElement("forInit") ? CreateForInit(node.Element("forInit")) : null; var condition = node.HasElement("expression") ? CreateExpression( node.Element("expression")) : null; var step = node.HasElement("expressionList") // TODO tuple? ? CreateExpressionList( node.Element("expressionList")) .ToTupleLiteral() : null; var body = UnifiedBlock.Create( CreateStatement(node.Element("statement"))); return UnifiedFor.Create(forInit, condition, step, body); }
public static UnifiedClassLikeDefinition CreateNormalClassDeclaration( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "normalClassDeclaration"); /* * normalClassDeclaration * : modifiers 'class' IDENTIFIER (typeParameters)? ('extends' type)? ('implements' typeList)? classBody */ var annotationsAndModifiers = CreateModifiers(node.Element("modifiers")); var name = node.NthElement(2).Value; var typeParameters = node.HasElement("typeParameters") ? CreateTypeParameters( node.Element("typeParameters")) : null; var constrains = UnifiedSet<UnifiedTypeConstrain>.Create(); if (node.HasElement("type")) { constrains.Add( UnifiedExtendConstrain.Create( CreateType(node.Element("type")))); } if (node.HasElement("typeList")) { foreach (var type in CreateTypeList(node.Element("typeList"))) { constrains.Add(UnifiedImplementsConstrain.Create(type)); } } var body = CreateClassBody(node.Element("classBody")); return UnifiedClassDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, UnifiedVariableIdentifier.Create(name), typeParameters, constrains, body); }
public static UnifiedClassLikeDefinition CreateEnumDeclaration( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "enumDeclaration"); /* * enumDeclaration * : modifiers ('enum') IDENTIFIER ('implements' typeList)? enumBody */ var annotationsAndModifiers = CreateModifiers(node.FirstElement()); var name = node.NthElement(2).Value; var typeListNode = node.Element("typeList"); var constrains = typeListNode != null ? CreateTypeList(typeListNode) .Select( UnifiedImplementsConstrain .Create) .ToSet<UnifiedTypeConstrain>() : null; var enumBody = CreateEnumBody(node.Element("enumBody")); return UnifiedEnumDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, UnifiedVariableIdentifier.Create(name), null, constrains, enumBody); }
public static UnifiedNew CreateArrayCreator(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "arrayCreator"); /* * arrayCreator * : 'new' createdName '[' ']' ('[' ']')* arrayInitializer * | 'new' createdName '[' expression ']' ( '[' expression ']' )* ('[' ']')* */ var type = CreateCreatedName(node.NthElement(1)); if (node.HasElement("arrayInitializer")) { var initVal = CreateArrayInitializer(node.Element("arrayInitializer")); var dimension = node.ElementsByContent("[").Count(); type = type.WrapArrayRepeatedly(dimension); return UnifiedNew.Create(type, null, null, initVal); } type = node.Elements("expression") .Aggregate( type, (current, exp) => current .WrapArray( CreateExpression ( exp) . ToArgument ())); { var dimension = node.ElementsByContent("[") .Where(e => e.NextElement().Value == "]") .Count(); type = type.WrapArrayRepeatedly(dimension); } return UnifiedNew.Create(type); }
CreateNonWildcardTypeArguments( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "nonWildcardTypeArguments"); /* * nonWildcardTypeArguments * : '<' typeList '>' */ var typeList = CreateTypeList(node.NthElement(1)); var typeArguments = UnifiedSet<UnifiedGenericArgument>.Create(); foreach (var type in typeList) { var argument = UnifiedGenericArgument.Create(type); typeArguments.Add(argument); } return typeArguments; }
public static UnifiedExpression CreateSelector( UnifiedExpression prefix, XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "selector"); /* * selector : '.' IDENTIFIER arguments? //student.getName() | '.' nonWildcardTypeArguments IDENTIFIER arguments // fix to support "obj.method().<Object>method2()" | '.' 'this' //OuterClass.this | '.' 'super' superSuffix //Outer.super() | innerCreator | '[' expression ']' */ var secondElement = node.NthElementOrDefault(1); if (secondElement == null) { return CreateInnerCreator(prefix, node.FirstElement()); } if (secondElement.Name() == "IDENTIFIER") { prefix = UnifiedProperty.Create( ".", prefix, UnifiedVariableIdentifier.Create(secondElement.Value)); var arguments = node.Element("arguments"); if (arguments != null) { prefix = UnifiedCall.Create( prefix, CreateArguments(arguments)); } return prefix; } if (secondElement.Name() == "nonWildcardTypeArguments") { prefix = UnifiedProperty.Create( ".", prefix, UnifiedVariableIdentifier.Create( node.NthElement(2).Value)); return UnifiedCall.Create( prefix, CreateArguments(node.NthElement(3)), CreateNonWildcardTypeArguments(secondElement)); } if (secondElement.Value == "this") { return UnifiedProperty.Create( ".", prefix, UnifiedVariableIdentifier.Create("this")); } if (secondElement.Value == "super") { var prop = UnifiedProperty.Create( ".", prefix, UnifiedSuperIdentifier.Create("super")); return CreateSuperSuffix(prop, node.Element("superSuffix")); } if (secondElement.Name() == "expression") { return UnifiedIndexer.Create( prefix, UnifiedArgument.Create( CreateExpression(secondElement), null, null). ToSet()); } throw new InvalidOperationException(); }
public static UnifiedExpression CreateCreator(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "creator"); /* * creator * : 'new' nonWildcardTypeArguments classOrInterfaceType classCreatorRest * | 'new' classOrInterfaceType classCreatorRest * | arrayCreator */ //コード例 //Sample<String> Sample = new <Integer> Sample<String> (1,2){ int z = 0; }; var first = node.FirstElement(); if (first.Name() == "arrayCreator") { return CreateArrayCreator(first); } var creatorRest = CreateClassCreatorRest(node.Element("classCreatorRest")); if (node.Elements().Count() == 4) { return UnifiedNew.Create( CreateClassOrInterfaceType(node.NthElement(2)), creatorRest.Item1, CreateNonWildcardTypeArguments( node.Element("nonWildcardTypeArguments")), null, creatorRest.Item2); } return UnifiedNew.Create( CreateClassOrInterfaceType(node.NthElement(1)), creatorRest.Item1, null, null, creatorRest.Item2); }
public static UnifiedCast CreateCastExpression(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "castExpression"); /* * castExpression * : '(' primitiveType ')' unaryExpression * | '(' type ')' unaryExpressionNotPlusMinus */ if (node.LastElement().Name() == "unaryExpression") { return UnifiedCast.Create( CreatePrimitiveType(node.NthElement(1)), CreateUnaryExpression(node.NthElement(3)) ); } return UnifiedCast.Create( CreateType(node.NthElement(1)), CreateUnaryExpressionNotPlusMinus(node.NthElement(3)) ); }
public static UnifiedExpression CreateIdentifierSuffix( UnifiedExpression prefixProp, XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "identifierSuffix"); /* * identifierSuffix * : ('[' ']')+ '.' 'class' // java.lang.String[].class * | ('[' expression ']' )+ // strs[10] * | arguments // func(1, 2) * | '.' 'class' // java.lang.String.class * // this.<Integer>m(1), super.<Integer>m(1) * | '.' nonWildcardTypeArguments IDENTIFIER arguments * | '.' 'this' // Outer.this * | '.' 'super' arguments // new Outer().super(); * | innerCreator // new Outer().new <Integer> Inner<String>(1); */ var second = node.NthElementOrDefault(1); if (second == null) { // arguments // func(1, 2) if (node.FirstElement().Name() == "arguments") { return UnifiedCall.Create( prefixProp, CreateArguments(node.FirstElement())); } // innerCreator // new Outer().new <Integer> Inner<String>(1); return CreateInnerCreator( prefixProp, node.Element("innerCreator")); } // ('[' expression ']' )+ // strs[10] if (second.Name() == "expression") { return node.Elements("expression") .Select(CreateExpression) .Aggregate( prefixProp, (current, exp) => UnifiedIndexer.Create( current, UnifiedSet<UnifiedArgument>. Create( UnifiedArgument . Create ( exp, null, null))) ); } // '.' 'class' // java.lang.String.class if (second.Value == "class") { return UnifiedTypeof.Create(UnifiedType.Create(prefixProp)); } // ('[' ']')+ '.' 'class' // java.lang.String[].class if (node.LastElement().Value == "class") { var d = node.ElementsByContent("[").Count(); return UnifiedTypeof.Create( (UnifiedType.Create(prefixProp)). WrapArrayRepeatedly(d)); } // '.' nonWildcardTypeArguments IDENTIFIER arguments if (second.Name() == "nonWildcardTypeArguments") { var prop = UnifiedProperty.Create( ".", prefixProp, UnifiedVariableIdentifier.Create( node.NthElement(2).Value)); return UnifiedCall.Create( prop, CreateArguments(node.Element("arguments")), CreateNonWildcardTypeArguments( node.Element("nonWildcardTypeArguments"))); } // '.' 'this' // Outer.this if (second.Value == "this") { return UnifiedProperty.Create( ".", prefixProp, UnifiedThisIdentifier.Create("this")); } // '.' 'super' arguments // new Outer().super(); if (second.Value == "super") { var prop = UnifiedProperty.Create( ".", prefixProp, UnifiedSuperIdentifier.Create("super")); return UnifiedCall.Create( prop, CreateArguments(node.Element("arguments"))); } throw new InvalidOperationException(); }
public static UnifiedExpression CreateAnnotationMethodDeclaration( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "annotationMethodDeclaration"); /* * annotationMethodDeclaration * : modifiers type IDENTIFIER '(' ')' ('default' elementValue)? ';' */ var annotationsAndModifiers = CreateModifiers(node.FirstElement()); var elementValueNode = node.Element("elementValue"); return UnifiedVariableDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, CreateType(node.NthElement(1)), UnifiedVariableIdentifier.Create(node.NthElement(2).Value), arguments: UnifiedSet<UnifiedArgument>.Create(), initialValue: elementValueNode != null ? CreateElementValue(elementValueNode) : null ).ToVariableDefinitionList(); }
public static UnifiedParameter CreateEllipsisParameterDecl( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "ellipsisParameterDecl"); /* * ellipsisParameterDecl * : variableModifiers type '...' IDENTIFIER */ var annotationsAndModifiers = CreateVariableModifiers(node.Element("variableModifiers")); annotationsAndModifiers.Item2.Add(UnifiedModifier.Create("...")); var type = CreateType(node.Element("type")); return UnifiedParameter.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, type, UnifiedVariableIdentifier.Create(node.NthElement(3).Value). ToSet<UnifiedIdentifier>()); }
public static UnifiedGenericArgument CreateTypeArgument(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "typeArgument"); /* * typeArgument * : type * | '?' (('extends' | 'super') type)? */ if (node.FirstElement().Name() == "type") { return UnifiedGenericArgument.Create( CreateType(node.FirstElement())); } var anyType = UnifiedType.Create(node.NthElement(0).Value); var typeNode = node.Element("type"); if (typeNode != null) { UnifiedTypeConstrain constrain; if (node.NthElement(1).Value == "extends") { constrain = UnifiedExtendConstrain.Create(CreateType(typeNode)); } else { constrain = UnifiedSuperConstrain.Create(CreateType(typeNode)); } return UnifiedGenericArgument.Create( anyType, null, constrain.ToSet()); } return UnifiedGenericArgument.Create(anyType); }
CreateFormalParameter( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "formalParameter"); /* * formalParameter * : variableModifiers type IDENTIFIER ('[' ']')* */ var type = CreateType(node.NthElement(1)); var dimension = node.ElementsByContent("[").Count(); type = type.WrapArrayRepeatedly(dimension); var annotationsAndModifiers = CreateVariableModifiers(node.FirstElement()); return Tuple.Create( annotationsAndModifiers, type, UnifiedVariableIdentifier.Create(node.NthElement(2).Value)); }
public static UnifiedTry CreateTrystatement(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "trystatement"); /* * trystatement * : 'try' block (catches 'finally' block | catches | 'finally' block) */ var body = CreateBlock(node.NthElement(1)); var catches = node.HasElement("catches") ? CreateCatches(node.Element("catches")) : null; var finallyBlock = node.HasElement("FINALLY") ? CreateBlock( node.Elements("block").ElementAt( 1)) : null; return UnifiedTry.Create(body, catches, null, finallyBlock); }
public static UnifiedParameter CreateNormalParameterDecl(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "normalParameterDecl"); /* * normalParameterDecl * : variableModifiers type IDENTIFIER ('[' ']')* */ var annotationsAndModifiers = CreateVariableModifiers(node.Element("variableModifiers")); var type = CreateType(node.Element("type")); var dimension = node.ElementsByContent("[").Count(); type = type.WrapArrayRepeatedly(dimension); return UnifiedParameter.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, type, UnifiedVariableIdentifier.Create(node.NthElement(2).Value). ToSet<UnifiedIdentifier>()); }
public static UnifiedExpression CreateParExpression(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "parExpression"); /* * parExpression * : '(' expression ')' */ return CreateExpression(node.NthElement(1)); }
public static UnifiedVariableDefinition CreateEnumerator(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "enumerator"); /* enumerator : IDENTIFIER ('=' constant_expression)? */ var identifier = UnifiedIdentifier.CreateVariable(node.NthElement(0).Value); UnifiedExpression value = null; var expression = node.Element("constant_expression"); if (expression != null) { value = CreateConstantExpression(expression); } return UnifiedVariableDefinition.Create( null, null, null, identifier, value); }
public static UnifiedExpression CreateConditionalExpression( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "conditionalExpression"); /* * conditionalExpression * : conditionalOrExpression ('?' expression ':' conditionalExpression)? */ if (node.HasElement("expression")) { return UnifiedTernaryExpression.Create( CreateConditionalOrExpression(node.NthElement(0)), CreateExpression(node.NthElement(2)), CreateConditionalExpression(node.NthElement(4)) ); } return CreateConditionalOrExpression(node.FirstElement()); }