sql.test.ts 13 KB

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