[antlr-interest] "explosion" rewrite

Steve Ebersole steve at hibernate.org
Fri May 1 17:08:55 PDT 2009


sortKey : expression;

expression
    : QUOTED_IDENTIFIER -> ^( COLUMN ALIAS_REF[Template.TEMPLATE]
QUOTED_IDENTIFIER[$QUOTED_IDENTIFIER] )
    // we treat the so-called standard functions differently because
they are handled differently by the HQL lexer which we also use here...
    | standardFunction
    | literal
    // not identDotIdentStructure because we dont want
QUOTED_IDENTIFIERs is here
    | ( IDENTIFIER ( DOT IDENTIFIER )* LEFT_PAREN ) => generalFunction
    // otherwise we fully expect a dot-identifier series, and then we
just need to decode the semantic of that structure
    | identDotIdentStructure
        -> { ( isFunctionName($identDotIdentStructure.text) ) }?
              // we have a function with parens (thus no args)
              ^( FUNCTION[$identDotIdentStructure.start,
$identDotIdentStructure.text] )
        -> { ( isPropertyName($identDotIdentStructure.text) ) }?
              // we have a reference to a mapped property
              { buildPropertyColumns( $identDotIdentStructure.tree ) }
        -> { ( $identDotIdentStructure.tree.getType() == DOT ) }?
              // we have a reference to a column which is already
qualified
              identDotIdentStructure
        ->
              // we have a reference to a column which is not qualified
              ^( COLUMN ALIAS_REF[Template.TEMPLATE]
IDENTIFIER[$identDotIdentStructure.start,$identDotIdentStructure.text] )
    ;


The piece of interest here is fragment in expression that reads

-> { ( isPropertyName($identDotIdentStructure.text) ) }?
              // we have a reference to a mapped property
              { buildPropertyColumns( $identDotIdentStructure.tree ) }

buildPropertyColumns() is a java action where I account for the fact
that in Hibernate a single property might map to multiple columns; in
that case buildPropertyColumns() returns the VECTOR_EXPR


On Fri, 2009-05-01 at 13:50 -0700, Jim Idle wrote:
> Steve Ebersole wrote:
> > Is there a syntax to "explode" a subrule result (sorry don't know a
> > better term).
> >
> > I have a rule:
> >
> > sortSpecification
> >     : sortKey collationSpecification? orderingSpecification?
> >         -> ^( SORT_SPEC sortKey collationSpecification?
> > orderingSpecification? )
> >     ;
> >
> > The result of the sortKey subrule could be a Tree of type VECTOR_EXPR
> > (its a "row value constructor" for those familiar with SQL). 
> >
> > In terms of eventual output, this rule translates as "(sort_key1,
> > sort_key2) asc" where "sort_key1, sort_key2)" is the VECTOR_EXPR.  
> >
> > Instead what I need to have happen is "sort_key1 asc, sort_key2 asc".
> >
> > I started down the path of trying an @after in the sortSpecification and
> > calling out to some java code to mutate the tree manually.  But I'd
> > prefer to keep this in the grammar if at all possible.
> >
> >
> >   
> I would need to see your sortKey rule but you can do things like this:
> 
> x : a+=something x? y? ->^(NODE $a $x? $y)+
> 
> That will probably get you on track. Also, you could reorder the tree 
> and more easily pick up the order:
> 
> -> ^( SORT_SPEC collationSpecification?
> orderingSpecification? sortKey)
> 
> Assuming the first two optional sequences have a root node. Then you could assume that the elements in sortKey have whatever you have established in the ordering.
> 
> If what you are trying to do is parse a SQL statement you could try inputting this into my online SQL parser and looking at the tree output:
> 
> http://www.temporal-wave.com
> 
> Jim
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
-- 
Steve Ebersole <steve at hibernate.org>
Hibernate.org



More information about the antlr-interest mailing list