Skip to content

Rego Grammar: Add v1 Tokens to the Accepted Token List #200

Description

@SeanLedford

What would you like to be added

Four keywords used in Rego v1 policies if, contains, every, in should be added to the parser's token table.

Why is this needed

The tokens above are parsed as variables via the grammar, which means many aspects of Rego syntax are currently being misinterpreted for parsing. There are several issues that this causes in modern Rego, which will have their own issues filed shortly

Implementation details

Very simple, the Rego.bnf grammar has a token table where we need to add these token types to:

tokens = [
        PACKAGE_TOKEN       = 'package'
        IMPORT_TOKEN        = 'import'
        AS                  = 'as'
        DEFAULT             = 'default'
        VAR_ASSIGN          = ':='
        UNIFICATION         = '='
        TRUE                = 'true'
        FALSE               = 'false'
        LPAREN              = '('
        RPAREN              = ')'
        LBRACK              = '['
        RBRACK              = ']'
        ELSE                = 'else'
        LBRACE              = '{'
        RBRACE              = '}'
        SEMICOLON           = ';'
        SOME                = 'some'
        COMMA               = ','
        DOT                 = '.'
        NOT                 = 'not'
        WITH                = 'with'
        COLLON              = ':'
        EQ                  = '=='
        NOT_EQ              = '!='
        LESS_OR_EQUAL       = '<='
        GREATER_OR_EQUAL    = '>='
        GREATHER            = '>'
        LESS                = '<'
        BIT_OR              = '|'
        BIT_AND             = '&'
        PLUS                = '+'
        MINUS               = '-'
        MUL                 = '*'
        QUOTIENT            = '/'
        REMAINDER           = '%'
        SET_OPEN            = 'set('
        NULL                = 'null'
        ASCII_LETTER        = 'regexp:[A-Za-z_][A-Za-z_0-9]*'
        NUMBER              = 'regexp:-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d*)?'
        WHITE_SPACE         = 'regexp:\s+'
        STRING_TOKEN        = 'regexp:"([^\\"\r\n]|\\[^\r\n])*"?'
        RAW_STRING          = 'regexp:`[^`]*(`)?'
        COMMENT             = 'regexp:[ \t]*#[^\r\n]*'
    ]

^^^

Adding these four keywords here is the first step to updating our grammar.

Metadata

Metadata

Assignees

Labels

area/psiIssue or pr related PSI

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions