Skip to content

Commit

Permalink
Add a genrule for cup (#442)
Browse files Browse the repository at this point in the history
* Add a genrule for cup
  - Update readme for cup
  - Fix sample
  • Loading branch information
regisd authored Oct 13, 2018
1 parent 1769baa commit 332c34a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cup/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ package(default_visibility = ["//visibility:public"])

licenses(["notice"]) # GPL-compatible

java_binary(
name = "cup_bin",
main_class = "java_cup.Main",
runtime_deps = [
":cup",
":cup_runtime",
],
)

java_import(
name = "cup",
jars = ["cup/target/cup-11b.jar"],
Expand Down
11 changes: 11 additions & 0 deletions cup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ Home directory and parent POM of 2 artefacts:
Note that this code is excluded from many rules of our codebase,
for instance google-java-format is not applied.


## Bazel rule

load("//cup:cup.bzl", "cup")

cup(
name = "rule_name",
src = "spec.cup",
)


[cup]: http://www2.cs.tum.edu/projects/cup/
24 changes: 24 additions & 0 deletions cup/cup.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Bazel rules for cup. """

# CUP can only read from stdin, which Skylark rules don't support. Use a genrule for now.
def cup(name, src, parser = "Parser", symbols = "Symbols", interface = False):
"""Generate a parser with CUP.
Args:
name: name of the rule
srcs: list of cup specifications
parser: name of the generated parser class
symbols: name of the generated symbols class
interface: whether to generate an interface
"""
opts = "-parser {parser} -symbols {symbols}".format(parser = parser, symbols = symbols)
if interface:
opts = opts + " -interface"
cmd = ("$(location //cup:cup_bin) -destdir $(@D) " + opts + " < $<")
native.genrule(
name = name,
srcs = [src],
tools = ["//cup:cup_bin"],
outs = [parser + ".java", symbols + ".java"],
cmd = cmd,
)
6 changes: 6 additions & 0 deletions cup/sample-project/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//cup:cup.bzl", "cup")

cup(
name = "gen_parser",
src = "src/main/cup/calculator.cup",
)

0 comments on commit 332c34a

Please sign in to comment.