~nahiljain/to-drizzle/parse_create_table

« back to all changes in this revision

Viewing changes to movetodrizzle/helpers/parser.py

  • Committer: Nahil Jain
  • Date: 2010-07-15 22:25:37 UTC
  • mfrom: (4.1.3 trunk)
  • Revision ID: nahil@localhost.localdomain-20100715222537-2hkuxivyf31ogihi
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from lexer import *
5
5
import re
6
6
from statement_nodes import *
 
7
 
7
8
# Begin grammar rules
8
9
def p_statement_list(p):
9
10
  '''statement_list : statement_list statement'''
34
35
    p[0]= [p[1]] + p[2]
35
36
  else:
36
37
    p[0]= []
37
 
 
38
38
 
39
39
def p_qualified_identifier(p):
40
40
  '''identifier_qualified_opt : IDENTIFIER DOT IDENTIFIER'''
65
65
 
66
66
def p_foreign_key_constraint(p):
67
67
  '''foreign_key_constraint : constraint_opt FOREIGN KEY index_name_opt LPAREN index_col_name_list RPAREN reference_definition'''
68
 
#  '''foreign_key_constraint : FOREIGN KEY LPAREN index_col_name_list RPAREN reference_definition'''
69
68
  p[0]= ForeignKeyConstraint(p[1], p[4] , p[6], p[8])
70
69
 
71
70
def p_constraint_opt(p):
81
80
          | empty '''
82
81
  p[0]=p[1]
83
82
 
84
 
 
85
83
def p_index_col_name_list(p):
86
84
  '''index_col_name_list : index_col_name COMMA index_col_name_list
87
85
            | index_col_name'''
90
88
  else:
91
89
    p[0]= [p[1]]
92
90
 
93
 
 
94
 
 
95
91
def p_index_col_name(p):
96
92
  '''index_col_name : identifier'''
97
93
  p[0]= p[1]
147
143
  '''col_definition : identifier data_type nullable_constraint default_p auto_increment_p unique_p primary_key comment_p'''
148
144
  p[0]= ColumnDefinition(p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8])
149
145
 
150
 
 
151
146
def p_nullable_constraint(p):
152
147
  '''nullable_constraint : NOT NULL
153
148
                         | NULL
209
204
  '''set_enum : SET LPAREN value_list RPAREN
210
205
              | ENUM LPAREN value_list RPAREN'''
211
206
  p[0]=p[1]
212
 
   
213
207
 
214
208
def p_value_list(p):
215
209
  '''value_list : STRING COMMA value_list
246
240
  '''length_opt : LPAREN NUMBER RPAREN 
247
241
                 | LPAREN NUMBER COMMA NUMBER RPAREN
248
242
                 | empty'''
249
 
 
250
243
  pass
251
244
 
252
245
def p_unsigned_opt(p):
254
247
                  | empty'''
255
248
  pass
256
249
 
257
 
 
258
250
def p_zerofill_opt(p):
259
251
  '''zerofill_opt : ZEROFILL
260
252
                  | empty'''
261
253
  pass
262
254
 
263
 
 
264
 
#def p_data_types(p):
265
 
#  '''data_type : mathematical'''
266
 
#  p[0]= p[1]
267
 
  
268
 
  
269
 
  
270
255
def p_mathematical(p):
271
256
  '''mathematical : TINYINT 
272
257
          | SMALLINT
281
266
          | NUMERIC'''
282
267
  p[0]= p[1]
283
268
def p_optional_temporary(p):
284
 
 '''temporary : TEMPORARY
 
269
  '''temporary : TEMPORARY
285
270
          | empty'''
286
 
 # Returns True if the TEMPORARY phrase appeared, False otherwise
287
 
 p[0]= len(p) > 1
 
271
  # Returns True if the TEMPORARY phrase appeared, False otherwise
 
272
  p[0]= len(p) > 1
288
273
 
289
274
def p_create_schema(p):
290
 
 '''statement_type : create_schema_simple
 
275
  '''statement_type : create_schema_simple
291
276
    | create_schema_with_charset
292
277
    | create_schema_with_collation
293
278
    | create_schema_with_charset_and_collation'''
294
 
 p[0]= p[1]
 
279
  p[0]= p[1]
295
280
 
296
281
def p_create_schema_simple(p):
297
 
 '''create_schema_simple : CREATE schema_or_database if_not_exists identifier'''
298
 
 p[0]= CreateSchemaStatement(p[4], None, None, p[3])
 
282
  '''create_schema_simple : CREATE schema_or_database if_not_exists identifier'''
 
283
  p[0]= CreateSchemaStatement(p[4], None, None, p[3])
299
284
 
300
285
def p_create_schema_with_charset(p):
301
 
 '''create_schema_with_charset : CREATE schema_or_database if_not_exists identifier charset identifier'''
302
 
 p[0]= CreateSchemaStatement(p[4], p[6], None, p[3])
 
286
  '''create_schema_with_charset : CREATE schema_or_database if_not_exists identifier charset identifier'''
 
287
  p[0]= CreateSchemaStatement(p[4], p[6], None, p[3])
303
288
 
304
289
def p_create_schema_with_collation(p):
305
 
 '''create_schema_with_collation : CREATE schema_or_database if_not_exists identifier COLLATE identifier'''
306
 
 p[0]= CreateSchemaStatement(p[4], None, p[6], p[3])
 
290
  '''create_schema_with_collation : CREATE schema_or_database if_not_exists identifier COLLATE identifier'''
 
291
  p[0]= CreateSchemaStatement(p[4], None, p[6], p[3])
307
292
 
308
293
def p_create_schema_with_charset_and_collation(p):
309
 
 '''create_schema_with_charset_and_collation : CREATE schema_or_database if_not_exists identifier charset identifier COLLATE identifier'''
310
 
 p[0]= CreateSchemaStatement(p[4], p[6], p[8], p[3])
 
294
  '''create_schema_with_charset_and_collation : CREATE schema_or_database if_not_exists identifier charset identifier COLLATE identifier'''
 
295
  p[0]= CreateSchemaStatement(p[4], p[6], p[8], p[3])
311
296
 
312
297
def p_optional_if_not_exists(p):
313
298
 '''if_not_exists : IF NOT EXISTS
314
299
          | empty'''
315
 
 # Returns True if the IF NOT EXISTS phrase appeared, False otherwise
316
 
 p[0]= len(p) > 2
 
300
  # Returns True if the IF NOT EXISTS phrase appeared, False otherwise
 
301
  p[0]= len(p) > 2
317
302
 
318
303
def p_schema_or_database(p):
319
304
 '''schema_or_database : SCHEMA
328
313
def p_charset(p):
329
314
 '''charset : CHARACTER SET
330
315
        | DEFAULT CHARACTER SET'''
331
 
 pass
332
 
 
333
 
 
 
316
  pass
334
317
 
335
318
def p_table_option(p):
336
319
  """table_option : ENGINE opt_equal identifier
340
323
  temp.append(p[1])
341
324
  temp.append(p[3])
342
325
  p[0]=temp;
343
 
 
344
326
  
345
327
def p_table_option(p):
346
328
  '''table_option : default_opt CHARACTER SET opt_equal identifier'''
348
330
  temp.append("CHARACTER SET",p[5]);
349
331
  p[0]= temp;
350
332
 
351
 
 
352
333
def p_default_opt(p):
353
334
  '''default_opt : DEFAULT
354
335
                  | empty'''
363
344
    p[0]= len(p)>1;
364
345
 
365
346
def p_empty(p):
366
 
 'empty : '
367
 
 pass # an empty rule that represents an optional
368
 
    # piece of a rule
 
347
  'empty : '
 
348
  pass # an empty rule that represents an optional piece
369
349
 
370
350
def p_error(p):
371
 
 # Called when a syntax or parse error occurs
372
 
 print "A parse error occurred at token %s" % p
 
351
  # Called when a syntax or parse error occurs
 
352
  print "A parse error occurred at token %s" % p
373
353
 
374
354
# Create the parser
375
355
parser= yacc.yacc(debug= False)
376
356
 
377
357
def parse_string(subject):
378
 
 try:
379
 
  return parser.parse(subject)
380
 
 except Exception as e:
381
 
  print e
 
358
  try:
 
359
    return parser.parse(subject)
 
360
  except Exception as e:
 
361
    print e