java.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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('java', [
  8. // Comments - single line
  9. [{
  10. line: '//',
  11. tokens: [
  12. { startIndex: 0, type: 'comment.java' }
  13. ]}],
  14. [{
  15. line: ' // a comment',
  16. tokens: [
  17. { startIndex: 0, type: '' },
  18. { startIndex: 4, type: 'comment.java' }
  19. ]}],
  20. // Broken nested tokens due to invalid comment tokenization
  21. [{
  22. line: '/* //*/ a',
  23. tokens: [
  24. { startIndex: 0, type: 'comment.java' },
  25. { startIndex: 7, type: '' },
  26. { startIndex: 8, type: 'identifier.java' }
  27. ]}],
  28. [{
  29. line: '// a comment',
  30. tokens: [
  31. { startIndex: 0, type: 'comment.java' }
  32. ]}],
  33. [{
  34. line: '//sticky comment',
  35. tokens: [
  36. { startIndex: 0, type: 'comment.java' }
  37. ]}],
  38. [{
  39. line: '/almost a comment',
  40. tokens: [
  41. { startIndex: 0, type: 'delimiter.java' },
  42. { startIndex: 1, type: 'identifier.java' },
  43. { startIndex: 7, type: '' },
  44. { startIndex: 8, type: 'identifier.java' },
  45. { startIndex: 9, type: '' },
  46. { startIndex: 10, type: 'identifier.java' }
  47. ]}],
  48. [{
  49. line: '1 / 2; /* comment',
  50. tokens: [
  51. { startIndex: 0, type: 'number.java' },
  52. { startIndex: 1, type: '' },
  53. { startIndex: 2, type: 'delimiter.java' },
  54. { startIndex: 3, type: '' },
  55. { startIndex: 4, type: 'number.java' },
  56. { startIndex: 5, type: 'delimiter.java' },
  57. { startIndex: 6, type: '' },
  58. { startIndex: 7, type: 'comment.java' }
  59. ]}],
  60. [{
  61. line: 'int x = 1; // my comment // is a nice one',
  62. tokens: [
  63. { startIndex: 0, type: 'keyword.int.java' },
  64. { startIndex: 3, type: '' },
  65. { startIndex: 4, type: 'identifier.java' },
  66. { startIndex: 5, type: '' },
  67. { startIndex: 6, type: 'delimiter.java' },
  68. { startIndex: 7, type: '' },
  69. { startIndex: 8, type: 'number.java' },
  70. { startIndex: 9, type: 'delimiter.java' },
  71. { startIndex: 10, type: '' },
  72. { startIndex: 11, type: 'comment.java' }
  73. ]}],
  74. // Comments - range comment, single line
  75. [{
  76. line: '/* a simple comment */',
  77. tokens: [
  78. { startIndex: 0, type: 'comment.java' }
  79. ]}],
  80. [{
  81. line: 'int x = /* a simple comment */ 1;',
  82. tokens: [
  83. { startIndex: 0, type: 'keyword.int.java' },
  84. { startIndex: 3, type: '' },
  85. { startIndex: 4, type: 'identifier.java' },
  86. { startIndex: 5, type: '' },
  87. { startIndex: 6, type: 'delimiter.java' },
  88. { startIndex: 7, type: '' },
  89. { startIndex: 8, type: 'comment.java' },
  90. { startIndex: 30, type: '' },
  91. { startIndex: 31, type: 'number.java' },
  92. { startIndex: 32, type: 'delimiter.java' }
  93. ]}],
  94. [{
  95. line: 'int x = /* comment */ 1; */',
  96. tokens: [
  97. { startIndex: 0, type: 'keyword.int.java' },
  98. { startIndex: 3, type: '' },
  99. { startIndex: 4, type: 'identifier.java' },
  100. { startIndex: 5, type: '' },
  101. { startIndex: 6, type: 'delimiter.java' },
  102. { startIndex: 7, type: '' },
  103. { startIndex: 8, type: 'comment.java' },
  104. { startIndex: 21, type: '' },
  105. { startIndex: 22, type: 'number.java' },
  106. { startIndex: 23, type: 'delimiter.java' },
  107. { startIndex: 24, type: '' }
  108. ]}],
  109. [{
  110. line: 'x = /**/;',
  111. tokens: [
  112. { startIndex: 0, type: 'identifier.java' },
  113. { startIndex: 1, type: '' },
  114. { startIndex: 2, type: 'delimiter.java' },
  115. { startIndex: 3, type: '' },
  116. { startIndex: 4, type: 'comment.java' },
  117. { startIndex: 8, type: 'delimiter.java' }
  118. ]}],
  119. [{
  120. line: 'x = /*/;',
  121. tokens: [
  122. { startIndex: 0, type: 'identifier.java' },
  123. { startIndex: 1, type: '' },
  124. { startIndex: 2, type: 'delimiter.java' },
  125. { startIndex: 3, type: '' },
  126. { startIndex: 4, type: 'comment.java' }
  127. ]}],
  128. // Comments - range comment, multiple lines
  129. [{
  130. line: '/* start of multiline comment',
  131. tokens: [
  132. { startIndex: 0, type: 'comment.java' }
  133. ]}, {
  134. line: 'a comment between without a star',
  135. tokens: [
  136. { startIndex: 0, type: 'comment.java' }
  137. ]}, {
  138. line: 'end of multiline comment*/',
  139. tokens: [
  140. { startIndex: 0, type: 'comment.java' }
  141. ]}],
  142. [{
  143. line: 'int x = /* start a comment',
  144. tokens: [
  145. { startIndex: 0, type: 'keyword.int.java' },
  146. { startIndex: 3, type: '' },
  147. { startIndex: 4, type: 'identifier.java' },
  148. { startIndex: 5, type: '' },
  149. { startIndex: 6, type: 'delimiter.java' },
  150. { startIndex: 7, type: '' },
  151. { startIndex: 8, type: 'comment.java' }
  152. ]}, {
  153. line: ' a ',
  154. tokens: [
  155. { startIndex: 0, type: 'comment.java' }
  156. ]}, {
  157. line: 'and end it */ 2;',
  158. tokens: [
  159. { startIndex: 0, type: 'comment.java' },
  160. { startIndex: 13, type: '' },
  161. { startIndex: 14, type: 'number.java' },
  162. { startIndex: 15, type: 'delimiter.java' }
  163. ]}],
  164. // Java Doc, multiple lines
  165. [{
  166. line: '/** start of Java Doc',
  167. tokens: [
  168. { startIndex: 0, type: 'comment.doc.java' }
  169. ]}, {
  170. line: 'a comment between without a star',
  171. tokens: [
  172. { startIndex: 0, type: 'comment.doc.java' }
  173. ]}, {
  174. line: 'end of multiline comment*/',
  175. tokens: [
  176. { startIndex: 0, type: 'comment.doc.java' }
  177. ]}],
  178. // Keywords
  179. [{
  180. line: 'package test; class Program { static void main(String[] args) {} } }',
  181. tokens: [
  182. { startIndex: 0, type: 'keyword.package.java' },
  183. { startIndex: 7, type: '' },
  184. { startIndex: 8, type: 'identifier.java' },
  185. { startIndex: 12, type: 'delimiter.java' },
  186. { startIndex: 13, type: '' },
  187. { startIndex: 14, type: 'keyword.class.java' },
  188. { startIndex: 19, type: '' },
  189. { startIndex: 20, type: 'identifier.java' },
  190. { startIndex: 27, type: '' },
  191. { startIndex: 28, type: 'delimiter.curly.java' },
  192. { startIndex: 29, type: '' },
  193. { startIndex: 30, type: 'keyword.static.java' },
  194. { startIndex: 36, type: '' },
  195. { startIndex: 37, type: 'keyword.void.java' },
  196. { startIndex: 41, type: '' },
  197. { startIndex: 42, type: 'identifier.java' },
  198. { startIndex: 46, type: 'delimiter.parenthesis.java' },
  199. { startIndex: 47, type: 'identifier.java' },
  200. { startIndex: 53, type: 'delimiter.square.java' },
  201. { startIndex: 55, type: '' },
  202. { startIndex: 56, type: 'identifier.java' },
  203. { startIndex: 60, type: 'delimiter.parenthesis.java' },
  204. { startIndex: 61, type: '' },
  205. { startIndex: 62, type: 'delimiter.curly.java' },
  206. { startIndex: 64, type: '' },
  207. { startIndex: 65, type: 'delimiter.curly.java' },
  208. { startIndex: 66, type: '' },
  209. { startIndex: 67, type: 'delimiter.curly.java' }
  210. ]}],
  211. // Numbers
  212. [{
  213. line: '0',
  214. tokens: [
  215. { startIndex: 0, type: 'number.java' }
  216. ]}],
  217. [{
  218. line: '0.10',
  219. tokens: [
  220. { startIndex: 0, type: 'number.float.java' }
  221. ]}],
  222. [{
  223. line: '0x',
  224. tokens: [
  225. { startIndex: 0, type: 'number.java' },
  226. { startIndex: 1, type: 'identifier.java' }
  227. ]}],
  228. [{
  229. line: '0x123',
  230. tokens: [
  231. { startIndex: 0, type: 'number.hex.java' }
  232. ]}],
  233. [{
  234. line: '0x5_2',
  235. tokens: [
  236. { startIndex: 0, type: 'number.hex.java' }
  237. ]}],
  238. [{
  239. line: '023L',
  240. tokens: [
  241. { startIndex: 0, type: 'number.octal.java' }
  242. ]}],
  243. [{
  244. line: '0123l',
  245. tokens: [
  246. { startIndex: 0, type: 'number.octal.java' }
  247. ]}],
  248. [{
  249. line: '05_2',
  250. tokens: [
  251. { startIndex: 0, type: 'number.octal.java' }
  252. ]}],
  253. [{
  254. line: '0b1010_0101',
  255. tokens: [
  256. { startIndex: 0, type: 'number.binary.java' }
  257. ]}],
  258. [{
  259. line: '0B001',
  260. tokens: [
  261. { startIndex: 0, type: 'number.binary.java' }
  262. ]}],
  263. [{
  264. line: '10e3',
  265. tokens: [
  266. { startIndex: 0, type: 'number.float.java' }
  267. ]}],
  268. [{
  269. line: '10f',
  270. tokens: [
  271. { startIndex: 0, type: 'number.float.java' }
  272. ]}],
  273. [{
  274. line: '23.5',
  275. tokens: [
  276. { startIndex: 0, type: 'number.float.java' }
  277. ]}],
  278. [{
  279. line: '23.5e3',
  280. tokens: [
  281. { startIndex: 0, type: 'number.float.java' }
  282. ]}],
  283. [{
  284. line: '23.5e-3',
  285. tokens: [
  286. { startIndex: 0, type: 'number.float.java' }
  287. ]}],
  288. [{
  289. line: '23.5E3',
  290. tokens: [
  291. { startIndex: 0, type: 'number.float.java' }
  292. ]}],
  293. [{
  294. line: '23.5E-3',
  295. tokens: [
  296. { startIndex: 0, type: 'number.float.java' }
  297. ]}],
  298. [{
  299. line: '23.5F',
  300. tokens: [
  301. { startIndex: 0, type: 'number.float.java' }
  302. ]}],
  303. [{
  304. line: '23.5f',
  305. tokens: [
  306. { startIndex: 0, type: 'number.float.java' }
  307. ]}],
  308. [{
  309. line: '23.5D',
  310. tokens: [
  311. { startIndex: 0, type: 'number.float.java' }
  312. ]}],
  313. [{
  314. line: '23.5d',
  315. tokens: [
  316. { startIndex: 0, type: 'number.float.java' }
  317. ]}],
  318. [{
  319. line: '1.72E3D',
  320. tokens: [
  321. { startIndex: 0, type: 'number.float.java' }
  322. ]}],
  323. [{
  324. line: '1.72E3d',
  325. tokens: [
  326. { startIndex: 0, type: 'number.float.java' }
  327. ]}],
  328. [{
  329. line: '1.72E-3d',
  330. tokens: [
  331. { startIndex: 0, type: 'number.float.java' }
  332. ]}],
  333. [{
  334. line: '1.72e3D',
  335. tokens: [
  336. { startIndex: 0, type: 'number.float.java' }
  337. ]}],
  338. [{
  339. line: '1.72e3d',
  340. tokens: [
  341. { startIndex: 0, type: 'number.float.java' }
  342. ]}],
  343. [{
  344. line: '1.72e-3d',
  345. tokens: [
  346. { startIndex: 0, type: 'number.float.java' }
  347. ]}],
  348. [{
  349. line: '23L',
  350. tokens: [
  351. { startIndex: 0, type: 'number.java' }
  352. ]}],
  353. [{
  354. line: '23l',
  355. tokens: [
  356. { startIndex: 0, type: 'number.java' }
  357. ]}],
  358. [{
  359. line: '0_52',
  360. tokens: [
  361. { startIndex: 0, type: 'number.java' }
  362. ]}],
  363. [{
  364. line: '5_2',
  365. tokens: [
  366. { startIndex: 0, type: 'number.java' }
  367. ]}],
  368. [{
  369. line: '5_______2',
  370. tokens: [
  371. { startIndex: 0, type: 'number.java' }
  372. ]}],
  373. [{
  374. line: '3_.1415F',
  375. tokens: [
  376. { startIndex: 0, type: 'number.java' },
  377. { startIndex: 1, type: 'identifier.java' },
  378. { startIndex: 2, type: 'delimiter.java' },
  379. { startIndex: 3, type: 'number.float.java' }
  380. ]}],
  381. [{
  382. line: '3._1415F',
  383. tokens: [
  384. { startIndex: 0, type: 'number.java' },
  385. { startIndex: 1, type: 'delimiter.java' },
  386. { startIndex: 2, type: 'identifier.java' }
  387. ]}],
  388. [{
  389. line: '999_99_9999_L',
  390. tokens: [
  391. { startIndex: 0, type: 'number.java' },
  392. { startIndex: 11, type: 'identifier.java' }
  393. ]}],
  394. [{
  395. line: '52_',
  396. tokens: [
  397. { startIndex: 0, type: 'number.java' },
  398. { startIndex: 2, type: 'identifier.java' }
  399. ]}],
  400. [{
  401. line: '0_x52',
  402. tokens: [
  403. { startIndex: 0, type: 'number.java' },
  404. { startIndex: 1, type: 'identifier.java' }
  405. ]}],
  406. [{
  407. line: '0x_52',
  408. tokens: [
  409. { startIndex: 0, type: 'number.java' },
  410. { startIndex: 1, type: 'identifier.java' }
  411. ]}],
  412. [{
  413. line: '0x52_',
  414. tokens: [
  415. { startIndex: 0, type: 'number.hex.java' },
  416. { startIndex: 4, type: 'identifier.java' }
  417. ]}],
  418. [{
  419. line: '052_',
  420. tokens: [
  421. { startIndex: 0, type: 'number.octal.java' },
  422. { startIndex: 3, type: 'identifier.java' }
  423. ]}],
  424. [{
  425. line: '23.5L',
  426. tokens: [
  427. { startIndex: 0, type: 'number.float.java' },
  428. { startIndex: 4, type: 'identifier.java' }
  429. ]}],
  430. [{
  431. line: '0+0',
  432. tokens: [
  433. { startIndex: 0, type: 'number.java' },
  434. { startIndex: 1, type: 'delimiter.java' },
  435. { startIndex: 2, type: 'number.java' }
  436. ]}],
  437. [{
  438. line: '100+10',
  439. tokens: [
  440. { startIndex: 0, type: 'number.java' },
  441. { startIndex: 3, type: 'delimiter.java' },
  442. { startIndex: 4, type: 'number.java' }
  443. ]}],
  444. [{
  445. line: '0 + 0',
  446. tokens: [
  447. { startIndex: 0, type: 'number.java' },
  448. { startIndex: 1, type: '' },
  449. { startIndex: 2, type: 'delimiter.java' },
  450. { startIndex: 3, type: '' },
  451. { startIndex: 4, type: 'number.java' }
  452. ]}],
  453. // single line Strings
  454. [{
  455. line: 'String s = "I\'m a Java String";',
  456. tokens: [
  457. { startIndex: 0, type: 'identifier.java' },
  458. { startIndex: 6, type: '' },
  459. { startIndex: 7, type: 'identifier.java' },
  460. { startIndex: 8, type: '' },
  461. { startIndex: 9, type: 'delimiter.java' },
  462. { startIndex: 10, type: '' },
  463. { startIndex: 11, type: 'string.java' },
  464. { startIndex: 30, type: 'delimiter.java' }
  465. ]}],
  466. [{
  467. line: 'String s = "concatenated" + " String" ;',
  468. tokens: [
  469. { startIndex: 0, type: 'identifier.java' },
  470. { startIndex: 6, type: '' },
  471. { startIndex: 7, type: 'identifier.java' },
  472. { startIndex: 8, type: '' },
  473. { startIndex: 9, type: 'delimiter.java' },
  474. { startIndex: 10, type: '' },
  475. { startIndex: 11, type: 'string.java' },
  476. { startIndex: 25, type: '' },
  477. { startIndex: 26, type: 'delimiter.java' },
  478. { startIndex: 27, type: '' },
  479. { startIndex: 28, type: 'string.java' },
  480. { startIndex: 37, type: '' },
  481. { startIndex: 38, type: 'delimiter.java' }
  482. ]}],
  483. [{
  484. line: '"quote in a string"',
  485. tokens: [
  486. { startIndex: 0, type: 'string.java' }
  487. ]}],
  488. [{
  489. line: '"escaping \\"quotes\\" is cool"',
  490. tokens: [
  491. { startIndex: 0, type: 'string.java' },
  492. { startIndex: 10, type: 'string.escape.java' },
  493. { startIndex: 12, type: 'string.java' },
  494. { startIndex: 18, type: 'string.escape.java' },
  495. { startIndex: 20, type: 'string.java' }
  496. ]}],
  497. [{
  498. line: '"\\"',
  499. tokens: [
  500. { startIndex: 0, type: 'string.invalid.java' }
  501. ]}],
  502. // Annotations
  503. [{
  504. line: '@',
  505. tokens: [
  506. { startIndex: 0, type: '' }
  507. ]}],
  508. [{
  509. line: '@Override',
  510. tokens: [
  511. { startIndex: 0, type: 'annotation.java' }
  512. ]}],
  513. [{
  514. line: '@SuppressWarnings(value = "aString")',
  515. tokens: [
  516. { startIndex: 0, type: 'annotation.java' },
  517. { startIndex: 17, type: 'delimiter.parenthesis.java' },
  518. { startIndex: 18, type: 'identifier.java' },
  519. { startIndex: 23, type: '' },
  520. { startIndex: 24, type: 'delimiter.java' },
  521. { startIndex: 25, type: '' },
  522. { startIndex: 26, type: 'string.java' },
  523. { startIndex: 35, type: 'delimiter.parenthesis.java' }
  524. ]}],
  525. [{
  526. line: '@ AnnotationWithKeywordAfter private',
  527. tokens: [
  528. { startIndex: 0, type: 'annotation.java' },
  529. { startIndex: 28, type: '' },
  530. { startIndex: 29, type: 'keyword.private.java' }
  531. ]}]
  532. ]);