Home > Mobile >  How to define case-insensitivity for only a subsection of lexer tokens?
How to define case-insensitivity for only a subsection of lexer tokens?

Time:08-12

Is there a way to do something like the following so the case-sensitivity is not an "all-or-nothing" option?

lexer grammar TestLexer;
options { caseInsensitive=true; } // ok, but it's not supported in the VS Code editor ??
BOOL: (TRUE|FALSE);

// I want these two keywords to be case-insensitive
TRUE:   'true'; // true, True, TRUE, ...
FALSE:  'false'; // false, False, FALSE, ...

options { caseInsensitive=false; } // ok, but it's not supported in the VS Code editor ??
// This keyword must only match exact case
YIELD:    'yield'; // 'yield' ONLY

If not, what is a possible way to do this? The way that I've seen before -- in my opinion is the worst choice -- is doing this kind of thing:

TRUE: T R U E  // case-insensitive
YIELD: 'yield' // case-sensitive

CodePudding user response:

Based on the documentation here, it appears that you can set caseInsensitivity on a rule by rule basis.

The example given:

options { caseInsensitive=true; }
STRING options { caseInsensitive=false; } : 'N'? '\'' (~'\'' | '\'\'')* '\''; // lower n is not allowed

CodePudding user response:

From Mike's answer, this is what worked for me:

lexer grammar TestLexer;
options { caseInsensitive=true; }
BOOL: (TRUE|FALSE);

// I want these two keywords to be case-insensitive
TRUE:   'true';
FALSE:  'false';

// This keyword must only match exact case
YIELD options { caseInsensitive=false; }: 'yield';
parser grammar TestParser;
options { tokenVocab = TestLexer; }

program
    : statement EOF
    ;

statement
    : YIELD SPACE BOOL
    ;

In other words, setting options at the top makes it the default, unless it is overriden for a particular Token by using the form:

TOKEN opens { caseInsensitive=true|false; }
    : VALUE
    ;
  • Related