SHARE
TWEET

Untitled

a guest Oct 9th, 2019 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from SQL_parser.SQL_lexer import tokens
  2. import exception
  3. import ply.yacc as yacc
  4.  
  5.  
  6. class Struct:
  7.  
  8.     def __init__(self, **dictionary):
  9.         self.__dict__.update(dictionary)
  10.  
  11.     def __getitem__(self, name):
  12.         return self.__dict__[name]
  13.  
  14.     def __setitem__(self, name, value):
  15.         self.__dict__[name] = value
  16.  
  17.     def __iter__(self):
  18.         for i in self.__dict__.keys():
  19.             yield i
  20.  
  21.  
  22. class PCreate(Struct):
  23.  
  24.     def __init__(self, name="", values=[]):
  25.         self.name = name
  26.         self.type = "create"
  27.         self.values = values
  28.  
  29.  
  30. class PShow(Struct):
  31.  
  32.     def __init__(self, name=""):
  33.         self.name = name
  34.         self.type = "show"
  35.  
  36.  
  37. class PDrop(Struct):
  38.  
  39.     def __init__(self, name=""):
  40.         self.name = name
  41.         self.type = "drop"
  42.  
  43.  
  44. class PSelect(Struct):
  45.  
  46.     def __init__(self, select_body, condition=["", ""]):
  47.         self.type = "select"
  48.         self.select = select_body
  49.         self.condition = condition
  50.  
  51.  
  52. class PSelectBody(Struct):
  53.  
  54.     def __init__(self, name="", fields=[], isStar=False):
  55.         self.name = name
  56.         self.fields = fields
  57.         self.isStar = isStar
  58.  
  59.  
  60. class PInsert(Struct):
  61.  
  62.     def __init__(self, insert_body):
  63.         self.type = "insert"
  64.         self.insert = insert_body
  65.  
  66.  
  67. class PInsertBody(Struct):
  68.  
  69.     def __init__(self, name="", fields=[], values=[]):
  70.         self.name = name
  71.         self.fields = fields
  72.         self.values = values
  73.  
  74.  
  75. class PUpdate(Struct):
  76.  
  77.     def __init__(self, name="", set=[[], []], condition=["", ""]):
  78.         self.name = name
  79.         self.type = "update"
  80.         self.fields = set[0]
  81.         self.values = set[1]
  82.         self.condition = condition
  83.  
  84.  
  85. class PDelete(Struct):
  86.  
  87.     def __init__(self, name="", condition=["", ""]):
  88.         self.name = name
  89.         self.type = "delete"
  90.         self.condition = condition
  91.  
  92.  
  93. def p_start(p):
  94.     '''start : create
  95.              | show
  96.              | drop
  97.              | select
  98.              | insert
  99.              | update
  100.              | delete'''
  101.  
  102.     p[0] = p[1]
  103.  
  104.  
  105. def p_create(p):
  106.     '''create : CREATE create_body ENDREQUEST'''
  107.  
  108.     p[0] = p[2]
  109.  
  110.  
  111. def p_create_body(p):
  112.     '''create_body : TABLE NAME LBRACKET values RBRACKET'''
  113.  
  114.     p[0] = PCreate(p[2], p[4])
  115.  
  116.  
  117. def p_values(p):
  118.     '''values : NAME type
  119.               | values COMMA NAME type'''
  120.  
  121.     if len(p) == 3:
  122.         p[0] = []
  123.         p[0].append([p[1], p[2]])
  124.     else:
  125.         p[0] = p[1]
  126.         p[0].append([p[3], p[4]])
  127.  
  128.  
  129. def p_show(p):
  130.     '''show : SHOW CREATE TABLE NAME ENDREQUEST'''
  131.  
  132.     p[0] = PShow(p[4])
  133.  
  134.  
  135. def p_drop(p):
  136.     '''drop : DROP TABLE NAME ENDREQUEST'''
  137.  
  138.     p[0] = PDrop(p[3])
  139.  
  140.  
  141. def p_select(p):
  142.     '''select : SELECT select_body ENDREQUEST
  143.               | SELECT select_body condition ENDREQUEST'''
  144.     if (len(p) == 4):
  145.         p[0] = PSelect(p[2])
  146.     else:
  147.         p[0] = PSelect(p[2], p[3])
  148.  
  149.  
  150. def p_select_body(p):
  151.     '''select_body : fields FROM NAME
  152.                    | STAR COMMA fields FROM NAME
  153.                    | STAR FROM NAME'''
  154.  
  155.     if (len(p) == 4) and (p[1] != '*'):
  156.         p[0] = PSelectBody(p[3], p[1])
  157.     elif (len(p) == 4) and (p[1] == '*'):
  158.         p[0] = PSelectBody(p[3], [], True)
  159.     elif (len(p) == 6):
  160.         p[0] = PSelectBody(p[5], p[3], True)
  161.  
  162.  
  163. def p_insert(p):
  164.     '''insert : INSERT insert_body ENDREQUEST'''
  165.  
  166.     p[0] = PInsert(p[2])
  167.  
  168.  
  169. def p_insert_body(p):
  170.     '''insert_body : INTO NAME VALUES LBRACKET fields RBRACKET
  171.                    | INTO NAME LBRACKET fields RBRACKET VALUES LBRACKET fields RBRACKET'''
  172.  
  173.     if len(p) == 7:
  174.         p[0] = PInsertBody(p[2], [], p[5])
  175.     else:
  176.         p[0] = PInsertBody(p[2], p[4], p[8])
  177.  
  178.  
  179. def p_update(p):
  180.     '''update : UPDATE update_body ENDREQUEST'''
  181.  
  182.     p[0] = p[2]
  183.  
  184.  
  185. def p_update_body(p):
  186.     '''update_body : NAME SET expression
  187.                    | NAME SET expression condition'''
  188.  
  189.     if len(p) == 4:
  190.         p[0] = PUpdate(p[1], p[3])
  191.     else:
  192.         p[0] = PUpdate(p[1], p[3], p[4])
  193.  
  194.  
  195. def p_expression(p):
  196.     '''expression : field operator field
  197.                   | expression COMMA field operator field'''
  198.  
  199.     if len(p) == 4:
  200.         p[0] = [[],[]]
  201.         p[0][0].append(p[1])
  202.         p[0][1].append(p[3])
  203.     else:
  204.         p[0] = p[1]
  205.         p[0][0].append(p[3])
  206.         p[0][1].append(p[5])
  207.  
  208.  
  209. def p_delete(p):
  210.     '''delete : DELETE FROM NAME ENDREQUEST
  211.               | DELETE FROM NAME condition ENDREQUEST'''
  212.  
  213.     if len(p) == 5:
  214.         p[0] = PDelete(p[3])
  215.     else:
  216.         p[0] = PDelete(p[3], p[4])
  217.  
  218.  
  219. def p_fields(p):
  220.     '''fields : NAME
  221.               | fields COMMA NAME'''
  222.  
  223.     if len(p) == 2:
  224.         p[0] = []
  225.         p[0].append(p[1])
  226.     else:
  227.         p[0] = p[1]
  228.         p[0].append(p[3])
  229.  
  230.  
  231. def p_field(p):
  232.     '''field : NAME'''
  233.  
  234.     p[0] = p[1]
  235.  
  236.  
  237. def p_operator(p):
  238.     '''operator : EQUAL'''
  239.  
  240.     p[0] = p[1]
  241.  
  242. def p_condition(p):
  243.     '''condition : WHERE field operator field'''
  244.  
  245.     p[0] = [p[2], p[4]]
  246.  
  247.  
  248. def p_type(p):
  249.     '''type : int
  250.             | str
  251.             | bol
  252.             | bool'''
  253.  
  254.     p[0] = p[1]
  255.  
  256. def p_error(p):
  257.     raise exception.IncorrectSyntax()
  258.  
  259. parser = yacc.yacc()
  260.  
  261.  
  262. def build_tree(code):
  263.  
  264.     result = parser.parse(code)
  265.  
  266.     return result
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top