Skip to content

Commit 54110ac

Browse files
committed
Fix Optimized ANTLR dependency loading
1 parent 29f9bbe commit 54110ac

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

antlr/repositories.bzl

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ v2 = [2, "2.7.7"]
1010
def rules_antlr_dependencies(*versionsAndLanguages):
1111
"""Loads the dependencies for the specified ANTLR releases.
1212
13+
You have to provide at least the version number of the ANTLR release you want to use. To
14+
load the dependencies for languages besides Java, you have to indicate the languages as well.
15+
16+
```python
17+
load("@rules_antlr//antlr:lang.bzl", "CPP", "PYTHON")
18+
load("@rules_antlr//antlr:repositories.bzl", "rules_antlr_dependencies")
19+
20+
rules_antlr_dependencies("4.7.2", CPP, PYTHON)
21+
```
22+
1323
Args:
1424
*versionsAndLanguages: the ANTLR release versions to make available for the provided target languages.
1525
"""
@@ -41,34 +51,37 @@ def rules_antlr_dependencies(*versionsAndLanguages):
4151
languages = [JAVA]
4252

4353
for version in sorted(versions, key = _toString):
44-
if (version == 4 or version == "4.7.2"):
54+
if version == 4 or version == "4.7.2":
4555
_antlr472_dependencies(languages)
46-
elif (version == "4.7.1"):
56+
elif version == "4.7.1":
4757
_antlr471_dependencies(languages)
48-
elif (version == 3 or version == "3.5.2"):
58+
elif version == 3 or version == "3.5.2":
4959
_antlr352_dependencies(languages)
50-
elif (version == 2 or version == "2.7.7"):
60+
elif version == 2 or version == "2.7.7":
5161
_antlr277_dependencies(languages)
5262
else:
5363
fail("Missing ANTLR version", attr = "versionsAndLanguages")
5464

55-
def rules_antlr_optimized_dependencies(*versions):
65+
def rules_antlr_optimized_dependencies(version):
5666
"""Loads the dependencies for the "optimized" fork of ANTLR 4 maintained by Sam Harwell.
5767
68+
```python
69+
load("@rules_antlr//antlr:repositories.bzl", "rules_antlr_optimized_dependencies")
70+
71+
rules_antlr_optimized_dependencies("4.7.2")
72+
```
73+
5874
Args:
59-
*versions: the ANTLR releases versions to make available.
75+
version: the ANTLR release version to make available.
6076
"""
61-
if versions:
62-
versions = sorted(versions)
63-
for version in versions:
64-
if (version == 4 or version == 472):
65-
_antlr472_optimized_dependencies()
66-
elif (version == 471):
67-
_antlr471_optimized_dependencies()
68-
else:
69-
fail("Invalid ANTLR version provided: {0}. Currently supported are: {1}".format(version, v4), attr = "versions")
70-
else:
77+
if version == 4 or version == "4.7.2":
7178
_antlr472_optimized_dependencies()
79+
elif version == "4.7.1":
80+
_antlr471_optimized_dependencies()
81+
elif type(version) == "int" or str(version).isdigit():
82+
fail('Integer version \'{}\' no longer valid. Use semantic version "{}" instead.'.format(version, ".".join(str(version).elems())), attr = "version")
83+
else:
84+
fail('Unsupported ANTLR version provided: "{0}". Currently supported are: {1}'.format(version, v4), attr = "version")
7285

7386
def _antlr472_dependencies(languages):
7487
_antlr4_dependencies(

src/it/java/org/antlr/bazel/RepositoriesTest.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void missingVersion() throws Exception
3333
+ "rules_antlr_dependencies()";
3434

3535
TestWorkspace workspace = new TestWorkspace(true);
36-
Path file = workspace.file("WORKSPACE", contents);
36+
workspace.file("WORKSPACE", contents);
3737
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
3838
assertEquals(c.output(), 1, c.exitValue());
3939
assertTrue(c.output().contains("attribute versionsAndLanguages: Missing ANTLR version"));
@@ -51,7 +51,7 @@ public void languageAndMissingVersion() throws Exception
5151
+ "rules_antlr_dependencies(\"Java\")";
5252

5353
TestWorkspace workspace = new TestWorkspace(true);
54-
Path file = workspace.file("WORKSPACE", contents);
54+
workspace.file("WORKSPACE", contents);
5555
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
5656
assertEquals(c.output(), 1, c.exitValue());
5757
assertTrue(c.output().contains("attribute versionsAndLanguages: Missing ANTLR version"));
@@ -69,10 +69,23 @@ public void unsupportedVersion() throws Exception
6969
+ "rules_antlr_dependencies(\"4.0\")";
7070

7171
TestWorkspace workspace = new TestWorkspace(true);
72-
Path file = workspace.file("WORKSPACE", contents);
72+
workspace.file("WORKSPACE", contents);
7373
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
7474
assertEquals(c.output(), 1, c.exitValue());
7575
assertTrue(c.output().contains("attribute versionsAndLanguages: Unsupported ANTLR version provided: \"4.0\"."));
76+
77+
contents = "workspace(name=\"examples\")\n"
78+
+ "local_repository(\n"
79+
+ " name = \"rules_antlr\",\n"
80+
+ " path = \"../../../rules_antlr\",\n"
81+
+ ")\n"
82+
+ "load(\"@rules_antlr//antlr:repositories.bzl\", \"rules_antlr_optimized_dependencies\")\n"
83+
+ "rules_antlr_optimized_dependencies(\"4.0\")";
84+
85+
workspace.file("WORKSPACE", contents);
86+
c = new Command(workspace.root, "//antlr4/HelloWorld/...").build();
87+
assertEquals(c.output(), 1, c.exitValue());
88+
assertTrue(c.output().contains("attribute version: Unsupported ANTLR version provided: \"4.0\"."));
7689
}
7790

7891
@Test
@@ -87,10 +100,23 @@ public void invalidVersion() throws Exception
87100
+ "rules_antlr_dependencies(471)";
88101

89102
TestWorkspace workspace = new TestWorkspace(true);
90-
Path file = workspace.file("WORKSPACE", contents);
103+
workspace.file("WORKSPACE", contents);
91104
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
92105
assertEquals(c.output(), 1, c.exitValue());
93106
assertTrue(c.output().contains("attribute versionsAndLanguages: Integer version '471' no longer valid. Use semantic version \"4.7.1\" instead."));
107+
108+
contents = "workspace(name=\"examples\")\n"
109+
+ "local_repository(\n"
110+
+ " name = \"rules_antlr\",\n"
111+
+ " path = \"../../../rules_antlr\",\n"
112+
+ ")\n"
113+
+ "load(\"@rules_antlr//antlr:repositories.bzl\", \"rules_antlr_optimized_dependencies\")\n"
114+
+ "rules_antlr_optimized_dependencies(471)";
115+
116+
workspace.file("WORKSPACE", contents);
117+
c = new Command(workspace.root, "//antlr4/HelloWorld/...").build();
118+
assertEquals(c.output(), 1, c.exitValue());
119+
assertTrue(c.output().contains("attribute version: Integer version '471' no longer valid. Use semantic version \"4.7.1\" instead."));
94120
}
95121

96122
@Test
@@ -105,7 +131,7 @@ public void invalidLanguage() throws Exception
105131
+ "rules_antlr_dependencies(4, \"Haskell\")";
106132

107133
TestWorkspace workspace = new TestWorkspace(true);
108-
Path file = workspace.file("WORKSPACE", contents);
134+
workspace.file("WORKSPACE", contents);
109135
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
110136
assertEquals(c.output(), 1, c.exitValue());
111137
assertTrue(c.output().contains("attribute versionsAndLanguages: Invalid language provided: \"Haskell\"."));
@@ -123,7 +149,7 @@ public void severalVersions() throws Exception
123149
+ "rules_antlr_dependencies(\"4.7.1\", \"4.7.2\")";
124150

125151
TestWorkspace workspace = new TestWorkspace(true);
126-
Path file = workspace.file("WORKSPACE", contents);
152+
workspace.file("WORKSPACE", contents);
127153
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
128154
assertEquals(c.output(), 1, c.exitValue());
129155
assertTrue(c.output().contains("attribute versionsAndLanguages: You can only load one version from ANTLR 4. You specified both \"4.7.1\" and \"4.7.2\"."));
@@ -141,7 +167,7 @@ public void missingLanguage() throws Exception
141167
+ "rules_antlr_dependencies(\"2.7.7\")";
142168

143169
TestWorkspace workspace = new TestWorkspace(true);
144-
Path file = workspace.file("WORKSPACE", contents);
170+
workspace.file("WORKSPACE", contents);
145171
Command c = new Command(workspace.root, "//antlr2/Cpp/...").build();
146172
assertEquals(c.output(), 1, c.exitValue());
147173
}
@@ -158,7 +184,7 @@ public void alwaysLoadJavaDependencies() throws Exception
158184
+ "rules_antlr_dependencies(\"2.7.7\", \"Cpp\")";
159185

160186
TestWorkspace workspace = new TestWorkspace(true);
161-
Path file = workspace.file("WORKSPACE", contents);
187+
workspace.file("WORKSPACE", contents);
162188
Command c = new Command(workspace.root, "//antlr2/Cpp/...").build();
163189
assertEquals(c.output(), 0, c.exitValue());
164190
}

0 commit comments

Comments
 (0)