qml.js 5.3 KB

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