The RuleBasedParser
is a fast and sophisticated parser that is at the core of Umple.
It reads in grammar files written in a modified/extended EBNF syntax and produces rules that can be used to parse files written in the corresponding language.
Currently, this can only be compiled as part of the main Umple build. This will be changed in the future to be an entirely independently compilable project with a separate ant build target.
The umple source is available in src/
, and -- once compiled -- the java source is available in src-gen-umple/
Using the RuleBasedParser is simple:
-
Import the package for the RuleBasedParser In Umple this is
depend cruise.umple.parser.analysis.RuleBasedParser
. In Java, justimport cruise.umple.parser.analysis.RuleBasedParser
-
Construct a parser with
RuleBasedParser rbp = new RuleBasedParser()
-
Read in any necessary grammar files for the parser with
rbp.addGrammarFile("<filepath>");
The grammar file filepath is relative to the root java package. -
[Optional] Assign a handler for linking files. To do this, call
rbp.setLikedFileHandler(...)
and pass a class that implementsLinkedFileHandler
-
[Optional] Assign a handler for generating analyzers. To do this, call
rbp.setAnalyzerGenerator(...)
and pass a class that implementsAnalyzerGeneratorHandler
-
[Optional] Create parser actions for specific tokens with
rbp.addParserAction(...)
, passing it the string action name and a class that implementsParserAction
-
Create a
File
object of the file to parse (e.g.File file = new File("somefile");
-
Parse the file with
rbp.parse( file )
. To retrieve the tokens from the parse, userbp.getRootToken()