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