@@ -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 */
1717object 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