1

Based on this answer I made my rule like:

ompParallel
  locals [java.util.HashSet<String> names = new java.util.HashSet<String>();]
  : SOME_LEXER_CONSTANT modifiers ;

modifiers
//Ensure that the full number of modifiers have been provided
: {$ompParallel::names.size() < 3}? predicateModifier modifiers
| {$ompParallel::names.size() == 3}? //match nothing once we have (any) three modifiers
;

predicateModifier
//Ensure that no duplicates have been provided
: {!$ompParallel::names.contains("a")}? A {$ompParallel::names.add("a");}
| {!$ompParallel::names.contains("b")}? B {$ompParallel::names.add("b");}
| {!$ompParallel::names.contains("c")}? C {$ompParallel::names.add("c");}
;

but in generated Parser there is only

public java.util.HashSet<String> names;

without declaration = new java.util.HashSet<String>();.

According to last snippet of this answer I tried something like

ruleName
  @init {
      java.util.HashSet<String> names = new java.util.HashSet<String>();
  }
  : SOME_LEXER_CONSTANT modifiers ;

This led to grammar-compilation error

 unknown attribute names for rule ompParallel in $ompParallel::names

I am really confused, could someone help me out? Preferably fixin the first alternative (using locals [...].

EDIT1: related source

Community
  • 1
  • 1
petrbel
  • 2,428
  • 5
  • 29
  • 49
  • 1
    Try to remove the `;` in your first attempt just before the `]`. If that doesn't work, as a workaround you could try to combine both approaches: declare the variable with `locals [java.util.HashSet names]` and init it with `@init { names = new java.util.HashSet(); }` but... meh. – Lucas Trzesniewski Nov 14 '14 at 00:56
  • The former solution didn't help, nevertheless the letter did (with some tweaks, please see my answer below) thanks! Anyway I consider this a problem and I'll try to report it to some issue tracker. – petrbel Nov 14 '14 at 07:30

1 Answers1

0

thanks to @Lucas I figured it out:

ompParallel    
locals [static java.util.HashSet<String> names]
@init {
    OmpParallelContext.names = new java.util.HashSet<String>();
}
: PARALLEL modifiers ;

One needs to define the variable statically in order to access it from different class. Unfortunately ANTLR4 generated Parser code seems to be little clumsy.

Community
  • 1
  • 1
petrbel
  • 2,428
  • 5
  • 29
  • 49