public static UnifiedExpression CreateInnerCreator( UnifiedExpression prefix, XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "innerCreator"); /* * innerCreator * : '.' 'new' (nonWildcardTypeArguments)? IDENTIFIER (typeArguments)? classCreatorRest */ //コード例 // X . new <T> Sample <E> (1,2){} var typeArguments = node.HasElement("typeArguments") ? CreateTypeArguments( node.Element("typeArguments")) : null; var type = UnifiedType.Create( node.Element("IDENTIFIER").Value).WrapGeneric(typeArguments); var creatorRest = CreateClassCreatorRest(node.Element("classCreatorRest")); var typeParameters = node.HasElement("nonWildcardTypeArguments") ? CreateNonWildcardTypeArguments( node.Element( "nonWildcardTypeArguments")) : null; var prop = UnifiedNew.Create( type, creatorRest.Item1, typeParameters, null, creatorRest.Item2); return UnifiedProperty.Create(".", prefix, prop); }
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()); }
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); }
public static UnifiedExpression CreateTryStatement(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "tryStatement"); /* * tryStatement * : 'try' LT!* statementBlock LT!* (finallyClause | catchClause (LT!* finallyClause)?) */ var body = CreateStatementBlock(node.Element("statementBlock")); var catches = node.HasElement("catchClause") ? CreateCatchClause( node.Element("catchClause")) : null; var finallyBody = node.HasElement("finallyClause") ? CreateFinallyClause( node.Element("finallyClause")) : null; return UnifiedTry.Create(body, catches, null, finallyBody); }
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 IEnumerable<UnifiedExpression> CreateClassBodyDeclaration ( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "classBodyDeclaration"); /* * classBodyDeclaration * : ';' * | ('static')? block * | memberDecl */ if (node.HasElement("block")) { //case static initializer var modifier = node.HasContent("static") ? UnifiedModifier.Create("static") : null; yield return UnifiedStaticInitializer.Create( CreateBlock(node.Element("block")), null, UnifiedSet<UnifiedModifier>.Create(modifier)); } if (node.HasElement("memberDecl")) { yield return CreateMemberDecl(node.Element("memberDecl")); } }
public static UnifiedVariableDefinition CreateVariableDeclarator ( XElement node, UnifiedSet<UnifiedAnnotation> annotations, UnifiedSet<UnifiedModifier> modifiers, UnifiedType type) { Contract.Requires(node != null); Contract.Requires(node.Name() == "variableDeclarator"); /* * variableDeclarator * : IDENTIFIER ('[' ']')* ('=' variableInitializer)? */ var initializer = node.HasElement("variableInitializer") ? CreateVariableInitializer( node.Element( "variableInitializer")) : null; var dimension = node.ElementsByContent("[").Count(); type = type.WrapArrayRepeatedly(dimension); return UnifiedVariableDefinition.Create( annotations, modifiers, type, UnifiedVariableIdentifier.Create(node.FirstElement().Value), initializer ); }
public static UnifiedFor CreateForStatement(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "forStatement"); /* * forStatement * : 'for' LT!* '(' (LT!* forStatementInitialiserPart)? LT!* ';' (LT!* expression)? LT!* ';' (LT!* expression)? LT!* ')' LT!* statement */ var init = node.HasElement("forStatementInitialiserPart") ? CreateForStatementInitialiserPart( node.Element( "forStatementInitialiserPart")) : null; //expressionを区別できないので、セミコロンの位置から条件なのかステップなのかを判断 var semicolons = node.Elements().Where(e => e.Value == ";"); var first = semicolons.ElementAt(0).NextElement(); var second = semicolons.ElementAt(1).NextElement(); var cond = first.Name() == "expression" ? CreateExpression(first) : null; var step = second.Name() == "expression" ? CreateExpression(second) : null; var body = UnifiedBlock.Create( CreateStatement(node.Element("statement"))); return UnifiedFor.Create(init, cond, step, body); }
public static UnifiedExpression CreateForStatementInitialiserPart( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "forStatementInitialiserPart"); /* * forStatementInitialiserPart * : expressionNoIn * | 'var' LT!* variableDeclarationListNoIn */ if (node.NthElement(0).Name() == "expressionNoIn") { return CreateExpressionNoIn(node.NthElement(0)); } if (node.HasElement("variableDeclarationListNoIn")) { return CreateVariableDeclarationListNoIn( node.Element("variableDeclarationListNoIn")); } throw new InvalidOperationException(); }
public static UnifiedCase CreateDefaultClause(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "defaultClause"); /* * defaultClause * : 'default' LT!* ':' LT!* statementList? */ var body = node.HasElement("statementList") ? CreateStatementList( node.Element("statementList")) : null; return UnifiedCase.Create(null, UnifiedBlock.Create(body)); }
public static UnifiedExpression CreateForInStatementInitialiserPart( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "forInStatementInitialiserPart"); /* * forInStatementInitialiserPart * : leftHandSideExpression * | 'var' LT!* variableDeclarationNoIn */ //左辺がCall or Newになる場合のコードはまだ未確認 if (node.NthElement(0).Name() == "leftHandSideExpression") { return CreateLeftHandSideExpression(node.NthElement(0)); } if (node.HasElement("variableDeclarationNoIn")) { return CreateVariableDeclarationNoIn( node.Element("variableDeclarationNoIn")) .ToVariableDefinitionList(); } throw new InvalidOperationException(); }
public static UnifiedExpression CreateContinueStatement(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "continueStatement"); /* * continueStatement * : 'continue' Identifier? statementEnd */ var identifier = node.HasElement("Identifier") ? UnifiedVariableIdentifier.Create( node.Element("Identifier").Value) : null; return UnifiedContinue.Create(identifier); }
public static UnifiedExpression CreateConditionalExpressionNoIn( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "conditionalExpressionNoIn"); /* * conditionalExpressionNoIn * : logicalORExpressionNoIn (LT!* '?' LT!* assignmentExpressionNoIn LT!* ':' LT!* assignmentExpressionNoIn)? */ if (node.HasElement("assignmentExpressionNoIn")) { return UnifiedTernaryExpression.Create( CreateLogicalORExpressionNoIn( node.Element("logicalORExpressionNoIn")), CreateAssignmentExpressionNoIn( node.Element("assignmentExpressionNoIn")), CreateAssignmentExpressionNoIn( node.Elements("assignmentExpressionNoIn"). ElementAt(1))); } return CreateLogicalORExpressionNoIn(node.NthElement(0)); }
public static UnifiedCase CreateCaseClause(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "caseClause"); /* * caseClause * : 'case' LT!* expression LT!* ':' LT!* statementList? */ var cond = CreateExpression(node.Element("expression")); var body = node.HasElement("statementList") ? CreateStatementList( node.Element("statementList")) : null; return UnifiedCase.Create(cond, UnifiedBlock.Create(body)); }
CreateClassCreatorRest(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "classCreatorRest"); /* * classCreatorRest * : arguments (classBody)? */ var body = node.HasElement("classBody") ? CreateClassBody(node.Element("classBody")) : null; return new Tuple<UnifiedSet<UnifiedArgument>, UnifiedBlock>( CreateArguments(node.Element("arguments")), body); }
public static UnifiedExpression CreateExplicitConstructorInvocation( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "explicitConstructorInvocation"); /* * explicitConstructorInvocation * : (nonWildcardTypeArguments)? ('this'|'super') arguments ';' * | primary '.' (nonWildcardTypeArguments)? 'super' arguments ';' */ var aruguments = CreateArguments(node.Element("arguments")); var typeArguments = node.HasElement("nonWildcardTypeArguments") ? CreateNonWildcardTypeArguments( node.Element( "nonWildcardTypeArguments")) : null; if (node.FirstElement().Name() == "primary") { var prop = UnifiedProperty.Create( ".", CreatePrimary(node.Element("primary")), UnifiedSuperIdentifier.Create("super")); return UnifiedCall.Create(prop, aruguments, typeArguments); } var target = node.FirstElement().Value == "this" ? (UnifiedExpression) UnifiedThisIdentifier.Create("this") : UnifiedSuperIdentifier.Create("super"); return UnifiedCall.Create(target, aruguments, typeArguments); }
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 UnifiedExpression CreateSwitchLabel(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "switchLabel"); /* * switchLabel * : 'case' expression ':' * | 'default' ':' */ if (node.HasElement("expression")) { return CreateExpression(node.Element("expression")); } return null; }
public static UnifiedExpression CreateMethodDeclaration(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "methodDeclaration"); /* * methodDeclaration * : //constructor * modifiers (typeParameters)? IDENTIFIER formalParameters ('throws' qualifiedNameList)? '{' (explicitConstructorInvocation)? (blockStatement)* '}' * | modifiers (typeParameters)? (type | 'void') IDENTIFIER formalParameters * ('[' ']')* ('throws' qualifiedNameList)? (block | ';' ) */ /* コード例 * public int[] getName(String className) [] throws Error { * int[][] a = null; * return a; * } */ var name = UnifiedVariableIdentifier.Create( node.Element("IDENTIFIER").Value); var typeParameters = node.HasElement("typeParameters") ? CreateTypeParameters( node.Element("typeParameters")) : null; var type = CreateType(node.Element("type")); //コンストラクタの場合はnullになるがどうせ使わない var dimension = node.ElementsByContent("[").Count(); type = type.WrapArrayRepeatedly(dimension); var annotationsAndModifiers = CreateModifiers(node.Element("modifiers")); var parameters = CreateFormalParameters(node.Element("formalParameters")); var throws = node.HasElement("qualifiedNameList") ? CreateQualifiedNameList( node.Element("qualifiedNameList")) .Select(UnifiedType.Create) .ToSet() : null; var body = node.HasElement("block") ? CreateBlock(node.Element("block")) : null; if (!node.HasElement("type") && !node.HasElementByContent("void")) { //case constructor var invocationNode = node.Element("explicitConstructorInvocation"); var invocations = invocationNode != null ? Enumerable.Repeat( CreateExplicitConstructorInvocation ( invocationNode), 1) : Enumerable.Empty<UnifiedExpression> (); var block = invocations.Concat( node.Elements("blockStatement") .SelectMany(CreateBlockStatement) ) .ToBlock(); return UnifiedConstructor.Create( block, annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, parameters, typeParameters, throws); } return UnifiedFunctionDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, type, typeParameters, name, parameters, throws, body); }
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 UnifiedFunctionDefinition CreateInterfaceMethodDeclaration ( XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "interfaceMethodDeclaration"); /* * interfaceMethodDeclaration * : modifiers (typeParameters)? (type |'void') IDENTIFIER formalParameters ('[' ']')* ('throws' qualifiedNameList)? ';' */ var annotationsAndModifiers = CreateModifiers(node.Element("modifiers")); var typeParameters = node.HasElement("typeParameters") ? CreateTypeParameters( node.Element("typeParameters")) : null; var type = CreateType(node.Element("type")); var dimension = node.ElementsByContent("[").Count(); type = type.WrapArrayRepeatedly(dimension); var name = UnifiedVariableIdentifier.Create( node.Element("IDENTIFIER").Value); var parameters = CreateFormalParameters(node.Element("formalParameters")); var throws = node.HasElement("qualifiedNameList") ? UnifiedSet<UnifiedType>.Create( CreateQualifiedNameList( node.Element( "qualifiedNameList")) .Select(UnifiedType.Create)) : null; return UnifiedFunctionDefinition.Create( annotationsAndModifiers.Item1, annotationsAndModifiers.Item2, type, typeParameters, name, parameters, throws); }
public static UnifiedExpression CreateReturnStatement(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "returnStatement"); /* * returnStatement * : 'return' expression? statementEnd */ var expression = node.HasElement("expression") ? CreateExpression( node.Element("expression")) : null; return UnifiedReturn.Create(expression); }