qml.js 5.0 KB

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