SQL parser syntax

BNF token specification

  1. DOCUMENT START
  2. TOKENS
  3. <DEFAULT> SKIP : {
  4. " "
  5. | "\t"
  6. | "\n"
  7. | "\r"
  8. }
  9. /** reserved words **/<DEFAULT> TOKEN : {
  10. <SELECT: ("s" | "S") ("e" | "E") ("l" | "L") ("e" | "E") ("c" | "C") ("t" | "T")>
  11. | <INSERT: ("i" | "I") ("n" | "N") ("s" | "S") ("e" | "E") ("r" | "R") ("t" | "T")>
  12. | <UPDATE: ("u" | "U") ("p" | "P") ("d" | "D") ("a" | "A") ("t" | "T") ("e" | "E")>
  13. | <DELETE: ("d" | "D") ("e" | "E") ("l" | "L") ("e" | "E") ("t" | "T") ("e" | "E")>
  14. | <FROM: ("f" | "F") ("r" | "R") ("o" | "O") ("m" | "M")>
  15. | <WHERE: ("w" | "W") ("h" | "H") ("e" | "E") ("r" | "R") ("e" | "E")>
  16. | <INTO: ("i" | "I") ("n" | "N") ("t" | "T") ("o" | "O")>
  17. | <VALUES: ("v" | "V") ("a" | "A") ("l" | "L") ("u" | "U") ("e" | "E") ("s" | "S")>
  18. | <SET: ("s" | "S") ("e" | "E") ("t" | "T")>
  19. | <ADD: ("a" | "A") ("d" | "D") ("d" | "D")>
  20. | <REMOVE: ("r" | "R") ("e" | "E") ("m" | "M") ("o" | "O") ("v" | "V") ("e" | "E")>
  21. | <AND: ("a" | "A") ("n" | "N") ("d" | "D")>
  22. | <OR: ("o" | "O") ("r" | "R")>
  23. | <NULL: ("N" | "n") ("U" | "u") ("L" | "l") ("L" | "l")>
  24. | <ORDER: ("o" | "O") ("r" | "R") ("d" | "D") ("e" | "E") ("r" | "R")>
  25. | <BY: ("b" | "B") ("y" | "Y")>
  26. | <LIMIT: ("l" | "L") ("i" | "I") ("m" | "M") ("i" | "I") ("t" | "T")>
  27. | <RANGE: ("r" | "R") ("a" | "A") ("n" | "N") ("g" | "G") ("e" | "E")>
  28. | <ASC: ("a" | "A") ("s" | "S") ("c" | "C")>
  29. | <AS: ("a" | "A") ("s" | "S")>
  30. | <DESC: ("d" | "D") ("e" | "E") ("s" | "S") ("c" | "C")>
  31. | <THIS: "@this">
  32. | <RECORD_ATTRIBUTE: <RID_ATTR> | <CLASS_ATTR> | <VERSION_ATTR> | <SIZE_ATTR> | <TYPE_ATTR>>
  33. | <#RID_ATTR: "@rid">
  34. | <#CLASS_ATTR: "@class">
  35. | <#VERSION_ATTR: "@version">
  36. | <#SIZE_ATTR: "@size">
  37. | <#TYPE_ATTR: "@type">
  38. }
  39. /** LITERALS **/<DEFAULT> TOKEN : {
  40. <INTEGER_LITERAL: <DECIMAL_LITERAL> ([| <HEX_LITERAL> (["l","L"]("l","L"].md)?))? | <OCTAL_LITERAL> ([| <#DECIMAL_LITERAL: ["1"-"9"]("l","L"].md)?>) ([| <#HEX_LITERAL: "0" ["x","X"]("0"-"9"].md)*>) ([| <#OCTAL_LITERAL: "0" (["0"-"7"]("0"-"9","a"-"f","A"-"F"].md)+>))**>
  41. | <FLOATING_POINT_LITERAL: <DECIMAL_FLOATING_POINT_LITERAL> | <HEXADECIMAL_FLOATING_POINT_LITERAL>>
  42. | <#DECIMAL_FLOATING_POINT_LITERAL: (["." (["0"-"9"]("0"-"9"].md)+))** (<DECIMAL_EXPONENT>)? ([| "." (["0"-"9"]("f","F","d","D"].md)?))+ (<DECIMAL_EXPONENT>)? ([| (["0"-"9"]("f","F","d","D"].md)?))+ <DECIMAL_EXPONENT> ([| (["0"-"9"]("f","F","d","D"].md)?))+ (<DECIMAL_EXPONENT>)? [| <#DECIMAL_EXPONENT: ["e","E"]("f","F","d","D"]>.md) ([(["0"-"9"]("+","-"].md)?))+>
  43. | <#HEXADECIMAL_FLOATING_POINT_LITERAL: "0" [(["0"-"9","a"-"f","A"-"F"]("x","X"].md))+ (".")? <HEXADECIMAL_EXPONENT> ([| "0" ["x","X"]("f","F","d","D"].md)?) (["." (["0"-"9","a"-"f","A"-"F"]("0"-"9","a"-"f","A"-"F"].md)*))+ <HEXADECIMAL_EXPONENT> ([| <#HEXADECIMAL_EXPONENT: ["p","P"]("f","F","d","D"].md)?>) ([(["0"-"9"]("+","-"].md)?))+>
  44. | <CHARACTER_LITERAL: "\'" (~[| "\\" (["n","t","b","r","f","\\","\'","\""]("\'","\\","\n","\r"].md) | [(["0"-"7"]("0"-"7"].md))? | ["0"-"7"]("0"-"7".md)"0"-"3"]) ["\'">
  45. | <STRING_LITERAL: "\"" (~["\"","\\","\n","\r"]("0"-"7"].md))) | "\\" ([| ["0"-"7"]("n","t","b","r","f","\\","\'","\""].md) ([| ["0"-"3"]("0"-"7"].md)?) ["0"-"7"]("0"-"7".md)"0"-"7"])))** "\"" | "\'" (~[| "\\" (["n","t","b","r","f","\\","\'","\""]("\'","\\","\n","\r"].md) | [(["0"-"7"]("0"-"7"].md))? | ["0"-"7"]("0"-"7".md)"0"-"3"]) ["\'">
  46. }
  47. /* SEPARATORS */<DEFAULT> TOKEN : {
  48. <LPAREN: "(">
  49. | <RPAREN: ")">
  50. | <LBRACE: "{">
  51. | <RBRACE: "}">
  52. | <LBRACKET: "[">
  53. | <RBRACKET: "]("0"-"7"].md))*)">
  54. | <SEMICOLON: ";">
  55. | <COMMA: ",">
  56. | <DOT: ".">
  57. | <AT: "@">
  58. }
  59. /** OPERATORS **/<DEFAULT> TOKEN : {
  60. <EQ: "=">
  61. | <LT: "<">
  62. | <GT: ">">
  63. | <BANG: "!">
  64. | <TILDE: "~">
  65. | <HOOK: "?">
  66. | <COLON: ":">
  67. | <LE: "<=">
  68. | <GE: ">=">
  69. | <NE: "!=">
  70. | <NEQ: "<>">
  71. | <SC_OR: "||">
  72. | <SC_AND: "&&">
  73. | <INCR: "++">
  74. | <DECR: "--">
  75. | <PLUS: "+">
  76. | <MINUS: "-">
  77. | <STAR: "**">
  78. | <SLASH: "/">
  79. | <BIT_AND: "&">
  80. | <BIT_OR: "|">
  81. | <XOR: "^">
  82. | <REM: "%">
  83. | <LSHIFT: "<<">
  84. | <PLUSASSIGN: "+=">
  85. | <MINUSASSIGN: "-=">
  86. | <STARASSIGN: "**=">
  87. | <SLASHASSIGN: "/=">
  88. | <ANDASSIGN: "&=">
  89. | <ORASSIGN: "|=">
  90. | <XORASSIGN: "^=">
  91. | <REMASSIGN: "%=">
  92. | <LSHIFTASSIGN: "<<=">
  93. | <RSIGNEDSHIFTASSIGN: ">>=">
  94. | <RUNSIGNEDSHIFTASSIGN: ">>>=">
  95. | <ELLIPSIS: "...">
  96. | <NOT: ("N" | "n") ("O" | "o") ("T" | "t")>
  97. | <LIKE: ("L" | "l") ("I" | "i") ("K" | "k") ("E" | "e")>
  98. | <IS: "is" | "IS" | "Is" | "iS">
  99. | <IN: "in" | "IN" | "In" | "iN">
  100. | <BETWEEN: ("B" | "b") ("E" | "e") ("T" | "t") ("W" | "w") ("E" | "e") ("E" | "e") ("N" | "n")>
  101. | <CONTAINS: ("C" | "c") ("O" | "o") ("N" | "n") ("T" | "t") ("A" | "a") ("I" | "i") ("N" | "n") ("S" | "s")>
  102. | <CONTAINSALL: ("C" | "c") ("O" | "o") ("N" | "n") ("T" | "t") ("A" | "a") ("I" | "i") ("N" | "n") ("S" | "s") ("A" | "a") ("L" | "l") ("L" | "l")>
  103. | <CONTAINSKEY: ("C" | "c") ("O" | "o") ("N" | "n") ("T" | "t") ("A" | "a") ("I" | "i") ("N" | "n") ("S" | "s") ("K" | "k") ("E" | "e") ("Y" | "y")>
  104. | <CONTAINSVALUE: ("C" | "c") ("O" | "o") ("N" | "n") ("T" | "t") ("A" | "a") ("I" | "i") ("N" | "n") ("S" | "s") ("V" | "v") ("A" | "a") ("L" | "l") ("U" | "u") ("E" | "e")>
  105. | <CONTAINSTEXT: ("C" | "c") ("O" | "o") ("N" | "n") ("T" | "t") ("A" | "a") ("I" | "i") ("N" | "n") ("S" | "s") ("T" | "t") ("E" | "e") ("X" | "x") ("T" | "t")>
  106. | <MATCHES: ("M" | "m") ("A" | "a") ("T" | "t") ("C" | "c") ("H" | "h") ("E" | "e") ("S" | "s")>
  107. | <TRAVERSE: ("T" | "t") ("R" | "r") ("A" | "a") ("V" | "v") ("E" | "e") ("R" | "r") ("S" | "s") ("E" | "e")>
  108. }
  109. <DEFAULT> TOKEN : {
  110. <IDENTIFIER: <LETTER> (<PART_LETTER>)**>
  111. | <#LETTER: [| <#PART_LETTER: ["0"-"9","A"-"Z","_","a"-"z"]("A"-"Z","_","a"-"z"]>.md)>
  112. }
  113. NON-TERMINALS
  114. Rid := "#" <INTEGER_LITERAL> <COLON> <INTEGER_LITERAL>
  115. | <INTEGER_LITERAL> <COLON> <INTEGER_LITERAL>
  116. /** Root production. **/ OrientGrammar := Statement <EOF>
  117. Statement := ( SelectStatement | DeleteStatement | InsertStatement | UpdateStatement )
  118. SelectStatement := <SELECT> ( Projection )? <FROM> FromClause ( <WHERE> WhereClause )? ( OrderBy )? ( Limit )? ( Range )?
  119. DeleteStatement := <DELETE> <FROM> <IDENTIFIER> ( <WHERE> WhereClause )?
  120. UpdateStatement := <UPDATE> ( <IDENTIFIER> | Cluster | IndexIdentifier ) ( ( <SET> UpdateItem ( "," UpdateItem )** ) ) ( <WHERE> WhereClause )?
  121. UpdateItem := <IDENTIFIER> <EQ> ( <NULL> | <STRING_LITERAL> | Rid | <INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> | <CHARACTER_LITERAL> | <LBRACKET> Rid ( "," Rid )** <RBRACKET> )
  122. UpdateAddItem := <IDENTIFIER> <EQ> ( <STRING_LITERAL> | Rid | <INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> | <CHARACTER_LITERAL> | <LBRACKET> Rid ( "," Rid )** <RBRACKET> )
  123. InsertStatement := <INSERT> <INTO> ( <IDENTIFIER> | Cluster ) <LPAREN> <IDENTIFIER> ( "," <IDENTIFIER> ) <RPAREN> <VALUES> <LPAREN> InsertExpression ( "," InsertExpression ) <RPAREN>
  124. InsertExpression := <NULL>
  125. | <STRING_LITERAL>
  126. | <INTEGER_LITERAL>
  127. | <FLOATING_POINT_LITERAL>
  128. | Rid
  129. | <CHARACTER_LITERAL>
  130. | <LBRACKET> Rid ( "," Rid )** <RBRACKET>
  131. InputParameter := "?"
  132. Projection := ProjectionItem ( "," ProjectionItem )**
  133. ProjectionItem := "**"
  134. | ( ( <NULL> | <INTEGER_LITERAL> | <STRING_LITERAL> | <FLOATING_POINT_LITERAL> | <CHARACTER_LITERAL> | FunctionCall | DottedIdentifier | RecordAttribute | ThisOperation | InputParameter ) ( <AS> Alias )? )
  135. FilterItem := <NULL>
  136. | Any
  137. | All
  138. | <INTEGER_LITERAL>
  139. | <STRING_LITERAL>
  140. | <FLOATING_POINT_LITERAL>
  141. | <CHARACTER_LITERAL>
  142. | FunctionCall
  143. | DottedIdentifier
  144. | RecordAttribute
  145. | ThisOperation
  146. | InputParameter
  147. Alias := <IDENTIFIER>
  148. Any := "any()"
  149. All := "all()"
  150. RecordAttribute := <RECORD_ATTRIBUTE>
  151. ThisOperation := <THIS> ( FieldOperator )**
  152. FunctionCall := <IDENTIFIER> <LPAREN> ( "**" | ( FilterItem ( "," FilterItem )** ) ) <RPAREN> ( FieldOperator )**
  153. FieldOperator := ( <DOT> <IDENTIFIER> <LPAREN> ( FilterItem ( "," FilterItem )** )? <RPAREN> )
  154. | ( "[<STRING_LITERAL> "](".md)" )
  155. DottedIdentifier := <IDENTIFIER> ( "[WhereClause "](".md)" )+
  156. | <IDENTIFIER> ( FieldOperator )+
  157. | <IDENTIFIER> ( <DOT> DottedIdentifier )?
  158. FromClause := FromItem
  159. FromItem := Rid
  160. | <LBRACKET> Rid ( "," Rid )** <RBRACKET>
  161. | Cluster
  162. | IndexIdentifier
  163. | <IDENTIFIER>
  164. Cluster := "cluster:" <IDENTIFIER>
  165. IndexIdentifier := "index:" <IDENTIFIER>
  166. WhereClause := OrBlock
  167. OrBlock := AndBlock ( <OR> AndBlock )**
  168. AndBlock := ( NotBlock ) ( <AND> ( NotBlock ) )**
  169. NotBlock := ( <NOT> )? ( ConditionBlock | ParenthesisBlock )
  170. ParenthesisBlock := <LPAREN> OrBlock <RPAREN>
  171. ConditionBlock := TraverseCondition
  172. | IsNotNullCondition
  173. | IsNullCondition
  174. | BinaryCondition
  175. | BetweenCondition
  176. | ContainsCondition
  177. | ContainsTextCondition
  178. | MatchesCondition
  179. CompareOperator := EqualsCompareOperator
  180. | LtOperator
  181. | GtOperator
  182. | NeOperator
  183. | NeqOperator
  184. | GeOperator
  185. | LeOperator
  186. | InOperator
  187. | NotInOperator
  188. | LikeOperator
  189. | ContainsKeyOperator
  190. | ContainsValueOperator
  191. LtOperator := <LT>
  192. GtOperator := <GT>
  193. NeOperator := <NE>
  194. NeqOperator := <NEQ>
  195. GeOperator := <GE>
  196. LeOperator := <LE>
  197. InOperator := <IN>
  198. NotInOperator := <NOT> <IN>
  199. LikeOperator := <LIKE>
  200. ContainsKeyOperator := <CONTAINSKEY>
  201. ContainsValueOperator := <CONTAINSVALUE>
  202. EqualsCompareOperator := <EQ>
  203. BinaryCondition := FilterItem CompareOperator ( Rid | FilterItem )
  204. BetweenCondition := FilterItem <BETWEEN> FilterItem <AND> FilterItem
  205. IsNullCondition := FilterItem <IS> <NULL>
  206. IsNotNullCondition := FilterItem <IS> <NOT> <NULL>
  207. ContainsCondition := FilterItem <CONTAINS> <LPAREN> OrBlock <RPAREN>
  208. ContainsAllCondition := FilterItem <CONTAINSALL> <LPAREN> OrBlock <RPAREN>
  209. ContainsTextCondition := FilterItem <CONTAINSTEXT> ( <STRING_LITERAL> | DottedIdentifier )
  210. MatchesCondition := FilterItem <MATCHES> <STRING_LITERAL>
  211. TraverseCondition := <TRAVERSE> ( <LPAREN> <INTEGER_LITERAL> ( "," <INTEGER_LITERAL> ( "," TraverseFields )? )? <RPAREN> )? <LPAREN> OrBlock <RPAREN>
  212. TraverseFields := <STRING_LITERAL>
  213. OrderBy := <ORDER> <BY> <IDENTIFIER> ( "," <IDENTIFIER> )** ( <DESC> | <ASC> )?
  214. Limit := <LIMIT> <INTEGER_LITERAL>
  215. Range := <RANGE> Rid ( "," Rid )?
  216. DOCUMENT END