Skip to content

Commit 8d72394

Browse files
author
Max Vorobev
authored
Support generating ANTLR Rust code (#318)
## What is the goal of this PR? Allow generating Rust code in ANTLR ## What are the changes implemented in this PR? * Upgrade `rules_antlr` to our fork with support for Rust codegen * Load `antlr-rust` library from Crates.io * Add `rust_grammar_adapter` to modify grammar to avoid using `type` (which is a keyword in Rust)
1 parent 69d1fb1 commit 8d72394

36 files changed

+2617
-3
lines changed

builder/antlr/deps.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
33
def deps():
44
git_repository(
55
name = "rules_antlr",
6-
remote = "https://github.com/marcohu/rules_antlr",
7-
tag = "0.5.0"
6+
remote = "https://github.com/vaticle/rules_antlr",
7+
branch = "0.5.0-rust"
88
)
99

10-
antlr_version = "4.7.2"
10+
antlr_version = "4.8.2-rust"

builder/antlr/rules.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@ def python_grammar_adapter(
99
cmd_bash = "$(location @vaticle_dependencies//builder/antlr:grammar-adapter) --in $< --out $@ --adapt-keyword type --adapt-keyword filter",
1010
tools = ["@vaticle_dependencies//builder/antlr:grammar-adapter"],
1111
)
12+
13+
def rust_grammar_adapter(
14+
name,
15+
input,
16+
output):
17+
native.genrule(
18+
name = name,
19+
srcs = [input],
20+
outs = [output],
21+
cmd_bash = "$(location @vaticle_dependencies//builder/antlr:grammar-adapter) --in $< --out $@ --adapt-keyword type",
22+
tools = ["@vaticle_dependencies//builder/antlr:grammar-adapter"],
23+
)

library/crates/BUILD.bazel

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
@generated
3+
cargo-raze generated Bazel file.
4+
5+
DO NOT EDIT! Replaced on runs of cargo-raze
6+
"""
7+
8+
package(default_visibility = ["//visibility:public"])
9+
10+
licenses([
11+
"notice", # See individual crates for specific licenses
12+
])
13+
14+
# Aliased targets
15+
alias(
16+
name = "antlr_rust",
17+
actual = "@raze__antlr_rust__0_2_0//:antlr_rust",
18+
tags = [
19+
"cargo-raze",
20+
"manual",
21+
],
22+
)
23+
24+
# Export file for Stardoc support
25+
exports_files(
26+
[
27+
"crates.bzl",
28+
],
29+
visibility = ["//visibility:public"],
30+
)

library/crates/Cargo.raze.lock

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
[[package]]
4+
name = "antlr-rust"
5+
version = "0.2.0"
6+
source = "registry+https://github.com/rust-lang/crates.io-index"
7+
checksum = "31cef0bab9c69cb4edd4764cd227b0db357a5634ad5a9200f9fb0a8d32e50947"
8+
dependencies = [
9+
"better_any",
10+
"bit-set",
11+
"byteorder",
12+
"lazy_static",
13+
"murmur3",
14+
"once_cell",
15+
"parking_lot",
16+
"typed-arena",
17+
"uuid",
18+
]
19+
20+
[[package]]
21+
name = "better_any"
22+
version = "0.1.1"
23+
source = "registry+https://github.com/rust-lang/crates.io-index"
24+
checksum = "b359aebd937c17c725e19efcb661200883f04c49c53e7132224dac26da39d4a0"
25+
dependencies = [
26+
"better_typeid_derive",
27+
]
28+
29+
[[package]]
30+
name = "better_typeid_derive"
31+
version = "0.1.1"
32+
source = "registry+https://github.com/rust-lang/crates.io-index"
33+
checksum = "3deeecb812ca5300b7d3f66f730cc2ebd3511c3d36c691dd79c165d5b19a26e3"
34+
dependencies = [
35+
"proc-macro2",
36+
"quote",
37+
"syn",
38+
]
39+
40+
[[package]]
41+
name = "bit-set"
42+
version = "0.5.2"
43+
source = "registry+https://github.com/rust-lang/crates.io-index"
44+
checksum = "6e11e16035ea35e4e5997b393eacbf6f63983188f7a2ad25bfb13465f5ad59de"
45+
dependencies = [
46+
"bit-vec",
47+
]
48+
49+
[[package]]
50+
name = "bit-vec"
51+
version = "0.6.3"
52+
source = "registry+https://github.com/rust-lang/crates.io-index"
53+
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
54+
55+
[[package]]
56+
name = "bitflags"
57+
version = "1.3.2"
58+
source = "registry+https://github.com/rust-lang/crates.io-index"
59+
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
60+
61+
[[package]]
62+
name = "byteorder"
63+
version = "1.4.3"
64+
source = "registry+https://github.com/rust-lang/crates.io-index"
65+
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
66+
67+
[[package]]
68+
name = "cfg-if"
69+
version = "1.0.0"
70+
source = "registry+https://github.com/rust-lang/crates.io-index"
71+
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
72+
73+
[[package]]
74+
name = "instant"
75+
version = "0.1.12"
76+
source = "registry+https://github.com/rust-lang/crates.io-index"
77+
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
78+
dependencies = [
79+
"cfg-if",
80+
]
81+
82+
[[package]]
83+
name = "lazy_static"
84+
version = "1.4.0"
85+
source = "registry+https://github.com/rust-lang/crates.io-index"
86+
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
87+
88+
[[package]]
89+
name = "libc"
90+
version = "0.2.106"
91+
source = "registry+https://github.com/rust-lang/crates.io-index"
92+
checksum = "a60553f9a9e039a333b4e9b20573b9e9b9c0bb3a11e201ccc48ef4283456d673"
93+
94+
[[package]]
95+
name = "lock_api"
96+
version = "0.4.5"
97+
source = "registry+https://github.com/rust-lang/crates.io-index"
98+
checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109"
99+
dependencies = [
100+
"scopeguard",
101+
]
102+
103+
[[package]]
104+
name = "murmur3"
105+
version = "0.4.1"
106+
source = "registry+https://github.com/rust-lang/crates.io-index"
107+
checksum = "a198f9589efc03f544388dfc4a19fe8af4323662b62f598b8dcfdac62c14771c"
108+
dependencies = [
109+
"byteorder",
110+
]
111+
112+
[[package]]
113+
name = "once_cell"
114+
version = "1.8.0"
115+
source = "registry+https://github.com/rust-lang/crates.io-index"
116+
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
117+
118+
[[package]]
119+
name = "parking_lot"
120+
version = "0.11.2"
121+
source = "registry+https://github.com/rust-lang/crates.io-index"
122+
checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
123+
dependencies = [
124+
"instant",
125+
"lock_api",
126+
"parking_lot_core",
127+
]
128+
129+
[[package]]
130+
name = "parking_lot_core"
131+
version = "0.8.5"
132+
source = "registry+https://github.com/rust-lang/crates.io-index"
133+
checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216"
134+
dependencies = [
135+
"cfg-if",
136+
"instant",
137+
"libc",
138+
"redox_syscall",
139+
"smallvec",
140+
"winapi",
141+
]
142+
143+
[[package]]
144+
name = "proc-macro2"
145+
version = "1.0.32"
146+
source = "registry+https://github.com/rust-lang/crates.io-index"
147+
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43"
148+
dependencies = [
149+
"unicode-xid",
150+
]
151+
152+
[[package]]
153+
name = "quote"
154+
version = "1.0.10"
155+
source = "registry+https://github.com/rust-lang/crates.io-index"
156+
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
157+
dependencies = [
158+
"proc-macro2",
159+
]
160+
161+
[[package]]
162+
name = "redox_syscall"
163+
version = "0.2.10"
164+
source = "registry+https://github.com/rust-lang/crates.io-index"
165+
checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
166+
dependencies = [
167+
"bitflags",
168+
]
169+
170+
[[package]]
171+
name = "scopeguard"
172+
version = "1.1.0"
173+
source = "registry+https://github.com/rust-lang/crates.io-index"
174+
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
175+
176+
[[package]]
177+
name = "smallvec"
178+
version = "1.7.0"
179+
source = "registry+https://github.com/rust-lang/crates.io-index"
180+
checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309"
181+
182+
[[package]]
183+
name = "syn"
184+
version = "1.0.81"
185+
source = "registry+https://github.com/rust-lang/crates.io-index"
186+
checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966"
187+
dependencies = [
188+
"proc-macro2",
189+
"quote",
190+
"unicode-xid",
191+
]
192+
193+
[[package]]
194+
name = "typed-arena"
195+
version = "2.0.1"
196+
source = "registry+https://github.com/rust-lang/crates.io-index"
197+
checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae"
198+
199+
[[package]]
200+
name = "unicode-xid"
201+
version = "0.2.2"
202+
source = "registry+https://github.com/rust-lang/crates.io-index"
203+
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
204+
205+
[[package]]
206+
name = "uuid"
207+
version = "0.8.2"
208+
source = "registry+https://github.com/rust-lang/crates.io-index"
209+
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
210+
211+
[[package]]
212+
name = "vaticle-dependencies"
213+
version = "0.0.0"
214+
dependencies = [
215+
"antlr-rust",
216+
]
217+
218+
[[package]]
219+
name = "winapi"
220+
version = "0.3.9"
221+
source = "registry+https://github.com/rust-lang/crates.io-index"
222+
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
223+
dependencies = [
224+
"winapi-i686-pc-windows-gnu",
225+
"winapi-x86_64-pc-windows-gnu",
226+
]
227+
228+
[[package]]
229+
name = "winapi-i686-pc-windows-gnu"
230+
version = "0.4.0"
231+
source = "registry+https://github.com/rust-lang/crates.io-index"
232+
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
233+
234+
[[package]]
235+
name = "winapi-x86_64-pc-windows-gnu"
236+
version = "0.4.0"
237+
source = "registry+https://github.com/rust-lang/crates.io-index"
238+
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

library/crates/Cargo.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[package]
2+
name = "vaticle-dependencies"
3+
version = "0.0.0"
4+
5+
[lib]
6+
path = "" # ignored by cargo raze
7+
8+
[dependencies]
9+
antlr-rust = "0.2.0"
10+
11+
[package.metadata.raze]
12+
# The path at which to write output files.
13+
#
14+
# `cargo raze` will generate Bazel-compatible BUILD files into this path.
15+
# This can either be a relative path (e.g. "foo/bar"), relative to this
16+
# Cargo.toml file; or relative to the Bazel workspace root (e.g. "//foo/bar").
17+
workspace_path = "//library/crates"
18+
19+
# This causes aliases for dependencies to be rendered in the BUILD
20+
# file located next to this `Cargo.toml` file.
21+
package_aliases_dir = "."
22+
23+
# The set of targets to generate BUILD rules for.
24+
targets = [
25+
"x86_64-apple-darwin",
26+
"x86_64-pc-windows-msvc",
27+
"x86_64-unknown-linux-gnu",
28+
]
29+
30+
# The two acceptable options are "Remote" and "Vendored" which
31+
# is used to indicate whether the user is using a non-vendored or
32+
# vendored set of dependencies.
33+
genmode = "Remote"

0 commit comments

Comments
 (0)