-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.ebnf
More file actions
134 lines (110 loc) · 5.94 KB
/
grammar.ebnf
File metadata and controls
134 lines (110 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(* AetherLang Grammar Specification v1.0 *)
(* EBNF Format - Machine-readable truth source for AI/Parser *)
(* ========================================================================== *)
(* AETHER SCRIPT GRAMMAR (.ath) - EXPERIMENTAL *)
(* ========================================================================== *)
(* Python-like indentation based syntax *)
ScriptProgram = { ScriptItem } ;
ScriptItem = script_function_def
| script_struct_def
| script_statement
;
script_function_def = [ "@comptime" ] "def" IDENTIFIER "(" [ script_params ] ")" [ "->" type ] ":" NEWLINE INDENT script_block DEDENT ;
script_block = { script_statement } ;
script_statement = script_expr NEWLINE
| "return" [ script_expr ] NEWLINE
| "if" script_expr ":" NEWLINE INDENT script_block DEDENT [ "else" ":" NEWLINE INDENT script_block DEDENT ]
;
script_expr = (* ... expression rules similar to python ... *)
;
(* ============ Program Structure ============ *)
program = { item } ;
item = function_def | struct_def | enum_def | impl_block | interface_def
| const_def | extern_block | static_def | union_def | module_def | use_decl ;
(* ============ Functions ============ *)
function_def = [ "pub" ] , "fn" , IDENT , [ generic_params ] , params , [ "->" , type ]
, [ contract_block ] , [ effect_annotation ] , block ;
params = "(" , [ param , { "," , param } ] , ")" ;
param = [ ownership ] , IDENT , ":" , type ;
ownership = "own" | "ref" | "mut" | "shared" ;
(* ============ Contracts ============ *)
contract_block = "[" , { contract_clause } , "]" ;
contract_clause = ( "requires" | "ensures" | "invariant" ) , expr , { "," , expr } ;
effect_annotation = "pure" | ( "effect" , "[" , IDENT , { "," , IDENT } , "]" ) ;
(* ============ Types ============ *)
type = named_type | pointer_type | ref_type | array_type | slice_type
| tuple_type | function_type | owned_type | volatile_type | "!" | "()" | "_" ;
named_type = IDENT , [ generic_args ] ;
pointer_type = "*" , type ;
ref_type = "&" , [ "mut" ] , type ;
array_type = "[" , type , ";" , INTEGER , "]" ;
slice_type = "[" , type , "]" ;
tuple_type = "(" , [ type , { "," , type } ] , ")" ;
function_type = "fn" , "(" , [ type , { "," , type } ] , ")" , "->" , type ;
owned_type = ( "own" | "shared" ) , type ;
volatile_type = "*" , "volatile" , type ;
generic_params = "<" , IDENT , { "," , IDENT } , ">" ;
generic_args = "<" , type , { "," , type } , ">" ;
(* ============ Struct / Enum / Union ============ *)
struct_def = [ "pub" ] , "struct" , IDENT , [ generic_params ] , "{" , { field } , "}" ;
field = IDENT , ":" , type , "," ;
enum_def = [ "pub" ] , "enum" , IDENT , [ generic_params ] , "{" , { variant } , "}" ;
variant = IDENT , [ "(" , type , { "," , type } , ")" | "{" , { field } , "}" ] , "," ;
union_def = [ "pub" ] , "union" , IDENT , "{" , { field } , "}" ;
(* ============ FFI (Phase 8) ============ *)
extern_block = "extern" , [ STRING ] , "{" , { foreign_item } , "}" ;
foreign_item = { annotation } , "fn" , IDENT , params , [ "->" , type ] , ";"
| "static" , [ "mut" ] , IDENT , ":" , type , ";" ;
static_def = [ "pub" ] , "static" , [ "mut" ] , IDENT , ":" , type , [ "=" , expr ] , ";" ;
annotation = "@" , IDENT , [ "(" , [ expr , { "," , expr } ] , ")" ] ;
(* ============ Unsafe (Phase 9) ============ *)
unsafe_block = "unsafe" , [ unsafe_metadata ] , block ;
unsafe_metadata = "(" , { IDENT , "=" , ( STRING | IDENT ) , [ "," ] } , ")" ;
(* ============ Statements ============ *)
stmt = let_stmt | expr_stmt | return_stmt | break_stmt | continue_stmt ;
let_stmt = "let" , [ "mut" ] , IDENT , [ ":" , type ] , [ "=" , expr ] , ";" ;
expr_stmt = expr , ";" ;
return_stmt = "return" , [ expr ] , ";" ;
break_stmt = "break" , ";" ;
continue_stmt = "continue" , ";" ;
(* ============ Expressions ============ *)
expr = binary_expr | unary_expr | primary_expr | postfix_expr ;
binary_expr = expr , binary_op , expr ;
unary_expr = unary_op , expr ;
postfix_expr = expr , ( call_args | field_access | index_access | "?" ) ;
primary_expr = literal | IDENT | "(" , expr , ")" | block | if_expr | match_expr
| loop_expr | while_expr | for_expr | array_lit | struct_lit | unsafe_block | asm_expr ;
call_args = "(" , [ expr , { "," , expr } ] , ")" ;
field_access = "." , IDENT ;
index_access = "[" , expr , "]" ;
if_expr = "if" , expr , block , [ "else" , ( block | if_expr ) ] ;
match_expr = "match" , expr , "{" , { match_arm } , "}" ;
match_arm = pattern , "=>" , expr , "," ;
loop_expr = "loop" , block ;
while_expr = "while" , expr , block ;
for_expr = "for" , IDENT , "in" , expr , block ;
block = "{" , { stmt } , [ expr ] , "}" ;
(* ============ Operators ============ *)
binary_op = "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | "<=" | ">" | ">="
| "&&" | "||" | "&" | "|" | "^" | "<<" | ">>" | "=" | "+=" | "-=" | "*=" | "/=" ;
unary_op = "-" | "!" | "~" | "&" | "*" ;
(* ============ Literals ============ *)
literal = INTEGER | FLOAT | STRING | CHAR | "true" | "false" ;
array_lit = "[" , [ expr , { "," , expr } ] , "]" ;
struct_lit = IDENT , "{" , [ IDENT , ":" , expr , { "," , IDENT , ":" , expr } ] , "}" ;
(* ============ Patterns ============ *)
pattern = "_" | literal | IDENT | tuple_pattern | struct_pattern ;
tuple_pattern = "(" , [ pattern , { "," , pattern } ] , ")" ;
struct_pattern = IDENT , "{" , [ IDENT , { "," , IDENT } ] , "}" ;
(* ============ Module System ============ *)
module_def = [ "pub" ] , "mod" , IDENT , ( "{" , { item } , "}" | ";" ) ;
use_decl = [ "pub" ] , "use" , path , [ "::" , ( "*" | "{" , IDENT , { "," , IDENT } , "}" | "as" , IDENT ) ] , ";" ;
path = IDENT , { "::" , IDENT } ;
(* ============ Terminals ============ *)
IDENT = letter , { letter | digit | "_" } ;
INTEGER = digit , { digit } ;
FLOAT = digit , { digit } , "." , digit , { digit } ;
STRING = '"' , { char } , '"' ;
CHAR = "'" , char , "'" ;
letter = "a" | ... | "z" | "A" | ... | "Z" | "_" ;
digit = "0" | ... | "9" ;