Skip to content

Instantly share code, notes, and snippets.

@micahrj
Created September 24, 2011 23:24
Show Gist options
  • Save micahrj/1239984 to your computer and use it in GitHub Desktop.
Save micahrj/1239984 to your computer and use it in GitHub Desktop.
function parse(text) { var i = 0
function expr() { var result = [], t
while (t = term()) { result.push(t) }
return result }
function term() { return paren() || scope() || label() }
function paren() { var result
if (whitespace() && character('(') && (result = expr()) && whitespace() && character(')')) {
return result }
else {
return false } }
function scope() { var result
if (whitespace() && character('{') && (result = expr()) && whitespace() && character('}')) {
return result }
else {
return false } }
function label() { var result = ''
whitespace()
var result = ''
while ("(){} ".indexOf(text[i]) === -1 && i < text.length) {
result = result.concat(text[i]); i++ }
return result.length > 0 ? result : false }
function whitespace() { while (text[i] === ' ' && i < text.length) { i++ }; return true }
function character(c) { if (text [i] === c) { i++; return true } else { return false } }
return expr()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment