Skip to content

Instantly share code, notes, and snippets.

@icholy
Created February 2, 2018 00:17
Show Gist options
  • Save icholy/3a6e7d8b338bf807349af43d2a196a3b to your computer and use it in GitHub Desktop.
Save icholy/3a6e7d8b338bf807349af43d2a196a3b to your computer and use it in GitHub Desktop.
// Simple Arithmetics Grammar
// ==========================
//
// Accepts expressions like "2 * (3 + 4)" and computes their value.
Filter
= "//" path:Path { return path }
Expression
= Path
/ "@" left:Name _ op:Operator _ right:Value {
return {
type: "filter",
left: left.text,
right: right.text,
op: op
};
}
Path
= name:Name "/" path:Path {
return [name].concat(path);
}
/ name:Name "[" _ expr:Expression _ "]" {
let slug = { name: name.text, type: "array" };
return [slug].concat(expr);
}
/ Name
Name
= [A-Za-z][A-Za-z1-9]* {
return { type: "name", text: text() }
}
Value
= [^\]]+ {
return { type: "value", text: text() }
}
Integer "integer"
= _ [0-9]+ { return parseInt(text(), 10); }
Operator
= [><=]
_ "whitespace"
= [ \t\n\r]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment