Skip to content
New issue

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

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

Already on GitHub? # to your account

antlr: drop rules_antlr for custom rules #153

Merged
merged 1 commit into from
Jun 2, 2022
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
8 changes: 8 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
package(default_visibility = ["//visibility:public"])

load("@rules_java//java:defs.bzl", "java_binary")

java_binary(
name = "antlr4_tool",
runtime_deps = ["@antlr4_jar//jar"],
main_class = "org.antlr.v4.Tool",
)
73 changes: 63 additions & 10 deletions bazel/antlr.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,18 @@
Generate C++ parser and lexer from a grammar file.
"""

load("@rules_antlr//antlr:antlr4.bzl", "antlr")

def antlr_cc_library(name, src, package = None, listener = False, visitor = True):
def antlr_cc_library(name, src, package):
"""Creates a C++ lexer and parser from a source grammar.

Args:
name: Base name for the lexer and the parser rules.
src: source ANTLR grammar file
package: The namespace for the generated code
listener: generate ANTLR listener (default: False)
visitor: generate ANTLR visitor (default: True)
"""
generated = name + "_grammar"
antlr(
antlr_library(
name = generated,
srcs = [src],
language = "Cpp",
listener = listener,
visitor = visitor,
src = src,
package = package,
)
native.cc_library(
Expand All @@ -46,3 +39,63 @@ def antlr_cc_library(name, src, package = None, listener = False, visitor = True
],
linkstatic = 1,
)

def _antlr_library(ctx):
output = ctx.actions.declare_directory(ctx.attr.name)

antlr_args = ctx.actions.args()
antlr_args.add("-Dlanguage=Cpp")
antlr_args.add("-no-listener")
antlr_args.add("-visitor")
antlr_args.add("-o", output.path)
antlr_args.add("-package", ctx.attr.package)
antlr_args.add(ctx.file.src)

basename = ctx.file.src.basename[:-3]
suffixes = ["Lexer", "Parser", "BaseVisitor", "Visitor"]

ctx.actions.run(
arguments = [antlr_args],
inputs = [ctx.file.src],
outputs = [output],
executable = ctx.executable._tool,
progress_message = "Processing ANTLR grammar",
)

files = []
for suffix in suffixes:
header = ctx.actions.declare_file(basename + suffix + ".h")
source = ctx.actions.declare_file(basename + suffix + ".cpp")
generated = output.path + "/" + ctx.file.src.short_path[:-3] + suffix

ctx.actions.run_shell(
mnemonic = "CopyHeader" + suffix,
inputs = [output],
outputs = [header],
command = 'cp "{generated}" "{out}"'.format(generated = generated + ".h", out = header.path),
)
ctx.actions.run_shell(
mnemonic = "CopySource" + suffix,
inputs = [output],
outputs = [source],
command = 'cp "{generated}" "{out}"'.format(generated = generated + ".cpp", out = source.path),
)

files.append(header)
files.append(source)

compilation_context = cc_common.create_compilation_context(headers = depset(files))
return [DefaultInfo(files = depset(files)), CcInfo(compilation_context = compilation_context)]

antlr_library = rule(
implementation = _antlr_library,
attrs = {
"src": attr.label(allow_single_file = [".g4"], mandatory = True),
"package": attr.string(),
"_tool": attr.label(
executable = True,
cfg = "host",
default = Label("//bazel:antlr4_tool"),
),
},
)
23 changes: 11 additions & 12 deletions bazel/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Main dependencies of cel-cpp.
"""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_jar")

def base_deps():
"""Base evaluator and test dependencies."""
Expand Down Expand Up @@ -69,15 +69,9 @@ def base_deps():

def parser_deps():
"""ANTLR dependency for the parser."""
http_archive(
name = "rules_antlr",
sha256 = "26e6a83c665cf6c1093b628b3a749071322f0f70305d12ede30909695ed85591",
strip_prefix = "rules_antlr-0.5.0",
urls = ["https://github.com/marcohu/rules_antlr/archive/0.5.0.tar.gz"],
)
# Apr 15, 2022
ANTLR4_VERSION = "4.10.1"

ANTLR4_RUNTIME_GIT_SHA = "70b2edcf98eb612a92d3dbaedb2ce0b69533b0cb" # Dec 7, 2021
ANTLR4_RUNTIME_SHA = "fae73909f95e1320701e29ac03bab9233293fb5b90d3ce857279f1b46b614c83"
http_archive(
name = "antlr4_runtimes",
build_file_content = """
Expand All @@ -89,9 +83,14 @@ cc_library(
includes = ["runtime/Cpp/runtime/src"],
)
""",
sha256 = ANTLR4_RUNTIME_SHA,
strip_prefix = "antlr4-" + ANTLR4_RUNTIME_GIT_SHA,
urls = ["https://github.com/antlr/antlr4/archive/" + ANTLR4_RUNTIME_GIT_SHA + ".tar.gz"],
sha256 = "a320568b738e42735946bebc5d9d333170e14a251c5734e8b852ad1502efa8a2",
strip_prefix = "antlr4-" + ANTLR4_VERSION,
urls = ["https://github.com/antlr/antlr4/archive/v" + ANTLR4_VERSION + ".tar.gz"],
)
http_jar(
name = "antlr4_jar",
urls = ["https://www.antlr.org/download/antlr-" + ANTLR4_VERSION + "-complete.jar"],
sha256 = "41949d41f20d31d5b8277187735dd755108df52b38db6c865108d3382040f918",
)

def flatbuffers_deps():
Expand Down
2 changes: 0 additions & 2 deletions bazel/deps_extra.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Transitive dependencies.

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
load("@rules_antlr//antlr:repositories.bzl", "rules_antlr_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

Expand Down Expand Up @@ -50,5 +49,4 @@ def cel_cpp_deps_extra():
cc = True,
go = True, # cel-spec requirement
)
rules_antlr_dependencies("4.8")
cel_spec_deps_extra()
6 changes: 3 additions & 3 deletions parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
#include "internal/strings.h"
#include "internal/unicode.h"
#include "internal/utf8.h"
#include "parser/internal/cel_grammar.inc/cel_parser_internal/CelBaseVisitor.h"
#include "parser/internal/cel_grammar.inc/cel_parser_internal/CelLexer.h"
#include "parser/internal/cel_grammar.inc/cel_parser_internal/CelParser.h"
#include "parser/internal/CelBaseVisitor.h"
#include "parser/internal/CelLexer.h"
#include "parser/internal/CelParser.h"
#include "parser/macro.h"
#include "parser/options.h"
#include "parser/source_factory.h"
Expand Down
2 changes: 1 addition & 1 deletion parser/source_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "antlr4-runtime.h"
#include "parser/internal/cel_grammar.inc/cel_parser_internal/CelParser.h"
#include "parser/internal/CelParser.h"

namespace google::api::expr::parser {

Expand Down