wast.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports == 'object' && typeof module == 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'), require('../../addon/mode/simple'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror', '../../addon/mode/simple'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. var kKeywords = [
  15. 'align',
  16. 'block',
  17. 'br(_if|_table|_on_(cast|data|func|i31|null))?',
  18. 'call(_indirect|_ref)?',
  19. 'current_memory',
  20. '\\bdata\\b',
  21. 'catch(_all)?',
  22. 'delegate',
  23. 'drop',
  24. 'elem',
  25. 'else',
  26. 'end',
  27. 'export',
  28. '\\bextern\\b',
  29. '\\bfunc\\b',
  30. 'global(\\.(get|set))?',
  31. 'if',
  32. 'import',
  33. 'local(\\.(get|set|tee))?',
  34. 'loop',
  35. 'module',
  36. 'mut',
  37. 'nop',
  38. 'offset',
  39. 'param',
  40. 'result',
  41. 'rethrow',
  42. 'return(_call(_indirect|_ref)?)?',
  43. 'select',
  44. 'start',
  45. 'table(\\.(size|get|set|size|grow|fill|init|copy))?',
  46. 'then',
  47. 'throw',
  48. 'try',
  49. 'type',
  50. 'unreachable',
  51. 'unwind',
  52. // Numeric opcodes.
  53. 'i(32|64)\\.(store(8|16)|(load(8|16)_[su]))',
  54. 'i64\\.(load32_[su]|store32)',
  55. '[fi](32|64)\\.(const|load|store)',
  56. 'f(32|64)\\.(abs|add|ceil|copysign|div|eq|floor|[gl][et]|max|min|mul|nearest|neg?|sqrt|sub|trunc)',
  57. 'i(32|64)\\.(a[dn]d|c[lt]z|(div|rem)_[su]|eqz?|[gl][te]_[su]|mul|ne|popcnt|rot[lr]|sh(l|r_[su])|sub|x?or)',
  58. 'i64\\.extend_[su]_i32',
  59. 'i32\\.wrap_i64',
  60. 'i(32|64)\\.trunc_f(32|64)_[su]',
  61. 'f(32|64)\\.convert_i(32|64)_[su]',
  62. 'f64\\.promote_f32',
  63. 'f32\\.demote_f64',
  64. 'f32\\.reinterpret_i32',
  65. 'i32\\.reinterpret_f32',
  66. 'f64\\.reinterpret_i64',
  67. 'i64\\.reinterpret_f64',
  68. // Atomics.
  69. 'memory(\\.((atomic\\.(notify|wait(32|64)))|grow|size))?',
  70. 'i64.atomic\\.(load32_u|store32|rmw32\\.(a[dn]d|sub|x?or|(cmp)?xchg)_u)',
  71. 'i(32|64)\\.atomic\\.(load((8|16)_u)?|store(8|16)?|rmw(\\.(a[dn]d|sub|x?or|(cmp)?xchg)|(8|16)\\.(a[dn]d|sub|x?or|(cmp)?xchg)_u))',
  72. // SIMD.
  73. 'v128\\.load(8x8|16x4|32x2)_[su]',
  74. 'v128\\.load(8|16|32|64)_splat',
  75. 'v128\\.(load|store)(8|16|32|64)_lane',
  76. 'v128\\.load(32|64)_zero',
  77. 'v128.(load|store|const|not|andnot|and|or|xor|bitselect|any_true)',
  78. 'i(8x16|16x8)\\.(extract_lane_[su]|(add|sub)_sat_[su]|avgr_u)',
  79. 'i(8x16|16x8|32x4|64x2)\\.(neg|add|sub|abs|shl|shr_[su]|all_true|bitmask|eq|ne|[lg][te]_s)',
  80. '(i(8x16|16x8|32x4|64x2)|f(32x4|64x2)).(splat|replace_lane)',
  81. 'i(8x16|16x8|32x4)\\.(([lg][te]_u)|((min|max)_[su]))',
  82. 'f(32x4|64x2)\\.(neg|add|sub|abs|nearest|eq|ne|[lg][te]|sqrt|mul|div|min|max|ceil|floor|trunc)',
  83. '[fi](32x4|64x2)\\.extract_lane',
  84. 'i8x16\\.(shuffle|swizzle|popcnt|narrow_i16x8_[su])',
  85. 'i16x8\\.(narrow_i32x4_[su]|mul|extadd_pairwise_i8x16_[su]|q15mulr_sat_s)',
  86. 'i16x8\\.(extend|extmul)_(low|high)_i8x16_[su]',
  87. 'i32x4\\.(mul|dot_i16x8_s|trunc_sat_f64x2_[su]_zero)',
  88. 'i32x4\\.((extend|extmul)_(low|high)_i16x8_|trunc_sat_f32x4_|extadd_pairwise_i16x8_)[su]',
  89. 'i64x2\\.(mul|(extend|extmul)_(low|high)_i32x4_[su])',
  90. 'f32x4\\.(convert_i32x4_[su]|demote_f64x2_zero)',
  91. 'f64x2\\.(promote_low_f32x4|convert_low_i32x4_[su])',
  92. // Reference types, function references, and GC.
  93. '\\bany\\b',
  94. 'array\\.len',
  95. '(array|struct)(\\.(new_(default_)?with_rtt|get(_[su])?|set))?',
  96. '\\beq\\b',
  97. 'field',
  98. 'i31\\.(new|get_[su])',
  99. '\\bnull\\b',
  100. 'ref(\\.(([ai]s_(data|func|i31))|cast|eq|func|(is_|as_non_)?null|test))?',
  101. 'rtt(\\.(canon|sub))?',
  102. ]
  103. CodeMirror.defineSimpleMode('wast', {
  104. start: [
  105. { regex: /[+\-]?(?:nan(?::0x[0-9a-fA-F]+)?|infinity|inf|0x[0-9a-fA-F]+\.?[0-9a-fA-F]*p[+\/-]?\d+|\d+(?:\.\d*)?[eE][+\-]?\d*|\d+\.\d*|0x[0-9a-fA-F]+|\d+)/, token: 'number' },
  106. { regex: new RegExp(kKeywords.join('|')), token: 'keyword' },
  107. { regex: /\b((any|data|eq|extern|i31|func)ref|[fi](32|64)|i(8|16))\b/, token: 'atom' },
  108. { regex: /\$([a-zA-Z0-9_`\+\-\*\/\\\^~=<>!\?@#$%&|:\.]+)/, token: 'variable-2' },
  109. { regex: /"(?:[^"\\\x00-\x1f\x7f]|\\[nt\\'"]|\\[0-9a-fA-F][0-9a-fA-F])*"/, token: 'string' },
  110. { regex: /\(;.*?/, token: 'comment', next: 'comment' },
  111. { regex: /;;.*$/, token: 'comment' },
  112. { regex: /\(/, indent: true },
  113. { regex: /\)/, dedent: true },
  114. ],
  115. comment: [
  116. { regex: /.*?;\)/, token: 'comment', next: 'start' },
  117. { regex: /.*/, token: 'comment' },
  118. ],
  119. meta: {
  120. dontIndentStates: ['comment'],
  121. },
  122. })
  123. // https://github.com/WebAssembly/design/issues/981 mentions text/webassembly,
  124. // which seems like a reasonable choice, although it's not standard right now.
  125. CodeMirror.defineMIME('text/webassembly', 'wast')
  126. })