|
| 1 | +/* |
| 2 | +Copyright (c) 2019 Renata Hodovan. |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | +1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + this list of conditions and the following disclaimer in the documentation |
| 13 | + and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | +3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | + may be used to endorse or promote products derived from this software |
| 17 | + without specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +lexer grammar WatLexer; |
| 32 | + |
| 33 | +LPAR : '(' ; |
| 34 | +RPAR : ')' ; |
| 35 | + |
| 36 | +NAT : Nat ; |
| 37 | +INT : Int ; |
| 38 | +FLOAT : Float ; |
| 39 | +STRING : String ; |
| 40 | +VALUE_TYPE : NXX ; |
| 41 | +CONST : NXX '.const' ; |
| 42 | + |
| 43 | +FUNCREF: 'funcref' ; |
| 44 | +MUT: 'mut' ; |
| 45 | + |
| 46 | +NOP: 'nop' ; |
| 47 | +UNREACHABLE: 'unreachable' ; |
| 48 | +DROP: 'drop' ; |
| 49 | +BLOCK: 'block' ; |
| 50 | +LOOP: 'loop' ; |
| 51 | +END: 'end' ; |
| 52 | +BR: 'br' ; |
| 53 | +BR_IF: 'br_if' ; |
| 54 | +BR_TABLE: 'br_table' ; |
| 55 | +RETURN: 'return' ; |
| 56 | +IF: 'if' ; |
| 57 | +THEN: 'then' ; |
| 58 | +ELSE: 'else' ; |
| 59 | +SELECT: 'select' ; |
| 60 | +CALL: 'call' ; |
| 61 | +CALL_INDIRECT: 'call_indirect' ; |
| 62 | + |
| 63 | +LOCAL_GET: 'local.get' ; |
| 64 | +LOCAL_SET: 'local.set' ; |
| 65 | +LOCAL_TEE: 'local.tee' ; |
| 66 | +GLOBAL_GET: 'global.get' ; |
| 67 | +GLOBAL_SET: 'global.set' ; |
| 68 | + |
| 69 | +LOAD : NXX '.load' (MEM_SIZE '_' SIGN)? ; |
| 70 | +STORE : NXX '.store' (MEM_SIZE)? ; |
| 71 | + |
| 72 | +OFFSET_EQ_NAT : 'offset=' Nat ; |
| 73 | +ALIGN_EQ_NAT : 'align=' Nat ; |
| 74 | + |
| 75 | +UNARY |
| 76 | + : IXX '.clz' |
| 77 | + | IXX '.ctz' |
| 78 | + | IXX '.popcnt' |
| 79 | + | FXX '.neg' |
| 80 | + | FXX '.abs' |
| 81 | + | FXX '.sqrt' |
| 82 | + | FXX '.ceil' |
| 83 | + | FXX '.floor' |
| 84 | + | FXX '.trunc' |
| 85 | + | FXX '.nearest' |
| 86 | + ; |
| 87 | + |
| 88 | +BINARY |
| 89 | + : IXX '.add' |
| 90 | + | IXX '.sub' |
| 91 | + | IXX '.mul' |
| 92 | + | IXX '.div_s' |
| 93 | + | IXX '.div_u' |
| 94 | + | IXX '.rem_s' |
| 95 | + | IXX '.rem_u' |
| 96 | + | IXX '.and' |
| 97 | + | IXX '.or' |
| 98 | + | IXX '.xor' |
| 99 | + | IXX '.shl' |
| 100 | + | IXX '.shr_s' |
| 101 | + | IXX '.shr_u' |
| 102 | + | IXX '.rotl' |
| 103 | + | IXX '.rotr' |
| 104 | + | FXX '.add' |
| 105 | + | FXX '.sub' |
| 106 | + | FXX '.mul' |
| 107 | + | FXX '.div' |
| 108 | + | FXX '.min' |
| 109 | + | FXX '.max' |
| 110 | + | FXX '.copysign' |
| 111 | + ; |
| 112 | + |
| 113 | +TEST |
| 114 | + : IXX '.eqz' |
| 115 | + ; |
| 116 | + |
| 117 | +COMPARE |
| 118 | + : IXX '.eq' |
| 119 | + | IXX '.ne' |
| 120 | + | IXX '.lt_s' |
| 121 | + | IXX '.lt_u' |
| 122 | + | IXX '.le_s' |
| 123 | + | IXX '.le_u' |
| 124 | + | IXX '.gt_s' |
| 125 | + | IXX '.gt_u' |
| 126 | + | IXX '.ge_s' |
| 127 | + | IXX '.ge_u' |
| 128 | + | FXX '.eq' |
| 129 | + | FXX '.ne' |
| 130 | + | FXX '.lt' |
| 131 | + | FXX '.le' |
| 132 | + | FXX '.gt' |
| 133 | + | FXX '.ge' |
| 134 | + ; |
| 135 | + |
| 136 | +CONVERT |
| 137 | + : 'i32.wrap_i64' |
| 138 | + | 'i64.extend_i32_s' |
| 139 | + | 'i64.extend_i32_u' |
| 140 | + | 'f32.demote_f64' |
| 141 | + | 'f64.promote_f32' |
| 142 | + | IXX '.trunc_f32_s' |
| 143 | + | IXX '.trunc_f32_u' |
| 144 | + | IXX '.trunc_f64_s' |
| 145 | + | IXX '.trunc_f64_u' |
| 146 | + | FXX '.convert_i32_s' |
| 147 | + | FXX '.convert_i32_u' |
| 148 | + | FXX '.convert_i64_s' |
| 149 | + | FXX '.convert_i64_u' |
| 150 | + | 'f32.reinterpret_i32' |
| 151 | + | 'f64.reinterpret_i64' |
| 152 | + | 'i32.reinterpret_f32' |
| 153 | + | 'i64.reinterpret_f64' |
| 154 | + ; |
| 155 | + |
| 156 | +MEMORY_SIZE : 'memory.size' ; |
| 157 | +MEMORY_GROW : 'memory.grow' ; |
| 158 | + |
| 159 | +TYPE: 'type' ; |
| 160 | +FUNC: 'func' ; |
| 161 | +START: 'start' ; |
| 162 | +PARAM: 'param' ; |
| 163 | +RESULT: 'result' ; |
| 164 | +LOCAL: 'local' ; |
| 165 | +GLOBAL: 'global' ; |
| 166 | +TABLE: 'table' ; |
| 167 | +MEMORY: 'memory' ; |
| 168 | +ELEM: 'elem' ; |
| 169 | +DATA: 'data' ; |
| 170 | +OFFSET: 'offset' ; |
| 171 | +IMPORT: 'import' ; |
| 172 | +EXPORT: 'export' ; |
| 173 | + |
| 174 | +MODULE : 'module' ; |
| 175 | +BIN : 'binary' ; |
| 176 | +QUOTE : 'quote' ; |
| 177 | + |
| 178 | +SCRIPT: 'script' ; |
| 179 | +REGISTER: 'register' ; |
| 180 | +INVOKE: 'invoke' ; |
| 181 | +GET: 'get' ; |
| 182 | +ASSERT_MALFORMED: 'assert_malformed' ; |
| 183 | +ASSERT_INVALID: 'assert_invalid' ; |
| 184 | +ASSERT_UNLINKABLE: 'assert_unlinkable' ; |
| 185 | +ASSERT_RETURN: 'assert_return' ; |
| 186 | +ASSERT_RETURN_CANONICAL_NAN: 'assert_return_canonical_nan' ; |
| 187 | +ASSERT_RETURN_ARITHMETIC_NAN: 'assert_return_arithmetic_nan' ; |
| 188 | +ASSERT_TRAP: 'assert_trap' ; |
| 189 | +ASSERT_EXHAUSTION: 'assert_exhaustion' ; |
| 190 | +INPUT: 'input' ; |
| 191 | +OUTPUT: 'output' ; |
| 192 | + |
| 193 | +VAR : Name ; |
| 194 | + |
| 195 | +SPACE |
| 196 | + : [ \t\r\n] -> skip |
| 197 | + ; |
| 198 | + |
| 199 | +COMMENT |
| 200 | + : ( '(;' .*? ';)' |
| 201 | + | ';;' .*? '\n')-> skip |
| 202 | + ; |
| 203 | + |
| 204 | +fragment Symbol |
| 205 | + : '.' | '+' | '-' | '*' | '/' | '\\' | '^' | '~' | '=' | '<' | '>' | '!' | '?' | '@' | '#' | '$' | '%' | '&' | '|' | ':' | '\'' | '`' |
| 206 | + ; |
| 207 | + |
| 208 | +fragment Num |
| 209 | + : Digit ('_'? Digit)* |
| 210 | + ; |
| 211 | + |
| 212 | +fragment HexNum |
| 213 | + : HexDigit ('_'? HexDigit)* |
| 214 | + ; |
| 215 | + |
| 216 | +fragment Sign |
| 217 | + : '+' | '-' |
| 218 | + ; |
| 219 | + |
| 220 | +fragment Digit |
| 221 | + : [0-9] |
| 222 | + ; |
| 223 | + |
| 224 | +fragment HexDigit |
| 225 | + : [0-9a-fA-F] |
| 226 | + ; |
| 227 | + |
| 228 | +fragment Letter |
| 229 | + : [a-zA-Z] |
| 230 | + ; |
| 231 | + |
| 232 | +fragment Nat : Num | ('0x' HexNum) ; |
| 233 | +fragment Int : Sign Nat ; |
| 234 | +fragment Frac : Num ; |
| 235 | +fragment HexFrac : HexNum ; |
| 236 | + |
| 237 | +fragment Float |
| 238 | + : Sign? Num '.' Frac? |
| 239 | + | Sign? Num ('.' Frac?)? ('e' | 'E') Sign? Num |
| 240 | + | Sign? '0x' HexNum '.' HexFrac? |
| 241 | + | Sign? '0x' HexNum ('.' HexFrac?)? ('p' | 'P') Sign? Num |
| 242 | + | Sign? 'inf' |
| 243 | + | Sign? 'nan' |
| 244 | + | Sign? 'nan:' '0x' HexNum |
| 245 | + ; |
| 246 | + |
| 247 | +fragment String |
| 248 | + : '"' ( Char | '\n' | '\t' | '\\' | '\'' | '\\' HexDigit HexDigit | '\\u{' HexDigit+ '}' )* '"' |
| 249 | + ; |
| 250 | + |
| 251 | +fragment Name |
| 252 | + : '$' (Letter | Digit | '_' | Symbol)+ |
| 253 | + ; |
| 254 | + |
| 255 | +fragment Escape : [nrt'"\\] ; |
| 256 | +
|
| 257 | +fragment IXX : 'i' ('32' | '64') ; |
| 258 | +fragment FXX : 'f' ('32' | '64') ; |
| 259 | +fragment NXX : IXX | FXX ; |
| 260 | +fragment MIXX : 'i' ('8' | '16' | '32' | '64') ; |
| 261 | +fragment MFXX : 'f' ('32' | '64') ; |
| 262 | +fragment SIGN : 's' | 'u' ; |
| 263 | +fragment MEM_SIZE : '8' | '16' | '32' ; |
| 264 | +
|
| 265 | +fragment Char : ~["'\\\u0000-\u001f\u007f-\u00ff] ; |
| 266 | +fragment Ascii : [\u0000-\u007f] ; |
| 267 | +fragment Ascii_no_nl : [\u0000-\u0009\u000b-\u007f] ; |
| 268 | +fragment Utf8Cont : [\u0080-\u00bf] ; |
| 269 | +fragment Utf8 : Ascii | Utf8Enc ; |
| 270 | +fragment Utf8_no_nl : Ascii_no_nl | Utf8Enc ; |
| 271 | + |
| 272 | +fragment Utf8Enc |
| 273 | + : [\u00c2-\u00df] Utf8Cont |
| 274 | + | [\u00e0] [\u00a0-\u00bf] Utf8Cont |
| 275 | + | [\u00ed] [\u0080-\u009f] Utf8Cont |
| 276 | + | [\u00e1-\u00ec\u00ee-\u00ef] Utf8Cont Utf8Cont |
| 277 | + | [\u00f0] [\u0090-\u00bf] Utf8Cont Utf8Cont |
| 278 | + | [\u00f4] [\u0080-\u008f] Utf8Cont Utf8Cont |
| 279 | + | [\u00f1-\u00f3] Utf8Cont Utf8Cont Utf8Cont |
| 280 | + ; |
0 commit comments