Chevrotain / chevrotain

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

Pattern for mode in multimode lexer #1999

Closed dhowe closed 7 months ago

dhowe commented 7 months ago

I'm sure this is quite simple, but I have a mode that should start with @{ and end with }@, and should accept anything else inside including }. My tokens are as below. How would I define the pattern for my Gate token ?

 const EnterGate = createToken({
    name: "EnterGate",
    pattern: /@{/
    push_mode: "gate_mode"
  });

  const ExitGate = createToken({
    name: "ExitGate",
    pattern: /}@/
    pop_mode: true
  });

  const Gate = createToken({
    name: "Gate",
    pattern: ??
  });
msujew commented 7 months ago

You can probably use something like (?:(?!}@).)*. It uses a negative lookahead group (?!) to prevent matching }@ and then attempts to match any character .. This is repeating until it arrives at the end of the input or the }@ sequence.