Skip to content

Commit

Permalink
Migrate unparser to use CelExprVisitor. Expose CelUnparserVisitor to …
Browse files Browse the repository at this point in the history
…be inheritable.

PiperOrigin-RevId: 555569436
  • Loading branch information
l46kok authored and copybara-github committed Aug 10, 2023
1 parent a1a8fc8 commit 770b95c
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 483 deletions.
21 changes: 17 additions & 4 deletions parser/src/main/java/dev/cel/parser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ java_library(
tags = [
],
deps = [
"@cel_spec//proto/cel/expr:expr_java_proto",
"//common/ast",
"@maven//:com_google_guava_guava",
],
)
Expand Down Expand Up @@ -127,9 +127,22 @@ java_library(
tags = [
],
deps = [
"//checker:proto_expr_visitor",
"//parser:operator",
"@cel_spec//proto/cel/expr:expr_java_proto",
":unparser_visitor",
"//common",
"//common:options",
],
)

java_library(
name = "unparser_visitor",
srcs = ["CelUnparserVisitor.java"],
tags = [
],
deps = [
":operator",
"//common",
"//common/ast",
"//common/ast:cel_expr_visitor",
"@maven//:com_google_protobuf_protobuf_java",
],
)
19 changes: 10 additions & 9 deletions parser/src/main/java/dev/cel/parser/CelUnparser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@

package dev.cel.parser;

import dev.cel.expr.ParsedExpr;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelOptions;

/**
* Provides an unparsing utility that converts a ParsedExpr back into a human readable format.
* Provides an unparsing utility that converts an AST back into a human-readable format.
*
* <p>Input to the unparser is a ParsedExpr. The unparser does not do any checks to see if the
* ParsedExpr is syntactically or semantically correct but does checks enough to prevent its crash
* and might return errors in such cases.
* <p>Input to the unparser is a {@link CelAbstractSyntaxTree}. The unparser does not do any checks
* to see if the AST is syntactically or semantically correct but does check enough to prevent its
* crash and might return errors in such cases.
*/
public interface CelUnparser {

/**
* Unparses the {@link ParsedExpr} value to a human-readable string.
* Unparses the {@link CelAbstractSyntaxTree} value to a human-readable string.
*
* <p>For the best results ensure that the expression is parsed with ParserOptions.add_macro_calls
* = true.
* <p>To reconstruct an expression that originally contained a macro call, ensure the expression
* was parsed with {@link CelOptions#populateMacroCalls()} enabled.
*/
String unparse(ParsedExpr parsedExpr);
String unparse(CelAbstractSyntaxTree ast);
}
Loading

0 comments on commit 770b95c

Please sign in to comment.