File: join.rb

package info (click to toggle)
ruby-rsec 0.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: ruby: 2,130; lisp: 13; makefile: 3
file content (86 lines) | stat: -rw-r--r-- 1,809 bytes parent folder | download | duplicates (3)
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
module Rsec
  
  # Join base
  class Join < Binary
    def _parse ctx
      e = left._parse ctx
      return INVALID if INVALID[e]
      ret = [e]
      loop do
        save_point = ctx.pos
        i = right._parse ctx
        if INVALID[i]
          ctx.pos = save_point
          break
        end

        t = left._parse ctx
        if INVALID[t]
          ctx.pos = save_point
          break
        end

        break if save_point == ctx.pos # stop if no advance, prevent infinite loop
        ret << i
        ret << t
      end # loop
      ret
    end
  end

  # keep only tokens
  class JoinEven < Binary
    def _parse ctx
      e = left._parse ctx
      return INVALID if INVALID[e]
      ret = [e]
      loop do
        save_point = ctx.pos
        i = right._parse ctx
        if INVALID[i]
          ctx.pos = save_point
          break
        end

        t = left._parse ctx
        if INVALID[t]
          ctx.pos = save_point
          break
        end

        break if save_point == ctx.pos # stop if no advance, prevent infinite loop
        ret << t
      end # loop
      ret
    end
  end

  # keep only inters
  # NOTE if only 1 token matches, return empty array
  class JoinOdd < Binary
    def _parse ctx
      e = left._parse ctx
      return INVALID if INVALID[e]
      ret = []
      loop do
        save_point = ctx.pos
        i = right._parse ctx
        if INVALID[i]
          ctx.pos = save_point
          break
        end

        t = left._parse ctx
        if INVALID[t]
          ctx.pos = save_point
          break
        end

        break if save_point == ctx.pos # stop if no advance, prevent infinite loop
        ret << i
      end # loop
      ret
    end
  end

end