pug.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. comments: {
  10. lineComment: '//'
  11. },
  12. brackets: [['{', '}'], ['[', ']'], ['(', ')']],
  13. autoClosingPairs: [
  14. { open: '"', close: '"', notIn: ['string', 'comment'] },
  15. { open: '\'', close: '\'', notIn: ['string', 'comment'] },
  16. { open: '{', close: '}', notIn: ['string', 'comment'] },
  17. { open: '[', close: ']', notIn: ['string', 'comment'] },
  18. { open: '(', close: ')', notIn: ['string', 'comment'] },
  19. ],
  20. folding: {
  21. offSide: true
  22. }
  23. };
  24. exports.language = {
  25. defaultToken: '',
  26. tokenPostfix: '.pug',
  27. ignoreCase: true,
  28. brackets: [
  29. { token: 'delimiter.curly', open: '{', close: '}' },
  30. { token: 'delimiter.array', open: '[', close: ']' },
  31. { token: 'delimiter.parenthesis', open: '(', close: ')' }
  32. ],
  33. keywords: ['append', 'block', 'case', 'default', 'doctype', 'each', 'else', 'extends',
  34. 'for', 'if', 'in', 'include', 'mixin', 'typeof', 'unless', 'var', 'when'],
  35. tags: [
  36. 'a', 'abbr', 'acronym', 'address', 'area', 'article', 'aside', 'audio',
  37. 'b', 'base', 'basefont', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button',
  38. 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'command',
  39. 'datalist', 'dd', 'del', 'details', 'dfn', 'div', 'dl', 'dt',
  40. 'em', 'embed',
  41. 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form', 'frame', 'frameset',
  42. 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html',
  43. 'i', 'iframe', 'img', 'input', 'ins',
  44. 'keygen', 'kbd',
  45. 'label', 'li', 'link',
  46. 'map', 'mark', 'menu', 'meta', 'meter',
  47. 'nav', 'noframes', 'noscript',
  48. 'object', 'ol', 'optgroup', 'option', 'output',
  49. 'p', 'param', 'pre', 'progress',
  50. 'q',
  51. 'rp', 'rt', 'ruby',
  52. 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup',
  53. 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'tracks', 'tt',
  54. 'u', 'ul',
  55. 'video',
  56. 'wbr'
  57. ],
  58. // we include these common regular expressions
  59. symbols: /[\+\-\*\%\&\|\!\=\/\.\,\:]+/,
  60. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  61. tokenizer: {
  62. root: [
  63. // Tag or a keyword at start
  64. [/^(\s*)([a-zA-Z_-][\w-]*)/,
  65. {
  66. cases: {
  67. '$2@tags': {
  68. cases: {
  69. '@eos': ['', 'tag'],
  70. '@default': ['', { token: 'tag', next: '@tag.$1' },]
  71. }
  72. },
  73. '$2@keywords': ['', { token: 'keyword.$2' },],
  74. '@default': ['', '',]
  75. }
  76. }
  77. ],
  78. // id
  79. [/^(\s*)(#[a-zA-Z_-][\w-]*)/, {
  80. cases: {
  81. '@eos': ['', 'tag.id'],
  82. '@default': ['', { token: 'tag.id', next: '@tag.$1' }]
  83. }
  84. }],
  85. // class
  86. [/^(\s*)(\.[a-zA-Z_-][\w-]*)/, {
  87. cases: {
  88. '@eos': ['', 'tag.class'],
  89. '@default': ['', { token: 'tag.class', next: '@tag.$1' }]
  90. }
  91. }],
  92. // plain text with pipe
  93. [/^(\s*)(\|.*)$/, ''],
  94. { include: '@whitespace' },
  95. // keywords
  96. [/[a-zA-Z_$][\w$]*/, {
  97. cases: {
  98. '@keywords': { token: 'keyword.$0' },
  99. '@default': ''
  100. }
  101. }],
  102. // delimiters and operators
  103. [/[{}()\[\]]/, '@brackets'],
  104. [/@symbols/, 'delimiter'],
  105. // numbers
  106. [/\d+\.\d+([eE][\-+]?\d+)?/, 'number.float'],
  107. [/\d+/, 'number'],
  108. // strings:
  109. [/"/, 'string', '@string."'],
  110. [/'/, 'string', '@string.\''],
  111. ],
  112. tag: [
  113. [/(\.)(\s*$)/, [{ token: 'delimiter', next: '@blockText.$S2.' }, '']],
  114. [/\s+/, { token: '', next: '@simpleText' }],
  115. // id
  116. [/#[a-zA-Z_-][\w-]*/, {
  117. cases: {
  118. '@eos': { token: 'tag.id', next: '@pop' },
  119. '@default': 'tag.id'
  120. }
  121. }],
  122. // class
  123. [/\.[a-zA-Z_-][\w-]*/, {
  124. cases: {
  125. '@eos': { token: 'tag.class', next: '@pop' },
  126. '@default': 'tag.class'
  127. }
  128. }],
  129. // attributes
  130. [/\(/, { token: 'delimiter.parenthesis', next: '@attributeList' }],
  131. ],
  132. simpleText: [
  133. [/[^#]+$/, { token: '', next: '@popall' }],
  134. [/[^#]+/, { token: '' }],
  135. // interpolation
  136. [/(#{)([^}]*)(})/, {
  137. cases: {
  138. '@eos': ['interpolation.delimiter', 'interpolation', { token: 'interpolation.delimiter', next: '@popall' }],
  139. '@default': ['interpolation.delimiter', 'interpolation', 'interpolation.delimiter']
  140. }
  141. }],
  142. [/#$/, { token: '', next: '@popall' }],
  143. [/#/, '']
  144. ],
  145. attributeList: [
  146. [/\s+/, ''],
  147. [/(\w+)(\s*=\s*)("|')/, ['attribute.name', 'delimiter', { token: 'attribute.value', next: '@value.$3' }]],
  148. [/\w+/, 'attribute.name'],
  149. [/,/, {
  150. cases: {
  151. '@eos': { token: 'attribute.delimiter', next: '@popall' },
  152. '@default': 'attribute.delimiter'
  153. }
  154. }],
  155. [/\)$/, { token: 'delimiter.parenthesis', next: '@popall' }],
  156. [/\)/, { token: 'delimiter.parenthesis', next: '@pop' }],
  157. ],
  158. whitespace: [
  159. [/^(\s*)(\/\/.*)$/, { token: 'comment', next: '@blockText.$1.comment' }],
  160. [/[ \t\r\n]+/, ''],
  161. [/<!--/, { token: 'comment', next: '@comment' }],
  162. ],
  163. blockText: [
  164. [/^\s+.*$/, {
  165. cases: {
  166. '($S2\\s+.*$)': { token: '$S3' },
  167. '@default': { token: '@rematch', next: '@popall' }
  168. }
  169. }],
  170. [/./, { token: '@rematch', next: '@popall' }]
  171. ],
  172. comment: [
  173. [/[^<\-]+/, 'comment.content'],
  174. [/-->/, { token: 'comment', next: '@pop' }],
  175. [/<!--/, 'comment.content.invalid'],
  176. [/[<\-]/, 'comment.content']
  177. ],
  178. string: [
  179. [/[^\\"'#]+/, {
  180. cases: {
  181. '@eos': { token: 'string', next: '@popall' },
  182. '@default': 'string'
  183. }
  184. }],
  185. [/@escapes/, {
  186. cases: {
  187. '@eos': { token: 'string.escape', next: '@popall' },
  188. '@default': 'string.escape'
  189. }
  190. }],
  191. [/\\./, {
  192. cases: {
  193. '@eos': { token: 'string.escape.invalid', next: '@popall' },
  194. '@default': 'string.escape.invalid'
  195. }
  196. }],
  197. // interpolation
  198. [/(#{)([^}]*)(})/, ['interpolation.delimiter', 'interpolation', 'interpolation.delimiter']],
  199. [/#/, 'string'],
  200. [/["']/, {
  201. cases: {
  202. '$#==$S2': { token: 'string', next: '@pop' },
  203. '@default': { token: 'string' }
  204. }
  205. }],
  206. ],
  207. // Almost identical to above, except for escapes and the output token
  208. value: [
  209. [/[^\\"']+/, {
  210. cases: {
  211. '@eos': { token: 'attribute.value', next: '@popall' },
  212. '@default': 'attribute.value'
  213. }
  214. }],
  215. [/\\./, {
  216. cases: {
  217. '@eos': { token: 'attribute.value', next: '@popall' },
  218. '@default': 'attribute.value'
  219. }
  220. }],
  221. [/["']/, {
  222. cases: {
  223. '$#==$S2': { token: 'attribute.value', next: '@pop' },
  224. '@default': { token: 'attribute.value' }
  225. }
  226. }],
  227. ],
  228. },
  229. };
  230. });