1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
module Rsec
# sequence combinator<br/>
# result in an array
class Seq < Unary
def _parse ctx
some.map do |e|
res = e._parse ctx
return INVALID if INVALID[res]
res
end
end
end
# sequence combinator<br/>
# the result is the result of the parser at idx
class SeqOne < Struct.new(:parsers, :idx)
include Parser
def _parse ctx
ret = INVALID
parsers.each_with_index do |p, i|
res = p._parse ctx
return INVALID if INVALID[res]
ret = res if i == idx
end
ret
end
end
# skips skipper between tokens
class Seq_ < Struct.new(:first, :rest, :skipper)
include Parser
def _parse ctx
res = first._parse ctx
return INVALID if INVALID[res]
ret = [res]
rest.each do |e|
return INVALID if INVALID[skipper._parse ctx]
res = e._parse ctx
return INVALID if INVALID[res]
ret << res
end
ret
end
end
# skips skipper between tokens
class SeqOne_ < Struct.new(:first, :rest, :skipper, :idx)
include Parser
def _parse ctx
ret = INVALID
res = first._parse ctx
return INVALID if INVALID[res]
ret = res if 0 == idx
check = idx - 1
rest.each_with_index do |p, i|
return INVALID if INVALID[skipper._parse ctx]
res = p._parse ctx
return INVALID if INVALID[res]
ret = res if i == check
end
ret
end
end
# unbox result size
# only work for seq and join and maybe'ed seq and join
class Unbox < Unary
def _parse ctx
res = some._parse ctx
return INVALID if INVALID[res]
res.size == 1 ? res.first : res
end
end
# inner
# only work for seq
class Inner < Unary
def _parse ctx
res = some._parse ctx
return INVALID if INVALID[res]
res.shift
res.pop
res
end
end
end
|