Skip to content

Instantly share code, notes, and snippets.

@pfrazee
Created March 5, 2020 01:25
Show Gist options
  • Save pfrazee/242a3b9142ab25781ca10b4d7e409096 to your computer and use it in GitHub Desktop.
Save pfrazee/242a3b9142ab25781ca10b4d7e409096 to your computer and use it in GitHub Desktop.
// Webterm Input Grammar (PEG.js)
// ==============================
Expression
= Term+
Term
= term:Switch _? { return {type: 'param', key: term.key, value: term.value} }
/ term:String _? { return {type: 'token', value: term} }
Switch "switch"
= '-'+ key:String _+ value:NonSwitch { return {key, value} }
/ '-'+ key:String { return {key, value: undefined} }
NonSwitch "non-switch"
= !'-' value:String { return value }
String "string"
= head:Char rest:CharOrQuote* { return head + rest.join('') }
/ '"' value:CharOr_OrSingleQuote* '"' { return value.join('') }
/ '\'' value:CharOr_OrDoubleQuote* '\'' { return value.join('') }
CharOr_OrDoubleQuote "character, whitespace, or double quote"
= CharOr_
/ '"'
CharOr_OrSingleQuote "character, whitespace, or single quote"
= CharOr_
/ '\''
CharOr_ "character or whitespace"
= Char
/ _
CharOrQuote "character, single quote, or double quote"
= Char
/ '\''
/ '"'
Char "character"
= '\\ ' { return ' ' } // escaped space
/ [^ "'\f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]i // regular characters
_ "whitespace"
= [ \t]+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment