1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15grammar Taihe; 16 17///////////// 18// Grammar // 19///////////// 20 21spec 22 : (UseLst_uses += use | SpecFieldLst_fields += specField | ScopeAttrLst_inner_attrs += scopeAttr)* 23 EOF 24 ; 25 26use 27 : KW_USE PkgName_pkg_name = pkgName (KW_AS TOKENOpt_pkg_alias = ID)? SEMICOLON # usePackage 28 | KW_FROM PkgName_pkg_name = pkgName KW_USE DeclAliasPairLst_decl_alias_pairs += declAliasPair (COMMA DeclAliasPairLst_decl_alias_pairs += declAliasPair)* SEMICOLON # useSymbol 29 ; 30 31pkgName 32 : (TOKENLst_parts += ID DOT)* TOKENLst_parts += ID 33 ; 34 35declAliasPair 36 : TOKEN_decl_name = ID (KW_AS TOKENOpt_decl_alias = ID)? 37 ; 38 39specField 40 : (DeclAttrLst_forward_attrs += declAttr)* 41 KW_ENUM TOKEN_name = ID COLON Type_enum_ty = type 42 LEFT_BRACE (EnumItemLst_fields += enumItem (COMMA EnumItemLst_fields += enumItem)* COMMA?)? RIGHT_BRACE # enum 43 | (DeclAttrLst_forward_attrs += declAttr)* 44 KW_STRUCT TOKEN_name = ID 45 LEFT_BRACE (StructFieldLst_fields += structField | ScopeAttrLst_inner_attrs += scopeAttr)* RIGHT_BRACE # struct 46 | (DeclAttrLst_forward_attrs += declAttr)* 47 KW_UNION TOKEN_name = ID 48 LEFT_BRACE (UnionFieldLst_fields += unionField | ScopeAttrLst_inner_attrs += scopeAttr)* RIGHT_BRACE # union 49 | (DeclAttrLst_forward_attrs += declAttr)* 50 KW_INTERFACE TOKEN_name = ID 51 (COLON InterfaceParentLst_extends += interfaceParent (COMMA InterfaceParentLst_extends += interfaceParent)* COMMA?)? 52 LEFT_BRACE (InterfaceFieldLst_fields += interfaceField | ScopeAttrLst_inner_attrs += scopeAttr)* RIGHT_BRACE # interface 53 | (DeclAttrLst_forward_attrs += declAttr)* 54 KW_FUNCTION TOKEN_name = ID 55 LEFT_PARENTHESIS (ParameterLst_parameters += parameter (COMMA ParameterLst_parameters += parameter)* COMMA?)? RIGHT_PARENTHESIS (COLON (TypeOpt_return_ty = type | KW_VOID))? SEMICOLON # globalFunction 56 ; 57 58enumItem 59 : (DeclAttrLst_forward_attrs += declAttr)* 60 TOKEN_name = ID (ASSIGN_TO AnyExprOpt_val = anyExpr)? # enumProperty 61 ; 62 63structField 64 : (DeclAttrLst_forward_attrs += declAttr)* 65 TOKEN_name = ID 66 COLON Type_ty = type SEMICOLON # structProperty 67 ; 68 69unionField 70 : (DeclAttrLst_forward_attrs += declAttr)* 71 TOKEN_name = ID 72 (COLON TypeOpt_ty = type)? SEMICOLON # unionProperty 73 ; 74 75interfaceField 76 : (DeclAttrLst_forward_attrs += declAttr)* 77 TOKEN_name = ID 78 LEFT_PARENTHESIS (ParameterLst_parameters += parameter (COMMA ParameterLst_parameters += parameter)* COMMA?)? RIGHT_PARENTHESIS (COLON (TypeOpt_return_ty = type | KW_VOID))? SEMICOLON # interfaceFunction 79 ; 80 81interfaceParent 82 : (DeclAttrLst_forward_attrs += declAttr)* 83 Type_ty = type 84 ; 85 86parameter 87 : (DeclAttrLst_forward_attrs += declAttr)* 88 TOKEN_name = ID COLON Type_ty = type 89 ; 90 91/////////////// 92// Attribute // 93/////////////// 94 95scopeAttr 96 : AT EXCLAMATION TOKEN_name = ID (LEFT_PARENTHESIS (AttrArgLst_args += attrArg (COMMA AttrArgLst_args += attrArg)* COMMA?)? RIGHT_PARENTHESIS)? 97 ; 98 99declAttr 100 : AT TOKEN_name = ID (LEFT_PARENTHESIS (AttrArgLst_args += attrArg (COMMA AttrArgLst_args += attrArg)* COMMA?)? RIGHT_PARENTHESIS)? 101 ; 102 103attrArg 104 : AnyExpr_val = anyExpr # unnamedAttrArg 105 | TOKEN_name = ID ASSIGN_TO AnyExpr_val = anyExpr # namedAttrArg 106 ; 107 108////////// 109// Type // 110////////// 111 112type 113 : (DeclAttrLst_forward_attrs += declAttr)* 114 PkgName_pkg_name = pkgName DOT TOKEN_decl_name = ID # longType 115 | (DeclAttrLst_forward_attrs += declAttr)* 116 TOKEN_decl_name = ID # shortType 117 | (DeclAttrLst_forward_attrs += declAttr)* 118 TOKEN_decl_name = ID LESS_THAN (TypeLst_args += type (COMMA TypeLst_args += type)* COMMA?)? GREATER_THAN # genericType 119 | <assoc = right> 120 (DeclAttrLst_forward_attrs += declAttr)* 121 LEFT_PARENTHESIS (ParameterLst_parameters += parameter (COMMA ParameterLst_parameters += parameter)* COMMA?)? RIGHT_PARENTHESIS ARROW (TypeOpt_return_ty = type | KW_VOID) # callbackType 122 ; 123 124//////////////// 125// Expression // 126//////////////// 127 128anyExpr 129 : StringExpr_expr = stringExpr # stringAnyExpr 130 | IntExpr_expr = intExpr # intAnyExpr 131 | FloatExpr_expr = floatExpr # floatAnyExpr 132 | BoolExpr_expr = boolExpr # boolAnyExpr 133 ; 134 135floatExpr 136 : TOKEN_val = FLOAT_LITERAL # literalFloatExpr 137 | LEFT_PARENTHESIS FloatExpr_expr = floatExpr RIGHT_PARENTHESIS # parenthesisFloatExpr 138 | TOKEN_op = (PLUS | MINUS | TILDE) FloatExpr_expr = floatExpr # unaryFloatExpr 139 | FloatExpr_left = floatExpr TOKEN_op = (STAR | SLASH) FloatExpr_right = floatExpr # binaryFloatExpr 140 | FloatExpr_left = floatExpr TOKEN_op = (PLUS | MINUS) FloatExpr_right = floatExpr # binaryFloatExpr 141 | KW_IF BoolExpr_cond = boolExpr KW_THEN FloatExpr_then_expr = floatExpr KW_ELSE FloatExpr_else_expr = floatExpr # conditionalFloatExpr 142 ; 143 144intExpr 145 : TOKEN_val = (DEC_LITERAL | OCT_LITERAL | HEX_LITERAL | BIN_LITERAL) # literalIntExpr 146 | LEFT_PARENTHESIS IntExpr_expr = intExpr RIGHT_PARENTHESIS # parenthesisIntExpr 147 | TOKEN_op = (PLUS | MINUS | TILDE) IntExpr_expr = intExpr # unaryIntExpr 148 | IntExpr_left = intExpr TOKEN_op = (STAR | SLASH | PERCENT) IntExpr_right = intExpr # binaryIntExpr 149 | IntExpr_left = intExpr TOKEN_op = (PLUS | MINUS) IntExpr_right = intExpr # binaryIntExpr 150 | IntExpr_left = intExpr (TOKEN_ch = LESS_THAN LESS_THAN | TOKEN_ch = GREATER_THAN GREATER_THAN) IntExpr_right = intExpr # binaryIntShiftExpr 151 | IntExpr_left = intExpr TOKEN_op = AMPERSAND IntExpr_right = intExpr # binaryIntExpr 152 | IntExpr_left = intExpr TOKEN_op = CARET IntExpr_right = intExpr # binaryIntExpr 153 | IntExpr_left = intExpr TOKEN_op = PIPE IntExpr_right = intExpr # binaryIntExpr 154 | KW_IF BoolExpr_cond = boolExpr KW_THEN IntExpr_then_expr = intExpr KW_ELSE IntExpr_else_expr = intExpr # conditionalIntExpr 155 ; 156 157boolExpr 158 : TOKEN_val = (KW_TRUE | KW_FALSE) # literalBoolExpr 159 | LEFT_PARENTHESIS BoolExpr_expr = boolExpr RIGHT_PARENTHESIS # parenthesisBoolExpr 160 | TOKEN_op = EXCLAMATION BoolExpr_expr = boolExpr # unaryBoolExpr 161 | IntExpr_left = intExpr TOKEN_op = (EQUAL_TO | NOT_EQUAL_TO | LESS_EQUAL_TO | GREATER_EQUAL_TO | LESS_THAN | GREATER_THAN) IntExpr_right = intExpr # intComparisonBoolExpr 162 | FloatExpr_left = floatExpr TOKEN_op = (EQUAL_TO | NOT_EQUAL_TO | LESS_EQUAL_TO | GREATER_EQUAL_TO | LESS_THAN | GREATER_THAN) FloatExpr_right = floatExpr # floatComparisonBoolExpr 163 | BoolExpr_left = boolExpr TOKEN_op = (AND | OR) BoolExpr_right = boolExpr # binaryBoolExpr 164 | KW_IF BoolExpr_cond = boolExpr KW_THEN BoolExpr_then_expr = boolExpr KW_ELSE BoolExpr_else_expr = boolExpr # conditionalBoolExpr 165 ; 166 167stringExpr 168 : TOKEN_val = STRING_LITERAL # literalStringExpr 169 | TOKEN_val = DOCSTRING_LITERAL # literalDocStringExpr 170 | StringExpr_left = stringExpr StringExpr_right = stringExpr # binaryStringExpr 171 ; 172 173/////////// 174// Token // 175/////////// 176 177SEMICOLON 178 : ';' 179 ; 180 181COLON 182 : ':' 183 ; 184 185DOT 186 : '.' 187 ; 188 189ARROW 190 : '=>' 191 ; 192 193COMMA 194 : ',' 195 ; 196 197LEFT_BRACE 198 : '{' 199 ; 200 201RIGHT_BRACE 202 : '}' 203 ; 204 205LEFT_PARENTHESIS 206 : '(' 207 ; 208 209RIGHT_PARENTHESIS 210 : ')' 211 ; 212 213LEFT_BRACKET 214 : '[' 215 ; 216 217RIGHT_BRACKET 218 : ']' 219 ; 220 221ASSIGN_TO 222 : '=' 223 ; 224 225PLUS 226 : '+' 227 ; 228 229MINUS 230 : '-' 231 ; 232 233STAR 234 : '*' 235 ; 236 237SLASH 238 : '/' 239 ; 240 241PERCENT 242 : '%' 243 ; 244 245AT 246 : '@' 247 ; 248 249DOLLAR 250 : '$' 251 ; 252 253QUESTION_MARK 254 : '?' 255 ; 256 257TILDE 258 : '~' 259 ; 260 261AMPERSAND 262 : '&' 263 ; 264 265PIPE 266 : '|' 267 ; 268 269CARET 270 : '^' 271 ; 272 273EXCLAMATION 274 : '!' 275 ; 276 277AND 278 : '&&' 279 ; 280 281OR 282 : '||' 283 ; 284 285LESS_THAN 286 : '<' 287 ; 288 289GREATER_THAN 290 : '>' 291 ; 292 293LESS_EQUAL_TO 294 : '<=' 295 ; 296 297GREATER_EQUAL_TO 298 : '>=' 299 ; 300 301EQUAL_TO 302 : '==' 303 ; 304 305NOT_EQUAL_TO 306 : '!=' 307 ; 308 309KW_IF 310 : 'if' 311 ; 312 313KW_THEN 314 : 'then' 315 ; 316 317KW_ELSE 318 : 'else' 319 ; 320 321KW_USE 322 : 'use' 323 ; 324 325KW_AS 326 : 'as' 327 ; 328 329KW_FROM 330 : 'from' 331 ; 332 333KW_ENUM 334 : 'enum' 335 ; 336 337KW_UNION 338 : 'union' 339 ; 340 341KW_STRUCT 342 : 'struct' 343 ; 344 345KW_INTERFACE 346 : 'interface' 347 ; 348 349KW_FUNCTION 350 : 'function' 351 ; 352 353KW_TRUE 354 : 'true' 355 ; 356 357KW_FALSE 358 : 'false' 359 ; 360 361KW_VOID 362 : 'void' 363 ; 364 365STRING_LITERAL 366 : '"' (ESCAPE_SEQUENCE | ~ ('\\' | '"'))* '"' 367 ; 368 369DOCSTRING_LITERAL 370 : '"""' .*? '"""' 371 ; 372 373fragment ESCAPE_SEQUENCE 374 : '\\' ('b' | 't' | 'n' | 'f' | 'r' | '"' | '\'' | '\\') 375 | UNICODE_ESCAPE 376 | OCTAL_ESCAPE 377 ; 378 379fragment OCTAL_ESCAPE 380 : '\\' 'x' HEX_DIGIT HEX_DIGIT 381 ; 382 383fragment UNICODE_ESCAPE 384 : '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT 385 ; 386 387fragment HEX_DIGIT 388 : '0' .. '9' 389 | 'a' .. 'f' 390 | 'A' .. 'F' 391 ; 392 393fragment OCT_DIGIT 394 : '0' .. '7' 395 ; 396 397fragment BIN_DIGIT 398 : '0' 399 | '1' 400 ; 401 402DEC_LITERAL 403 : DIGIT+ 404 ; 405 406HEX_LITERAL 407 : '0x' HEX_DIGIT+ 408 ; 409 410OCT_LITERAL 411 : '0o' OCT_DIGIT+ 412 ; 413 414BIN_LITERAL 415 : '0b' HEX_DIGIT+ 416 ; 417 418FLOAT_LITERAL 419 : DIGIT* '.' DIGIT* 420 ; 421 422ID 423 : (UNDERLINE | LETTER) (UNDERLINE | LETTER | DIGIT)* 424 ; 425 426fragment LETTER 427 : 'A' .. 'Z' 428 | 'a' .. 'z' 429 ; 430 431fragment UNDERLINE 432 : '_' 433 ; 434 435fragment DIGIT 436 : '0' .. '9' 437 ; 438 439WS 440 : (' ' | '\r' | '\t' | '\u000C' | '\n') -> channel (HIDDEN) 441 ; 442 443MULTI_LINE_COMMENT 444 : '/*' .*? '*/' -> channel (HIDDEN) 445 ; 446 447SINGLE_LINE_COMMENT 448 : '//' ~ ('\n' | '\r')* '\r'? '\n'? -> channel (HIDDEN) 449 ; 450 451ERROR_CHAR 452 : . 453 ; 454