|
| 1 | +/* |
| 2 | +Copyright (c) 2019 Renata Hodovan. |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Modified by Guannan Wei (2023). |
| 6 | +
|
| 7 | +Modified by Mikail Khan (2023). |
| 8 | +
|
| 9 | +Redistribution and use in source and binary forms, with or without |
| 10 | +modification, are permitted provided that the following conditions are met: |
| 11 | +
|
| 12 | +1. Redistributions of source code must retain the above copyright notice, this |
| 13 | + list of conditions and the following disclaimer. |
| 14 | +
|
| 15 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + this list of conditions and the following disclaimer in the documentation |
| 17 | + and/or other materials provided with the distribution. |
| 18 | +
|
| 19 | +3. Neither the name of the copyright holder nor the names of its contributors |
| 20 | + may be used to endorse or promote products derived from this software |
| 21 | + without specific prior written permission. |
| 22 | +
|
| 23 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 24 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 25 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 27 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 29 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 30 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 31 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | +*/ |
| 34 | + |
| 35 | +lexer grammar WatLexer; |
| 36 | + |
| 37 | +LPAR : '(' ; |
| 38 | +RPAR : ')' ; |
| 39 | + |
| 40 | +NAT : Nat ; |
| 41 | +INT : Int ; |
| 42 | +FLOAT : Float ; |
| 43 | +STRING_ : String_ ; |
| 44 | +VALUE_TYPE : NXX ; |
| 45 | +CONST : NXX '.const' ; |
| 46 | +SYMBOLIC : NXX '.symbolic' ; |
| 47 | + |
| 48 | +FUNCREF: 'funcref' ; |
| 49 | +EXTERNREF: 'externref' ; |
| 50 | +MUT: 'mut' ; |
| 51 | +REF: 'ref' ; |
| 52 | +CONT: 'cont' ; |
| 53 | +NULL: 'null' ; |
| 54 | + |
| 55 | +NOP: 'nop' ; |
| 56 | +SYM_ASSERT: 'sym_assert' ; |
| 57 | +ALLOC: 'alloc' ; |
| 58 | +FREE: 'free' ; |
| 59 | +UNREACHABLE: 'unreachable' ; |
| 60 | +DROP: 'drop' ; |
| 61 | +BLOCK: 'block' ; |
| 62 | +LOOP: 'loop' ; |
| 63 | +FOR : 'for'; |
| 64 | +VBAR: '|'; |
| 65 | +END: 'end' ; |
| 66 | +BR: 'br' ; |
| 67 | +BR_IF: 'br_if' ; |
| 68 | +BR_TABLE: 'br_table' ; |
| 69 | +RETURN: 'return' ; |
| 70 | +IF: 'if' ; |
| 71 | +THEN: 'then' ; |
| 72 | +ELSE: 'else' ; |
| 73 | +SELECT: '.select' ; |
| 74 | +CALL: 'call' ; |
| 75 | +CALL_INDIRECT: 'call_indirect' ; |
| 76 | +RETURN_CALL: 'return_call' ; |
| 77 | +RETURN_CALL_INDIRECT: 'return_call_indirect' ; |
| 78 | +REFFUNC: 'ref.func' ; |
| 79 | +CALLREF: 'call_ref' ; |
| 80 | +RESUME: 'resume' ; |
| 81 | +ON: 'on' ; |
| 82 | +CONTNEW: 'cont.new' ; |
| 83 | +CONTBIND: 'cont.bind' ; |
| 84 | +SUSPEND: 'suspend' ; |
| 85 | +REFNULL: 'ref.null' ; |
| 86 | +REFISNULL: 'ref.is_null' ; |
| 87 | + |
| 88 | +// resumable try-catch extension: |
| 89 | +TRY: 'try' ; |
| 90 | +CATCH: 'catch' ; |
| 91 | +THROW: 'throw' ; |
| 92 | +RESUME0: 'resume0' ; |
| 93 | + |
| 94 | +LOCAL_GET: 'local.get' ; |
| 95 | +LOCAL_SET: 'local.set' ; |
| 96 | +LOCAL_TEE: 'local.tee' ; |
| 97 | +GLOBAL_GET: 'global.get' ; |
| 98 | +GLOBAL_SET: 'global.set' ; |
| 99 | + |
| 100 | +LOAD : '.load' (MEM_SIZE UNDERSCORE SIGN_POSTFIX)?; |
| 101 | + |
| 102 | +STORE : '.store' MEM_SIZE? ; |
| 103 | +UNDERSCORE : '_'; |
| 104 | + |
| 105 | +OFFSET_EQ : 'offset=' ; |
| 106 | +ALIGN_EQ : 'align=' ; |
| 107 | + |
| 108 | +SIGN_POSTFIX : 's' | 'u'; |
| 109 | + |
| 110 | +MEM_SIZE : '8' | '16' | '32' | '64'; |
| 111 | + |
| 112 | +I32 : 'i32'; |
| 113 | +I64 : 'i64'; |
| 114 | + |
| 115 | +F32 : 'f32'; |
| 116 | +F64 : 'f64'; |
| 117 | + |
| 118 | +IXX : I32 | I64; |
| 119 | +FXX : F32 | F64; |
| 120 | + |
| 121 | +OP_EQZ : '.eqz'; |
| 122 | + |
| 123 | +OP_EQ : '.eq'; |
| 124 | +OP_NE : '.ne'; |
| 125 | +OP_LT : '.lt'; |
| 126 | +OP_LTS : '.lt_s'; |
| 127 | +OP_LTU : '.lt_u'; |
| 128 | +OP_LE : '.le'; |
| 129 | +OP_LES : '.le_s'; |
| 130 | +OP_LEU : '.le_u'; |
| 131 | +OP_GT : '.gt'; |
| 132 | +OP_GTS : '.gt_s'; |
| 133 | +OP_GTU : '.gt_u'; |
| 134 | +OP_GE : '.ge'; |
| 135 | +OP_GES : '.ge_s'; |
| 136 | +OP_GEU : '.ge_u'; |
| 137 | + |
| 138 | +OP_CLZ : '.clz'; |
| 139 | +OP_CTZ : '.ctz'; |
| 140 | +OP_POPCNT : '.popcnt'; |
| 141 | +OP_NEG : '.neg'; |
| 142 | +OP_ABS : '.abs'; |
| 143 | +OP_SQRT : '.sqrt'; |
| 144 | +OP_CEIL : '.ceil'; |
| 145 | +OP_FLOOR : '.floor'; |
| 146 | +OP_TRUNC : '.trunc'; |
| 147 | +OP_NEAREST : '.nearest'; |
| 148 | + |
| 149 | +OP_ADD : '.add'; |
| 150 | +OP_SUB : '.sub'; |
| 151 | +OP_MUL : '.mul'; |
| 152 | +OP_DIV : '.div'; |
| 153 | +OP_DIV_S : '.div_s'; |
| 154 | +OP_DIV_U : '.div_u'; |
| 155 | +OP_REM_S : '.rem_s'; |
| 156 | +OP_REM_U : '.rem_u'; |
| 157 | +OP_AND : '.and'; |
| 158 | +OP_OR : '.or'; |
| 159 | +OP_XOR : '.xor'; |
| 160 | +OP_SHL : '.shl'; |
| 161 | +OP_SHR_S : '.shr_s'; |
| 162 | +OP_SHR_U : '.shr_u'; |
| 163 | +OP_ROTL : '.rotl'; |
| 164 | +OP_ROTR : '.rotr'; |
| 165 | +OP_MIN : '.min'; |
| 166 | +OP_MAX : '.max'; |
| 167 | +OP_COPYSIGN : '.copysign'; |
| 168 | + |
| 169 | +OP_WRAP : '.wrap_'; |
| 170 | +OP_TRUNC_ : '.trunc_'; |
| 171 | +OP_TRUNC_SAT : '.trunc_sat_'; |
| 172 | +OP_CONVERT : '.convert_'; |
| 173 | +OP_EXTEND : '.extend_'; |
| 174 | +OP_DEMOTE : '.demote_'; |
| 175 | +OP_PROMOTE : '.promote_'; |
| 176 | +OP_REINTER : '.reinterpret_'; |
| 177 | + |
| 178 | +MEMORY_SIZE : 'memory.size' ; |
| 179 | +MEMORY_GROW : 'memory.grow' ; |
| 180 | +MEMORY_FILL : 'memory.fill' ; |
| 181 | +MEMORY_COPY : 'memory.copy' ; |
| 182 | +MEMORY_INIT : 'memory.init' ; |
| 183 | + |
| 184 | +TEST : IXX OP_EQZ; |
| 185 | + |
| 186 | +COMPARE : |
| 187 | + IXX '.eq' |
| 188 | + | IXX '.ne' |
| 189 | + | IXX '.lt_s' |
| 190 | + | IXX '.lt_u' |
| 191 | + | IXX '.le_s' |
| 192 | + | IXX '.le_u' |
| 193 | + | IXX '.gt_s' |
| 194 | + | IXX '.gt_u' |
| 195 | + | IXX '.ge_s' |
| 196 | + | IXX '.ge_u' |
| 197 | + | FXX '.eq' |
| 198 | + | FXX '.ne' |
| 199 | + | FXX '.lt' |
| 200 | + | FXX '.le' |
| 201 | + | FXX '.gt' |
| 202 | + | FXX '.ge' |
| 203 | + ; |
| 204 | + |
| 205 | +UNARY |
| 206 | + : IXX '.clz' |
| 207 | + | IXX '.ctz' |
| 208 | + | IXX '.popcnt' |
| 209 | + | FXX '.neg' |
| 210 | + | FXX '.abs' |
| 211 | + | FXX '.sqrt' |
| 212 | + | FXX '.ceil' |
| 213 | + | FXX '.floor' |
| 214 | + | FXX '.trunc' |
| 215 | + | FXX '.nearest' |
| 216 | + ; |
| 217 | + |
| 218 | +BINARY |
| 219 | + : IXX '.add' |
| 220 | + | IXX '.sub' |
| 221 | + | IXX '.mul' |
| 222 | + | IXX '.div_s' |
| 223 | + | IXX '.div_u' |
| 224 | + | IXX '.rem_s' |
| 225 | + | IXX '.rem_u' |
| 226 | + | IXX '.and' |
| 227 | + | IXX '.or' |
| 228 | + | IXX '.xor' |
| 229 | + | IXX '.shl' |
| 230 | + | IXX '.shr_s' |
| 231 | + | IXX '.shr_u' |
| 232 | + | IXX '.rotl' |
| 233 | + | IXX '.rotr' |
| 234 | + | FXX '.add' |
| 235 | + | FXX '.sub' |
| 236 | + | FXX '.mul' |
| 237 | + | FXX '.div' |
| 238 | + | FXX '.min' |
| 239 | + | FXX '.max' |
| 240 | + | FXX '.copysign' |
| 241 | + ; |
| 242 | + |
| 243 | +CONVERT |
| 244 | + : I32 '.wrap_' I64 |
| 245 | + | IXX '.trunc_' FXX UNDERSCORE SIGN_POSTFIX |
| 246 | + | IXX '.trunc_sat_' FXX UNDERSCORE SIGN_POSTFIX |
| 247 | + | I64 '.extend_' I32 UNDERSCORE SIGN_POSTFIX |
| 248 | + | FXX '.convert_' IXX UNDERSCORE SIGN_POSTFIX |
| 249 | + | F32 '.demote_' F64 |
| 250 | + | F64 '.promote_' F32 |
| 251 | + | F32 '.reinterpret_' I32 |
| 252 | + | F64 '.reinterpret_' I64 |
| 253 | + | I32 '.reinterpret_' F32 |
| 254 | + | I64 '.reinterpret_' F64 |
| 255 | + ; |
| 256 | + |
| 257 | +TYPE: 'type' ; |
| 258 | +FUNC: 'func' ; |
| 259 | +EXTERN: 'extern' ; |
| 260 | +START_: 'start' ; |
| 261 | +PARAM: 'param' ; |
| 262 | +RESULT: 'result' ; |
| 263 | +LOCAL: 'local' ; |
| 264 | +GLOBAL: 'global' ; |
| 265 | +TABLE: 'table' ; |
| 266 | +MEMORY: 'memory' ; |
| 267 | +ELEM: 'elem' ; |
| 268 | +DATA: 'data' ; |
| 269 | +OFFSET: 'offset' ; |
| 270 | +IMPORT: 'import' ; |
| 271 | +EXPORT: 'export' ; |
| 272 | +TAG: 'tag' ; |
| 273 | + |
| 274 | +DECLARE: 'declare' ; |
| 275 | + |
| 276 | +MODULE : 'module' ; |
| 277 | +BIN : 'binary' ; |
| 278 | +QUOTE : 'quote' ; |
| 279 | +DEFINITION : 'definition' ; |
| 280 | +INSTANCE : 'instance' ; |
| 281 | + |
| 282 | +SCRIPT: 'script' ; |
| 283 | +REGISTER: 'register' ; |
| 284 | +INVOKE: 'invoke' ; |
| 285 | +GET: 'get' ; |
| 286 | +ASSERT_MALFORMED: 'assert_malformed' ; |
| 287 | +ASSERT_INVALID: 'assert_invalid' ; |
| 288 | +ASSERT_UNLINKABLE: 'assert_unlinkable' ; |
| 289 | +ASSERT_RETURN: 'assert_return' ; |
| 290 | +ASSERT_RETURN_CANONICAL_NAN: 'assert_return_canonical_nan' ; |
| 291 | +ASSERT_RETURN_ARITHMETIC_NAN: 'assert_return_arithmetic_nan' ; |
| 292 | +ASSERT_TRAP: 'assert_trap' ; |
| 293 | +ASSERT_EXHAUSTION: 'assert_exhaustion' ; |
| 294 | +INPUT: 'input' ; |
| 295 | +OUTPUT: 'output' ; |
| 296 | + |
| 297 | +VAR : Name ; |
| 298 | + |
| 299 | +V128 : 'v128' ; |
| 300 | + |
| 301 | +SPACE |
| 302 | + : [ \t\r\n]+ -> skip |
| 303 | + ; |
| 304 | + |
| 305 | +COMMENT |
| 306 | + : ( '(;' .*? ';)' |
| 307 | + | ';;' .*? '\n')-> skip |
| 308 | + ; |
| 309 | + |
| 310 | +fragment Symbol |
| 311 | + : '.' | '+' | '-' | '*' | '/' | '\\' | '^' | '~' | '=' | '<' | '>' | '!' | '?' | '@' | '#' | '$' | '%' | '&' | '|' | ':' | '\'' | '`' |
| 312 | + ; |
| 313 | + |
| 314 | +fragment Num |
| 315 | + : Digit ('_'? Digit)* |
| 316 | + ; |
| 317 | + |
| 318 | +fragment HexNum |
| 319 | + : HexDigit ('_'? HexDigit)* |
| 320 | + ; |
| 321 | + |
| 322 | +fragment Sign |
| 323 | + : '+' | '-' |
| 324 | + ; |
| 325 | + |
| 326 | +fragment Digit |
| 327 | + : [0-9] |
| 328 | + ; |
| 329 | + |
| 330 | +fragment HexDigit |
| 331 | + : [0-9a-fA-F] |
| 332 | + ; |
| 333 | + |
| 334 | +fragment Letter |
| 335 | + : [a-zA-Z] |
| 336 | + ; |
| 337 | + |
| 338 | +fragment Nat : Num | ('0x' HexNum) ; |
| 339 | +fragment Int : Sign Nat ; |
| 340 | +fragment Frac : Num ; |
| 341 | +fragment HexFrac : HexNum ; |
| 342 | + |
| 343 | +fragment Float |
| 344 | + : Sign? Num '.' Frac? |
| 345 | + | Sign? Num ('.' Frac?)? ('e' | 'E') Sign? Num |
| 346 | + | Sign? '0x' HexNum '.' HexFrac? |
| 347 | + | Sign? '0x' HexNum ('.' HexFrac?)? ('p' | 'P') Sign? Num |
| 348 | + | Sign? 'inf' |
| 349 | + | Sign? 'nan' |
| 350 | + | Sign? 'nan:' '0x' HexNum |
| 351 | + ; |
| 352 | + |
| 353 | +fragment String_ |
| 354 | + : '"' ( Char | '\n' | '\t' | '\\' | '\'' | '\\' HexDigit HexDigit | '\\u{' HexDigit+ '}' )* '"' |
| 355 | + ; |
| 356 | + |
| 357 | +fragment Name |
| 358 | + : '$' (Letter | Digit | '_' | Symbol)+ |
| 359 | + ; |
| 360 | + |
| 361 | +fragment Escape : [nrt'"\\] ; |
| 362 | +
|
| 363 | +fragment NXX : IXX | FXX ; |
| 364 | +/* fragment MIXX : 'i' ('8' | '16' | '32' | '64') ; */ |
| 365 | +/* fragment MFXX : 'f' ('32' | '64') ; */ |
| 366 | +/* fragment SIGN : 's' | 'u' ; */ |
| 367 | +
|
| 368 | +fragment Char : ~["'\\\u0000-\u001f\u007f-\u00ff] ; |
| 369 | +fragment Ascii : [\u0000-\u007f] ; |
| 370 | +fragment Ascii_no_nl : [\u0000-\u0009\u000b-\u007f] ; |
| 371 | +fragment Utf8Cont : [\u0080-\u00bf] ; |
| 372 | +fragment Utf8 : Ascii | Utf8Enc ; |
| 373 | +fragment Utf8_no_nl : Ascii_no_nl | Utf8Enc ; |
| 374 | + |
| 375 | +fragment Utf8Enc |
| 376 | + : [\u00c2-\u00df] Utf8Cont |
| 377 | + | [\u00e0] [\u00a0-\u00bf] Utf8Cont |
| 378 | + | [\u00ed] [\u0080-\u009f] Utf8Cont |
| 379 | + | [\u00e1-\u00ec\u00ee-\u00ef] Utf8Cont Utf8Cont |
| 380 | + | [\u00f0] [\u0090-\u00bf] Utf8Cont Utf8Cont |
| 381 | + | [\u00f4] [\u0080-\u008f] Utf8Cont Utf8Cont |
| 382 | + | [\u00f1-\u00f3] Utf8Cont Utf8Cont Utf8Cont |
| 383 | + ; |
0 commit comments