Skip to content

Commit 7c6a32a

Browse files
committed
Add an access function for Gbnf Grammar
1 parent 91028b4 commit 7c6a32a

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

  • language/shared/src/main/scala/com/ossuminc/riddl/language

language/shared/src/main/scala/com/ossuminc/riddl/language/Grammar.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import scala.util.{Try, Using}
1111

1212
/** Provides access to the RIDDL grammar resources bundled with this module.
1313
*
14-
* The EBNF grammar is included as a classpath resource and can be loaded
15-
* by any project that depends on riddl-language.
14+
* The EBNF grammar is included as a classpath resource and can be loaded by any project that
15+
* depends on riddl-language.
1616
*/
1717
object Grammar:
1818

1919
/** The classpath location of the EBNF grammar file */
2020
val EbnfResourcePath: String = "riddl/grammar/ebnf-grammar.ebnf"
21+
val GbnfResourcePath: String = "riddl/grammar/riddl-grammar.gbnf"
2122

2223
/** Load the EBNF grammar from the classpath.
2324
*
@@ -47,4 +48,20 @@ object Grammar:
4748
case Right(grammar) => grammar
4849
case Left(error) => throw new RuntimeException(error)
4950

50-
end Grammar
51+
def loadGbnfGrammar: Either[String, String] =
52+
val classLoader = getClass.getClassLoader
53+
Option(classLoader.getResourceAsStream(GbnfResourcePath)) match
54+
case Some(stream) =>
55+
Using(Source.fromInputStream(stream, "UTF-8")) { source =>
56+
source.mkString
57+
}.toEither.left.map(_.getMessage)
58+
case None =>
59+
Left(s"Grammar resource not found: $GbnfResourcePath")
60+
end loadGbnfGrammar
61+
62+
def loadGbnfGrammarOrThrow: String =
63+
loadGbnfGrammar match
64+
case Right(grammar) => grammar
65+
case Left(error) => throw new RuntimeException(error)
66+
67+
end Grammar

0 commit comments

Comments
 (0)