use of com.sonar.sslr.api.typed.Optional in project sonar-java by SonarSource.
the class TreeFactory method applySelectors.
private static ExpressionTree applySelectors(ExpressionTree primary, Optional<List<Tuple<Optional<InternalSyntaxToken>, ExpressionTree>>> selectors) {
ExpressionTree result = primary;
if (selectors.isPresent()) {
for (Tuple<Optional<InternalSyntaxToken>, ExpressionTree> tuple : selectors.get()) {
Optional<InternalSyntaxToken> dotTokenOptional = tuple.first();
ExpressionTree selector = tuple.second();
if (dotTokenOptional.isPresent()) {
InternalSyntaxToken dotToken = dotTokenOptional.get();
if (selector.is(Kind.IDENTIFIER)) {
IdentifierTreeImpl identifier = (IdentifierTreeImpl) selector;
result = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
} else {
MethodInvocationTreeImpl methodInvocation = (MethodInvocationTreeImpl) selector;
IdentifierTreeImpl identifier = (IdentifierTreeImpl) methodInvocation.methodSelect();
MemberSelectExpressionTreeImpl memberSelect = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
result = new MethodInvocationTreeImpl(memberSelect, methodInvocation.typeArguments(), (ArgumentListTreeImpl) methodInvocation.arguments());
}
} else if (selector.is(Kind.NEW_CLASS)) {
NewClassTreeImpl newClass = (NewClassTreeImpl) selector;
result = newClass.completeWithEnclosingExpression(result);
} else if (selector.is(Kind.ARRAY_ACCESS_EXPRESSION)) {
ArrayAccessExpressionTreeImpl arrayAccess = (ArrayAccessExpressionTreeImpl) selector;
result = arrayAccess.complete(result);
} else if (selector.is(Kind.MEMBER_SELECT)) {
MemberSelectExpressionTreeImpl memberSelect = (MemberSelectExpressionTreeImpl) selector;
result = memberSelect.completeWithExpression(result);
} else {
throw new IllegalStateException();
}
}
}
return result;
}
use of com.sonar.sslr.api.typed.Optional in project sonar-java by SonarSource.
the class TreeFactory method newArrayCreatorWithInitializer.
public NewArrayTreeImpl newArrayCreatorWithInitializer(InternalSyntaxToken openBracketToken, InternalSyntaxToken closeBracketToken, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> dimensions, NewArrayTreeImpl partial) {
ImmutableList.Builder<ArrayDimensionTree> dDimensionsBuilder = ImmutableList.builder();
dDimensionsBuilder.add(new ArrayDimensionTreeImpl(openBracketToken, null, closeBracketToken));
if (dimensions.isPresent()) {
for (Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>> dim : dimensions.get()) {
List<AnnotationTreeImpl> annotations = dim.first().or(ImmutableList.<AnnotationTreeImpl>of());
Tuple<InternalSyntaxToken, InternalSyntaxToken> brackets = dim.second();
dDimensionsBuilder.add(new ArrayDimensionTreeImpl(annotations, brackets.first(), null, brackets.second()));
}
}
return partial.completeDimensions(dDimensionsBuilder.build());
}
use of com.sonar.sslr.api.typed.Optional in project sonar-java by SonarSource.
the class TreeFactory method newEnumConstant.
public EnumConstantTreeImpl newEnumConstant(Optional<List<AnnotationTreeImpl>> annotations, InternalSyntaxToken identifierToken, Optional<ArgumentListTreeImpl> arguments, Optional<ClassTreeImpl> classBody, Optional<InternalSyntaxToken> commaToken) {
IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
ArgumentListTreeImpl defaultArguments = new ArgumentListTreeImpl(ImmutableList.<ExpressionTree>of(), ImmutableList.<SyntaxToken>of());
NewClassTreeImpl newClass = new NewClassTreeImpl(arguments.or(defaultArguments), classBody.orNull());
newClass.completeWithIdentifier(identifier);
return new EnumConstantTreeImpl(modifiers((Optional<List<ModifierTree>>) (Optional<?>) annotations), identifier, newClass, commaToken.orNull());
}
use of com.sonar.sslr.api.typed.Optional in project sonar-java by SonarSource.
the class TreeFactory method newArrayInitializer.
public NewArrayTreeImpl newArrayInitializer(InternalSyntaxToken openBraceToken, Optional<InternalSyntaxToken> optionalComma, Optional<List<Tuple<ExpressionTree, Optional<InternalSyntaxToken>>>> rests, InternalSyntaxToken closeBraceToken) {
ImmutableList.Builder<ExpressionTree> initializers = ImmutableList.builder();
ImmutableList.Builder<SyntaxToken> separators = ImmutableList.builder();
if (optionalComma.isPresent()) {
separators.add(optionalComma.get());
}
if (rests.isPresent()) {
for (Tuple<ExpressionTree, Optional<InternalSyntaxToken>> rest : rests.get()) {
initializers.add(rest.first());
if (rest.second().isPresent()) {
separators.add(rest.second().get());
}
}
}
return new NewArrayTreeImpl(ImmutableList.<ArrayDimensionTree>of(), new InitializerListTreeImpl(initializers.build(), separators.build())).completeWithCurlyBraces(openBraceToken, closeBraceToken);
}
use of com.sonar.sslr.api.typed.Optional in project sonar-java by SonarSource.
the class TreeFactory method newInferedParameters.
public LambdaParameterListTreeImpl newInferedParameters(InternalSyntaxToken openParenToken, Optional<Tuple<VariableTreeImpl, Optional<List<Tuple<InternalSyntaxToken, VariableTreeImpl>>>>> identifiersOpt, InternalSyntaxToken closeParenToken) {
ImmutableList.Builder<VariableTreeImpl> params = ImmutableList.builder();
if (identifiersOpt.isPresent()) {
Tuple<VariableTreeImpl, Optional<List<Tuple<InternalSyntaxToken, VariableTreeImpl>>>> identifiers = identifiersOpt.get();
VariableTreeImpl variable = identifiers.first();
params.add(variable);
VariableTreeImpl previousVariable = variable;
if (identifiers.second().isPresent()) {
for (Tuple<InternalSyntaxToken, VariableTreeImpl> identifier : identifiers.second().get()) {
variable = identifier.second();
params.add(variable);
InternalSyntaxToken comma = identifier.first();
previousVariable.setEndToken(comma);
previousVariable = variable;
}
}
}
return new LambdaParameterListTreeImpl(openParenToken, params.build(), closeParenToken);
}
Aggregations