ohmjs / ohm

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to not match a newline?

ApproximateIdentity opened this issue · comments

How do I match everything except a newline? For example if I have the following grammar:


  Line = expression? comment
  
  expression = (alnum | " " | "=")*
  
  comment =  "--" (~"\n" any)*
}

If I use that in the Ohm editor https://ohmlang.github.io/editor/ and test it against -- hello"\n" then it does match at it says that the comment portion matches against a = b -- hello\n then the comment portion matches -- hello\n. How do I make it so the comment portion does not match the newline? The documentation here ( https://github.com/harc/ohm/blob/master/doc/syntax-reference.md#negative-lookahead- ) seems to indicate that this should not match.

Of course after more than a day I manage to figure out the issue immediately after making this post. The grammar I want is the following:

Comment {

  line = expression? comment

  expression = (alnum | " " | "=")*

  comment =  "--" (~"\n" any)*
}

That will not match a = b -- hello\n due to the newline. The issue apparently was that Line was eating up that final newline since I guess it ignores the whitespace. Then if I want it to match that final new line I can just put it into the grammar explicitly like so:

Comment { 

  line = expression? comment "\n"

  expression = (alnum | " " | "=")*
  
  comment =  "--" (~"\n" any)*
}

I think my confusion was compounded by the apparently fact that the online editor is not escaping the newline. I.e. the \n in a = b -- hello\n is interpreted as a \ and then a n instead of as a single newline character. So it does match the rule (~"\n" any)* in the online editor. Maybe the fact that the editor is not allowing the escape sequences is documented somewhere and I missed it.