下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
def parse(line) s = line.scan(/\(|\)|"(?:\\.|[^"])*"|[^()" \n]+/) f = ->(t=nil){ case x = t || s.shift when ?( y = [] while x = s.shift return y if x==?) y << f[x] end fail ")?" when nil nil when /^#(.*)$/ {?t=>true,?f=>false}[$~[1]] when /\d+/ x.to_i when /^\"(.*)\"$/m $~[1].gsub(/\\./,'\n'=>"\n","\\\\"=>"\\","\\\""=>'"') else x.to_sym end } f[] end $tests = { "" => nil,"1" => 1,"(a b)" => [:a,:b],"(1 2(2 3))" => [1,2,[2,3]],'(a"b"c"d"(ef))' => [:a,"b",:c,"d",[:ef]],'("a b" (c d-e))' => ["a b",[:c,:"d-e"]],"(\"a\nb\"\nb\nc (d))" => ["a\nb",:b,[:d]],' "a\nb"' => "a\nb",'"a\\"b"'=>'a"b',"\"\\\\\""=> "\\","(#t #f)" => [true,false],"(define (f x) (if (zero? x) 0 (+ x (f (- x 1)))))" => [:define,[:f,:x],[:if,[:zero?,[:+,:x,[:-,1]]]]],} p $tests.all?{|x,y|parse(x)==y}
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。