This project has moved. For the latest updates, please go here.

Parsing SQL Arrays

Aug 1, 2014 at 5:57 AM
First, Irony is awesome, and I really appreciate the time you've invested in building it. Second, I've got a PostgreSQL grammar I'd love to contribute (I took the SQL89 grammar as a starting point and built upon it). Third, and to the point of this post, I'm having a lot of trouble parsing arrays in my PostgreSQL grammar. I tried studying the Java and C# languages to see if I could glean anything from them, but nothing I've tried has worked. Here's the code:
exprList.Rule = MakePlusRule(exprList, comma, expression);
expression.Rule = term | unExpr | binExpr;// | betweenExpr; //-- BETWEEN doesn't work - yet; brings a few parsing conflicts 
term.Rule = Id + castOpt | any_string + castOpt | number + castOpt | array | funCall + castOpt | tuple | parSelectStmt;// | inStmt;
array.Rule = Id + "[" + exprList + "]";
And here's a sample statement:
a1[i] <> a2[i]
I thought this would parse as (binExpr (term (array ... )) (binOp) (term (array ... ))), but the parser thinks there's a syntax error after the "a1" identifier. I've tried marking the left and right brackets as punctuation, since I saw the left and right parentheses marked as such, but to no avail. Any help would be greatly appreciated. Thanks very much,
  • Daniel
Aug 1, 2014 at 6:05 PM
What might be a problem is that [ and ] brackets are treated in original SQL grammar as 'quote' symbols - it is in fact TSQL (MS SQL) grammar. So scanner catches this symbol and treats the following as part of quoted identifier.
Don't remember how it is in Postgres, but standard SQL uses mostly double quote " symbol as name quote symbol. So modify or use different definition for id_simple identifier, without using brackets.
Aug 12, 2014 at 4:40 AM
Yes, that did the trick -- totally overlooked the use of brackets as identifier quotes. Thanks very much for pointing that out.