Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/963bf8066ade0253bb78 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/963bf8066ade0253bb78 to your computer and use it in GitHub Desktop.
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