Last active
November 10, 2023 11:33
-
-
Save suica/fdb1b51c04cbb842a06b141643bb7a1f to your computer and use it in GitHub Desktop.
An ANTLR4 grammar for TAT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grammar TAT; | |
// Lexer Rules | |
fragment Num: '0'..'9'; | |
Integer: ('1'..'9')Num+ | Num; | |
Float: Integer '.' Integer; | |
fragment Char: 'a'..'z' | 'A'..'Z'; | |
BinOp1: '*' | '/'; | |
BinOp2: '+' | '-'; | |
BinOp3: '>' | '<' | '!==' | '==='; | |
UnOp: '+' | '-' | '!'; | |
BraceL: '{'; | |
BraceR: '}'; | |
ID: Char(Char | Integer)*; | |
STR: '"' .*? '"'; | |
WHITESPACE: [ \r\t\n]+ -> skip; | |
fragment NEWLINE: ('\r' | '\n')+; | |
INLINE_COMMENT: ('//' .*? NEWLINE) -> skip; | |
// Syntax Rules | |
// 1. Value World | |
// 1.1 Value-level Literals | |
string_literal: STR; | |
number_literal: Integer | Float; | |
boolean_literal: 'true' | 'false'; | |
// 1.2 Value Basics | |
program: stmt* EOF; | |
id: ID; | |
// 1.2.1 Expressions | |
unary_op: BinOp2; | |
expr: | |
| expr BinOp1 expr | |
| expr BinOp2 expr | |
| expr BinOp3 expr | |
| unary_op expr | |
| object_literal | |
| array_literal | |
| number_literal | |
| string_literal | |
| boolean_literal | |
| id | |
| arrow_function | |
| '(' expr ')' | |
| expr ('<' type_arguments '>')? '(' function_arguments ')' | |
; | |
// 1.2.2 Statments | |
stmt: expr ';' | |
| return_statement ';' | |
| function_declaration | |
| variable_declaration ';' | |
| type_declaration | |
| if_stmt | |
| BraceL stmt* BraceR | |
; | |
if_stmt: 'if' '(' expr ')' stmt ('else' stmt)*; | |
return_statement: 'return' expr; | |
// 1.3 Composite Structures | |
variable_declaration: 'const' ID ':' type_annotation '=' expr; // only one variable supported | |
function_declaration: 'function' ID '(' function_parameters? ')' ':' type_annotation '{' stmt* '}'; | |
arrow_function: '(' function_parameters ')' '=>' stmt; | |
function_parameters: function_parameter_item (',' function_parameter_item)* ','?; | |
function_parameter_item: id ':' type_annotation; | |
function_arguments: function_argument_item (',' function_argument_item)* ','?; | |
function_argument_item: expr; | |
array_literal: '[' array_elements ']' | '[]'; | |
array_elements: expr (',' expr)*; | |
object_literal: '{' (object_property (',' object_property)*)? '}'; | |
object_property: property_name ':' expr; | |
property_name: string_literal number_literal | ID; | |
// 2. Type World | |
// 2.1 Type-level Literals | |
type_literal: number_literal | object_literal | boolean_literal; | |
// 2.2 Type Basics | |
type_annotation: type_expr; | |
type_expr: | |
| '(' type_expr ')' | |
| type_expr '[]' // convenient constructing for an Array type | |
| type_literal | |
| function_type | |
| ID // id could also be a type | |
| type_expr '<' type_arguments '>' | |
| type_expr 'extends' type_expr '?' type_expr ':' type_expr | |
; | |
// 2.3 Type-level Composite Structures | |
function_type: ('<' type_parameters '>')? '(' function_parameters ')' '=>' type_expr; | |
type_declaration: 'type' ID ('<' type_parameters '>')? '=' type_expr; | |
type_parameters: type_parameter_item (',' type_parameter_item)* ','?; | |
type_parameter_item: ID ('extends' type_expr)?; | |
type_arguments: type_argument_item (',' type_argument_item)* ','?; | |
type_argument_item: type_expr; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment