sql.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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: '$action',
  325. tokens: [
  326. { startIndex: 0, type: 'predefined.sql' }
  327. ]
  328. }],
  329. [{
  330. line: '$nonexistent',
  331. tokens: [
  332. { startIndex: 0, type: 'identifier.sql' }
  333. ]
  334. }],
  335. [{
  336. line: '@@DBTS',
  337. tokens: [
  338. { startIndex: 0, type: 'predefined.sql' }
  339. ]
  340. }],
  341. [{
  342. line: '@@nonexistent',
  343. tokens: [
  344. { startIndex: 0, type: 'identifier.sql' }
  345. ]
  346. }],
  347. [{
  348. line: 'declare [abc 321];',
  349. tokens: [
  350. { startIndex: 0, type: 'keyword.sql' },
  351. { startIndex: 7, type: 'white.sql' },
  352. { startIndex: 8, type: 'identifier.quote.sql' },
  353. { startIndex: 9, type: 'identifier.sql' },
  354. { startIndex: 16, type: 'identifier.quote.sql' },
  355. { startIndex: 17, type: 'delimiter.sql' }
  356. ]
  357. }],
  358. [{
  359. line: '[abc[[ 321 ]] xyz]',
  360. tokens: [
  361. { startIndex: 0, type: 'identifier.quote.sql' },
  362. { startIndex: 1, type: 'identifier.sql' },
  363. { startIndex: 17, type: 'identifier.quote.sql' }
  364. ]
  365. }],
  366. [{
  367. line: '[abc',
  368. tokens: [
  369. { startIndex: 0, type: 'identifier.quote.sql' },
  370. { startIndex: 1, type: 'identifier.sql' }
  371. ]
  372. }],
  373. [{
  374. line: 'declare "abc 321";',
  375. tokens: [
  376. { startIndex: 0, type: 'keyword.sql' },
  377. { startIndex: 7, type: 'white.sql' },
  378. { startIndex: 8, type: 'identifier.quote.sql' },
  379. { startIndex: 9, type: 'identifier.sql' },
  380. { startIndex: 16, type: 'identifier.quote.sql' },
  381. { startIndex: 17, type: 'delimiter.sql' }
  382. ]
  383. }],
  384. [{
  385. line: '"abc"" 321 "" xyz"',
  386. tokens: [
  387. { startIndex: 0, type: 'identifier.quote.sql' },
  388. { startIndex: 1, type: 'identifier.sql' },
  389. { startIndex: 17, type: 'identifier.quote.sql' }
  390. ]
  391. }],
  392. [{
  393. line: '"abc',
  394. tokens: [
  395. { startIndex: 0, type: 'identifier.quote.sql' },
  396. { startIndex: 1, type: 'identifier.sql' }
  397. ]
  398. }],
  399. [{
  400. line: 'int',
  401. tokens: [
  402. { startIndex: 0, type: 'keyword.sql' }
  403. ]
  404. }],
  405. [{
  406. line: '[int]',
  407. tokens: [
  408. { startIndex: 0, type: 'identifier.quote.sql' },
  409. { startIndex: 1, type: 'identifier.sql' },
  410. { startIndex: 4, type: 'identifier.quote.sql' }
  411. ]
  412. }],
  413. // Strings
  414. [{
  415. line: 'declare @x=\'a string\';',
  416. tokens: [
  417. { startIndex: 0, type: 'keyword.sql' },
  418. { startIndex: 7, type: 'white.sql' },
  419. { startIndex: 8, type: 'identifier.sql' },
  420. { startIndex: 10, type: 'operator.sql' },
  421. { startIndex: 11, type: 'string.sql' },
  422. { startIndex: 21, type: 'delimiter.sql' }
  423. ]
  424. }],
  425. [{
  426. line: '\'a \'\' string with quotes\'',
  427. tokens: [
  428. { startIndex: 0, type: 'string.sql' },
  429. ]
  430. }],
  431. [{
  432. line: '\'a " string with quotes\'',
  433. tokens: [
  434. { startIndex: 0, type: 'string.sql' },
  435. ]
  436. }],
  437. [{
  438. line: '\'a -- string with comment\'',
  439. tokens: [
  440. { startIndex: 0, type: 'string.sql' },
  441. ]
  442. }],
  443. [{
  444. line: 'N\'a unicode string\'',
  445. tokens: [
  446. { startIndex: 0, type: 'string.sql' },
  447. ]
  448. }],
  449. [{
  450. line: '\'a endless string',
  451. tokens: [
  452. { startIndex: 0, type: 'string.sql' },
  453. ]
  454. }],
  455. // Operators
  456. [{
  457. line: 'SET @x=@x+1',
  458. tokens: [
  459. { startIndex: 0, type: 'keyword.sql' },
  460. { startIndex: 3, type: 'white.sql' },
  461. { startIndex: 4, type: 'identifier.sql' },
  462. { startIndex: 6, type: 'operator.sql' },
  463. { startIndex: 7, type: 'identifier.sql' },
  464. { startIndex: 9, type: 'operator.sql' },
  465. { startIndex: 10, type: 'number.sql' }
  466. ]
  467. }],
  468. [{
  469. line: '@x^=@x',
  470. tokens: [
  471. { startIndex: 0, type: 'identifier.sql' },
  472. { startIndex: 2, type: 'operator.sql' },
  473. { startIndex: 4, type: 'identifier.sql' }
  474. ]
  475. }],
  476. [{
  477. line: 'WHERE x IS NOT NULL',
  478. tokens: [
  479. { startIndex: 0, type: 'keyword.sql' },
  480. { startIndex: 5, type: 'white.sql' },
  481. { startIndex: 6, type: 'identifier.sql' },
  482. { startIndex: 7, type: 'white.sql' },
  483. { startIndex: 8, type: 'operator.sql' },
  484. { startIndex: 10, type: 'white.sql' },
  485. { startIndex: 11, type: 'operator.sql' },
  486. { startIndex: 14, type: 'white.sql' },
  487. { startIndex: 15, type: 'operator.sql' }
  488. ]
  489. }],
  490. [{
  491. line: 'SELECT * FROM dbo.MyTable WHERE MyColumn IN (1,2)',
  492. tokens: [
  493. { startIndex: 0, type: 'keyword.sql' },
  494. { startIndex: 6, type: 'white.sql' },
  495. { startIndex: 7, type: 'operator.sql' },
  496. { startIndex: 8, type: 'white.sql' },
  497. { startIndex: 9, type: 'keyword.sql' },
  498. { startIndex: 13, type: 'white.sql' },
  499. { startIndex: 14, type: 'identifier.sql' },
  500. { startIndex: 17, type: 'delimiter.sql' },
  501. { startIndex: 18, type: 'identifier.sql' },
  502. { startIndex: 25, type: 'white.sql' },
  503. { startIndex: 26, type: 'keyword.sql' },
  504. { startIndex: 31, type: 'white.sql' },
  505. { startIndex: 32, type: 'identifier.sql' },
  506. { startIndex: 40, type: 'white.sql' },
  507. { startIndex: 41, type: 'operator.sql' },
  508. { startIndex: 43, type: 'white.sql' },
  509. { startIndex: 44, type: 'delimiter.parenthesis.sql' },
  510. { startIndex: 45, type: 'number.sql' },
  511. { startIndex: 46, type: 'delimiter.sql' },
  512. { startIndex: 47, type: 'number.sql' },
  513. { startIndex: 48, type: 'delimiter.parenthesis.sql' }
  514. ]
  515. }],
  516. // Scopes
  517. [{
  518. line: 'WHILE() BEGIN END',
  519. tokens: [
  520. { startIndex: 0, type: 'keyword.sql' },
  521. { startIndex: 5, type: 'delimiter.parenthesis.sql' },
  522. { startIndex: 7, type: 'white.sql' },
  523. { startIndex: 8, type: 'keyword.block.sql' },
  524. { startIndex: 13, type: 'white.sql' },
  525. { startIndex: 14, type: 'keyword.block.sql' }
  526. ]
  527. }],
  528. [{
  529. line: 'BEGIN TRAN BEGIN TRY SELECT $ COMMIT END TRY BEGIN CATCH ROLLBACK END CATCH',
  530. tokens: [
  531. { startIndex: 0, type: 'keyword.sql' },
  532. { startIndex: 10, type: 'white.sql' },
  533. { startIndex: 11, type: 'keyword.try.sql' },
  534. { startIndex: 20, type: 'white.sql' },
  535. { startIndex: 21, type: 'keyword.sql' },
  536. { startIndex: 27, type: 'white.sql' },
  537. { startIndex: 28, type: 'number.sql' },
  538. { startIndex: 29, type: 'white.sql' },
  539. { startIndex: 30, type: 'keyword.sql' },
  540. { startIndex: 36, type: 'white.sql' },
  541. { startIndex: 37, type: 'keyword.try.sql' },
  542. { startIndex: 44, type: 'white.sql' },
  543. { startIndex: 45, type: 'keyword.catch.sql' },
  544. { startIndex: 56, type: 'white.sql' },
  545. { startIndex: 57, type: 'keyword.sql' },
  546. { startIndex: 65, type: 'white.sql' },
  547. { startIndex: 66, type: 'keyword.catch.sql' }
  548. ]
  549. }],
  550. [{
  551. line: 'SELECT CASE $ WHEN 3 THEN 4 ELSE 5 END',
  552. tokens: [
  553. { startIndex: 0, type: 'keyword.sql' },
  554. { startIndex: 6, type: 'white.sql' },
  555. { startIndex: 7, type: 'keyword.block.sql' },
  556. { startIndex: 11, type: 'white.sql' },
  557. { startIndex: 12, type: 'number.sql' },
  558. { startIndex: 13, type: 'white.sql' },
  559. { startIndex: 14, type: 'keyword.choice.sql' },
  560. { startIndex: 18, type: 'white.sql' },
  561. { startIndex: 19, type: 'number.sql' },
  562. { startIndex: 20, type: 'white.sql' },
  563. { startIndex: 21, type: 'keyword.choice.sql' },
  564. { startIndex: 25, type: 'white.sql' },
  565. { startIndex: 26, type: 'number.sql' },
  566. { startIndex: 27, type: 'white.sql' },
  567. { startIndex: 28, type: 'keyword.sql' },
  568. { startIndex: 32, type: 'white.sql' },
  569. { startIndex: 33, type: 'number.sql' },
  570. { startIndex: 34, type: 'white.sql' },
  571. { startIndex: 35, type: 'keyword.block.sql' }
  572. ]
  573. }]
  574. ]);