HQL Parser: Progress
I still have recursive headache, but I think that I managed to grok how this works. To the point where I can now follow errors in the grammar and actually fix them. I was very surprised (pleasantly so) that the output from ANTLR is actually debuggable and make sense.
At any rate, I now got this test to pass, which is very encouraging:
[Test] public void ComplexWhere_AlternativeCase() { Query q = Parse("from Customer c where 1 = (case c.Name when 'ayende' then 1 else 0 end)"); BinaryExpression expression = (BinaryExpression) q.WhereClause.Expression; Assert.AreEqual(BinaryOperatorType.Eq, expression.Operator); Assert.AreEqual("1", ((ConstantExpression) expression.Left).Value); CaseExpression caseExpr = (CaseExpression) expression.Right; Assert.AreEqual("c.Name", caseExpr.ConditionalExpression.ToString()); AlternativeWhenExpression whenClause = (AlternativeWhenExpression) caseExpr.Conditions[0]; Assert.AreEqual("'ayende'",((ConstantExpression) whenClause.Value).Value); Assert.AreEqual("1",((ConstantExpression)whenClause.Then).Value); Assert.AreEqual("0", ((ConstantExpression) caseExpr.ElseClause).Value); }
About the whole point of this exercise, beyond sharpening my parsing skills, is to allow an AST based HQL query translator. Right now the HQL query translator for NHibernate is an... item of historical interest. Very hard to modify and change, so that is a high priority item for making NHibernate gives us better error messages and easier extensibility. The end goal of having a unified AST for all the query capabilities, HQL, Criteria and Linq, but that is a long way yet.
My main goal is to have reasonable error messages, because while it is gratifying to know that "in expected" means that you misspelled a class name, it is usually preferred to have a more accurate error message.
As before, you can get the Hql Parser source here: svn co https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/experiments/Hql
Note: I am keeping it there until it is matured enough to move it to the NHibernate's trunk.
Next, I need to figure out how to get parser error messages from ANTLR, right now I can't seem to manage to get reasonable errors for things like:
from Customer where
It simply reports a no viable alternative, which is a very vague error message. To be exact, it reports: "line 0:-1 no viable alternative at input '<EOF>'" which is no help at all. It is not even the correct location from the point of view of the user (from the parser point of view, it backtracked to try to find a better solution, failed, and then reported from the backtracked location, I presume).
There has to be a better way to do it.
Comments
Oren, are you aware of AntlrWorks? it's a java based IDE for antlr that can let you test your grammar file in real time.
I have that, but as I am trying to generate my own AST, rather than ANTLR's trees, that is not really an option.
TDD works fairly well there.
which version of ANTLR are you using? version 3 enables better error handling.
version 3.0
I thought your first post included a screencap from AntlrWorks? I even went and downloaded it after seeing your first post in which it looked like you were using it to create your grammar. Pretty cool (OK, really cool), but I got hung up trying to add logical expressions, ala a where clause - I think I need to purchase the Antlr book you've been blogging about the past few weeks since this is clearly over my head as of now. I feel so feeble with this sort of tech.
It's great to hear of this work in progress. Anyway, I hope this doesn't stop accepting patches for the current (historically interesting :) parser. Some time ago, I posted a patch for supporting HQL functions with parameters. It's a good use case for your new parser, too.
Will this new parser support this kind of queries?:
http://forum.hibernate.org/viewtopic.php?t=963057&highlight=recursion
Tree building is an interesting problem, because it is not standard a feature of SQL.
That would certainly make it easier to implement, certainly
Hey, glad to hear that!
Is there an NH JIRA issue for this?
We could open it in order to discuss this new feature. I plan to giving a bounty for developing this, or assign resources from my company.
Thanks in advance.
This more or less dependent on completing the HQL Parser feature, which is a pretty big one, first.
Is there a JIRA for the new parser then? :)
I would like to track its progress in order to be ready to create the next JIRA for recursive HQL ;)
No, there isn't a JIRA right now, it is being handled here for now:
https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/experiments/Hql
You can register to the rhino tools commits for notifications on that, or subscribe to the NH mailing list, since it will appear there when it is mature enough to replace the current one
Comment preview