Class: JGrep::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/scanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ Scanner

Returns a new instance of Scanner.



5
6
7
8
# File 'lib/parser/scanner.rb', line 5

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

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



3
4
5
# File 'lib/parser/scanner.rb', line 3

def arguments
  @arguments
end

#token_indexObject

Returns the value of attribute token_index.



3
4
5
# File 'lib/parser/scanner.rb', line 3

def token_index
  @token_index
end

Instance Method Details

#get_tokenObject

Scans the input string and identifies single language tokens



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
# File 'lib/parser/scanner.rb', line 11

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