Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing for quotes and unicode in PEG.js grammar definitions

How would one allow for single and double quotes, as well as unicode characters inside of a PEG.js grammar definition? To be more specific, I'd like to be able to capture strings that can contain both single and double quotes (will most likely have to be \ escaped) and all unicode characters.

At the moment I have something like the following:

_ name:$(PROP_ASCII+) CHAR_SQ val:$(PROP_ASCII_INNER*) CHAR_SQ

which would capture something like

key'value'

PROP_ASCII* is defined as

PROP_ASCII = [!-&(-<>-~] PROP_ASCII_INNER = [ -&(-~]

So this works fine and dandy if value contains standard ASCII characters and does not contain single quotes... But I'd like to support what I've described above, so something like this would become possible:

key'somé\'value\'☂'

Thoughts?

like image 999
Speedy Avatar asked Nov 26 '15 22:11

Speedy


1 Answers

This example should get you going. It supports both single and double quotes which can also be escaped within the string.

Try it in the online editor.

Value
  = '"' chars:DoubleStringCharacter* '"' { return chars.join(''); }
  / "'" chars:SingleStringCharacter* "'" { return chars.join(''); }

DoubleStringCharacter
  = !('"' / "\\") char:. { return char; }
  / "\\" sequence:EscapeSequence { return sequence; }

SingleStringCharacter
  = !("'" / "\\") char:. { return char; }
  / "\\" sequence:EscapeSequence { return sequence; }

EscapeSequence
  = "'"
  / '"'
  / "\\"
  / "b"  { return "\b";   }
  / "f"  { return "\f";   }
  / "n"  { return "\n";   }
  / "r"  { return "\r";   }
  / "t"  { return "\t";   }
  / "v"  { return "\x0B"; }
like image 123
dan Avatar answered Sep 25 '22 09:09

dan