Chevrotain / chevrotain

Parser Building Toolkit for JavaScript
https://chevrotain.io
Apache License 2.0
2.46k stars 201 forks source link

Question: Consume Token, only if another token was found #929

Closed haexhub closed 5 years ago

haexhub commented 5 years ago

Hello,

I've got a question about writing the right grammer for relational algebra.

$.RULE('tableExpression', () => {
      $.OPTION(() => {
        $.CONSUME(openBrace)
      })
      $.OR([
        { ALT: () => $.CONSUME(Identifier, { LABEL: 'tableName' }) }
      , { ALT: () => $.SUBRULE($.renameClause) }
      ])
      $.OPTION1(() => {
        $.CONSUME(closeBrace)
      })
    })

It is optional to write a table expression with and without braces. But if an open brace is used, than there must be a closing brace. For example:

π column_a, column_b table_name 

is as valid as

π column_a, column_b (table_name)

but its not valid to write it like this

π column_a, column_b table_name)

nor

π column_a, column_b (table_name 

How do i check this? In fact, it is also allowed to use as much braces as wanted, but for each open brace a corresponding close brace is needed.

(((( π column_a, column_b (table_name) ))))

is valid too.

Thank you very much for help.

bd82 commented 5 years ago

Have a look here. https://github.com/SAP/chevrotain/blob/master/examples/grammars/calculator/calculator_pure_grammar.js#L121-L135

haexhub commented 5 years ago

Your are incredible fast in answering! Thank you very much, I'll have a look! :)

bd82 commented 5 years ago

re-open this if any farther help is required