twig.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. define(["require", "exports"], function (require, exports) {
  6. 'use strict';
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.conf = {
  9. wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
  10. comments: {
  11. blockComment: ['{#', '#}'],
  12. },
  13. brackets: [
  14. ['{#', '#}'],
  15. ['{%', '%}'],
  16. ['{{', '}}'],
  17. ['(', ')'],
  18. ['[', ']'],
  19. // HTML
  20. ['<!--', '-->'],
  21. ['<', '>'],
  22. ],
  23. autoClosingPairs: [
  24. { open: '{# ', close: ' #}' },
  25. { open: '{% ', close: ' %}' },
  26. { open: '{{ ', close: ' }}' },
  27. { open: '[', close: ']' },
  28. { open: '(', close: ')' },
  29. { open: '"', close: '"' },
  30. { open: '\'', close: '\'' },
  31. ],
  32. surroundingPairs: [
  33. { open: '"', close: '"' },
  34. { open: '\'', close: '\'' },
  35. // HTML
  36. { open: '<', close: '>' },
  37. ],
  38. };
  39. exports.language = {
  40. defaultToken: '',
  41. tokenPostfix: '',
  42. ignoreCase: true,
  43. keywords: [
  44. // (opening) tags
  45. 'apply', 'autoescape', 'block', 'deprecated', 'do', 'embed', 'extends',
  46. 'flush', 'for', 'from', 'if', 'import', 'include', 'macro', 'sandbox',
  47. 'set', 'use', 'verbatim', 'with',
  48. // closing tags
  49. 'endapply', 'endautoescape', 'endblock', 'endembed', 'endfor', 'endif',
  50. 'endmacro', 'endsandbox', 'endset', 'endwith',
  51. // literals
  52. 'true', 'false',
  53. ],
  54. tokenizer: {
  55. root: [
  56. // whitespace
  57. [/\s+/],
  58. // Twig Tag Delimiters
  59. [/{#/, 'comment.twig', '@commentState'],
  60. [/{%[-~]?/, 'delimiter.twig', '@blockState'],
  61. [/{{[-~]?/, 'delimiter.twig', '@variableState'],
  62. // HTML
  63. [/<!DOCTYPE/, 'metatag.html', '@doctype'],
  64. [/<!--/, 'comment.html', '@comment'],
  65. [/(<)((?:[\w\-]+:)?[\w\-]+)(\s*)(\/>)/, ['delimiter.html', 'tag.html', '', 'delimiter.html']],
  66. [/(<)(script)/, ['delimiter.html', { token: 'tag.html', next: '@script' }]],
  67. [/(<)(style)/, ['delimiter.html', { token: 'tag.html', next: '@style' }]],
  68. [/(<)((?:[\w\-]+:)?[\w\-]+)/, ['delimiter.html', { token: 'tag.html', next: '@otherTag' }]],
  69. [/(<\/)((?:[\w\-]+:)?[\w\-]+)/, ['delimiter.html', { token: 'tag.html', next: '@otherTag' }]],
  70. [/</, 'delimiter.html'],
  71. [/[^<]+/],
  72. ],
  73. /**
  74. * Comment Tag Handling
  75. */
  76. commentState: [
  77. [/#}/, 'comment.twig', '@pop'],
  78. [/./, 'comment.twig'],
  79. ],
  80. /**
  81. * Block Tag Handling
  82. */
  83. blockState: [
  84. [/[-~]?%}/, 'delimiter.twig', '@pop'],
  85. // whitespace
  86. [/\s+/],
  87. // verbatim
  88. // Unlike other blocks, verbatim ehas its own state
  89. // transition to ensure we mark its contents as strings.
  90. [/(verbatim)(\s*)([-~]?%})/, [
  91. 'keyword.twig',
  92. '',
  93. { token: 'delimiter.twig', next: '@rawDataState' },
  94. ]],
  95. { include: 'expression' }
  96. ],
  97. rawDataState: [
  98. // endverbatim
  99. [/({%[-~]?)(\s*)(endverbatim)(\s*)([-~]?%})/, [
  100. 'delimiter.twig',
  101. '',
  102. 'keyword.twig',
  103. '',
  104. { token: 'delimiter.twig', next: '@popall' },
  105. ]],
  106. [/./, 'string.twig'],
  107. ],
  108. /**
  109. * Variable Tag Handling
  110. */
  111. variableState: [
  112. [/[-~]?}}/, 'delimiter.twig', '@pop'],
  113. { include: 'expression' },
  114. ],
  115. stringState: [
  116. // closing double quoted string
  117. [/"/, 'string.twig', '@pop'],
  118. // interpolation start
  119. [/#{\s*/, 'string.twig', '@interpolationState'],
  120. // string part
  121. [/[^#"\\]*(?:(?:\\.|#(?!\{))[^#"\\]*)*/, 'string.twig'],
  122. ],
  123. interpolationState: [
  124. // interpolation end
  125. [/}/, 'string.twig', '@pop'],
  126. { include: 'expression' },
  127. ],
  128. /**
  129. * Expression Handling
  130. */
  131. expression: [
  132. // whitespace
  133. [/\s+/],
  134. // operators - math
  135. [/\+|-|\/{1,2}|%|\*{1,2}/, 'operators.twig'],
  136. // operators - logic
  137. [/(and|or|not|b-and|b-xor|b-or)(\s+)/, ['operators.twig', '']],
  138. // operators - comparison (symbols)
  139. [/==|!=|<|>|>=|<=/, 'operators.twig'],
  140. // operators - comparison (words)
  141. [/(starts with|ends with|matches)(\s+)/, ['operators.twig', '']],
  142. // operators - containment
  143. [/(in)(\s+)/, ['operators.twig', '']],
  144. // operators - test
  145. [/(is)(\s+)/, ['operators.twig', '']],
  146. // operators - misc
  147. [/\||~|:|\.{1,2}|\?{1,2}/, 'operators.twig'],
  148. // names
  149. [/[^\W\d][\w]*/, {
  150. cases: {
  151. '@keywords': 'keyword.twig',
  152. '@default': 'variable.twig'
  153. }
  154. }],
  155. // numbers
  156. [/\d+(\.\d+)?/, 'number.twig'],
  157. // punctuation
  158. [/\(|\)|\[|\]|{|}|,/, 'delimiter.twig'],
  159. // strings
  160. [/"([^#"\\]*(?:\\.[^#"\\]*)*)"|\'([^\'\\]*(?:\\.[^\'\\]*)*)\'/, 'string.twig'],
  161. // opening double quoted string
  162. [/"/, 'string.twig', '@stringState'],
  163. // misc syntactic constructs
  164. // These are not operators per se, but for the purposes of lexical analysis we
  165. // can treat them as such.
  166. // arrow functions
  167. [/=>/, 'operators.twig'],
  168. // assignment
  169. [/=/, 'operators.twig'],
  170. ],
  171. /**
  172. * HTML
  173. */
  174. doctype: [
  175. [/[^>]+/, 'metatag.content.html'],
  176. [/>/, 'metatag.html', '@pop'],
  177. ],
  178. comment: [
  179. [/-->/, 'comment.html', '@pop'],
  180. [/[^-]+/, 'comment.content.html'],
  181. [/./, 'comment.content.html']
  182. ],
  183. otherTag: [
  184. [/\/?>/, 'delimiter.html', '@pop'],
  185. [/"([^"]*)"/, 'attribute.value.html'],
  186. [/'([^']*)'/, 'attribute.value.html'],
  187. [/[\w\-]+/, 'attribute.name.html'],
  188. [/=/, 'delimiter.html'],
  189. [/[ \t\r\n]+/],
  190. ],
  191. // -- BEGIN <script> tags handling
  192. // After <script
  193. script: [
  194. [/type/, 'attribute.name.html', '@scriptAfterType'],
  195. [/"([^"]*)"/, 'attribute.value.html'],
  196. [/'([^']*)'/, 'attribute.value.html'],
  197. [/[\w\-]+/, 'attribute.name.html'],
  198. [/=/, 'delimiter.html'],
  199. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded', nextEmbedded: 'text/javascript' }],
  200. [/[ \t\r\n]+/],
  201. [/(<\/)(script\s*)(>)/, ['delimiter.html', 'tag.html', { token: 'delimiter.html', next: '@pop' }]]
  202. ],
  203. // After <script ... type
  204. scriptAfterType: [
  205. [/=/, 'delimiter.html', '@scriptAfterTypeEquals'],
  206. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded', nextEmbedded: 'text/javascript' }],
  207. [/[ \t\r\n]+/],
  208. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  209. ],
  210. // After <script ... type =
  211. scriptAfterTypeEquals: [
  212. [/"([^"]*)"/, { token: 'attribute.value.html', switchTo: '@scriptWithCustomType.$1' }],
  213. [/'([^']*)'/, { token: 'attribute.value.html', switchTo: '@scriptWithCustomType.$1' }],
  214. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded', nextEmbedded: 'text/javascript' }],
  215. [/[ \t\r\n]+/],
  216. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  217. ],
  218. // After <script ... type = $S2
  219. scriptWithCustomType: [
  220. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.$S2', nextEmbedded: '$S2' }],
  221. [/"([^"]*)"/, 'attribute.value.html'],
  222. [/'([^']*)'/, 'attribute.value.html'],
  223. [/[\w\-]+/, 'attribute.name.html'],
  224. [/=/, 'delimiter.html'],
  225. [/[ \t\r\n]+/],
  226. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  227. ],
  228. scriptEmbedded: [
  229. [/<\/script/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }],
  230. [/[^<]+/, '']
  231. ],
  232. // -- END <script> tags handling
  233. // -- BEGIN <style> tags handling
  234. // After <style
  235. style: [
  236. [/type/, 'attribute.name.html', '@styleAfterType'],
  237. [/"([^"]*)"/, 'attribute.value.html'],
  238. [/'([^']*)'/, 'attribute.value.html'],
  239. [/[\w\-]+/, 'attribute.name.html'],
  240. [/=/, 'delimiter.html'],
  241. [/>/, { token: 'delimiter.html', next: '@styleEmbedded', nextEmbedded: 'text/css' }],
  242. [/[ \t\r\n]+/],
  243. [/(<\/)(style\s*)(>)/, ['delimiter.html', 'tag.html', { token: 'delimiter.html', next: '@pop' }]]
  244. ],
  245. // After <style ... type
  246. styleAfterType: [
  247. [/=/, 'delimiter.html', '@styleAfterTypeEquals'],
  248. [/>/, { token: 'delimiter.html', next: '@styleEmbedded', nextEmbedded: 'text/css' }],
  249. [/[ \t\r\n]+/],
  250. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  251. ],
  252. // After <style ... type =
  253. styleAfterTypeEquals: [
  254. [/"([^"]*)"/, { token: 'attribute.value.html', switchTo: '@styleWithCustomType.$1' }],
  255. [/'([^']*)'/, { token: 'attribute.value.html', switchTo: '@styleWithCustomType.$1' }],
  256. [/>/, { token: 'delimiter.html', next: '@styleEmbedded', nextEmbedded: 'text/css' }],
  257. [/[ \t\r\n]+/],
  258. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  259. ],
  260. // After <style ... type = $S2
  261. styleWithCustomType: [
  262. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.$S2', nextEmbedded: '$S2' }],
  263. [/"([^"]*)"/, 'attribute.value.html'],
  264. [/'([^']*)'/, 'attribute.value.html'],
  265. [/[\w\-]+/, 'attribute.name.html'],
  266. [/=/, 'delimiter.html'],
  267. [/[ \t\r\n]+/],
  268. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  269. ],
  270. styleEmbedded: [
  271. [/<\/style/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }],
  272. [/[^<]+/, '']
  273. ],
  274. }
  275. };
  276. });