SQL.g 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. grammar SQL;
  2. options {
  3. language = Cpp;
  4. k = 4;
  5. memoize=true;
  6. }
  7. // Input is a list of statements.
  8. sql_stmt_list: sql_stmt (SEMI sql_stmt)* SEMI? EOF;
  9. lambda_body: (lambda_stmt SEMI)* RETURN expr SEMI?;
  10. lambda_stmt:
  11. named_nodes_stmt
  12. | import_stmt
  13. ;
  14. sql_stmt: (EXPLAIN (Q U E R Y PLAN)?)? sql_stmt_core;
  15. sql_stmt_core:
  16. pragma_stmt
  17. | select_stmt
  18. | named_nodes_stmt
  19. | create_table_stmt
  20. | drop_table_stmt
  21. | use_stmt
  22. | into_table_stmt
  23. | commit_stmt
  24. | update_stmt
  25. | delete_stmt
  26. | rollback_stmt
  27. | declare_stmt
  28. | import_stmt
  29. | export_stmt
  30. | alter_table_stmt
  31. | do_stmt
  32. | define_action_or_subquery_stmt
  33. | evaluate_if_stmt
  34. | evaluate_for_stmt
  35. ;
  36. expr: or_subexpr (OR or_subexpr)*;
  37. or_subexpr: and_subexpr (AND and_subexpr)*;
  38. and_subexpr: xor_subexpr (XOR xor_subexpr)*;
  39. xor_subexpr: eq_subexpr cond_expr?;
  40. cond_expr:
  41. NOT? match_op eq_subexpr (ESCAPE eq_subexpr)?
  42. | NOT? IN COMPACT? in_expr
  43. | (ISNULL | NOTNULL | IS NULL | (IS)? NOT NULL)
  44. | NOT? BETWEEN eq_subexpr AND eq_subexpr
  45. | ((EQUALS | EQUALS2 | NOT_EQUALS | NOT_EQUALS2) eq_subexpr)+ /* order of the eq subexpressions is reversed! */
  46. ;
  47. match_op: LIKE | ILIKE | GLOB | REGEXP | RLIKE | MATCH;
  48. eq_subexpr: neq_subexpr ((LESS | LESS_OR_EQ | GREATER | GREATER_OR_EQ) neq_subexpr)*;
  49. neq_subexpr: bit_subexpr ((SHIFT_LEFT | SHIFT_RIGHT | ROT_LEFT | ROT_RIGHT | AMPERSAND | PIPE | CARET) bit_subexpr)* (DOUBLE_QUESTION neq_subexpr)?;
  50. bit_subexpr: add_subexpr ((PLUS | MINUS) add_subexpr)*;
  51. add_subexpr: mul_subexpr ((ASTERISK | SLASH | PERCENT) mul_subexpr)*;
  52. mul_subexpr: con_subexpr (DOUBLE_PIPE con_subexpr)*;
  53. con_subexpr: unary_subexpr | unary_op unary_subexpr;
  54. unary_op: PLUS | MINUS | TILDA | NOT;
  55. unary_subexpr: (id_expr | atom_expr ) key_expr* (DOT (bind_parameter | DIGITS | id_or_string) key_expr*)* (COLLATE id)?;
  56. in_unary_subexpr: (in_id_expr | in_atom_expr) key_expr* (DOT (bind_parameter | DIGITS | id_or_string) key_expr*)* (COLLATE id)?;
  57. atom_expr:
  58. literal_value
  59. | bind_parameter
  60. | window_function
  61. | lambda
  62. | cast_expr
  63. | exists_expr
  64. | case_expr
  65. | id_or_string NAMESPACE id_or_string
  66. | bitcast_expr
  67. ;
  68. in_atom_expr:
  69. literal_value
  70. | bind_parameter
  71. | in_window_function
  72. | smart_parenthesis
  73. | cast_expr
  74. | case_expr
  75. | LPAREN select_stmt RPAREN
  76. | bitcast_expr
  77. ;
  78. cast_expr: CAST LPAREN expr AS type_name RPAREN;
  79. bitcast_expr: BITCAST LPAREN expr AS type_name RPAREN;
  80. exists_expr: EXISTS LPAREN select_stmt RPAREN;
  81. case_expr: CASE expr? when_expr+ (ELSE expr)? END;
  82. lambda: smart_parenthesis (ARROW LBRACE_CURLY lambda_body RBRACE_CURLY)?;
  83. in_expr: in_unary_subexpr;
  84. // struct, tuple or named list
  85. smart_parenthesis: LPAREN named_expr_list? COMMA? RPAREN;
  86. expr_list: expr (COMMA expr)*;
  87. pure_column_list: LPAREN id_or_string (COMMA id_or_string)* RPAREN;
  88. pure_column_or_named: bind_parameter | id_or_string;
  89. pure_column_or_named_list: LPAREN pure_column_or_named (COMMA pure_column_or_named)* RPAREN;
  90. column_name: opt_id_prefix id_or_string;
  91. column_list: column_name (COMMA column_name)*;
  92. named_expr: expr (AS id_or_string)?;
  93. named_expr_list: named_expr (COMMA named_expr)*;
  94. named_column: column_name (AS id_or_string)?;
  95. named_column_list: named_column (COMMA named_column)*;
  96. call_expr: ((id_or_string NAMESPACE id_or_string) | id_expr | bind_parameter) LPAREN (opt_set_quantifier named_expr_list COMMA? | ASTERISK)? RPAREN;
  97. in_call_expr: ((id_or_string NAMESPACE id_or_string) | in_id_expr | bind_parameter) LPAREN (opt_set_quantifier named_expr_list COMMA? | ASTERISK)? RPAREN;
  98. key_expr: LBRACE_CURLY expr RBRACE_CURLY;
  99. when_expr: WHEN expr THEN expr;
  100. literal_value:
  101. integer
  102. | real
  103. | STRING
  104. | BLOB // it's unused right now
  105. | NULL
  106. | CURRENT_TIME // it's unused right now
  107. | CURRENT_DATE // it's unused right now
  108. | CURRENT_TIMESTAMP // it's unused right now
  109. | bool_value
  110. | EMPTY_ACTION
  111. ;
  112. bind_parameter: DOLLAR id;
  113. bind_parameter_list: bind_parameter (COMMA bind_parameter)*;
  114. named_bind_parameter: bind_parameter (AS bind_parameter)?;
  115. named_bind_parameter_list: named_bind_parameter (COMMA named_bind_parameter)*;
  116. unsigned_number: integer | real;
  117. signed_number: (PLUS | MINUS)? (integer | real);
  118. type_name: id (LPAREN integer (COMMA integer)? RPAREN)?;
  119. flex_type: STRING | type_name;
  120. declare_stmt: DECLARE bind_parameter AS flex_type (EQUALS literal_value)?;
  121. module_path: DOT? id (DOT id)*;
  122. import_stmt: IMPORT module_path SYMBOLS named_bind_parameter_list;
  123. export_stmt: EXPORT bind_parameter_list;
  124. do_stmt: DO (bind_parameter | EMPTY_ACTION) LPAREN expr_list? RPAREN;
  125. pragma_stmt: PRAGMA opt_id_prefix id_or_string (EQUALS pragma_value | LPAREN pragma_value (COMMA pragma_value)* RPAREN)?;
  126. pragma_value:
  127. signed_number
  128. | id
  129. | STRING
  130. | bool_value
  131. | bind_parameter
  132. ;
  133. /// TODO: NULLS FIRST\LAST?
  134. sort_specification: expr (ASC | DESC)?;
  135. sort_specification_list: sort_specification (COMMA sort_specification)*;
  136. select_stmt: select_kind_parenthesis (select_op select_kind_parenthesis)*;
  137. select_kind_parenthesis: select_kind_partial | LPAREN select_kind_partial RPAREN;
  138. select_op: UNION (ALL)? | INTERSECT | EXCEPT;
  139. select_kind_partial: select_kind
  140. (LIMIT expr ((OFFSET | COMMA) expr)?)?
  141. ;
  142. select_kind: (DISCARD)? (process_core | reduce_core | select_core) (INTO RESULT pure_column_or_named)?;
  143. process_core:
  144. PROCESS STREAM? named_single_source (COMMA named_single_source)* (USING call_expr (AS id_or_string)?
  145. (WHERE expr)? (HAVING expr)?)?
  146. ;
  147. reduce_core:
  148. REDUCE named_single_source (COMMA named_single_source)* (PRESORT sort_specification_list)?
  149. ON column_list USING ALL? call_expr (AS id_or_string)?
  150. (WHERE expr)? (HAVING expr)?
  151. ;
  152. opt_set_quantifier: (ALL | DISTINCT)?;
  153. select_core:
  154. (FROM join_source)? SELECT STREAM? opt_set_quantifier result_column (COMMA result_column)* (WITHOUT column_list)? (FROM join_source)? (WHERE expr)?
  155. group_by_clause? (HAVING expr)? window_clause? order_by_clause?
  156. ;
  157. order_by_clause: ORDER BY sort_specification_list;
  158. group_by_clause: GROUP BY opt_set_quantifier grouping_element_list;
  159. grouping_element_list: grouping_element (COMMA grouping_element)*;
  160. grouping_element:
  161. ordinary_grouping_set
  162. | rollup_list
  163. | cube_list
  164. | grouping_sets_specification
  165. //empty_grouping_set inside smart_parenthesis
  166. | hopping_window_specification
  167. ;
  168. /// expect column (named column), or parenthesis list columns, or expression (named expression), or list expression
  169. ordinary_grouping_set: named_expr;
  170. ordinary_grouping_set_list: ordinary_grouping_set (COMMA ordinary_grouping_set)*;
  171. rollup_list: ROLLUP LPAREN ordinary_grouping_set_list RPAREN;
  172. cube_list: CUBE LPAREN ordinary_grouping_set_list RPAREN;
  173. /// SQL2003 grouping_set_list == grouping_element_list
  174. grouping_sets_specification: GROUPING SETS LPAREN grouping_element_list RPAREN;
  175. hopping_window_specification: HOP LPAREN expr COMMA expr COMMA expr COMMA expr RPAREN;
  176. result_column:
  177. opt_id_prefix ASTERISK
  178. | expr (AS id_or_string)?
  179. ;
  180. join_source: flatten_source (join_op flatten_source join_constraint?)*;
  181. ordinary_named_column_list:
  182. named_column
  183. | LPAREN named_column_list RPAREN
  184. ;
  185. flatten_source: named_single_source (FLATTEN ((OPTIONAL|LIST|DICT)? BY ordinary_named_column_list | COLUMNS))?;
  186. named_single_source: single_source (AS id_or_string)? (sample_clause | tablesample_clause)?;
  187. single_source:
  188. table_ref
  189. | LPAREN select_stmt RPAREN
  190. | AT? bind_parameter (LPAREN expr_list? RPAREN)?
  191. ;
  192. sample_clause: SAMPLE expr;
  193. tablesample_clause: TABLESAMPLE sampling_mode LPAREN expr RPAREN repeatable_clause?;
  194. sampling_mode: (BERNOULLI | SYSTEM);
  195. repeatable_clause: REPEATABLE LPAREN expr RPAREN;
  196. join_op:
  197. COMMA
  198. | (NATURAL)? ((LEFT (ONLY | SEMI_JOIN)? | RIGHT (ONLY | SEMI_JOIN)? | EXCLUSION | FULL)? (OUTER)? | INNER | CROSS) JOIN
  199. ;
  200. join_constraint:
  201. ON expr
  202. | USING pure_column_or_named_list
  203. ;
  204. into_table_stmt: (INSERT | INSERT OR ABORT | INSERT OR REVERT | INSERT OR IGNORE | UPSERT | REPLACE) INTO into_simple_table_ref into_values_source;
  205. into_values_source:
  206. pure_column_list? values_source
  207. | DEFAULT VALUES
  208. ;
  209. values_source: VALUES values_source_row_list | select_stmt;
  210. values_source_row_list: values_source_row (COMMA values_source_row)*;
  211. values_source_row: LPAREN expr_list RPAREN;
  212. simple_values_source: expr_list | select_stmt;
  213. create_table_stmt: CREATE TABLE simple_table_ref LPAREN create_table_entry (COMMA create_table_entry)* RPAREN;
  214. create_table_entry: column_schema | table_constraint;
  215. alter_table_stmt: ALTER TABLE simple_table_ref alter_table_action;
  216. alter_table_action: alter_table_add_column | alter_table_drop_column;
  217. alter_table_add_column: ADD COLUMN? column_schema (COMMA ADD COLUMN? column_schema)*;
  218. alter_table_drop_column: DROP COLUMN? id;
  219. column_schema: id_schema flex_type (NOT? NULL)?;
  220. column_order_by_specification: id (ASC | DESC)?;
  221. table_constraint:
  222. PRIMARY KEY LPAREN id (COMMA id)* RPAREN
  223. | PARTITION BY LPAREN id (COMMA id)* RPAREN
  224. | ORDER BY LPAREN column_order_by_specification (COMMA column_order_by_specification)* RPAREN
  225. ;
  226. drop_table_stmt: DROP TABLE (IF EXISTS)? simple_table_ref;
  227. define_action_or_subquery_stmt: DEFINE (ACTION|SUBQUERY) bind_parameter LPAREN bind_parameter_list? RPAREN AS define_action_or_subquery_body END DEFINE;
  228. define_action_or_subquery_body: (sql_stmt_core SEMI)* SEMI?;
  229. evaluate_if_stmt: EVALUATE IF expr do_stmt (ELSE do_stmt)?;
  230. evaluate_for_stmt: EVALUATE FOR bind_parameter IN expr do_stmt (ELSE do_stmt)?;
  231. table_ref: opt_id_prefix (table_key | id_expr LPAREN table_arg (COMMA table_arg)* RPAREN) table_hints?;
  232. table_key: id_table_or_at (COLON id_or_string)?;
  233. table_arg: AT? expr (COLON id_or_string)?;
  234. table_hints: WITH (id_or_string | pure_column_list);
  235. simple_table_ref: ((opt_id_prefix id_or_at) | AT? bind_parameter) table_hints?;
  236. into_simple_table_ref: simple_table_ref (ERASE BY pure_column_list)?;
  237. delete_stmt: DELETE FROM simple_table_ref (WHERE expr | ON into_values_source)?;
  238. update_stmt: UPDATE simple_table_ref (SET set_clause_choice (WHERE expr)? | ON into_values_source);
  239. /// out of 2003 standart
  240. set_clause_choice: set_clause_list | multiple_column_assignment;
  241. set_clause_list: set_clause (COMMA set_clause)*;
  242. set_clause: set_target EQUALS expr;
  243. set_target: column_name;
  244. multiple_column_assignment: set_target_list EQUALS LPAREN simple_values_source RPAREN;
  245. set_target_list: LPAREN set_target (COMMA set_target)* RPAREN;
  246. /// window function supp
  247. // differ from 2003 for resolve conflict
  248. window_function: call_expr (null_treatment? OVER window_name_or_specification)?;
  249. in_window_function: in_call_expr (null_treatment? OVER window_name_or_specification)?;
  250. null_treatment: RESPECT NULLS | IGNORE NULLS;
  251. window_name_or_specification: window_name | in_line_window_specification;
  252. in_line_window_specification: window_specification;
  253. window_name: id;
  254. window_clause: WINDOW window_definition_list;
  255. window_definition_list: window_definition (COMMA window_definition)*;
  256. window_definition: new_window_name AS window_specification;
  257. new_window_name: window_name;
  258. window_specification: LPAREN window_specification_details RPAREN;
  259. window_specification_details:
  260. existing_window_name?
  261. window_partition_clause?
  262. window_order_clause?
  263. window_frame_clause?
  264. ;
  265. existing_window_name: window_name;
  266. window_partition_clause: PARTITION BY named_expr_list;
  267. window_order_clause: order_by_clause;
  268. window_frame_clause: window_frame_units window_frame_extent window_frame_exclusion?;
  269. window_frame_units: ROWS | RANGE;
  270. window_frame_extent: window_frame_start | window_frame_between;
  271. window_frame_start:
  272. UNBOUNDED PRECEDING
  273. | window_frame_preceding
  274. | CURRENT ROW
  275. ;
  276. window_frame_preceding: unsigned_number PRECEDING;
  277. window_frame_following: unsigned_number FOLLOWING;
  278. window_frame_between: BETWEEN window_frame_bound AND window_frame_bound;
  279. window_frame_bound:
  280. window_frame_start
  281. | UNBOUNDED FOLLOWING
  282. | window_frame_following
  283. ;
  284. window_frame_exclusion: EXCLUDE CURRENT ROW | EXCLUDE GROUP | EXCLUDE TIES | EXCLUDE NO OTHERS;
  285. // EXTRAS
  286. use_stmt: USE id_or_string;
  287. named_nodes_stmt: bind_parameter_list EQUALS (expr | LPAREN select_stmt RPAREN);
  288. commit_stmt: COMMIT;
  289. rollback_stmt: ROLLBACK;
  290. // Special rules that allow to use certain keywords as identifiers.
  291. id_or_string: IDENTIFIER | STRING | keyword;
  292. id: IDENTIFIER | keyword;
  293. id_schema: IDENTIFIER | keyword_restricted;
  294. id_expr: IDENTIFIER | keyword_compat | keyword_alter_uncompat | keyword_in_uncompat;
  295. in_id_expr: IDENTIFIER | keyword_compat | keyword_alter_uncompat;
  296. id_table: IDENTIFIER | keyword_restricted;
  297. id_table_or_at: AT? id_table;
  298. id_or_at: AT? id;
  299. opt_id_prefix: (id_or_string DOT)?;
  300. keyword: keyword_restricted | keyword_alter_uncompat | keyword_table_uncompat;
  301. keyword_restricted: keyword_compat | keyword_expr_uncompat | keyword_select_uncompat | keyword_in_uncompat;
  302. keyword_expr_uncompat:
  303. BITCAST
  304. | CASE
  305. | CAST
  306. | CUBE
  307. | CURRENT_TIME
  308. | CURRENT_DATE
  309. | CURRENT_TIMESTAMP
  310. | EMPTY_ACTION
  311. | EXISTS
  312. | FROM
  313. | FULL
  314. | HOP
  315. | NOT
  316. | NULL
  317. | PROCESS
  318. | REDUCE
  319. | RETURN
  320. | ROLLUP
  321. | SELECT
  322. | WHEN
  323. | WHERE
  324. ;
  325. keyword_table_uncompat:
  326. ERASE
  327. | STREAM
  328. ;
  329. keyword_select_uncompat:
  330. ALL
  331. | AS
  332. | DISTINCT
  333. | HAVING
  334. ;
  335. keyword_alter_uncompat:
  336. COLUMN
  337. ;
  338. keyword_in_uncompat:
  339. COMPACT
  340. ;
  341. keyword_compat: (
  342. ABORT
  343. | ACTION
  344. | ADD
  345. | AFTER
  346. | ALTER
  347. | ANALYZE
  348. | AND
  349. | ASC
  350. | ATTACH
  351. | AUTOINCREMENT
  352. | BEFORE
  353. | BEGIN
  354. | BERNOULLI
  355. | BETWEEN
  356. | BY
  357. | CASCADE
  358. | CHECK
  359. | COLLATE
  360. | COLUMNS
  361. | COMMIT
  362. | CONFLICT
  363. | CONSTRAINT
  364. | CREATE
  365. | CROSS
  366. | CURRENT
  367. | DATABASE
  368. | DECLARE
  369. | DEFAULT
  370. | DEFERRABLE
  371. | DEFERRED
  372. | DEFINE
  373. | DELETE
  374. | DESC
  375. | DETACH
  376. | DICT
  377. | DISCARD
  378. | DO
  379. | DROP
  380. | EACH
  381. | ELSE
  382. | END
  383. | ESCAPE
  384. | EVALUATE
  385. | EXCEPT
  386. | EXCLUDE
  387. | EXCLUSIVE
  388. | EXCLUSION
  389. | EXPLAIN
  390. | EXPORT
  391. | FAIL
  392. | FLATTEN
  393. | FOLLOWING
  394. | FOR
  395. | FOREIGN
  396. | GLOB
  397. | GROUP
  398. | GROUPING
  399. | IF
  400. | IGNORE
  401. | ILIKE
  402. | IMMEDIATE
  403. | IMPORT
  404. | IN
  405. | INDEX
  406. | INDEXED
  407. | INITIALLY
  408. | INNER
  409. | INSERT
  410. | INSTEAD
  411. | INTERSECT
  412. | INTO
  413. | IS
  414. | ISNULL
  415. | JOIN
  416. | KEY
  417. | LEFT
  418. | LIKE
  419. | LIMIT
  420. | LIST
  421. | MATCH
  422. | NATURAL
  423. | NULLS
  424. | NO
  425. | NOTNULL
  426. | OF
  427. | OFFSET
  428. | ON
  429. | ONLY
  430. | OPTIONAL
  431. | OR
  432. | ORDER
  433. | OTHERS
  434. | OUTER
  435. | OVER
  436. | PLAN
  437. | PARTITION
  438. | PRAGMA
  439. | PRECEDING
  440. | PRESORT
  441. | PRIMARY
  442. // | QUERY
  443. | RAISE
  444. | RANGE
  445. | REFERENCES
  446. | REGEXP
  447. | REINDEX
  448. | RELEASE
  449. | RENAME
  450. | REPLACE
  451. | RESPECT
  452. | RESTRICT
  453. | RESULT
  454. | REVERT
  455. | RIGHT
  456. | RLIKE
  457. | ROLLBACK
  458. | ROW
  459. | ROWS
  460. | SAMPLE
  461. | SAVEPOINT
  462. | SEMI_JOIN
  463. | SET
  464. | SETS
  465. | SUBQUERY
  466. | SYMBOLS
  467. | SYSTEM
  468. | TABLE
  469. | TABLESAMPLE
  470. | TEMPORARY
  471. | TIES
  472. | THEN
  473. | TO
  474. | TRANSACTION
  475. | TRIGGER
  476. | UNBOUNDED
  477. | UNION
  478. | UNIQUE
  479. | UPDATE
  480. | UPSERT
  481. | USING
  482. | VACUUM
  483. | VALUES
  484. | VIEW
  485. | VIRTUAL
  486. | WINDOW
  487. | WITH
  488. | WITHOUT
  489. | XOR
  490. );
  491. bool_value: (TRUE | FALSE);
  492. //
  493. // Lexer
  494. //
  495. EQUALS: '=';
  496. EQUALS2: '==';
  497. NOT_EQUALS: '!=';
  498. NOT_EQUALS2: '<>';
  499. LESS: '<';
  500. LESS_OR_EQ: '<=';
  501. GREATER: '>';
  502. GREATER_OR_EQ: '>=';
  503. SHIFT_LEFT: '<<';
  504. SHIFT_RIGHT: '>>';
  505. ROT_LEFT: '|<<';
  506. ROT_RIGHT: '>>|';
  507. AMPERSAND: '&';
  508. PIPE: '|';
  509. DOUBLE_PIPE: '||';
  510. PLUS: '+';
  511. MINUS: '-';
  512. TILDA: '~';
  513. ASTERISK: '*';
  514. SLASH: '/';
  515. BACKSLASH: '\\';
  516. PERCENT: '%';
  517. SEMI: ';';
  518. DOT: '.';
  519. COMMA: ',';
  520. LPAREN: '(';
  521. RPAREN: ')';
  522. QUESTION: '?';
  523. DOUBLE_QUESTION: '??';
  524. COLON: ':';
  525. AT: '@';
  526. DOUBLE_AT: '@@';
  527. DOLLAR: '$';
  528. QUOTE_DOUBLE: '"'; // This comment for fix syntax highlighting "
  529. QUOTE_SINGLE: '\'';
  530. APOSTROPHE: '`';
  531. LBRACE_CURLY: '{';
  532. RBRACE_CURLY: '}';
  533. UNDERSCORE: '_';
  534. CARET: '^';
  535. NAMESPACE: '::';
  536. ARROW: '->';
  537. RBRACE_SQUARE: ']';
  538. LBRACE_SQUARE: '['; // pair ]
  539. // http://www.antlr.org/wiki/pages/viewpage.action?pageId=1782
  540. fragment A:('a'|'A');
  541. fragment B:('b'|'B');
  542. fragment C:('c'|'C');
  543. fragment D:('d'|'D');
  544. fragment E:('e'|'E');
  545. fragment F:('f'|'F');
  546. fragment G:('g'|'G');
  547. fragment H:('h'|'H');
  548. fragment I:('i'|'I');
  549. fragment J:('j'|'J');
  550. fragment K:('k'|'K');
  551. fragment L:('l'|'L');
  552. fragment M:('m'|'M');
  553. fragment N:('n'|'N');
  554. fragment O:('o'|'O');
  555. fragment P:('p'|'P');
  556. fragment Q:('q'|'Q');
  557. fragment R:('r'|'R');
  558. fragment S:('s'|'S');
  559. fragment T:('t'|'T');
  560. fragment U:('u'|'U');
  561. fragment V:('v'|'V');
  562. fragment W:('w'|'W');
  563. fragment X:('x'|'X');
  564. fragment Y:('y'|'Y');
  565. fragment Z:('z'|'Z');
  566. ABORT: A B O R T;
  567. ACTION: A C T I O N;
  568. ADD: A D D;
  569. AFTER: A F T E R;
  570. ALL: A L L;
  571. ALTER: A L T E R;
  572. ANALYZE: A N A L Y Z E;
  573. AND: A N D;
  574. AS: A S;
  575. ASC: A S C;
  576. ATTACH: A T T A C H;
  577. AUTOINCREMENT: A U T O I N C R E M E N T;
  578. BEFORE: B E F O R E;
  579. BEGIN: B E G I N;
  580. BERNOULLI: B E R N O U L L I;
  581. BETWEEN: B E T W E E N;
  582. BITCAST: B I T C A S T;
  583. BY: B Y;
  584. CASCADE: C A S C A D E;
  585. CASE: C A S E;
  586. CAST: C A S T;
  587. CHECK: C H E C K;
  588. COLLATE: C O L L A T E;
  589. COLUMN: C O L U M N;
  590. COLUMNS: C O L U M N S;
  591. COMMIT: C O M M I T;
  592. COMPACT: C O M P A C T;
  593. CONFLICT: C O N F L I C T;
  594. CONSTRAINT: C O N S T R A I N T;
  595. CREATE: C R E A T E;
  596. CROSS: C R O S S;
  597. CUBE: C U B E;
  598. CURRENT: C U R R E N T;
  599. CURRENT_TIME: C U R R E N T '_' T I M E;
  600. CURRENT_DATE: C U R R E N T '_' D A T E;
  601. CURRENT_TIMESTAMP: C U R R E N T '_' T I M E S T A M P;
  602. DATABASE: D A T A B A S E;
  603. DECLARE: D E C L A R E;
  604. DEFAULT: D E F A U L T;
  605. DEFERRABLE: D E F E R R A B L E;
  606. DEFERRED: D E F E R R E D;
  607. DEFINE: D E F I N E;
  608. DELETE: D E L E T E;
  609. DESC: D E S C;
  610. DETACH: D E T A C H;
  611. DICT: D I C T;
  612. DISCARD: D I S C A R D;
  613. DISTINCT: D I S T I N C T;
  614. DO: D O;
  615. DROP: D R O P;
  616. EACH: E A C H;
  617. ELSE: E L S E;
  618. EMPTY_ACTION: E M P T Y '_' A C T I O N;
  619. END: E N D;
  620. ERASE: E R A S E;
  621. ESCAPE: E S C A P E;
  622. EVALUATE: E V A L U A T E;
  623. EXCEPT: E X C E P T;
  624. EXCLUDE: E X C L U D E;
  625. EXCLUSIVE: E X C L U S I V E;
  626. EXCLUSION: E X C L U S I O N;
  627. EXISTS: E X I S T S;
  628. EXPLAIN: E X P L A I N;
  629. EXPORT: E X P O R T;
  630. FAIL: F A I L;
  631. FLATTEN: F L A T T E N;
  632. FOLLOWING: F O L L O W I N G;
  633. FOR: F O R;
  634. FOREIGN: F O R E I G N;
  635. FROM: F R O M;
  636. FULL: F U L L;
  637. GLOB: G L O B;
  638. GROUP: G R O U P;
  639. GROUPING: G R O U P I N G;
  640. HAVING: H A V I N G;
  641. HOP: H O P;
  642. IF: I F;
  643. IGNORE: I G N O R E;
  644. ILIKE: I L I K E;
  645. IMMEDIATE: I M M E D I A T E;
  646. IMPORT: I M P O R T;
  647. IN: I N;
  648. INDEX: I N D E X;
  649. INDEXED: I N D E X E D;
  650. INITIALLY: I N I T I A L L Y;
  651. INNER: I N N E R;
  652. INSERT: I N S E R T;
  653. INSTEAD: I N S T E A D;
  654. INTERSECT: I N T E R S E C T;
  655. INTO: I N T O;
  656. IS: I S;
  657. ISNULL: I S N U L L;
  658. JOIN: J O I N;
  659. KEY: K E Y;
  660. LEFT: L E F T;
  661. LIKE: L I K E;
  662. LIMIT: L I M I T;
  663. LIST: L I S T;
  664. MATCH: M A T C H;
  665. NATURAL: N A T U R A L;
  666. NO: N O;
  667. NOT: N O T;
  668. NOTNULL: N O T N U L L;
  669. NULL: N U L L;
  670. NULLS: N U L L S;
  671. OF: O F;
  672. OFFSET: O F F S E T;
  673. ON: O N;
  674. ONLY: O N L Y;
  675. OPTIONAL: O P T I O N A L;
  676. OR: O R;
  677. ORDER: O R D E R;
  678. OTHERS: O T H E R S;
  679. OUTER: O U T E R;
  680. OVER: O V E R;
  681. PARTITION: P A R T I T I O N;
  682. PLAN: P L A N;
  683. PRAGMA: P R A G M A;
  684. PRECEDING: P R E C E D I N G;
  685. PRESORT: P R E S O R T;
  686. PRIMARY: P R I M A R Y;
  687. PROCESS: P R O C E S S;
  688. //QUERY: Q U E R Y;
  689. RAISE: R A I S E;
  690. RANGE: R A N G E;
  691. REDUCE: R E D U C E;
  692. REFERENCES: R E F E R E N C E S;
  693. REGEXP: R E G E X P;
  694. REINDEX: R E I N D E X;
  695. RELEASE: R E L E A S E;
  696. RENAME: R E N A M E;
  697. REPEATABLE: R E P E A T A B L E;
  698. REPLACE: R E P L A C E;
  699. RESPECT: R E S P E C T;
  700. RESTRICT: R E S T R I C T;
  701. RESULT: R E S U L T;
  702. RETURN: R E T U R N;
  703. REVERT: R E V E R T;
  704. RIGHT: R I G H T;
  705. RLIKE: R L I K E;
  706. ROLLBACK: R O L L B A C K;
  707. ROLLUP: R O L L U P;
  708. ROW: R O W;
  709. ROWS: R O W S;
  710. SAMPLE: S A M P L E;
  711. SAVEPOINT: S A V E P O I N T;
  712. SELECT: S E L E C T;
  713. SEMI_JOIN: S E M I;
  714. SET: S E T;
  715. SETS: S E T S;
  716. SUBQUERY: S U B Q U E R Y;
  717. STREAM: S T R E A M;
  718. SYMBOLS: S Y M B O L S;
  719. SYSTEM: S Y S T E M;
  720. TABLE: T A B L E;
  721. TABLESAMPLE: T A B L E S A M P L E;
  722. TEMPORARY: T E M P ( O R A R Y )?;
  723. THEN: T H E N;
  724. TIES: T I E S;
  725. TO: T O;
  726. TRANSACTION: T R A N S A C T I O N;
  727. TRIGGER: T R I G G E R;
  728. UNBOUNDED: U N B O U N D E D;
  729. UNION: U N I O N;
  730. UNIQUE: U N I Q U E;
  731. UPDATE: U P D A T E;
  732. UPSERT: U P S E R T;
  733. USE: U S E;
  734. USING: U S I N G;
  735. VACUUM: V A C U U M;
  736. VALUES: V A L U E S;
  737. VIEW: V I E W;
  738. VIRTUAL: V I R T U A L;
  739. WHEN: W H E N;
  740. WHERE: W H E R E;
  741. WINDOW: W I N D O W;
  742. WITH: W I T H;
  743. WITHOUT: W I T H O U T;
  744. XOR: X O R;
  745. TRUE: T R U E;
  746. FALSE: F A L S E;
  747. fragment STRING_CORE_SINGLE: ( ~(QUOTE_SINGLE | BACKSLASH) | (BACKSLASH .) )*;
  748. fragment STRING_CORE_DOUBLE: ( ~(QUOTE_DOUBLE | BACKSLASH) | (BACKSLASH .) )*;
  749. fragment STRING_SINGLE: (QUOTE_SINGLE STRING_CORE_SINGLE QUOTE_SINGLE);
  750. fragment STRING_DOUBLE: (QUOTE_DOUBLE STRING_CORE_DOUBLE QUOTE_DOUBLE);
  751. fragment STRING_MULTILINE: (DOUBLE_AT .* DOUBLE_AT)+ AT?;
  752. STRING: (STRING_SINGLE | STRING_DOUBLE | STRING_MULTILINE);
  753. fragment ID_START: ('a'..'z' | 'A'..'Z' | UNDERSCORE);
  754. fragment ID_CORE: (ID_START | DIGIT | DOLLAR);
  755. fragment ID_PLAIN: ID_START (ID_CORE)*;
  756. fragment ID_QUOTED_ESCAPE_APOSTROPHE: BACKSLASH APOSTROPHE;
  757. fragment ID_QUOTED_ESCAPE_SQUARE: BACKSLASH (LBRACE_SQUARE | RBRACE_SQUARE);
  758. fragment ID_QUOTED_CORE_SQUARE: (~(LBRACE_SQUARE | RBRACE_SQUARE) | ID_QUOTED_ESCAPE_SQUARE)*;
  759. fragment ID_QUOTED_CORE_APOSTROPHE: (~APOSTROPHE | ID_QUOTED_ESCAPE_APOSTROPHE)*;
  760. fragment ID_QUOTED_SQUARE: LBRACE_SQUARE ID_QUOTED_CORE_SQUARE RBRACE_SQUARE;
  761. fragment ID_QUOTED_APOSTROPHE: APOSTROPHE ID_QUOTED_CORE_APOSTROPHE APOSTROPHE;
  762. fragment ID_QUOTED: ID_QUOTED_SQUARE | ID_QUOTED_APOSTROPHE;
  763. IDENTIFIER: ID_PLAIN | ID_QUOTED;
  764. fragment DIGIT: '0'..'9';
  765. fragment HEXDIGIT: '0'..'9' | 'a'..'f' | 'A'..'F';
  766. fragment HEXDIGITS: '0' X HEXDIGIT+;
  767. fragment OCTDIGITS: '0' O ('0'..'8')+;
  768. fragment BINDIGITS: '0' B ('0' | '1')+;
  769. fragment DECDIGITS: DIGIT+;
  770. DIGITS: DECDIGITS | HEXDIGITS | OCTDIGITS | BINDIGITS;
  771. INTEGER: DIGITS (U? (L | S | T)?);
  772. LEGACY_TINY_INTEGER: DIGITS (U? B?);
  773. integer: DIGITS | INTEGER | LEGACY_TINY_INTEGER;
  774. fragment FLOAT_EXP : E (PLUS | MINUS)? DECDIGITS ;
  775. REAL:
  776. DECDIGITS DOT DIGIT* FLOAT_EXP? F?
  777. | DECDIGITS FLOAT_EXP F?
  778. // | DOT DECDIGITS FLOAT_EXP? // Conflicts with tuple element access through DOT
  779. ;
  780. real: REAL;
  781. BLOB: X QUOTE_SINGLE HEXDIGIT+ QUOTE_SINGLE;
  782. fragment MULTILINE_COMMENT: '/*' ( options {greedy=false;} : . )* '*/';
  783. fragment LINE_COMMENT: '--' ~('\n'|'\r')* ('\n' | '\r' | EOF);
  784. WS: (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;};
  785. COMMENT: (MULTILINE_COMMENT|LINE_COMMENT) {$channel=HIDDEN;};