[antlr-interest] fado's & DebugEventListener
Jason Osgood
jason at jasonosgood.com
Tue Nov 29 17:18:20 PST 2011
Hi Terr, Everyone.
Long time.
I'm loving ANTLR 3.2. I'm rejoined this list to share some of my
recent work and ask a question.
My fado project reads (native) SQL and code generates JDBC
PreparedStatement wrappers.
Very small example. Imagine your app uses this query:
SELECT name FROM People WHERE age = 40;
Your PreparedStatement query text would look like this:
SELECT name FROM People WHERE age = ?;
And your ResultSet wrapper (sometimes called a DAO) would have methods
getName() and setAgeParam( int age ).
Pretty straight forward.
What fado does is generate those instrumented JDBC wrappers for you.
Pretty awesome.
Over the years, I tried all sorts of strategies. Builders, subclassing
a special Building parser, trying to use ASTs and tree writing, etc.
Long story short, I'm now using my own DebugEventListener to build a
tree of my ParseNodes. Then I use a lightweight XPath-like expression
evaluator to drill down that parse tree to identify just the bits I
care about, ignoring the rest.
For example, the expression to find all the table references looks
kinda like rootNode.findNodes(
"statement/select/from/fromItem/tableRef/tableName" ). Note the
components of the xpath are my grammar's rule names. So this
xpath-like drill down strategy could work for any grammar.
When I find a literal used in a SQL WHERE condition, I replace the
token's text with a JDBC '?' parameter placeholder.
Fado uses normal SQL source code as a template, but without any
template syntax. Inspired by your talks about using ANTLR to do source
code translation.
Any way. I need to find a better hook than DebugEventListener. Its API
changed 3.2->3.4. And ANTLR requires a command line option to generate
parsers with debug events.
Ideas?
Cheers, Jason
PS- Fado is at http://code.google.com/p/fado/, it's not quite ready
for prime time yet, people can look it over.
More information about the antlr-interest
mailing list