Skip to content

Instantly share code, notes, and snippets.

@manveru
Created September 28, 2010 11:02
Show Gist options
  • Save manveru/600811 to your computer and use it in GitHub Desktop.
Save manveru/600811 to your computer and use it in GitHub Desktop.
Simple StringScanner solution for parsing nested braces
require 'strscan'
s = StringScanner.new("1{2{3}; 1{2} }")
stack = [[]]
until s.eos?
if s.scan(/\{/)
stack << []
elsif s.scan(/\}/)
tmp = stack.pop
stack.last << tmp
elsif s.scan(/[[:word:]]+/)
stack.last << s.matched
elsif s.scan(/[;\s]+/)
end
p stack
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment