All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openrewrite.hcl.internal.HclParserVisitor Maven / Gradle / Ivy

There is a newer version: 8.40.2
Show newest version
/*
 * Copyright 2021 the original author or authors.
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* https://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.openrewrite.hcl.internal; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.TerminalNode; import org.openrewrite.internal.lang.NonNull; import org.jspecify.annotations.Nullable; import org.openrewrite.FileAttributes; import org.openrewrite.hcl.internal.grammar.HCLParser; import org.openrewrite.hcl.internal.grammar.HCLParserBaseVisitor; import org.openrewrite.hcl.tree.*; import org.openrewrite.marker.Markers; import java.nio.charset.Charset; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.function.BiFunction; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; import static org.openrewrite.Tree.randomId; @SuppressWarnings("ConstantConditions") public class HclParserVisitor extends HCLParserBaseVisitor { private final Path path; private final String source; private final Charset charset; private final boolean charsetBomMarked; @Nullable private final FileAttributes fileAttributes; private int cursor = 0; public HclParserVisitor(Path path, String source, Charset charset, boolean charsetBomMarked, @Nullable FileAttributes fileAttributes) { this.path = path; this.source = source; this.charset = charset; this.charsetBomMarked = charsetBomMarked; this.fileAttributes = fileAttributes; } @Override public Hcl visitAttribute(HCLParser.AttributeContext ctx) { return convert(ctx, (c, prefix) -> new Hcl.Attribute( randomId(), Space.format(prefix), Markers.EMPTY, visitIdentifier(c.Identifier()), new HclLeftPadded<>( sourceBefore("="), Hcl.Attribute.Type.Assignment, Markers.EMPTY ), (Expression) visit(c.expression()), null )); } @Override public Hcl visitAttributeAccessExpression(HCLParser.AttributeAccessExpressionContext ctx) { return convert(ctx, (c, prefix) -> new Hcl.AttributeAccess( randomId(), Space.format(prefix), Markers.EMPTY, (Expression) visit(c.exprTerm()), new HclLeftPadded<>( sourceBefore("."), visitIdentifier(c.getAttr().Identifier()), Markers.EMPTY ) )); } @Override public Hcl visitBinaryOp(HCLParser.BinaryOpContext ctx) { return convert(ctx, (c, prefix) -> { Expression left, right; // left can be unaryOp or exprTerm, right can be another operation or exprTerm if (c.unaryOp() != null) { left = (Expression) visit(c.unaryOp()); }else { left = (Expression) visit(c.exprTerm(0)); } Hcl.Binary.Type op; switch (ctx.binaryOperator().getText()) { case "+": op = Hcl.Binary.Type.Addition; break; case "-": op = Hcl.Binary.Type.Subtraction; break; case "*": op = Hcl.Binary.Type.Multiplication; break; case "/": op = Hcl.Binary.Type.Division; break; case "%": op = Hcl.Binary.Type.Modulo; break; case "||": op = Hcl.Binary.Type.Or; break; case "&&": op = Hcl.Binary.Type.And; break; case "<": op = Hcl.Binary.Type.LessThan; break; case "<=": op = Hcl.Binary.Type.LessThanOrEqual; break; case ">": op = Hcl.Binary.Type.GreaterThan; break; case ">=": op = Hcl.Binary.Type.GreaterThanOrEqual; break; case "==": op = Hcl.Binary.Type.Equal; break; case "!=": default: op = Hcl.Binary.Type.NotEqual; break; } Space opPrefix = Space.format(prefix(ctx.binaryOperator())); cursor = ctx.binaryOperator().getStop().getStopIndex() + 1; if (c.unaryOp() != null) { right = (Expression) visit(c.operation() != null ? c.operation() : c.exprTerm(0)); }else { right = (Expression) visit(c.operation() != null ? c.operation() : c.exprTerm(1)); } return new Hcl.Binary( randomId(), Space.format(prefix), Markers.EMPTY, left, new HclLeftPadded<>(opPrefix, op, Markers.EMPTY), right ); }); } @Override public Hcl visitBlockExpr(HCLParser.BlockExprContext ctx) { return convert(ctx, (c, prefix) -> new Hcl.Block( randomId(), Space.format(prefix), Markers.EMPTY, null, emptyList(), sourceBefore("{"), c.body().bodyContent().stream() .map(bc -> (BodyContent) visit(bc)) .collect(toList()), sourceBefore("}"))); } @Override public Hcl visitBlock(HCLParser.BlockContext ctx) { return convert(ctx, (c, prefix) -> { Hcl.Identifier type = visitIdentifier(ctx.Identifier()); List





© 2015 - 2024 Weber Informatics LLC | Privacy Policy