pegtextx

Is it possible to use a link rule reference in a lookahead fashion?


I am attempting to use textx in order to parse a language similar to SQL. I would like to use link rule references in order to reference aliases before they are declared.

I get an "Unknown Object of "Foo" of class "Bar"" error when attempting to do this sort of forward reference.

The simplest example showing what I would like to parse is given below:

SELECT B.c
FROM A AS B

In this case I would like the B in B.c to refer to the alias defined afterwards (B in A AS B).

EDIT

Looking into it further it seems that the grammar I have parses the simple example I gave above just fine, but runs into problems when I attempt to use a nested query in the FROM clause.

Here's the grammar I'm using:

Expression: Query | Atomic | Tuple;

Query:
    'SELECT' selections+=Selection[',']
    'FROM' from=From
;

Selection[noskipws]:
    /\s*/
    source=[SourceAlias] ('.' source_selectors+=ID['.'])?  /\s*/ ('AS' /\s*/ alias=ID)?
    /\s*/
;

From:
        (source=DataSource | '(' Query ')') 'AS' alias=SourceAlias
;

DataSource: source_name=ID '.' source_attributes+=ID['.'];

SourceAlias: name=ID;

Tuple: '(' atoms+=Atomic[','] ')';

Atomic: NUMBER | STRING | BOOL;

And Here's an example that my grammar fails on, with error "test.qql:2:14: error: Unknown object "B" of class "SourceAlias":

SELECT inner.o AS outer
FROM (SELECT B.huh AS aha FROM A.b AS B) AS inner

Solution

  • The problem is that in the From rule the Query match is not being assigned to anything, so it is being discarded and thus the references in the inner query are discarded. Here's the updated From rule:

    From:
            (source=DataSource | '(' query=Query ')') 'AS' alias=SourceAlias
    ;