Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
OMetaJS: String Matching Syntax

OMetaJS String Matching Syntax

OMeta can parse more than just text streams. It can operate on typed objects that the host language understands. That's why we have different string matching syntax for different purposes.

  • #abc or ``abc` => match the string object 'abc'
  • 'abc' => match the string object 'abc'
  • 'c' => match the string object 'c'
  • ```abc''orseq("abc")` => match the sequence of string objects 'a', 'b', 'c'
  • "abc" => match token('abc')

When matching single string characters use '', like '\n' or 'a'. When parsing text streams, it is capturing on a per character basis. When it takes each character, it's trying to compare it against 'abc', then of course it doesn't work. For 'abc' to actually work, the object it captured must actually be 'abc' as well, and that can only happen if you say passed in a typed string object!

When matching a word or phrase, use "" like "for" or "while I say this". You can use whitespace including newlines in between the words/phrases, but you cannot start with spaces or newlines. For example, this does not work: "\n\n" or " l".

When matching a literal sequence of characters or special characters use the seq higher order rule, or use the ```literalsequence''. For example: this one seq("\n\n")` matches 2 newlines.

However none of this applies to string parameters that are passed into higher order rules. All string parameters will be considered string literals. Therefore there is no difference between seq("") and seq('').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.