qml.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Language: QML
  3. Requires: javascript.js, xml.js
  4. Author: John Foster <[email protected]>
  5. Description: Syntax highlighting for the Qt Quick QML scripting language, based mostly off
  6. the JavaScript parser.
  7. Category: scripting
  8. */
  9. function(hljs) {
  10. var KEYWORDS = {
  11. keyword:
  12. 'in of on if for while finally var new function do return void else break catch ' +
  13. 'instanceof with throw case default try this switch continue typeof delete ' +
  14. 'let yield const export super debugger as async await import',
  15. literal:
  16. 'true false null undefined NaN Infinity',
  17. built_in:
  18. 'eval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent ' +
  19. 'encodeURI encodeURIComponent escape unescape Object Function Boolean Error ' +
  20. 'EvalError InternalError RangeError ReferenceError StopIteration SyntaxError ' +
  21. 'TypeError URIError Number Math Date String RegExp Array Float32Array ' +
  22. 'Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array ' +
  23. 'Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require ' +
  24. 'module console window document Symbol Set Map WeakSet WeakMap Proxy Reflect ' +
  25. 'Behavior bool color coordinate date double enumeration font geocircle georectangle ' +
  26. 'geoshape int list matrix4x4 parent point quaternion real rect ' +
  27. 'size string url var variant vector2d vector3d vector4d' +
  28. 'Promise'
  29. };
  30. var QML_IDENT_RE = '[a-zA-Z_][a-zA-Z0-9\\._]*';
  31. // Isolate property statements. Ends at a :, =, ;, ,, a comment or end of line.
  32. // Use property class.
  33. var PROPERTY = {
  34. className: 'keyword',
  35. begin: '\\bproperty\\b',
  36. starts: {
  37. className: 'string',
  38. end: '(:|=|;|,|//|/\\*|$)',
  39. returnEnd: true
  40. }
  41. };
  42. // Isolate signal statements. Ends at a ) a comment or end of line.
  43. // Use property class.
  44. var SIGNAL = {
  45. className: 'keyword',
  46. begin: '\\bsignal\\b',
  47. starts: {
  48. className: 'string',
  49. end: '(\\(|:|=|;|,|//|/\\*|$)',
  50. returnEnd: true
  51. }
  52. };
  53. // id: is special in QML. When we see id: we want to mark the id: as attribute and
  54. // emphasize the token following.
  55. var ID_ID = {
  56. className: 'attribute',
  57. begin: '\\bid\\s*:',
  58. starts: {
  59. className: 'string',
  60. end: QML_IDENT_RE,
  61. returnEnd: false
  62. }
  63. };
  64. // Find QML object attribute. An attribute is a QML identifier followed by :.
  65. // Unfortunately it's hard to know where it ends, as it may contain scalars,
  66. // objects, object definitions, or javascript. The true end is either when the parent
  67. // ends or the next attribute is detected.
  68. var QML_ATTRIBUTE = {
  69. begin: QML_IDENT_RE + '\\s*:',
  70. returnBegin: true,
  71. contains: [
  72. {
  73. className: 'attribute',
  74. begin: QML_IDENT_RE,
  75. end: '\\s*:',
  76. excludeEnd: true,
  77. relevance: 0
  78. }
  79. ],
  80. relevance: 0
  81. };
  82. // Find QML object. A QML object is a QML identifier followed by { and ends at the matching }.
  83. // All we really care about is finding IDENT followed by { and just mark up the IDENT and ignore the {.
  84. var QML_OBJECT = {
  85. begin: QML_IDENT_RE + '\\s*{', end: '{',
  86. returnBegin: true,
  87. relevance: 0,
  88. contains: [
  89. hljs.inherit(hljs.TITLE_MODE, {begin: QML_IDENT_RE})
  90. ]
  91. };
  92. return {
  93. aliases: ['qt'],
  94. case_insensitive: false,
  95. keywords: KEYWORDS,
  96. contains: [
  97. {
  98. className: 'meta',
  99. begin: /^\s*['"]use (strict|asm)['"]/
  100. },
  101. hljs.APOS_STRING_MODE,
  102. hljs.QUOTE_STRING_MODE,
  103. { // template string
  104. className: 'string',
  105. begin: '`', end: '`',
  106. contains: [
  107. hljs.BACKSLASH_ESCAPE,
  108. {
  109. className: 'subst',
  110. begin: '\\$\\{', end: '\\}'
  111. }
  112. ]
  113. },
  114. hljs.C_LINE_COMMENT_MODE,
  115. hljs.C_BLOCK_COMMENT_MODE,
  116. {
  117. className: 'number',
  118. variants: [
  119. { begin: '\\b(0[bB][01]+)' },
  120. { begin: '\\b(0[oO][0-7]+)' },
  121. { begin: hljs.C_NUMBER_RE }
  122. ],
  123. relevance: 0
  124. },
  125. { // "value" container
  126. begin: '(' + hljs.RE_STARTERS_RE + '|\\b(case|return|throw)\\b)\\s*',
  127. keywords: 'return throw case',
  128. contains: [
  129. hljs.C_LINE_COMMENT_MODE,
  130. hljs.C_BLOCK_COMMENT_MODE,
  131. hljs.REGEXP_MODE,
  132. { // E4X / JSX
  133. begin: /</, end: />\s*[);\]]/,
  134. relevance: 0,
  135. subLanguage: 'xml'
  136. }
  137. ],
  138. relevance: 0
  139. },
  140. SIGNAL,
  141. PROPERTY,
  142. {
  143. className: 'function',
  144. beginKeywords: 'function', end: /\{/, excludeEnd: true,
  145. contains: [
  146. hljs.inherit(hljs.TITLE_MODE, {begin: /[A-Za-z$_][0-9A-Za-z$_]*/}),
  147. {
  148. className: 'params',
  149. begin: /\(/, end: /\)/,
  150. excludeBegin: true,
  151. excludeEnd: true,
  152. contains: [
  153. hljs.C_LINE_COMMENT_MODE,
  154. hljs.C_BLOCK_COMMENT_MODE
  155. ]
  156. }
  157. ],
  158. illegal: /\[|%/
  159. },
  160. {
  161. begin: '\\.' + hljs.IDENT_RE, relevance: 0 // hack: prevents detection of keywords after dots
  162. },
  163. ID_ID,
  164. QML_ATTRIBUTE,
  165. QML_OBJECT
  166. ],
  167. illegal: /#/
  168. };
  169. }