Skip to content

Instantly share code, notes, and snippets.

@riffraff
Created February 22, 2012 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riffraff/1883670 to your computer and use it in GitHub Desktop.
Save riffraff/1883670 to your computer and use it in GitHub Desktop.
ruby 1.9 regex to match s-expressions
#matches something like an s-expression
R = %r@
(?<atom> [-\w]+){0}
(?<quote> '\g<expr>){0}
(?<expr>
\g<atom> |
\g<quote> |
\(
\g<expr>
(?<exprs> (\s+ \g<expr>)* )
\)
){0}
^\g<expr>$
@imx
def t s
p '='*30
p s
p R.match(s)
p s.scan R
end
t('(ciao)')
t('(ciao miao)')
t('(ciao miao)')
t('(ciao (miao))')
t('(ciao (miao bau))')
t("(ciao '(miao bau))")
t('(sum-iter k 1)')
t(<<SEXPR)
(define sum1 (lambda (k)
(sum-iter k 0 1)))
SEXPR
t("ciao '(miao bau))")
t('(ciao')
t(<<SEXPR)
(define sum1 (lambda (k)
(sum-iter k 0 1))
SEXPR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment