Question

I am trying to match ***, but I find that javacc matches *** anywhere in the line. How do I make sure that it only matches *** when there are no other characters other than spaces or tabs before *** ? This is what I currently have

< HORIZONTAL_RULE: <ZERO_OR_MORE_OF_TAB_OR_SPACE> ("**")("*")+>
| <#ZERO_OR_MORE_OF_TAB_OR_SPACE: (" " | "\t")*>

But again, this matches any *** prepended by zero or more spaces.

Was it helpful?

Solution

Use lexical states. Use the DEFAULT state for the start of a line.

// Note that states do not apply to private regular expression definitions.
TOKEN: <#ZERO_OR_MORE_OF_TAB_OR_SPACE: (" " | "\t")*>

<DEFAULT> TOKEN: {
    <HORIZONTAL_RULE: <ZERO_OR_MORE_OF_TAB_OR_SPACE> ("**")("*")+ > : MIDLINE
}
<DEFAULT, MIDLINE> SKIP { <OTHERCHAR: ~["\n","\r"]> : MIDLINE }
<DEFAULT, MIDLINE> SKIP { <NEWLINE: "\n" | "\n\r" | "\r" | "\r\n"> : DEFAULT }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top