ohmjs / ohm

A library and language for building parsers, interpreters, compilers, etc.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How would I match anything up until certain character sequence?

DreadBoy opened this issue · comments

Let's say I try to match these 2 sentences:

The mind flayer magically emits psychic energy in a 60-foot cone.
The dragon exhales lightning in a 30-foot line that is 5 feet wide.

I don't have a problem writing a grammar for in a 60-foot cone or in a 30-foot line that is 5 feet wide together with a semantic rules but I'm struggling with text before these 2 sequences. I'd like to match any text before " in " and disregard it since it's just a fluff. I tried:

start = any+ " in " // this will match entire sentence but I'd like it to stop at first occurence of "in"
start =  ~" in "+ " in " // this isn't a valid syntax...

If I were to write this as regex, I'd write it as .+? in (...rest of regex), how would I express that in Ohm?

I think what you want to do is:

start = (~" in " any)* " in "

Thanks for answering this, @AbigailBuccaneer!