Class: SQLParser::Parser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/sql-parser/parser.rex.rb,
lib/sql-parser/parser.racc.rb

Defined Under Namespace

Classes: ScanError

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
'$end',
'error',
'ORDER',
'BY',
'comma',
'unsigned_integer',
'ASC',
'DESC',
'left_paren',
'right_paren',
'SELECT',
'asterisk',
'AS',
'FROM',
'CROSS',
'JOIN',
'INNER',
'LEFT',
'OUTER',
'RIGHT',
'FULL',
'ON',
'USING',
'WHERE',
'GROUP',
'HAVING',
'NOT',
'BETWEEN',
'AND',
'IN',
'LIKE',
'IS',
'NULL',
'EXISTS',
'identifier',
'OR',
'equals_operator',
'not_equals_operator',
'less_than_operator',
'greater_than_operator',
'less_than_or_equals_operator',
'greater_than_or_equals_operator',
'plus_sign',
'minus_sign',
'solidus',
'CURRENT_USER',
'period',
'COUNT',
'AVG',
'MAX',
'MIN',
'SUM',
'E',
'quote',
'character_string_literal',
'DATE',
'date_string',
'$start',
'direct_sql_statement',
'direct_sql_data_statement',
'direct_select_statement_multiple_rows',
'query_expression',
'order_by_clause',
'sort_specification_list',
'sort_specification',
'sort_key',
'ordering_specification',
'column_name',
'subquery',
'query_specification',
'select_list',
'table_expression',
'select_sublist',
'derived_column',
'value_expression',
'from_clause',
'where_clause',
'group_by_clause',
'having_clause',
'table_reference',
'table_name',
'joined_table',
'table_subquery',
'cross_join',
'qualified_join',
'join_type',
'join_specification',
'join_condition',
'named_columns_join',
'search_condition',
'join_column_list',
'column_name_list',
'grouping_column_reference_list',
'grouping_column_reference',
'column_reference',
'row_subquery',
'between_predicate',
'row_value_constructor',
'in_predicate',
'in_predicate_value',
'in_value_list',
'like_predicate',
'null_predicate',
'exists_predicate',
'boolean_term',
'boolean_factor',
'boolean_test',
'boolean_primary',
'predicate',
'comparison_predicate',
'row_value_constructor_element',
'numeric_value_expression',
'term',
'factor',
'sign',
'value_expression_primary',
'unsigned_value_specification',
'set_function_specification',
'unsigned_literal',
'general_value_specification',
'unsigned_numeric_literal',
'general_literal',
'qualifier',
'general_set_function',
'exact_numeric_literal',
'approximate_numeric_literal',
'mantissa',
'exponent',
'signed_integer',
'datetime_literal',
'date_literal']
Racc_debug_parser =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/sql-parser/parser.rex.rb', line 8

def filename
  @filename
end

#linenoObject (readonly)

Returns the value of attribute lineno.



7
8
9
# File 'lib/sql-parser/parser.rex.rb', line 7

def lineno
  @lineno
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



1418
1419
1420
# File 'lib/sql-parser/parser.racc.rb', line 1418

def _reduce_none( val, _values, result )
 result
end

#action(&block) ⇒ Object



12
13
14
# File 'lib/sql-parser/parser.rex.rb', line 12

def action &block
  yield
end

#load_file(filename) ⇒ Object



21
22
23
24
25
26
# File 'lib/sql-parser/parser.rex.rb', line 21

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

#next_tokenObject



33
34
35
# File 'lib/sql-parser/parser.rex.rb', line 33

def next_token
  @rex_tokens.shift
end

#scan_evaluate(str) ⇒ Object



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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/sql-parser/parser.rex.rb', line 37

def scan_evaluate( str )
  scan_setup
  @rex_tokens = []
  @lineno  =  1
  ss = StringScanner.new(str)
  state = nil
  until ss.eos?
    text = ss.peek(1)
    @lineno  +=  1  if text == "\n"
    case state
    when nil
      case
      when (text = ss.scan(/\"[0-9]+-[0-9]+-[0-9]+\"/i))
         @rex_tokens.push action { [:date_string, Date.parse(text)] }

      when (text = ss.scan(/\'[0-9]+-[0-9]+-[0-9]+\'/i))
         @rex_tokens.push action { [:date_string, Date.parse(text)] }

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

      when (text = ss.scan(/\"/i))
         @rex_tokens.push action { state = :STRD;  [:quote, text] }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      when (text = ss.scan(/\w+/i))
         @rex_tokens.push 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))
         @rex_tokens.push action { state = nil;    [:quote, text] }

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

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

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

      when (text = ss.scan(/.*(?=\")/i))
         @rex_tokens.push 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
       
  end  
       
end

#scan_file(filename) ⇒ Object



28
29
30
31
# File 'lib/sql-parser/parser.rex.rb', line 28

def scan_file( filename )
  load_file  filename
  do_parse
end

#scan_setupObject



10
# File 'lib/sql-parser/parser.rex.rb', line 10

def scan_setup ; end

#scan_str(str) ⇒ Object



16
17
18
19
# File 'lib/sql-parser/parser.rex.rb', line 16

def scan_str( str )
  scan_evaluate  str
  do_parse
end