1
0

pgsql.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. 'use strict';
  6. import { testTokenization } from './testRunner';
  7. testTokenization('sql', [
  8. // Comments
  9. [{
  10. line: '-- a comment',
  11. tokens: [
  12. { startIndex: 0, type: 'comment.sql' }
  13. ]
  14. }],
  15. [{
  16. line: '---sticky -- comment',
  17. tokens: [
  18. { startIndex: 0, type: 'comment.sql' }
  19. ]
  20. }],
  21. [{
  22. line: '-almost a comment',
  23. tokens: [
  24. { startIndex: 0, type: 'operator.sql' },
  25. { startIndex: 1, type: 'identifier.sql' },
  26. { startIndex: 7, type: 'white.sql' },
  27. { startIndex: 8, type: 'identifier.sql' },
  28. { startIndex: 9, type: 'white.sql' },
  29. { startIndex: 10, type: 'identifier.sql' }
  30. ]
  31. }],
  32. [{
  33. line: '/* a full line comment */',
  34. tokens: [
  35. { startIndex: 0, type: 'comment.quote.sql' },
  36. { startIndex: 2, type: 'comment.sql' },
  37. { startIndex: 23, type: 'comment.quote.sql' }
  38. ]
  39. }],
  40. [{
  41. line: '/* /// *** /// */',
  42. tokens: [
  43. { startIndex: 0, type: 'comment.quote.sql' },
  44. { startIndex: 2, type: 'comment.sql' },
  45. { startIndex: 15, type: 'comment.quote.sql' }
  46. ]
  47. }],
  48. [{
  49. line: 'declare _x int = /* a simple comment */ 1;',
  50. tokens: [
  51. { startIndex: 0, type: 'keyword.sql' },
  52. { startIndex: 7, type: 'white.sql' },
  53. { startIndex: 8, type: 'identifier.sql' },
  54. { startIndex: 10, type: 'white.sql' },
  55. { startIndex: 11, type: 'keyword.sql' },
  56. { startIndex: 14, type: 'white.sql' },
  57. { startIndex: 15, type: 'operator.sql' },
  58. { startIndex: 16, type: 'white.sql' },
  59. { startIndex: 17, type: 'comment.quote.sql' },
  60. { startIndex: 19, type: 'comment.sql' },
  61. { startIndex: 37, type: 'comment.quote.sql' },
  62. { startIndex: 39, type: 'white.sql' },
  63. { startIndex: 40, type: 'number.sql' },
  64. { startIndex: 41, type: 'delimiter.sql' }
  65. ]
  66. }],
  67. // Not supporting nested comments, as nested comments seem to not be standard?
  68. // i.e. http://stackoverflow.com/questions/728172/are-there-multiline-comment-delimiters-in-sql-that-are-vendor-agnostic
  69. [{
  70. line: '_x=/* a /* nested comment 1*/;',
  71. tokens: [
  72. { startIndex: 0, type: 'identifier.sql' },
  73. { startIndex: 2, type: 'operator.sql' },
  74. { startIndex: 3, type: 'comment.quote.sql' },
  75. { startIndex: 5, type: 'comment.sql' },
  76. { startIndex: 28, type: 'comment.quote.sql' },
  77. { startIndex: 30, type: 'delimiter.sql' }
  78. ]
  79. }],
  80. [{
  81. line: '_x=/* another comment */ 1*/;',
  82. tokens: [
  83. { startIndex: 0, type: 'identifier.sql' },
  84. { startIndex: 2, type: 'operator.sql' },
  85. { startIndex: 3, type: 'comment.quote.sql' },
  86. { startIndex: 5, type: 'comment.sql' },
  87. { startIndex: 22, type: 'comment.quote.sql' },
  88. { startIndex: 24, type: 'white.sql' },
  89. { startIndex: 25, type: 'number.sql' },
  90. { startIndex: 26, type: 'operator.sql' },
  91. { startIndex: 28, type: 'delimiter.sql' }
  92. ]
  93. }],
  94. [{
  95. line: '_x=/*/;',
  96. tokens: [
  97. { startIndex: 0, type: 'identifier.sql' },
  98. { startIndex: 2, type: 'operator.sql' },
  99. { startIndex: 3, type: 'comment.quote.sql' },
  100. { startIndex: 5, type: 'comment.sql' }
  101. ]
  102. }],
  103. // Numbers
  104. [{
  105. line: '123',
  106. tokens: [
  107. { startIndex: 0, type: 'number.sql' }
  108. ]
  109. }],
  110. [{
  111. line: '-123',
  112. tokens: [
  113. { startIndex: 0, type: 'operator.sql' },
  114. { startIndex: 1, type: 'number.sql' }
  115. ]
  116. }],
  117. [{
  118. line: '0xaBc123',
  119. tokens: [
  120. { startIndex: 0, type: 'number.sql' }
  121. ]
  122. }],
  123. [{
  124. line: '0XaBc123',
  125. tokens: [
  126. { startIndex: 0, type: 'number.sql' }
  127. ]
  128. }],
  129. [{
  130. line: '0x',
  131. tokens: [
  132. { startIndex: 0, type: 'number.sql' }
  133. ]
  134. }],
  135. [{
  136. line: '0x0',
  137. tokens: [
  138. { startIndex: 0, type: 'number.sql' }
  139. ]
  140. }],
  141. [{
  142. line: '0xAB_CD',
  143. tokens: [
  144. { startIndex: 0, type: 'number.sql' },
  145. { startIndex: 4, type: 'identifier.sql' }
  146. ]
  147. }],
  148. [{
  149. line: '$',
  150. tokens: [
  151. { startIndex: 0, type: 'number.sql' }
  152. ]
  153. }],
  154. [{
  155. line: '$-123',
  156. tokens: [
  157. { startIndex: 0, type: 'number.sql' }
  158. ]
  159. }],
  160. [{
  161. line: '$-+-123',
  162. tokens: [
  163. { startIndex: 0, type: 'number.sql' }
  164. ]
  165. }],
  166. [{
  167. line: '$123.5678',
  168. tokens: [
  169. { startIndex: 0, type: 'number.sql' }
  170. ]
  171. }],
  172. [{
  173. line: '$0.99',
  174. tokens: [
  175. { startIndex: 0, type: 'number.sql' }
  176. ]
  177. }],
  178. [{
  179. line: '$.99',
  180. tokens: [
  181. { startIndex: 0, type: 'number.sql' }
  182. ]
  183. }],
  184. [{
  185. line: '$99.',
  186. tokens: [
  187. { startIndex: 0, type: 'number.sql' }
  188. ]
  189. }],
  190. [{
  191. line: '$0.',
  192. tokens: [
  193. { startIndex: 0, type: 'number.sql' }
  194. ]
  195. }],
  196. [{
  197. line: '$.0',
  198. tokens: [
  199. { startIndex: 0, type: 'number.sql' }
  200. ]
  201. }],
  202. [{
  203. line: '.',
  204. tokens: [
  205. { startIndex: 0, type: 'delimiter.sql' }
  206. ]
  207. }],
  208. [{
  209. line: '123',
  210. tokens: [
  211. { startIndex: 0, type: 'number.sql' }
  212. ]
  213. }],
  214. [{
  215. line: '123.5678',
  216. tokens: [
  217. { startIndex: 0, type: 'number.sql' }
  218. ]
  219. }],
  220. [{
  221. line: '0.99',
  222. tokens: [
  223. { startIndex: 0, type: 'number.sql' }
  224. ]
  225. }],
  226. [{
  227. line: '.99',
  228. tokens: [
  229. { startIndex: 0, type: 'number.sql' }
  230. ]
  231. }],
  232. [{
  233. line: '99.',
  234. tokens: [
  235. { startIndex: 0, type: 'number.sql' }
  236. ]
  237. }],
  238. [{
  239. line: '0.',
  240. tokens: [
  241. { startIndex: 0, type: 'number.sql' }
  242. ]
  243. }],
  244. [{
  245. line: '.0',
  246. tokens: [
  247. { startIndex: 0, type: 'number.sql' }
  248. ]
  249. }],
  250. [{
  251. line: '1E-2',
  252. tokens: [
  253. { startIndex: 0, type: 'number.sql' }
  254. ]
  255. }],
  256. [{
  257. line: '1E+2',
  258. tokens: [
  259. { startIndex: 0, type: 'number.sql' }
  260. ]
  261. }],
  262. [{
  263. line: '1E2',
  264. tokens: [
  265. { startIndex: 0, type: 'number.sql' }
  266. ]
  267. }],
  268. [{
  269. line: '0.1E2',
  270. tokens: [
  271. { startIndex: 0, type: 'number.sql' }
  272. ]
  273. }],
  274. [{
  275. line: '1.E2',
  276. tokens: [
  277. { startIndex: 0, type: 'number.sql' }
  278. ]
  279. }],
  280. [{
  281. line: '.1E2',
  282. tokens: [
  283. { startIndex: 0, type: 'number.sql' }
  284. ]
  285. }],
  286. // Identifiers
  287. [{
  288. line: '_abc$01',
  289. tokens: [
  290. { startIndex: 0, type: 'identifier.sql' }
  291. ]
  292. }],
  293. [{
  294. line: '#abc$01',
  295. tokens: [
  296. { startIndex: 0, type: 'identifier.sql' }
  297. ]
  298. }],
  299. [{
  300. line: '##abc$01',
  301. tokens: [
  302. { startIndex: 0, type: 'identifier.sql' }
  303. ]
  304. }],
  305. [{
  306. line: '@abc$01',
  307. tokens: [
  308. { startIndex: 0, type: 'identifier.sql' }
  309. ]
  310. }],
  311. [{
  312. line: '@@abc$01',
  313. tokens: [
  314. { startIndex: 0, type: 'identifier.sql' }
  315. ]
  316. }],
  317. [{
  318. line: '$abc',
  319. tokens: [
  320. { startIndex: 0, type: 'identifier.sql' }
  321. ]
  322. }],
  323. [{
  324. line: '$nonexistent',
  325. tokens: [
  326. { startIndex: 0, type: 'identifier.sql' }
  327. ]
  328. }],
  329. [{
  330. line: '@@nonexistent',
  331. tokens: [
  332. { startIndex: 0, type: 'identifier.sql' }
  333. ]
  334. }],
  335. [{
  336. line: 'declare "abc 321";',
  337. tokens: [
  338. { startIndex: 0, type: 'keyword.sql' },
  339. { startIndex: 7, type: 'white.sql' },
  340. { startIndex: 8, type: 'identifier.quote.sql' },
  341. { startIndex: 9, type: 'identifier.sql' },
  342. { startIndex: 16, type: 'identifier.quote.sql' },
  343. { startIndex: 17, type: 'delimiter.sql' }
  344. ]
  345. }],
  346. [{
  347. line: '"abc"" 321 "" xyz"',
  348. tokens: [
  349. { startIndex: 0, type: 'identifier.quote.sql' },
  350. { startIndex: 1, type: 'identifier.sql' },
  351. { startIndex: 17, type: 'identifier.quote.sql' }
  352. ]
  353. }],
  354. [{
  355. line: '"abc',
  356. tokens: [
  357. { startIndex: 0, type: 'identifier.quote.sql' },
  358. { startIndex: 1, type: 'identifier.sql' }
  359. ]
  360. }],
  361. [{
  362. line: 'declare "abc 321";',
  363. tokens: [
  364. { startIndex: 0, type: 'keyword.sql' },
  365. { startIndex: 7, type: 'white.sql' },
  366. { startIndex: 8, type: 'identifier.quote.sql' },
  367. { startIndex: 9, type: 'identifier.sql' },
  368. { startIndex: 16, type: 'identifier.quote.sql' },
  369. { startIndex: 17, type: 'delimiter.sql' }
  370. ]
  371. }],
  372. [{
  373. line: '"abc"" 321 "" xyz"',
  374. tokens: [
  375. { startIndex: 0, type: 'identifier.quote.sql' },
  376. { startIndex: 1, type: 'identifier.sql' },
  377. { startIndex: 17, type: 'identifier.quote.sql' }
  378. ]
  379. }],
  380. [{
  381. line: '"abc',
  382. tokens: [
  383. { startIndex: 0, type: 'identifier.quote.sql' },
  384. { startIndex: 1, type: 'identifier.sql' }
  385. ]
  386. }],
  387. [{
  388. line: 'int',
  389. tokens: [
  390. { startIndex: 0, type: 'keyword.sql' }
  391. ]
  392. }],
  393. [{
  394. line: '"int"',
  395. tokens: [
  396. { startIndex: 0, type: 'identifier.quote.sql' },
  397. { startIndex: 1, type: 'identifier.sql' },
  398. { startIndex: 4, type: 'identifier.quote.sql' }
  399. ]
  400. }],
  401. // Strings
  402. [{
  403. line: 'declare _x=\'a string\';',
  404. tokens: [
  405. { startIndex: 0, type: 'keyword.sql' },
  406. { startIndex: 7, type: 'white.sql' },
  407. { startIndex: 8, type: 'identifier.sql' },
  408. { startIndex: 10, type: 'operator.sql' },
  409. { startIndex: 11, type: 'string.sql' },
  410. { startIndex: 21, type: 'delimiter.sql' }
  411. ]
  412. }],
  413. [{
  414. line: '\'a \'\' string with quotes\'',
  415. tokens: [
  416. { startIndex: 0, type: 'string.sql' },
  417. ]
  418. }],
  419. [{
  420. line: '\'a -- string with comment\'',
  421. tokens: [
  422. { startIndex: 0, type: 'string.sql' },
  423. ]
  424. }],
  425. [{
  426. line: '\'a endless string',
  427. tokens: [
  428. { startIndex: 0, type: 'string.sql' },
  429. ]
  430. }],
  431. // Operators
  432. [{
  433. line: 'x=x+1',
  434. tokens: [
  435. { startIndex: 0, type: 'identifier.sql' },
  436. { startIndex: 1, type: 'operator.sql' },
  437. { startIndex: 2, type: 'identifier.sql' },
  438. { startIndex: 3, type: 'operator.sql' },
  439. { startIndex: 4, type: 'number.sql' }
  440. ]
  441. }],
  442. [{
  443. line: '_x^=_x',
  444. tokens: [
  445. { startIndex: 0, type: 'identifier.sql' },
  446. { startIndex: 2, type: 'operator.sql' },
  447. { startIndex: 4, type: 'identifier.sql' }
  448. ]
  449. }],
  450. [{
  451. line: 'WHERE x IS NOT NULL',
  452. tokens: [
  453. { startIndex: 0, type: 'keyword.sql' },
  454. { startIndex: 5, type: 'white.sql' },
  455. { startIndex: 6, type: 'identifier.sql' },
  456. { startIndex: 7, type: 'white.sql' },
  457. { startIndex: 8, type: 'operator.sql' },
  458. { startIndex: 10, type: 'white.sql' },
  459. { startIndex: 11, type: 'operator.sql' },
  460. { startIndex: 14, type: 'white.sql' },
  461. { startIndex: 15, type: 'operator.sql' }
  462. ]
  463. }],
  464. [{
  465. line: 'SELECT * FROM sch.MyTable WHERE MyColumn IN (1,2)',
  466. tokens: [
  467. { startIndex: 0, type: 'keyword.sql' },
  468. { startIndex: 6, type: 'white.sql' },
  469. { startIndex: 7, type: 'operator.sql' },
  470. { startIndex: 8, type: 'white.sql' },
  471. { startIndex: 9, type: 'keyword.sql' },
  472. { startIndex: 13, type: 'white.sql' },
  473. { startIndex: 14, type: 'identifier.sql' },
  474. { startIndex: 17, type: 'delimiter.sql' },
  475. { startIndex: 18, type: 'identifier.sql' },
  476. { startIndex: 25, type: 'white.sql' },
  477. { startIndex: 26, type: 'keyword.sql' },
  478. { startIndex: 31, type: 'white.sql' },
  479. { startIndex: 32, type: 'identifier.sql' },
  480. { startIndex: 40, type: 'white.sql' },
  481. { startIndex: 41, type: 'operator.sql' },
  482. { startIndex: 43, type: 'white.sql' },
  483. { startIndex: 44, type: 'delimiter.parenthesis.sql' },
  484. { startIndex: 45, type: 'number.sql' },
  485. { startIndex: 46, type: 'delimiter.sql' },
  486. { startIndex: 47, type: 'number.sql' },
  487. { startIndex: 48, type: 'delimiter.parenthesis.sql' }
  488. ]
  489. }]
  490. ]);