File: scanner.rb

package info (click to toggle)
jgrep 1.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 184 kB
  • sloc: ruby: 1,004; makefile: 8
file content (164 lines) | stat: -rw-r--r-- 4,271 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
module JGrep
  class Scanner
    attr_accessor :arguments, :token_index

    def initialize(arguments)
      @token_index = 0
      @arguments = arguments
    end

    # Scans the input string and identifies single language tokens
    def get_token
      return nil if @token_index >= @arguments.size

      begin
        case chr(@arguments[@token_index])
        when "["
          return "statement", gen_substatement

        when "]"
          return "]"

        when "("
          return "(", "("

        when ")"
          return ")", ")"

        when "n"
          if (chr(@arguments[@token_index + 1]) == "o") && (chr(@arguments[@token_index + 2]) == "t") && ((chr(@arguments[@token_index + 3]) == " ") || (chr(@arguments[@token_index + 3]) == "("))
            @token_index += 2
            return "not", "not"
          else
            gen_statement
          end

        when "!"
          return "not", "not"

        when "a"
          if (chr(@arguments[@token_index + 1]) == "n") && (chr(@arguments[@token_index + 2]) == "d") && ((chr(@arguments[@token_index + 3]) == " ") || (chr(@arguments[@token_index + 3]) == "("))
            @token_index += 2
            return "and", "and"
          else
            gen_statement
          end

        when "&"
          if chr(@arguments[@token_index + 1]) == "&"
            @token_index += 1
            return "and", "and"
          else
            gen_statement
          end

        when "o"
          if (chr(@arguments[@token_index + 1]) == "r") && ((chr(@arguments[@token_index + 2]) == " ") || (chr(@arguments[@token_index + 2]) == "("))
            @token_index += 1
            return "or", "or"
          else
            gen_statement
          end

        when "|"
          if chr(@arguments[@token_index + 1]) == "|"
            @token_index += 1
            return "or", "or"
          else
            gen_statement
          end

        when "+"
          value = ""
          i = @token_index + 1

          begin
            value += chr(@arguments[i])
            i += 1
          end until (i >= @arguments.size) || (chr(@arguments[i]) =~ /\s|\)/)

          @token_index = i - 1
          return "+", value

        when "-"
          value = ""
          i = @token_index + 1

          begin
            value += chr(@arguments[i])
            i += 1
          end until (i >= @arguments.size) || (chr(@arguments[i]) =~ /\s|\)/)

          @token_index = i - 1
          return "-", value

        when " "
          return " ", " "

        else
          gen_statement
        end
      end
    rescue NoMethodError
      raise "Error. Expression cannot be parsed."
    end

    private

    def gen_substatement
      @token_index += 1
      returnval = []

      while (val = get_token) != "]"
        @token_index += 1
        returnval << val unless val[0] == " "
      end

      returnval
    end

    def gen_statement
      current_token_value = ""
      j = @token_index

      begin
        if chr(@arguments[j]) == "/"
          begin
            current_token_value << chr(@arguments[j])
            j += 1
            if chr(@arguments[j]) == "/"
              current_token_value << "/"
              break
            end
          end until (j >= @arguments.size) || (chr(@arguments[j]) =~ /\//)
        else
          begin
            current_token_value << chr(@arguments[j])
            j += 1
            if chr(@arguments[j]) =~ /'|"/
              begin
                current_token_value << chr(@arguments[j])
                j += 1
              end until (j >= @arguments.size) || (chr(@arguments[j]) =~ /'|"/)
            end
          end until (j >= @arguments.size) || (chr(@arguments[j]) =~ /\s|\)|\]/ && chr(@arguments[j - 1]) != '\\')
        end
      rescue
        raise "Invalid token found - '#{current_token_value}'"
      end

      if current_token_value =~ /^(and|or|not|!)$/
        raise "Class name cannot be 'and', 'or', 'not'. Found '#{current_token_value}'"
      end

      @token_index += current_token_value.size - 1

      ["statement", current_token_value]
    end

    # Compatibility with 1.8.7, which returns a Fixnum from String#[]
    def chr(character)
      character.chr unless character.nil?
    end
  end
end