Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gtkatakura/1178922a64b0d624f9138481bdc840b2 to your computer and use it in GitHub Desktop.
Save gtkatakura/1178922a64b0d624f9138481bdc840b2 to your computer and use it in GitHub Desktop.
{
const flatten = array => [].concat(...array)
const uniq = array => array.reduce((filtered, element) => (
filtered.includes(element)
? filtered
: [...filtered, element]
), [])
const normalize = array => uniq(flatten(array)).sort()
}
Grammar
= symbols:Expression+ { return normalize(symbols) }
Expression
= left:TermExpression rights:("|" TermExpression)* { return left.concat(rights ? flatten(rights.map(right => right[1])) : []) }
TermExpression
= symbols:(Alphanumeric+ / CaptureExpression) RepetitionPart? { return symbols }
CaptureExpression
= "(" symbols:Expression+ ")" { return flatten(symbols) }
AlphanumericExpression
= symbols:Alphanumeric+ { return symbols }
RepetitionPart
= ("?" / "+" / "*" / "{" Integer+ "}")?
Alphanumeric
= (Integer / Letter)
Integer "integer"
= [0-9]
Letter "letter"
= [a-z]
_ "whitespace"
= [ \t\n\r]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment