Class: CmisServer::Query::ParserRex

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/cmis_server/query/parser_rex.rb

Defined Under Namespace

Classes: ScanError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



17
18
19
# File 'lib/cmis_server/query/parser_rex.rb', line 17

def filename
  @filename
end

#linenoObject (readonly)

Returns the value of attribute lineno.



16
17
18
# File 'lib/cmis_server/query/parser_rex.rb', line 16

def lineno
  @lineno
end

#stateObject

Returns the value of attribute state.



18
19
20
# File 'lib/cmis_server/query/parser_rex.rb', line 18

def state
  @state
end

Instance Method Details

#_next_tokenObject



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cmis_server/query/parser_rex.rb', line 57

def _next_token
  text = @ss.peek(1)
  @lineno  +=  1  if text == "\n"
  token = case @state
  when nil
    case
    when (text = @ss.scan(/\'[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9][0-9][0-9]\'/i))
       action { [:date_string, DateTime.parse(text)] }

    when (text = @ss.scan(/\'/i))
       action { @state = :STRS;  [:quote, text] }

    when (text = @ss.scan(/[0-9]+/i))
       action { [:unsigned_integer, text.to_i] }

    when (text = @ss.scan(/\s+/i))
      ;

    when (text = @ss.scan(/SELECT/i))
       action { [:SELECT, text] }

    when (text = @ss.scan(/SCORE/i))
       action { [:SCORE, text] }

    when (text = @ss.scan(/AS/i))
       action { [:AS, text] }

    when (text = @ss.scan(/FROM/i))
       action { [:FROM, text] }

    when (text = @ss.scan(/LEFT/i))
       action { [:LEFT, text] }

    when (text = @ss.scan(/INNER/i))
       action { [:INNER, text] }

    when (text = @ss.scan(/OUTER/i))
       action { [:OUTER, text] }

    when (text = @ss.scan(/ON/i))
       action { [:ON, text] }

    when (text = @ss.scan(/JOIN/i))
       action { [:JOIN, text] }

    when (text = @ss.scan(/WHERE/i))
       action { [:WHERE, text] }

    when (text = @ss.scan(/TIMESTAMP/i))
       action { [:TIMESTAMP, text] }

    when (text = @ss.scan(/IN_FOLDER/i))
       action { [:IN_FOLDER, text] }

    when (text = @ss.scan(/IN_TREE/i))
       action { [:IN_TREE, text] }

    when (text = @ss.scan(/IN/i))
       action { [:IN, text] }

    when (text = @ss.scan(/NOT/i))
       action { [:NOT, text] }

    when (text = @ss.scan(/IS/i))
       action { [:IS, text] }

    when (text = @ss.scan(/NULL/i))
       action { [:NULL, text] }

    when (text = @ss.scan(/LIKE/i))
       action { [:LIKE, text] }

    when (text = @ss.scan(/AND/i))
       action { [:AND, text] }

    when (text = @ss.scan(/ANY/i))
       action { [:ANY, text] }

    when (text = @ss.scan(/ORDER/i))
       action { [:ORDER, text] }

    when (text = @ss.scan(/BY/i))
       action { [:BY, text] }

    when (text = @ss.scan(/OR/i))
       action { [:OR, text] }

    when (text = @ss.scan(/ASC/i))
       action { [:ASC, text] }

    when (text = @ss.scan(/DESC/i))
       action { [:DESC, text] }

    when (text = @ss.scan(/TRUE/i))
       action { [:TRUE, true]}

    when (text = @ss.scan(/FALSE/i))
       action { [:FALSE, false]}

    when (text = @ss.scan(/E/i))
       action { [:E, text] }

    when (text = @ss.scan(/<>/i))
       action { [:not_equals_operator, text] }

    when (text = @ss.scan(/!=/i))
       action { [:not_equals_operator, text] }

    when (text = @ss.scan(/=/i))
       action { [:equals_operator, text] }

    when (text = @ss.scan(/<=/i))
       action { [:less_than_or_equals_operator, text] }

    when (text = @ss.scan(/</i))
       action { [:less_than_operator, text] }

    when (text = @ss.scan(/>=/i))
       action { [:greater_than_or_equals_operator, text] }

    when (text = @ss.scan(/>/i))
       action { [:greater_than_operator, text] }

    when (text = @ss.scan(/\(/i))
       action { [:left_paren, text] }

    when (text = @ss.scan(/\)/i))
       action { [:right_paren, text] }

    when (text = @ss.scan(/\*/i))
       action { [:asterisk, text] }

    when (text = @ss.scan(/\//i))
       action { [:solidus, text] }

    when (text = @ss.scan(/\+/i))
       action { [:plus_sign, text] }

    when (text = @ss.scan(/\-/i))
       action { [:minus_sign, text] }

    when (text = @ss.scan(/\./i))
       action { [:period, text] }

    when (text = @ss.scan(/,/i))
       action { [:comma, text] }

    when (text = @ss.scan(/`[\w:]+`/i))
       action { [:identifier, text[1..-2]] }

    when (text = @ss.scan(/[\w:]+/i))
       action { [:identifier, text] }

    when (text = @ss.scan(/----/i))
      ;

    when (text = @ss.scan(/require/i))
      ;

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  when :STRS
    case
    when (text = @ss.scan(/\'/i))
       action { @state = nil;    [:quote, text] }

    when (text = @ss.scan(/[^\']*/i))
       action {                  [:character_string_literal, text.gsub("''", "'")] }

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  else
    raise  ScanError, "undefined state: '" + state.to_s + "'"
  end  # case state
  p token
  token
end

#actionObject



26
27
28
# File 'lib/cmis_server/query/parser_rex.rb', line 26

def action
  yield
end

#load_file(filename) ⇒ Object



36
37
38
39
40
41
# File 'lib/cmis_server/query/parser_rex.rb', line 36

def load_file( filename )
  @filename = filename
  open(filename, "r") do |f|
    scan_setup(f.read)
  end
end

#next_tokenObject



49
50
51
52
53
54
55
# File 'lib/cmis_server/query/parser_rex.rb', line 49

def next_token
  return if @ss.eos?
  
  # skips empty actions
  until token = _next_token or @ss.eos?; end
  token
end

#scan_file(filename) ⇒ Object



43
44
45
46
# File 'lib/cmis_server/query/parser_rex.rb', line 43

def scan_file( filename )
  load_file(filename)
  do_parse
end

#scan_setup(str) ⇒ Object



20
21
22
23
24
# File 'lib/cmis_server/query/parser_rex.rb', line 20

def scan_setup(str)
  @ss = StringScanner.new(str)
  @lineno =  1
  @state  = nil
end

#scan_str(str) ⇒ Object Also known as: scan



30
31
32
33
# File 'lib/cmis_server/query/parser_rex.rb', line 30

def scan_str(str)
  scan_setup(str)
  do_parse
end