antlr -- http://www.antlr.org/ -- was what I wanted.
Define schema / grammar and parse and instance of it
Question
I'm wondering that is there a Java lib out there which I can use to define a schema and then if I have an instance of that schema, parse it?
I mean like the schema would be looking something like this:
string: [a-zA-Z]+
number: [1-9][0-9]*
attribute: "attribute " name: string " " type: string :: MyParser.newAttribute(name, type)
node: "node " name: string " {" :: MyParser.newNode($name)
attribute+
"}" :: MyParser.closeNode()
node+
And then an instance of the schema would be looking like this:
node MyNode {
attribute myAttribute1 Int
attribute myAttribute2 Long
}
node SecondNode {
attribute x String
}
And the parser would be generated or I don't know, but it would work like this:
- Whenever it reaches a node line it the MyParser.newNode method would be called
- Whenever it reaches an attribute line, the MyParser.newAttribute would be called
- And whenever it reaches a } line the MyParser.closeNode would be called
Of course I must garantue that the MyParse class with those static methods will exist in my parser project.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow