postiats.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Artyom Shalkhakov. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *
  5. * Based on the ATS/Postiats lexer by Hongwei Xi.
  6. *--------------------------------------------------------------------------------------------*/
  7. define('vs/basic-languages/postiats/postiats',["require", "exports"], function (require, exports) {
  8. "use strict";
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.language = exports.conf = void 0;
  11. exports.conf = {
  12. comments: {
  13. lineComment: '//',
  14. blockComment: ['(*', '*)']
  15. },
  16. brackets: [
  17. ['{', '}'],
  18. ['[', ']'],
  19. ['(', ')'],
  20. ['<', '>']
  21. ],
  22. autoClosingPairs: [
  23. { open: '"', close: '"', notIn: ['string', 'comment'] },
  24. { open: '{', close: '}', notIn: ['string', 'comment'] },
  25. { open: '[', close: ']', notIn: ['string', 'comment'] },
  26. { open: '(', close: ')', notIn: ['string', 'comment'] }
  27. ]
  28. };
  29. exports.language = {
  30. tokenPostfix: '.pats',
  31. // TODO: staload and dynload are followed by a special kind of string literals
  32. // with {$IDENTIFER} variables, and it also may make sense to highlight
  33. // the punctuation (. and / and \) differently.
  34. // Set defaultToken to invalid to see what you do not tokenize yet
  35. defaultToken: 'invalid',
  36. // keyword reference: https://github.com/githwxi/ATS-Postiats/blob/master/src/pats_lexing_token.dats
  37. keywords: [
  38. //
  39. 'abstype',
  40. 'abst0ype',
  41. 'absprop',
  42. 'absview',
  43. 'absvtype',
  44. 'absviewtype',
  45. 'absvt0ype',
  46. 'absviewt0ype',
  47. //
  48. 'as',
  49. //
  50. 'and',
  51. //
  52. 'assume',
  53. //
  54. 'begin',
  55. //
  56. /*
  57. "case", // CASE
  58. */
  59. //
  60. 'classdec',
  61. //
  62. 'datasort',
  63. //
  64. 'datatype',
  65. 'dataprop',
  66. 'dataview',
  67. 'datavtype',
  68. 'dataviewtype',
  69. //
  70. 'do',
  71. //
  72. 'end',
  73. //
  74. 'extern',
  75. 'extype',
  76. 'extvar',
  77. //
  78. 'exception',
  79. //
  80. 'fn',
  81. 'fnx',
  82. 'fun',
  83. //
  84. 'prfn',
  85. 'prfun',
  86. //
  87. 'praxi',
  88. 'castfn',
  89. //
  90. 'if',
  91. 'then',
  92. 'else',
  93. //
  94. 'ifcase',
  95. //
  96. 'in',
  97. //
  98. 'infix',
  99. 'infixl',
  100. 'infixr',
  101. 'prefix',
  102. 'postfix',
  103. //
  104. 'implmnt',
  105. 'implement',
  106. //
  107. 'primplmnt',
  108. 'primplement',
  109. //
  110. 'import',
  111. //
  112. /*
  113. "lam", // LAM
  114. "llam", // LLAM
  115. "fix", // FIX
  116. */
  117. //
  118. 'let',
  119. //
  120. 'local',
  121. //
  122. 'macdef',
  123. 'macrodef',
  124. //
  125. 'nonfix',
  126. //
  127. 'symelim',
  128. 'symintr',
  129. 'overload',
  130. //
  131. 'of',
  132. 'op',
  133. //
  134. 'rec',
  135. //
  136. 'sif',
  137. 'scase',
  138. //
  139. 'sortdef',
  140. /*
  141. // HX: [sta] is now deprecated
  142. */
  143. 'sta',
  144. 'stacst',
  145. 'stadef',
  146. 'static',
  147. /*
  148. "stavar", // T_STAVAR
  149. */
  150. //
  151. 'staload',
  152. 'dynload',
  153. //
  154. 'try',
  155. //
  156. 'tkindef',
  157. //
  158. /*
  159. "type", // TYPE
  160. */
  161. 'typedef',
  162. 'propdef',
  163. 'viewdef',
  164. 'vtypedef',
  165. 'viewtypedef',
  166. //
  167. /*
  168. "val", // VAL
  169. */
  170. 'prval',
  171. //
  172. 'var',
  173. 'prvar',
  174. //
  175. 'when',
  176. 'where',
  177. //
  178. /*
  179. "for", // T_FOR
  180. "while", // T_WHILE
  181. */
  182. //
  183. 'with',
  184. //
  185. 'withtype',
  186. 'withprop',
  187. 'withview',
  188. 'withvtype',
  189. 'withviewtype' // WITHVIEWTYPE
  190. //
  191. ],
  192. keywords_dlr: [
  193. '$delay',
  194. '$ldelay',
  195. //
  196. '$arrpsz',
  197. '$arrptrsize',
  198. //
  199. '$d2ctype',
  200. //
  201. '$effmask',
  202. '$effmask_ntm',
  203. '$effmask_exn',
  204. '$effmask_ref',
  205. '$effmask_wrt',
  206. '$effmask_all',
  207. //
  208. '$extern',
  209. '$extkind',
  210. '$extype',
  211. '$extype_struct',
  212. //
  213. '$extval',
  214. '$extfcall',
  215. '$extmcall',
  216. //
  217. '$literal',
  218. //
  219. '$myfilename',
  220. '$mylocation',
  221. '$myfunction',
  222. //
  223. '$lst',
  224. '$lst_t',
  225. '$lst_vt',
  226. '$list',
  227. '$list_t',
  228. '$list_vt',
  229. //
  230. '$rec',
  231. '$rec_t',
  232. '$rec_vt',
  233. '$record',
  234. '$record_t',
  235. '$record_vt',
  236. //
  237. '$tup',
  238. '$tup_t',
  239. '$tup_vt',
  240. '$tuple',
  241. '$tuple_t',
  242. '$tuple_vt',
  243. //
  244. '$break',
  245. '$continue',
  246. //
  247. '$raise',
  248. //
  249. '$showtype',
  250. //
  251. '$vcopyenv_v',
  252. '$vcopyenv_vt',
  253. //
  254. '$tempenver',
  255. //
  256. '$solver_assert',
  257. '$solver_verify' // T_DLRSOLVERIFY
  258. ],
  259. keywords_srp: [
  260. //
  261. '#if',
  262. '#ifdef',
  263. '#ifndef',
  264. //
  265. '#then',
  266. //
  267. '#elif',
  268. '#elifdef',
  269. '#elifndef',
  270. //
  271. '#else',
  272. '#endif',
  273. //
  274. '#error',
  275. //
  276. '#prerr',
  277. '#print',
  278. //
  279. '#assert',
  280. //
  281. '#undef',
  282. '#define',
  283. //
  284. '#include',
  285. '#require',
  286. //
  287. '#pragma',
  288. '#codegen2',
  289. '#codegen3' // T_SRPCODEGEN3 // for level-3 codegen
  290. //
  291. // HX: end of special tokens
  292. //
  293. ],
  294. irregular_keyword_list: [
  295. 'val+',
  296. 'val-',
  297. 'val',
  298. 'case+',
  299. 'case-',
  300. 'case',
  301. 'addr@',
  302. 'addr',
  303. 'fold@',
  304. 'free@',
  305. 'fix@',
  306. 'fix',
  307. 'lam@',
  308. 'lam',
  309. 'llam@',
  310. 'llam',
  311. 'viewt@ype+',
  312. 'viewt@ype-',
  313. 'viewt@ype',
  314. 'viewtype+',
  315. 'viewtype-',
  316. 'viewtype',
  317. 'view+',
  318. 'view-',
  319. 'view@',
  320. 'view',
  321. 'type+',
  322. 'type-',
  323. 'type',
  324. 'vtype+',
  325. 'vtype-',
  326. 'vtype',
  327. 'vt@ype+',
  328. 'vt@ype-',
  329. 'vt@ype',
  330. 'viewt@ype+',
  331. 'viewt@ype-',
  332. 'viewt@ype',
  333. 'viewtype+',
  334. 'viewtype-',
  335. 'viewtype',
  336. 'prop+',
  337. 'prop-',
  338. 'prop',
  339. 'type+',
  340. 'type-',
  341. 'type',
  342. 't@ype',
  343. 't@ype+',
  344. 't@ype-',
  345. 'abst@ype',
  346. 'abstype',
  347. 'absviewt@ype',
  348. 'absvt@ype',
  349. 'for*',
  350. 'for',
  351. 'while*',
  352. 'while'
  353. ],
  354. keywords_types: [
  355. 'bool',
  356. 'double',
  357. 'byte',
  358. 'int',
  359. 'short',
  360. 'char',
  361. 'void',
  362. 'unit',
  363. 'long',
  364. 'float',
  365. 'string',
  366. 'strptr'
  367. ],
  368. // TODO: reference for this?
  369. keywords_effects: [
  370. '0',
  371. 'fun',
  372. 'clo',
  373. 'prf',
  374. 'funclo',
  375. 'cloptr',
  376. 'cloref',
  377. 'ref',
  378. 'ntm',
  379. '1' // all effects
  380. ],
  381. operators: [
  382. '@',
  383. '!',
  384. '|',
  385. '`',
  386. ':',
  387. '$',
  388. '.',
  389. '=',
  390. '#',
  391. '~',
  392. //
  393. '..',
  394. '...',
  395. //
  396. '=>',
  397. // "=<", // T_EQLT
  398. '=<>',
  399. '=/=>',
  400. '=>>',
  401. '=/=>>',
  402. //
  403. '<',
  404. '>',
  405. //
  406. '><',
  407. //
  408. '.<',
  409. '>.',
  410. //
  411. '.<>.',
  412. //
  413. '->',
  414. //"-<", // T_MINUSLT
  415. '-<>' // T_MINUSLTGT
  416. //
  417. /*
  418. ":<", // T_COLONLT
  419. */
  420. ],
  421. brackets: [
  422. { open: ',(', close: ')', token: 'delimiter.parenthesis' },
  423. { open: '`(', close: ')', token: 'delimiter.parenthesis' },
  424. { open: '%(', close: ')', token: 'delimiter.parenthesis' },
  425. { open: "'(", close: ')', token: 'delimiter.parenthesis' },
  426. { open: "'{", close: '}', token: 'delimiter.parenthesis' },
  427. { open: '@(', close: ')', token: 'delimiter.parenthesis' },
  428. { open: '@{', close: '}', token: 'delimiter.brace' },
  429. { open: '@[', close: ']', token: 'delimiter.square' },
  430. { open: '#[', close: ']', token: 'delimiter.square' },
  431. { open: '{', close: '}', token: 'delimiter.curly' },
  432. { open: '[', close: ']', token: 'delimiter.square' },
  433. { open: '(', close: ')', token: 'delimiter.parenthesis' },
  434. { open: '<', close: '>', token: 'delimiter.angle' }
  435. ],
  436. // we include these common regular expressions
  437. symbols: /[=><!~?:&|+\-*\/\^%]+/,
  438. IDENTFST: /[a-zA-Z_]/,
  439. IDENTRST: /[a-zA-Z0-9_'$]/,
  440. symbolic: /[%&+-./:=@~`^|*!$#?<>]/,
  441. digit: /[0-9]/,
  442. digitseq0: /@digit*/,
  443. xdigit: /[0-9A-Za-z]/,
  444. xdigitseq0: /@xdigit*/,
  445. INTSP: /[lLuU]/,
  446. FLOATSP: /[fFlL]/,
  447. fexponent: /[eE][+-]?[0-9]+/,
  448. fexponent_bin: /[pP][+-]?[0-9]+/,
  449. deciexp: /\.[0-9]*@fexponent?/,
  450. hexiexp: /\.[0-9a-zA-Z]*@fexponent_bin?/,
  451. irregular_keywords: /val[+-]?|case[+-]?|addr\@?|fold\@|free\@|fix\@?|lam\@?|llam\@?|prop[+-]?|type[+-]?|view[+-@]?|viewt@?ype[+-]?|t@?ype[+-]?|v(iew)?t@?ype[+-]?|abst@?ype|absv(iew)?t@?ype|for\*?|while\*?/,
  452. ESCHAR: /[ntvbrfa\\\?'"\(\[\{]/,
  453. start: 'root',
  454. // The main tokenizer for ATS/Postiats
  455. // reference: https://github.com/githwxi/ATS-Postiats/blob/master/src/pats_lexing.dats
  456. tokenizer: {
  457. root: [
  458. // lexing_blankseq0
  459. { regex: /[ \t\r\n]+/, action: { token: '' } },
  460. // NOTE: (*) is an invalid ML-like comment!
  461. { regex: /\(\*\)/, action: { token: 'invalid' } },
  462. {
  463. regex: /\(\*/,
  464. action: { token: 'comment', next: 'lexing_COMMENT_block_ml' }
  465. },
  466. {
  467. regex: /\(/,
  468. action: '@brackets' /*{ token: 'delimiter.parenthesis' }*/
  469. },
  470. {
  471. regex: /\)/,
  472. action: '@brackets' /*{ token: 'delimiter.parenthesis' }*/
  473. },
  474. {
  475. regex: /\[/,
  476. action: '@brackets' /*{ token: 'delimiter.bracket' }*/
  477. },
  478. {
  479. regex: /\]/,
  480. action: '@brackets' /*{ token: 'delimiter.bracket' }*/
  481. },
  482. {
  483. regex: /\{/,
  484. action: '@brackets' /*{ token: 'delimiter.brace' }*/
  485. },
  486. {
  487. regex: /\}/,
  488. action: '@brackets' /*{ token: 'delimiter.brace' }*/
  489. },
  490. // lexing_COMMA
  491. {
  492. regex: /,\(/,
  493. action: '@brackets' /*{ token: 'delimiter.parenthesis' }*/
  494. },
  495. { regex: /,/, action: { token: 'delimiter.comma' } },
  496. { regex: /;/, action: { token: 'delimiter.semicolon' } },
  497. // lexing_AT
  498. {
  499. regex: /@\(/,
  500. action: '@brackets' /* { token: 'delimiter.parenthesis' }*/
  501. },
  502. {
  503. regex: /@\[/,
  504. action: '@brackets' /* { token: 'delimiter.bracket' }*/
  505. },
  506. {
  507. regex: /@\{/,
  508. action: '@brackets' /*{ token: 'delimiter.brace' }*/
  509. },
  510. // lexing_COLON
  511. {
  512. regex: /:</,
  513. action: { token: 'keyword', next: '@lexing_EFFECT_commaseq0' }
  514. },
  515. /*
  516. lexing_DOT:
  517. . // SYMBOLIC => lexing_IDENT_sym
  518. . FLOATDOT => lexing_FLOAT_deciexp
  519. . DIGIT => T_DOTINT
  520. */
  521. { regex: /\.@symbolic+/, action: { token: 'identifier.sym' } },
  522. // FLOATDOT case
  523. {
  524. regex: /\.@digit*@fexponent@FLOATSP*/,
  525. action: { token: 'number.float' }
  526. },
  527. { regex: /\.@digit+/, action: { token: 'number.float' } },
  528. // lexing_DOLLAR:
  529. // '$' IDENTFST IDENTRST* => lexing_IDENT_dlr, _ => lexing_IDENT_sym
  530. {
  531. regex: /\$@IDENTFST@IDENTRST*/,
  532. action: {
  533. cases: {
  534. '@keywords_dlr': { token: 'keyword.dlr' },
  535. '@default': { token: 'namespace' } // most likely a module qualifier
  536. }
  537. }
  538. },
  539. // lexing_SHARP:
  540. // '#' IDENTFST IDENTRST* => lexing_ident_srp, _ => lexing_IDENT_sym
  541. {
  542. regex: /\#@IDENTFST@IDENTRST*/,
  543. action: {
  544. cases: {
  545. '@keywords_srp': { token: 'keyword.srp' },
  546. '@default': { token: 'identifier' }
  547. }
  548. }
  549. },
  550. // lexing_PERCENT:
  551. { regex: /%\(/, action: { token: 'delimiter.parenthesis' } },
  552. {
  553. regex: /^%{(#|\^|\$)?/,
  554. action: {
  555. token: 'keyword',
  556. next: '@lexing_EXTCODE',
  557. nextEmbedded: 'text/javascript'
  558. }
  559. },
  560. { regex: /^%}/, action: { token: 'keyword' } },
  561. // lexing_QUOTE
  562. { regex: /'\(/, action: { token: 'delimiter.parenthesis' } },
  563. { regex: /'\[/, action: { token: 'delimiter.bracket' } },
  564. { regex: /'\{/, action: { token: 'delimiter.brace' } },
  565. [/(')(\\@ESCHAR|\\[xX]@xdigit+|\\@digit+)(')/, ['string', 'string.escape', 'string']],
  566. [/'[^\\']'/, 'string'],
  567. // lexing_DQUOTE
  568. [/"/, 'string.quote', '@lexing_DQUOTE'],
  569. // lexing_BQUOTE
  570. {
  571. regex: /`\(/,
  572. action: '@brackets' /* { token: 'delimiter.parenthesis' }*/
  573. },
  574. // TODO: otherwise, try lexing_IDENT_sym
  575. { regex: /\\/, action: { token: 'punctuation' } },
  576. // lexing_IDENT_alp:
  577. // NOTE: (?!regex) is syntax for "not-followed-by" regex
  578. // to resolve ambiguity such as foreach$fwork being incorrectly lexed as [for] [each$fwork]!
  579. {
  580. regex: /@irregular_keywords(?!@IDENTRST)/,
  581. action: { token: 'keyword' }
  582. },
  583. {
  584. regex: /@IDENTFST@IDENTRST*[<!\[]?/,
  585. action: {
  586. cases: {
  587. // TODO: dynload and staload should be specially parsed
  588. // dynload whitespace+ "special_string"
  589. // this special string is really:
  590. // '/' '\\' '.' => punctuation
  591. // ({\$)([a-zA-Z_][a-zA-Z_0-9]*)(}) => punctuation,keyword,punctuation
  592. // [^"] => identifier/literal
  593. '@keywords': { token: 'keyword' },
  594. '@keywords_types': { token: 'type' },
  595. '@default': { token: 'identifier' }
  596. }
  597. }
  598. },
  599. // lexing_IDENT_sym:
  600. {
  601. regex: /\/\/\/\//,
  602. action: { token: 'comment', next: '@lexing_COMMENT_rest' }
  603. },
  604. { regex: /\/\/.*$/, action: { token: 'comment' } },
  605. {
  606. regex: /\/\*/,
  607. action: { token: 'comment', next: '@lexing_COMMENT_block_c' }
  608. },
  609. // AS-20160627: specifically for effect annotations
  610. {
  611. regex: /-<|=</,
  612. action: { token: 'keyword', next: '@lexing_EFFECT_commaseq0' }
  613. },
  614. {
  615. regex: /@symbolic+/,
  616. action: {
  617. cases: {
  618. '@operators': 'keyword',
  619. '@default': 'operator'
  620. }
  621. }
  622. },
  623. // lexing_ZERO:
  624. // FIXME: this one is quite messy/unfinished yet
  625. // TODO: lexing_INT_hex
  626. // - testing_hexiexp => lexing_FLOAT_hexiexp
  627. // - testing_fexponent_bin => lexing_FLOAT_hexiexp
  628. // - testing_intspseq0 => T_INT_hex
  629. // lexing_INT_hex:
  630. {
  631. regex: /0[xX]@xdigit+(@hexiexp|@fexponent_bin)@FLOATSP*/,
  632. action: { token: 'number.float' }
  633. },
  634. { regex: /0[xX]@xdigit+@INTSP*/, action: { token: 'number.hex' } },
  635. {
  636. regex: /0[0-7]+(?![0-9])@INTSP*/,
  637. action: { token: 'number.octal' }
  638. },
  639. //{regex: /0/, action: { token: 'number' } }, // INTZERO
  640. // lexing_INT_dec:
  641. // - testing_deciexp => lexing_FLOAT_deciexp
  642. // - testing_fexponent => lexing_FLOAT_deciexp
  643. // - otherwise => intspseq0 ([0-9]*[lLuU]?)
  644. {
  645. regex: /@digit+(@fexponent|@deciexp)@FLOATSP*/,
  646. action: { token: 'number.float' }
  647. },
  648. {
  649. regex: /@digit@digitseq0@INTSP*/,
  650. action: { token: 'number.decimal' }
  651. },
  652. // DIGIT, if followed by digitseq0, is lexing_INT_dec
  653. { regex: /@digit+@INTSP*/, action: { token: 'number' } }
  654. ],
  655. lexing_COMMENT_block_ml: [
  656. [/[^\(\*]+/, 'comment'],
  657. [/\(\*/, 'comment', '@push'],
  658. [/\(\*/, 'comment.invalid'],
  659. [/\*\)/, 'comment', '@pop'],
  660. [/\*/, 'comment']
  661. ],
  662. lexing_COMMENT_block_c: [
  663. [/[^\/*]+/, 'comment'],
  664. // [/\/\*/, 'comment', '@push' ], // nested C-style block comments not allowed
  665. // [/\/\*/, 'comment.invalid' ], // NOTE: this breaks block comments in the shape of /* //*/
  666. [/\*\//, 'comment', '@pop'],
  667. [/[\/*]/, 'comment']
  668. ],
  669. lexing_COMMENT_rest: [
  670. [/$/, 'comment', '@pop'],
  671. [/.*/, 'comment']
  672. ],
  673. // NOTE: added by AS, specifically for highlighting
  674. lexing_EFFECT_commaseq0: [
  675. {
  676. regex: /@IDENTFST@IDENTRST+|@digit+/,
  677. action: {
  678. cases: {
  679. '@keywords_effects': { token: 'type.effect' },
  680. '@default': { token: 'identifier' }
  681. }
  682. }
  683. },
  684. { regex: /,/, action: { token: 'punctuation' } },
  685. { regex: />/, action: { token: '@rematch', next: '@pop' } }
  686. ],
  687. lexing_EXTCODE: [
  688. {
  689. regex: /^%}/,
  690. action: {
  691. token: '@rematch',
  692. next: '@pop',
  693. nextEmbedded: '@pop'
  694. }
  695. },
  696. { regex: /[^%]+/, action: '' }
  697. ],
  698. lexing_DQUOTE: [
  699. { regex: /"/, action: { token: 'string.quote', next: '@pop' } },
  700. // AS-20160628: additional hi-lighting for variables in staload/dynload strings
  701. {
  702. regex: /(\{\$)(@IDENTFST@IDENTRST*)(\})/,
  703. action: [
  704. { token: 'string.escape' },
  705. { token: 'identifier' },
  706. { token: 'string.escape' }
  707. ]
  708. },
  709. { regex: /\\$/, action: { token: 'string.escape' } },
  710. {
  711. regex: /\\(@ESCHAR|[xX]@xdigit+|@digit+)/,
  712. action: { token: 'string.escape' }
  713. },
  714. { regex: /[^\\"]+/, action: { token: 'string' } }
  715. ]
  716. }
  717. };
  718. });