Skip to content

Commit 0c1af97

Browse files
respencer-nclclaude
andcommitted
Replace jar-resource EBNF extraction with Grammar API tool
- Add Scala 3.7.4 main program (src/main/scala/ExtractGrammar.scala) that calls Grammar.loadEbnfGrammarOrThrow() from riddl-language - Add shell script (tools/extract-grammar.sh) to run it with java - Update build.sbt: extractGrammar task compiles and invokes the tool, Scala 3.7.4 via With.Scala3.configure(), riddl-language 1.7.0 - Remove project/ExtractEbnf.scala (old jar-resource approach) - Rename ebnf-grammar.ebnf → riddl-grammar.ebnf (it's a RIDDL grammar) - Upgrade sbt to 1.12.1 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f90910a commit 0c1af97

7 files changed

Lines changed: 151 additions & 193 deletions

File tree

build.sbt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,36 @@ lazy val developers: List[Developer] = List(
99
)
1010
)
1111

12-
// Task to extract EBNF grammar from riddl-language jar
13-
lazy val extractEbnf = taskKey[File]("Extract EBNF grammar from riddl-language jar if newer")
12+
lazy val extractGrammar = taskKey[Unit]("Extract RIDDL grammar via Grammar API")
1413

1514
lazy val root = Root(
1615
ghRepoName = "ossum-tech",
1716
ghOrgName = "ossuminc",
1817
startYr = 2025,
1918
devs = developers
2019
).configure(
21-
With.Scala3
20+
With.Scala3.configure(version = Some("3.7.4"))
2221
).settings(
2322
resolvers += "GitHub Package Registry" at "https://maven.pkg.github.com/ossuminc/riddl",
2423
libraryDependencies += "com.ossuminc" %% "riddl-language" % "1.7.0",
2524

26-
// Define the extractEbnf task - automatically triggers update via dependencyClasspath
27-
extractEbnf := ExtractEbnf(
28-
baseDirectory.value,
29-
(Compile / dependencyClasspath).value.files,
30-
streams.value.log
31-
),
32-
33-
// Automatically extract EBNF after update resolves dependencies
34-
update := {
35-
val updateReport = update.value
36-
ExtractEbnf.fromUpdateReport(
25+
// Extract RIDDL grammar by compiling and running ExtractGrammar
26+
extractGrammar := {
27+
(Compile / compile).value
28+
val log = streams.value.log
29+
val cp = (Runtime / fullClasspathAsJars).value
30+
.map(_.data.getAbsolutePath)
31+
.mkString(java.io.File.pathSeparator)
32+
val target = baseDirectory.value / "docs" / "riddl" / "references" / "riddl-grammar.ebnf"
33+
val script = baseDirectory.value / "tools" / "extract-grammar.sh"
34+
log.info("Extracting RIDDL grammar...")
35+
val exitCode = scala.sys.process.Process(
36+
Seq("bash", script.getAbsolutePath, target.getAbsolutePath),
3737
baseDirectory.value,
38-
updateReport,
39-
streams.value.log
40-
)
41-
updateReport
38+
"CLASSPATH" -> cp
39+
).!
40+
if (exitCode != 0) {
41+
throw new MessageOnlyException("Grammar extraction failed")
42+
}
4243
}
4344
)

docs/riddl/references/ebnf-grammar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ automatically extracted from the `riddl-language` library and kept in sync
77
with the reference grammar written in Scala/fastparse form.
88

99
```ebnf
10-
--8<-- "riddl/references/ebnf-grammar.ebnf"
10+
--8<-- "riddl/references/riddl-grammar.ebnf"
1111
```
Lines changed: 105 additions & 98 deletions
Large diffs are not rendered by default.

project/ExtractEbnf.scala

Lines changed: 0 additions & 75 deletions
This file was deleted.

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.10.7
1+
sbt.version=1.12.1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import com.ossuminc.riddl.language.Grammar
2+
import java.nio.file.{Files, Path}
3+
4+
@main def extractGrammar(outputPath: String): Unit =
5+
val ebnf = Grammar.loadEbnfGrammarOrThrow
6+
Files.writeString(Path.of(outputPath), ebnf)
7+
println(s"Wrote ${ebnf.length} chars to $outputPath")

tools/extract-grammar.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Extract the RIDDL grammar using Grammar.loadEbnfGrammarOrThrow().
5+
#
6+
# Usage:
7+
# CLASSPATH=<jars> ./tools/extract-grammar.sh <output-file>
8+
#
9+
# Called by `sbt extractGrammar` which sets CLASSPATH automatically.
10+
11+
OUTPUT="${1:?Usage: extract-grammar.sh <output-file>}"
12+
13+
if [ -z "${CLASSPATH:-}" ]; then
14+
echo "Error: CLASSPATH not set. Run via 'sbt extractGrammar' instead." >&2
15+
exit 1
16+
fi
17+
18+
java -cp "$CLASSPATH" extractGrammar "$OUTPUT"

0 commit comments

Comments
 (0)