Skip to content

Instantly share code, notes, and snippets.

@benchristel
Created December 15, 2020 01:22
Show Gist options
  • Save benchristel/15c91da370cc736da15f0d2dcfc71dbd to your computer and use it in GitHub Desktop.
Save benchristel/15c91da370cc736da15f0d2dcfc71dbd to your computer and use it in GitHub Desktop.
PEG.js grammar for Fifth
// Paste this into https://pegjs.org/online
Program = _ ts: TermSequence {
return ts
}
TermSequence = terms: Term* _ {
return terms
}
Term = _ t: (Number / Name / String / List / Map) {
return t
}
Number = head: [1-9] tail: [0-9]* {
return parseInt(head + tail.join(""))
}
Name = head: [a-z-<>=+*/-] tail: [0-9a-z<>=+*/-]* {
return ":" + head + tail.join("")
}
String = '"' chars: (Nonquote / EscapedQuote)* '"' {
return "$" + chars.join("")
}
Nonquote = [^"]
EscapedQuote = '""' {
return '"'
}
List = '[' items: TermSequence ']' {
return items
}
Map = '{' pairs: Pair* _ '}' {
return {
pairs
}
}
Pair = a: Term b: Term {
return [a, b]
}
_ = ([ \t\n\r] / Comment)*
Comment = "#" [^\n]* "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment