webassembly.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. pygments.lexers.webassembly
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the WebAssembly text format.
  5. The grammar can be found at https://github.com/WebAssembly/spec/blob/master/interpreter/README.md
  6. and https://webassembly.github.io/spec/core/text/.
  7. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  8. :license: BSD, see LICENSE for details.
  9. """
  10. from pygments.lexer import RegexLexer, words, bygroups, default
  11. from pygments.token import Text, Comment, Operator, Keyword, String, Number, Punctuation, Name
  12. __all__ = ['WatLexer']
  13. keywords = (
  14. 'module', 'import', 'func', 'funcref', 'start', 'param', 'local', 'type',
  15. 'result', 'export', 'memory', 'global', 'mut', 'data', 'table', 'elem',
  16. 'if', 'then', 'else', 'end', 'block', 'loop'
  17. )
  18. builtins = (
  19. 'unreachable', 'nop', 'block', 'loop', 'if', 'else', 'end', 'br', 'br_if',
  20. 'br_table', 'return', 'call', 'call_indirect', 'drop', 'select',
  21. 'local.get', 'local.set', 'local.tee', 'global.get', 'global.set',
  22. 'i32.load', 'i64.load', 'f32.load', 'f64.load', 'i32.load8_s',
  23. 'i32.load8_u', 'i32.load16_s', 'i32.load16_u', 'i64.load8_s',
  24. 'i64.load8_u', 'i64.load16_s', 'i64.load16_u', 'i64.load32_s',
  25. 'i64.load32_u', 'i32.store', 'i64.store', 'f32.store', 'f64.store',
  26. 'i32.store8', 'i32.store16', 'i64.store8', 'i64.store16', 'i64.store32',
  27. 'memory.size', 'memory.grow', 'i32.const', 'i64.const', 'f32.const',
  28. 'f64.const', 'i32.eqz', 'i32.eq', 'i32.ne', 'i32.lt_s', 'i32.lt_u',
  29. 'i32.gt_s', 'i32.gt_u', 'i32.le_s', 'i32.le_u', 'i32.ge_s', 'i32.ge_u',
  30. 'i64.eqz', 'i64.eq', 'i64.ne', 'i64.lt_s', 'i64.lt_u', 'i64.gt_s',
  31. 'i64.gt_u', 'i64.le_s', 'i64.le_u', 'i64.ge_s', 'i64.ge_u', 'f32.eq',
  32. 'f32.ne', 'f32.lt', 'f32.gt', 'f32.le', 'f32.ge', 'f64.eq', 'f64.ne',
  33. 'f64.lt', 'f64.gt', 'f64.le', 'f64.ge', 'i32.clz', 'i32.ctz', 'i32.popcnt',
  34. 'i32.add', 'i32.sub', 'i32.mul', 'i32.div_s', 'i32.div_u', 'i32.rem_s',
  35. 'i32.rem_u', 'i32.and', 'i32.or', 'i32.xor', 'i32.shl', 'i32.shr_s',
  36. 'i32.shr_u', 'i32.rotl', 'i32.rotr', 'i64.clz', 'i64.ctz', 'i64.popcnt',
  37. 'i64.add', 'i64.sub', 'i64.mul', 'i64.div_s', 'i64.div_u', 'i64.rem_s',
  38. 'i64.rem_u', 'i64.and', 'i64.or', 'i64.xor', 'i64.shl', 'i64.shr_s',
  39. 'i64.shr_u', 'i64.rotl', 'i64.rotr', 'f32.abs', 'f32.neg', 'f32.ceil',
  40. 'f32.floor', 'f32.trunc', 'f32.nearest', 'f32.sqrt', 'f32.add', 'f32.sub',
  41. 'f32.mul', 'f32.div', 'f32.min', 'f32.max', 'f32.copysign', 'f64.abs',
  42. 'f64.neg', 'f64.ceil', 'f64.floor', 'f64.trunc', 'f64.nearest', 'f64.sqrt',
  43. 'f64.add', 'f64.sub', 'f64.mul', 'f64.div', 'f64.min', 'f64.max',
  44. 'f64.copysign', 'i32.wrap_i64', 'i32.trunc_f32_s', 'i32.trunc_f32_u',
  45. 'i32.trunc_f64_s', 'i32.trunc_f64_u', 'i64.extend_i32_s',
  46. 'i64.extend_i32_u', 'i64.trunc_f32_s', 'i64.trunc_f32_u',
  47. 'i64.trunc_f64_s', 'i64.trunc_f64_u', 'f32.convert_i32_s',
  48. 'f32.convert_i32_u', 'f32.convert_i64_s', 'f32.convert_i64_u',
  49. 'f32.demote_f64', 'f64.convert_i32_s', 'f64.convert_i32_u',
  50. 'f64.convert_i64_s', 'f64.convert_i64_u', 'f64.promote_f32',
  51. 'i32.reinterpret_f32', 'i64.reinterpret_f64', 'f32.reinterpret_i32',
  52. 'f64.reinterpret_i64',
  53. )
  54. class WatLexer(RegexLexer):
  55. """Lexer for the WebAssembly text format.
  56. .. versionadded:: 2.9
  57. """
  58. name = 'WebAssembly'
  59. url = 'https://webassembly.org/'
  60. aliases = ['wast', 'wat']
  61. filenames = ['*.wat', '*.wast']
  62. tokens = {
  63. 'root': [
  64. (words(keywords, suffix=r'(?=[^a-z_\.])'), Keyword),
  65. (words(builtins), Name.Builtin, 'arguments'),
  66. (words(['i32', 'i64', 'f32', 'f64']), Keyword.Type),
  67. (r'\$[A-Za-z0-9!#$%&\'*+./:<=>?@\\^_`|~-]+', Name.Variable), # yes, all of the are valid in identifiers
  68. (r';;.*?$', Comment.Single),
  69. (r'\(;', Comment.Multiline, 'nesting_comment'),
  70. (r'[+-]?0x[\dA-Fa-f](_?[\dA-Fa-f])*(.([\dA-Fa-f](_?[\dA-Fa-f])*)?)?([pP][+-]?[\dA-Fa-f](_?[\dA-Fa-f])*)?', Number.Float),
  71. (r'[+-]?\d.\d(_?\d)*[eE][+-]?\d(_?\d)*', Number.Float),
  72. (r'[+-]?\d.\d(_?\d)*', Number.Float),
  73. (r'[+-]?\d.[eE][+-]?\d(_?\d)*', Number.Float),
  74. (r'[+-]?(inf|nan:0x[\dA-Fa-f](_?[\dA-Fa-f])*|nan)', Number.Float),
  75. (r'[+-]?0x[\dA-Fa-f](_?[\dA-Fa-f])*', Number.Hex),
  76. (r'[+-]?\d(_?\d)*', Number.Integer),
  77. (r'[\(\)]', Punctuation),
  78. (r'"', String.Double, 'string'),
  79. (r'\s+', Text),
  80. ],
  81. 'nesting_comment': [
  82. (r'\(;', Comment.Multiline, '#push'),
  83. (r';\)', Comment.Multiline, '#pop'),
  84. (r'[^;(]+', Comment.Multiline),
  85. (r'[;(]', Comment.Multiline),
  86. ],
  87. 'string': [
  88. (r'\\[\dA-Fa-f][\dA-Fa-f]', String.Escape), # must have exactly two hex digits
  89. (r'\\t', String.Escape),
  90. (r'\\n', String.Escape),
  91. (r'\\r', String.Escape),
  92. (r'\\"', String.Escape),
  93. (r"\\'", String.Escape),
  94. (r'\\u\{[\dA-Fa-f](_?[\dA-Fa-f])*\}', String.Escape),
  95. (r'\\\\', String.Escape),
  96. (r'"', String.Double, '#pop'),
  97. (r'[^"\\]+', String.Double),
  98. ],
  99. 'arguments': [
  100. (r'\s+', Text),
  101. (r'(offset)(=)(0x[\dA-Fa-f](_?[\dA-Fa-f])*)', bygroups(Keyword, Operator, Number.Hex)),
  102. (r'(offset)(=)(\d(_?\d)*)', bygroups(Keyword, Operator, Number.Integer)),
  103. (r'(align)(=)(0x[\dA-Fa-f](_?[\dA-Fa-f])*)', bygroups(Keyword, Operator, Number.Hex)),
  104. (r'(align)(=)(\d(_?\d)*)', bygroups(Keyword, Operator, Number.Integer)),
  105. default('#pop'),
  106. ]
  107. }