twig.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*!-----------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Version: 0.31.1(337587859b1c171314b40503171188b6cea6a32a)
  4. * Released under the MIT license
  5. * https://github.com/microsoft/monaco-editor/blob/main/LICENSE.txt
  6. *-----------------------------------------------------------------------------*/
  7. // src/basic-languages/twig/twig.ts
  8. var conf = {
  9. wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
  10. comments: {
  11. blockComment: ["{#", "#}"]
  12. },
  13. brackets: [
  14. ["{#", "#}"],
  15. ["{%", "%}"],
  16. ["{{", "}}"],
  17. ["(", ")"],
  18. ["[", "]"],
  19. ["<!--", "-->"],
  20. ["<", ">"]
  21. ],
  22. autoClosingPairs: [
  23. { open: "{# ", close: " #}" },
  24. { open: "{% ", close: " %}" },
  25. { open: "{{ ", close: " }}" },
  26. { open: "[", close: "]" },
  27. { open: "(", close: ")" },
  28. { open: '"', close: '"' },
  29. { open: "'", close: "'" }
  30. ],
  31. surroundingPairs: [
  32. { open: '"', close: '"' },
  33. { open: "'", close: "'" },
  34. { open: "<", close: ">" }
  35. ]
  36. };
  37. var language = {
  38. defaultToken: "",
  39. tokenPostfix: "",
  40. ignoreCase: true,
  41. keywords: [
  42. "apply",
  43. "autoescape",
  44. "block",
  45. "deprecated",
  46. "do",
  47. "embed",
  48. "extends",
  49. "flush",
  50. "for",
  51. "from",
  52. "if",
  53. "import",
  54. "include",
  55. "macro",
  56. "sandbox",
  57. "set",
  58. "use",
  59. "verbatim",
  60. "with",
  61. "endapply",
  62. "endautoescape",
  63. "endblock",
  64. "endembed",
  65. "endfor",
  66. "endif",
  67. "endmacro",
  68. "endsandbox",
  69. "endset",
  70. "endwith",
  71. "true",
  72. "false"
  73. ],
  74. tokenizer: {
  75. root: [
  76. [/\s+/],
  77. [/{#/, "comment.twig", "@commentState"],
  78. [/{%[-~]?/, "delimiter.twig", "@blockState"],
  79. [/{{[-~]?/, "delimiter.twig", "@variableState"],
  80. [/<!DOCTYPE/, "metatag.html", "@doctype"],
  81. [/<!--/, "comment.html", "@comment"],
  82. [/(<)((?:[\w\-]+:)?[\w\-]+)(\s*)(\/>)/, ["delimiter.html", "tag.html", "", "delimiter.html"]],
  83. [/(<)(script)/, ["delimiter.html", { token: "tag.html", next: "@script" }]],
  84. [/(<)(style)/, ["delimiter.html", { token: "tag.html", next: "@style" }]],
  85. [/(<)((?:[\w\-]+:)?[\w\-]+)/, ["delimiter.html", { token: "tag.html", next: "@otherTag" }]],
  86. [/(<\/)((?:[\w\-]+:)?[\w\-]+)/, ["delimiter.html", { token: "tag.html", next: "@otherTag" }]],
  87. [/</, "delimiter.html"],
  88. [/[^<]+/]
  89. ],
  90. commentState: [
  91. [/#}/, "comment.twig", "@pop"],
  92. [/./, "comment.twig"]
  93. ],
  94. blockState: [
  95. [/[-~]?%}/, "delimiter.twig", "@pop"],
  96. [/\s+/],
  97. [
  98. /(verbatim)(\s*)([-~]?%})/,
  99. ["keyword.twig", "", { token: "delimiter.twig", next: "@rawDataState" }]
  100. ],
  101. { include: "expression" }
  102. ],
  103. rawDataState: [
  104. [
  105. /({%[-~]?)(\s*)(endverbatim)(\s*)([-~]?%})/,
  106. ["delimiter.twig", "", "keyword.twig", "", { token: "delimiter.twig", next: "@popall" }]
  107. ],
  108. [/./, "string.twig"]
  109. ],
  110. variableState: [[/[-~]?}}/, "delimiter.twig", "@pop"], { include: "expression" }],
  111. stringState: [
  112. [/"/, "string.twig", "@pop"],
  113. [/#{\s*/, "string.twig", "@interpolationState"],
  114. [/[^#"\\]*(?:(?:\\.|#(?!\{))[^#"\\]*)*/, "string.twig"]
  115. ],
  116. interpolationState: [
  117. [/}/, "string.twig", "@pop"],
  118. { include: "expression" }
  119. ],
  120. expression: [
  121. [/\s+/],
  122. [/\+|-|\/{1,2}|%|\*{1,2}/, "operators.twig"],
  123. [/(and|or|not|b-and|b-xor|b-or)(\s+)/, ["operators.twig", ""]],
  124. [/==|!=|<|>|>=|<=/, "operators.twig"],
  125. [/(starts with|ends with|matches)(\s+)/, ["operators.twig", ""]],
  126. [/(in)(\s+)/, ["operators.twig", ""]],
  127. [/(is)(\s+)/, ["operators.twig", ""]],
  128. [/\||~|:|\.{1,2}|\?{1,2}/, "operators.twig"],
  129. [
  130. /[^\W\d][\w]*/,
  131. {
  132. cases: {
  133. "@keywords": "keyword.twig",
  134. "@default": "variable.twig"
  135. }
  136. }
  137. ],
  138. [/\d+(\.\d+)?/, "number.twig"],
  139. [/\(|\)|\[|\]|{|}|,/, "delimiter.twig"],
  140. [/"([^#"\\]*(?:\\.[^#"\\]*)*)"|\'([^\'\\]*(?:\\.[^\'\\]*)*)\'/, "string.twig"],
  141. [/"/, "string.twig", "@stringState"],
  142. [/=>/, "operators.twig"],
  143. [/=/, "operators.twig"]
  144. ],
  145. doctype: [
  146. [/[^>]+/, "metatag.content.html"],
  147. [/>/, "metatag.html", "@pop"]
  148. ],
  149. comment: [
  150. [/-->/, "comment.html", "@pop"],
  151. [/[^-]+/, "comment.content.html"],
  152. [/./, "comment.content.html"]
  153. ],
  154. otherTag: [
  155. [/\/?>/, "delimiter.html", "@pop"],
  156. [/"([^"]*)"/, "attribute.value.html"],
  157. [/'([^']*)'/, "attribute.value.html"],
  158. [/[\w\-]+/, "attribute.name.html"],
  159. [/=/, "delimiter.html"],
  160. [/[ \t\r\n]+/]
  161. ],
  162. script: [
  163. [/type/, "attribute.name.html", "@scriptAfterType"],
  164. [/"([^"]*)"/, "attribute.value.html"],
  165. [/'([^']*)'/, "attribute.value.html"],
  166. [/[\w\-]+/, "attribute.name.html"],
  167. [/=/, "delimiter.html"],
  168. [
  169. />/,
  170. {
  171. token: "delimiter.html",
  172. next: "@scriptEmbedded",
  173. nextEmbedded: "text/javascript"
  174. }
  175. ],
  176. [/[ \t\r\n]+/],
  177. [
  178. /(<\/)(script\s*)(>)/,
  179. ["delimiter.html", "tag.html", { token: "delimiter.html", next: "@pop" }]
  180. ]
  181. ],
  182. scriptAfterType: [
  183. [/=/, "delimiter.html", "@scriptAfterTypeEquals"],
  184. [
  185. />/,
  186. {
  187. token: "delimiter.html",
  188. next: "@scriptEmbedded",
  189. nextEmbedded: "text/javascript"
  190. }
  191. ],
  192. [/[ \t\r\n]+/],
  193. [/<\/script\s*>/, { token: "@rematch", next: "@pop" }]
  194. ],
  195. scriptAfterTypeEquals: [
  196. [
  197. /"([^"]*)"/,
  198. {
  199. token: "attribute.value.html",
  200. switchTo: "@scriptWithCustomType.$1"
  201. }
  202. ],
  203. [
  204. /'([^']*)'/,
  205. {
  206. token: "attribute.value.html",
  207. switchTo: "@scriptWithCustomType.$1"
  208. }
  209. ],
  210. [
  211. />/,
  212. {
  213. token: "delimiter.html",
  214. next: "@scriptEmbedded",
  215. nextEmbedded: "text/javascript"
  216. }
  217. ],
  218. [/[ \t\r\n]+/],
  219. [/<\/script\s*>/, { token: "@rematch", next: "@pop" }]
  220. ],
  221. scriptWithCustomType: [
  222. [
  223. />/,
  224. {
  225. token: "delimiter.html",
  226. next: "@scriptEmbedded.$S2",
  227. nextEmbedded: "$S2"
  228. }
  229. ],
  230. [/"([^"]*)"/, "attribute.value.html"],
  231. [/'([^']*)'/, "attribute.value.html"],
  232. [/[\w\-]+/, "attribute.name.html"],
  233. [/=/, "delimiter.html"],
  234. [/[ \t\r\n]+/],
  235. [/<\/script\s*>/, { token: "@rematch", next: "@pop" }]
  236. ],
  237. scriptEmbedded: [
  238. [/<\/script/, { token: "@rematch", next: "@pop", nextEmbedded: "@pop" }],
  239. [/[^<]+/, ""]
  240. ],
  241. style: [
  242. [/type/, "attribute.name.html", "@styleAfterType"],
  243. [/"([^"]*)"/, "attribute.value.html"],
  244. [/'([^']*)'/, "attribute.value.html"],
  245. [/[\w\-]+/, "attribute.name.html"],
  246. [/=/, "delimiter.html"],
  247. [
  248. />/,
  249. {
  250. token: "delimiter.html",
  251. next: "@styleEmbedded",
  252. nextEmbedded: "text/css"
  253. }
  254. ],
  255. [/[ \t\r\n]+/],
  256. [
  257. /(<\/)(style\s*)(>)/,
  258. ["delimiter.html", "tag.html", { token: "delimiter.html", next: "@pop" }]
  259. ]
  260. ],
  261. styleAfterType: [
  262. [/=/, "delimiter.html", "@styleAfterTypeEquals"],
  263. [
  264. />/,
  265. {
  266. token: "delimiter.html",
  267. next: "@styleEmbedded",
  268. nextEmbedded: "text/css"
  269. }
  270. ],
  271. [/[ \t\r\n]+/],
  272. [/<\/style\s*>/, { token: "@rematch", next: "@pop" }]
  273. ],
  274. styleAfterTypeEquals: [
  275. [
  276. /"([^"]*)"/,
  277. {
  278. token: "attribute.value.html",
  279. switchTo: "@styleWithCustomType.$1"
  280. }
  281. ],
  282. [
  283. /'([^']*)'/,
  284. {
  285. token: "attribute.value.html",
  286. switchTo: "@styleWithCustomType.$1"
  287. }
  288. ],
  289. [
  290. />/,
  291. {
  292. token: "delimiter.html",
  293. next: "@styleEmbedded",
  294. nextEmbedded: "text/css"
  295. }
  296. ],
  297. [/[ \t\r\n]+/],
  298. [/<\/style\s*>/, { token: "@rematch", next: "@pop" }]
  299. ],
  300. styleWithCustomType: [
  301. [
  302. />/,
  303. {
  304. token: "delimiter.html",
  305. next: "@styleEmbedded.$S2",
  306. nextEmbedded: "$S2"
  307. }
  308. ],
  309. [/"([^"]*)"/, "attribute.value.html"],
  310. [/'([^']*)'/, "attribute.value.html"],
  311. [/[\w\-]+/, "attribute.name.html"],
  312. [/=/, "delimiter.html"],
  313. [/[ \t\r\n]+/],
  314. [/<\/style\s*>/, { token: "@rematch", next: "@pop" }]
  315. ],
  316. styleEmbedded: [
  317. [/<\/style/, { token: "@rematch", next: "@pop", nextEmbedded: "@pop" }],
  318. [/[^<]+/, ""]
  319. ]
  320. }
  321. };
  322. export {
  323. conf,
  324. language
  325. };