From 63f6f2a233a3841d9b1b90877093fa8223a5edfa Mon Sep 17 00:00:00 2001 From: Andy Kitchen Date: Wed, 16 Jun 2010 04:04:20 +1000 Subject: [PATCH] Added examples. --- example.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 example.rb diff --git a/example.rb b/example.rb new file mode 100644 index 0000000..89f53a1 --- /dev/null +++ b/example.rb @@ -0,0 +1,68 @@ +include Rasta + +def list1 + number = t(/[0-9]+/) + word = t(/[A-Za-z]+/) + + atom = number | word + space = t(" ").star + + l = t("[") >> space + r = space >> t("]") + sep = space >> t(",") >> space + + list = l >> atom >> (sep >> atom).star >> r +end + +def list2 + number = t(/[0-9]+/).mk_i + word = t(/[A-Za-z]+/).mk_s + + atom = number | word + space = t(" ").star.drop + + l = drop(t("[") >> space) + r = drop(space >> t("]")) + sep = space >> t(",").drop >> space + + list = unbox(l >> flat(atom >> (unbox(sep >> atom)).star) >> r) +end + +def list3 + number = t(/[0-9]+/).mk_i + word = t(/[A-Za-z]+/).mk_s + + atom = number | word + space = t(" ").star.drop + + l = drop(t("[") >> space) + r = drop(space >> t("]")) + sep = space >> t(",").drop >> space + + list = unbox( + l >> flat(ref{list} >> (unbox(sep >> ref{list})).star) >> r + ) | atom + + list.mk_a +end + +def list_of(atom, sep) + flat(atom >> (unbox(sep.drop >> atom)).star) +end + +def list4 + number = t(/[0-9]+/).mk_i + word = t(/[A-Za-z]+/).mk_s + + atom = number | word + space = t(" ").star.drop + + l = drop(t("[") >> space) + r = drop(space >> t("]")) + sep = space >> t(",") >> space + + # list = unbox(l >> ref{list}.mk_list(sep) >> r) | atom + list = unbox(l >> list_of(ref{list}, sep) >> r) | atom + + list.mk_a +end