prism-qsharp.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. (function (Prism) {
  2. /**
  3. * Replaces all placeholders "<<n>>" of given pattern with the n-th replacement (zero based).
  4. *
  5. * Note: This is a simple text based replacement. Be careful when using backreferences!
  6. *
  7. * @param {string} pattern the given pattern.
  8. * @param {string[]} replacements a list of replacement which can be inserted into the given pattern.
  9. * @returns {string} the pattern with all placeholders replaced with their corresponding replacements.
  10. * @example replace(/a<<0>>a/.source, [/b+/.source]) === /a(?:b+)a/.source
  11. */
  12. function replace(pattern, replacements) {
  13. return pattern.replace(/<<(\d+)>>/g, function (m, index) {
  14. return '(?:' + replacements[+index] + ')';
  15. });
  16. }
  17. /**
  18. * @param {string} pattern
  19. * @param {string[]} replacements
  20. * @param {string} [flags]
  21. * @returns {RegExp}
  22. */
  23. function re(pattern, replacements, flags) {
  24. return RegExp(replace(pattern, replacements), flags || '');
  25. }
  26. /**
  27. * Creates a nested pattern where all occurrences of the string `<<self>>` are replaced with the pattern itself.
  28. *
  29. * @param {string} pattern
  30. * @param {number} depthLog2
  31. * @returns {string}
  32. */
  33. function nested(pattern, depthLog2) {
  34. for (var i = 0; i < depthLog2; i++) {
  35. pattern = pattern.replace(/<<self>>/g, function () { return '(?:' + pattern + ')'; });
  36. }
  37. return pattern.replace(/<<self>>/g, '[^\\s\\S]');
  38. }
  39. // https://docs.microsoft.com/en-us/azure/quantum/user-guide/language/typesystem/
  40. // https://github.com/microsoft/qsharp-language/tree/main/Specifications/Language/5_Grammar
  41. var keywordKinds = {
  42. // keywords which represent a return or variable type
  43. type: 'Adj BigInt Bool Ctl Double false Int One Pauli PauliI PauliX PauliY PauliZ Qubit Range Result String true Unit Zero',
  44. // all other keywords
  45. other: 'Adjoint adjoint apply as auto body borrow borrowing Controlled controlled distribute elif else fail fixup for function if in internal intrinsic invert is let mutable namespace new newtype open operation repeat return self set until use using while within'
  46. };
  47. // keywords
  48. function keywordsToPattern(words) {
  49. return '\\b(?:' + words.trim().replace(/ /g, '|') + ')\\b';
  50. }
  51. var keywords = RegExp(keywordsToPattern(keywordKinds.type + ' ' + keywordKinds.other));
  52. // types
  53. var identifier = /\b[A-Za-z_]\w*\b/.source;
  54. var qualifiedName = replace(/<<0>>(?:\s*\.\s*<<0>>)*/.source, [identifier]);
  55. var typeInside = {
  56. 'keyword': keywords,
  57. 'punctuation': /[<>()?,.:[\]]/
  58. };
  59. // strings
  60. var regularString = /"(?:\\.|[^\\"])*"/.source;
  61. Prism.languages.qsharp = Prism.languages.extend('clike', {
  62. 'comment': /\/\/.*/,
  63. 'string': [
  64. {
  65. pattern: re(/(^|[^$\\])<<0>>/.source, [regularString]),
  66. lookbehind: true,
  67. greedy: true
  68. }
  69. ],
  70. 'class-name': [
  71. {
  72. // open Microsoft.Quantum.Canon;
  73. // open Microsoft.Quantum.Canon as CN;
  74. pattern: re(/(\b(?:as|open)\s+)<<0>>(?=\s*(?:;|as\b))/.source, [qualifiedName]),
  75. lookbehind: true,
  76. inside: typeInside
  77. },
  78. {
  79. // namespace Quantum.App1;
  80. pattern: re(/(\bnamespace\s+)<<0>>(?=\s*\{)/.source, [qualifiedName]),
  81. lookbehind: true,
  82. inside: typeInside
  83. },
  84. ],
  85. 'keyword': keywords,
  86. 'number': /(?:\b0(?:x[\da-f]+|b[01]+|o[0-7]+)|(?:\B\.\d+|\b\d+(?:\.\d*)?)(?:e[-+]?\d+)?)l?\b/i,
  87. 'operator': /\band=|\bor=|\band\b|\bnot\b|\bor\b|<[-=]|[-=]>|>>>=?|<<<=?|\^\^\^=?|\|\|\|=?|&&&=?|w\/=?|~~~|[*\/+\-^=!%]=?/,
  88. 'punctuation': /::|[{}[\];(),.:]/
  89. });
  90. Prism.languages.insertBefore('qsharp', 'number', {
  91. 'range': {
  92. pattern: /\.\./,
  93. alias: 'operator'
  94. }
  95. });
  96. // single line
  97. var interpolationExpr = nested(replace(/\{(?:[^"{}]|<<0>>|<<self>>)*\}/.source, [regularString]), 2);
  98. Prism.languages.insertBefore('qsharp', 'string', {
  99. 'interpolation-string': {
  100. pattern: re(/\$"(?:\\.|<<0>>|[^\\"{])*"/.source, [interpolationExpr]),
  101. greedy: true,
  102. inside: {
  103. 'interpolation': {
  104. pattern: re(/((?:^|[^\\])(?:\\\\)*)<<0>>/.source, [interpolationExpr]),
  105. lookbehind: true,
  106. inside: {
  107. 'punctuation': /^\{|\}$/,
  108. 'expression': {
  109. pattern: /[\s\S]+/,
  110. alias: 'language-qsharp',
  111. inside: Prism.languages.qsharp
  112. }
  113. }
  114. },
  115. 'string': /[\s\S]+/
  116. }
  117. }
  118. });
  119. }(Prism));
  120. Prism.languages.qs = Prism.languages.qsharp;