Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[engine][ast]Implement Break Statement #500

Merged
merged 2 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public interface AstNodeVisitor {

public void visit(EmptyLineStatement emptyLineStatement);

public void visit(BreakStatement breakStatement);

/** =============================== OTHER =============================== */
public void visit(MethodDefinition methodDefinition);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020 Google LLC
//
// 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
//
// http://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 com.google.api.generator.engine.ast;

public class BreakStatement implements Statement {
private BreakStatement() {}

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
}

public static BreakStatement create() {
return new BreakStatement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.api.generator.engine.ast.AstNodeVisitor;
import com.google.api.generator.engine.ast.BlockComment;
import com.google.api.generator.engine.ast.BlockStatement;
import com.google.api.generator.engine.ast.BreakStatement;
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
Expand Down Expand Up @@ -335,6 +336,11 @@ public void visit(EmptyLineStatement emptyLineStatement) {
// Nothing to do.
}

@Override
public void visit(BreakStatement breakStatement) {
// Nothing to do.
}

/** =============================== COMMENT =============================== */
@Override
public void visit(LineComment lineComment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.api.generator.engine.ast.AstNodeVisitor;
import com.google.api.generator.engine.ast.BlockComment;
import com.google.api.generator.engine.ast.BlockStatement;
import com.google.api.generator.engine.ast.BreakStatement;
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
Expand Down Expand Up @@ -108,6 +109,7 @@ public class JavaWriterVisitor implements AstNodeVisitor {
private static final String TRY = "try";
private static final String VOLATILE = "volatile";
private static final String WHILE = "while";
private static final String BREAK = "break";

// Operators
private static final String OPERATOR_ADDITION = "+";
Expand Down Expand Up @@ -631,6 +633,12 @@ public void visit(EmptyLineStatement emptyLineStatement) {
newline();
}

@Override
public void visit(BreakStatement breakStatement) {
buffer.append(BREAK);
semicolon();
}

/** =============================== COMMENT =============================== */
public void visit(LineComment lineComment) {
// Split comments by new line and add `//` to each line.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.api.generator.engine.ast.AssignmentOperationExpr;
import com.google.api.generator.engine.ast.BlockComment;
import com.google.api.generator.engine.ast.BlockStatement;
import com.google.api.generator.engine.ast.BreakStatement;
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
Expand Down Expand Up @@ -2244,6 +2245,13 @@ public void writeEmptyLineStatement() {
assertEquals("\n", writerVisitor.write());
}

@Test
public void writeBreakStatement() {
BreakStatement statement = BreakStatement.create();
statement.accept(writerVisitor);
assertEquals("break;", writerVisitor.write());
}

@Test
public void writePackageInfoDefinition() {
PackageInfoDefinition packageInfo =
Expand Down