diff --git a/test/corpus/declaration.txt b/test/corpus/declaration.txt new file mode 100644 index 0000000..382d10f --- /dev/null +++ b/test/corpus/declaration.txt @@ -0,0 +1,685 @@ +================================================================================ +Module declarations +================================================================================ + +module program::main; + +import std::io; + +fn void main() { + io::printf("Hello, World!\n"); +} + +-------------------------------------------------------------------------------- + +(source_file + (module + path: (path_ident + (module_resolution + (ident)) + (ident))) + (import_declaration + path: (path_ident + (module_resolution + (ident)) + (ident))) + (func_definition + (func_header + return_type: (type + (base_type + (base_type_name))) + name: (ident)) + (fn_parameter_list) + body: (macro_func_body + (compound_stmt + (expr_stmt + (call_expr + function: (module_ident_expr + (module_resolution + (ident)) + ident: (ident)) + arguments: (call_invocation + (arg + (string_literal + (string_content) + (escape_sequence)))))))))) + +================================================================================ +Import declarations +================================================================================ + +import std::io; +import std::math; +import std::data::json; + +-------------------------------------------------------------------------------- + +(source_file + (import_declaration + (path_ident + (module_resolution + (ident)) + (ident))) + (import_declaration + (path_ident + (module_resolution + (ident)) + (ident))) + (import_declaration + (path_ident + (module_resolution + (ident)) + (module_resolution + (ident)) + (ident)))) + +================================================================================ +Define declarations +================================================================================ + +def CharPtr = char*; +def Numbers = int[10]; +distinct Foo = int; +def bar = foo; +def bar_var = foo_var; +def BarFoo = bar::Foo; +def Bar_foo = bar::Foo(); +def FooFloat = foo_test::Foo(); +def FooFloat2 = Foo(); +def testFloat = foo_test::test(); +def Callback = fn int(char* text, int value); +def PI = std::math::PI; + +-------------------------------------------------------------------------------- + +(source_file + (define_declaration + (type_ident) + (typedef_type + (type + (base_type + (base_type_name)) + (type_suffix)))) + (define_declaration + (type_ident) + (typedef_type + (type + (base_type + (base_type_name)) + (type_suffix + (integer_literal))))) + (distinct_declaration + (type_ident) + (type + (base_type + (base_type_name)))) + (define_declaration + (define_ident + (ident) + (path_ident + (ident)))) + (define_declaration + (define_ident + (ident) + (path_ident + (ident)))) + (define_declaration + (type_ident) + (typedef_type + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (type_ident)))))) + (define_declaration + (type_ident) + (typedef_type + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (type_ident)) + (generic_arguments + (type + (base_type + (base_type_name)))))))) + (define_declaration + (type_ident) + (typedef_type + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (type_ident)) + (generic_arguments + (type + (base_type + (base_type_name))) + (type + (base_type + (base_type_name)))))))) + (define_declaration + (type_ident) + (typedef_type + (type + (base_type + (type_ident) + (generic_arguments + (type + (base_type + (base_type_name))) + (type + (base_type + (base_type_name)))))))) + (define_declaration + (define_ident + (ident) + (path_ident + (module_resolution + (ident)) + (ident)) + (generic_arguments + (type + (base_type + (base_type_name))) + (type + (base_type + (base_type_name)))))) + (define_declaration + (type_ident) + (typedef_type + (func_typedef + (type + (base_type + (base_type_name))) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident)) + (parameter + (type + (base_type + (base_type_name))) + (ident)))))) + (define_declaration + (define_ident + (const_ident) + (path_const_ident + (module_resolution + (ident)) + (module_resolution + (ident)) + (const_ident))))) + +================================================================================ +Function declarations +================================================================================ + +fn int! main(int argc, char** argv) { + int ret = 0; + return ret; +} + +fn int add(int x, int y) @inline { + return x + y; +} + +fn int multiply(int x, int y = 1) { + return x * y; +} + +fn int square_short(int x) => x * x; + +fn int short_name(String name) => name == "is_short"; + +fn int quick(int speed) => speed >= 120; + +extern fn void printf(char* str, ...); + +extern fn int sum(int... ints); + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (optional_type + (type + (base_type + (base_type_name)))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name))) + (ident)) + (parameter + (type + (base_type + (base_type_name)) + (type_suffix) + (type_suffix)) + (ident))) + (macro_func_body + (compound_stmt + (declaration_stmt + (type + (base_type + (base_type_name))) + (local_decl_after_type + (ident) + (integer_literal))) + (return_stmt + (ident))))) + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name))) + (ident)) + (parameter + (type + (base_type + (base_type_name))) + (ident))) + (attributes + (attribute + (at_ident))) + (macro_func_body + (compound_stmt + (return_stmt + (binary_expr + (ident) + (ident)))))) + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name))) + (ident)) + (parameter + (type + (base_type + (base_type_name))) + (ident) + (parameter_default + (integer_literal)))) + (macro_func_body + (compound_stmt + (return_stmt + (binary_expr + (ident) + (ident)))))) + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name))) + (ident))) + (macro_func_body + (implies_body + (binary_expr + (ident) + (ident))))) + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (type_ident))) + (ident))) + (macro_func_body + (implies_body + (binary_expr + (ident) + (string_literal + (string_content)))))) + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name))) + (ident))) + (macro_func_body + (implies_body + (binary_expr + (ident) + (integer_literal))))) + (func_declaration + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident)) + (parameter))) + (func_declaration + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name))) + (ident))))) + +================================================================================ +Struct declarations +================================================================================ + +struct Person { + char age; + char* name; +} + +struct Player @packed { + inline Person p; + char* name; + uint health @aligned; +} + +fn bool Player.is_alive(Player* p) { + return p.health > 0; +} + +-------------------------------------------------------------------------------- + +(source_file + (struct_declaration + (type_ident) + (struct_body + (struct_member_declaration + (type + (base_type + (base_type_name))) + (identifier_list + (ident))) + (struct_member_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (identifier_list + (ident))))) + (struct_declaration + (type_ident) + (attributes + (attribute + (at_ident))) + (struct_body + (struct_member_declaration + (type + (base_type + (type_ident))) + (ident)) + (struct_member_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (identifier_list + (ident))) + (struct_member_declaration + (type + (base_type + (base_type_name))) + (identifier_list + (ident)) + (attributes + (attribute + (at_ident)))))) + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (type + (base_type + (type_ident))) + (ident)) + (fn_parameter_list + (parameter + (type + (base_type + (type_ident)) + (type_suffix)) + (ident))) + (macro_func_body + (compound_stmt + (return_stmt + (binary_expr + (field_expr + (ident) + (access_ident + (ident))) + (integer_literal))))))) + +================================================================================ +Bitstruct declarations +================================================================================ + +bitstruct ShortBE : short @bigendian +{ + short val : 0..15; +} + +bitstruct ByteStruct : char +{ + bool start : 0; + bool center : 1..6; + bool end : 7; +} + +-------------------------------------------------------------------------------- + +(source_file + (bitstruct_declaration + (type_ident) + (type + (base_type + (base_type_name))) + (attributes + (attribute + (at_ident))) + (bitstruct_body + (bitstruct_def + (base_type + (base_type_name)) + (ident) + (integer_literal) + (integer_literal)))) + (bitstruct_declaration + (type_ident) + (type + (base_type + (base_type_name))) + (bitstruct_body + (bitstruct_def + (base_type + (base_type_name)) + (ident) + (integer_literal)) + (bitstruct_def + (base_type + (base_type_name)) + (ident) + (integer_literal) + (integer_literal)) + (bitstruct_def + (base_type + (base_type_name)) + (ident) + (integer_literal))))) + +================================================================================ +Union declarations +================================================================================ + +union Set { + int x; + float y; + struct p { + int z; + float a; + } +} + +-------------------------------------------------------------------------------- + +(source_file + (struct_declaration + (type_ident) + (struct_body + (struct_member_declaration + (type + (base_type + (base_type_name))) + (identifier_list + (ident))) + (struct_member_declaration + (type + (base_type + (base_type_name))) + (identifier_list + (ident))) + (struct_member_declaration + (ident) + (struct_body + (struct_member_declaration + (type + (base_type + (base_type_name))) + (identifier_list + (ident))) + (struct_member_declaration + (type + (base_type + (base_type_name))) + (identifier_list + (ident)))))))) + +================================================================================ +Enum declarations +================================================================================ + +enum OptionTag @packed { + SOME, + NONE, +} + +enum State : int { + PENDING, + RUNNING, + TERMINATED, +} + +enum TokenKind @packed @opaque { + KEYWORD, + OPERATOR, + FUNCTION, +} + +-------------------------------------------------------------------------------- + +(source_file + (enum_declaration + (type_ident) + (attributes + (attribute + (at_ident))) + (enum_body + (enum_constant + (const_ident)) + (enum_constant + (const_ident)))) + (enum_declaration + (type_ident) + (enum_spec + (type + (base_type + (base_type_name)))) + (enum_body + (enum_constant + (const_ident)) + (enum_constant + (const_ident)) + (enum_constant + (const_ident)))) + (enum_declaration + (type_ident) + (attributes + (attribute + (at_ident)) + (attribute + (at_ident))) + (enum_body + (enum_constant + (const_ident)) + (enum_constant + (const_ident)) + (enum_constant + (const_ident))))) + +================================================================================ +Error declarations +================================================================================ + +fault IoError +{ + FILE_NOT_FOUND, + FILE_NOT_READABLE, +} + +-------------------------------------------------------------------------------- + +(source_file + (fault_declaration + name: (type_ident) + body: (fault_body + (const_ident) + (const_ident)))) diff --git a/test/corpus/expression.txt b/test/corpus/expression.txt new file mode 100644 index 0000000..699cad2 --- /dev/null +++ b/test/corpus/expression.txt @@ -0,0 +1,1386 @@ +================================================================================ +Integer literals +================================================================================ + +int a = 128; +short a = 128i16; +ushort a = 128u16; +int b = 3_000_000; +long b = 3_000_000i64; +ulong b = 3_000_000u64; +uint c = 4_000u; +uint c = 4_000u32; +int c = 4_000i32; +long d = 0xC4_FF; +long d = 0xC4_FFi128; +ulong d = 0xC4_FFu128; +int e = 0o775_111; +int e = 0o775_111i16; +uint e = 0o775_111u16; +char f = 0b1001_1001; +ichar f = 0b1001_1001i8; +char f = 0b1001_1001u8; + +-------------------------------------------------------------------------------- + +(source_file + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (integer_literal))) + +================================================================================ +Float literals +================================================================================ + +float a = 1f; +float a = 1.; +float a = 1.0; +float a = 1.f; +float a = 1.0f; +float a = 1.e1; +float a = 1.0e1; +float a = 1.0e1f; +float a = 1.0e-1f; +float a = 1.0e-1f32; +float a = 1.E1; +float a = 1.0E1; +float a = 1.0E1f; +float a = 1.0E-1f; +float a = 1.0E-1f32; +double c = 0xc1f.; +double c = 0xc1f.f; +double c = 0xc1f.f64; +double c = 0xc1f.2ff; +double c = 0xc1f.2ff64; +double c = 0xc2f.p4; +double c = 0xc2f.p4f; +double c = 0xc2f.p4f64; +double c = 0xc3f.2fp+4; +double c = 0xc3f.2fp+4f; +double c = 0xc3f.2fp+4f64; +double c = 0xc4f.2fp-4; +double c = 0xc4f.2fp-4f; +double c = 0xc4f.2fp-4f64; +double c = 0XC1F.; +double c = 0XC1F.f; +double c = 0XC1F.f64; +double c = 0XC1F.2Ff; +double c = 0XC1F.2Ff64; +double c = 0XC2F.P4; +double c = 0XC2F.P4f; +double c = 0XC2F.P4f64; +double c = 0XC3F.2FP+4; +double c = 0XC3F.2FP+4f; +double c = 0XC3F.2FP+4f64; +double c = 0XC4F.2FP-4; +double c = 0XC4F.2FP-4f; +double c = 0XC4F.2FP-4f64; +float128 d = 1.0e+1f128; +float128 d = 1.0E+1f128; +float128 d = 0xdead.0eafP+1f128; +float128 d = 0XDEAD.0EAFP+1f128; + +-------------------------------------------------------------------------------- + +(source_file + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (real_literal))) + +================================================================================ +Boolean literals +================================================================================ + +bool a = true; +bool b = false; + +-------------------------------------------------------------------------------- + +(source_file + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident))) + +================================================================================ +Character literals +================================================================================ + +char a = 'a'; +char b = '\0'; +char c = '\t'; +char d = '\''; +char e = 'b'; +char f = 'c'; +char g = '\xa1'; +char h = '\x1A'; +char i = '\u1A4F'; +char j = '\U1A4F2D5B'; + +-------------------------------------------------------------------------------- + +(source_file + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (character))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (escape_sequence))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (escape_sequence))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (escape_sequence))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (character))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (character))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (escape_sequence))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (escape_sequence))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (escape_sequence))) + (global_declaration + (type + (base_type + (base_type_name))) + (ident) + (char_literal + (escape_sequence)))) + +================================================================================ +String literals +================================================================================ + +char* a = "a"; +char* b = "\"hi\""; +char* c = "bonjour"; +char* d = "guten morgen"; +char* e = "buenos dias"; +char* f = "buongiorno"; +char* g = `testing this ``now`` `; +char* h = x"4865 6c6c 6f20 776f 726c 6421"; +char* i = b64"Rk9PQkFSMTE="; + +-------------------------------------------------------------------------------- + +(source_file + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (string_literal + (string_content))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (string_literal + (escape_sequence) + (string_content) + (escape_sequence))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (string_literal + (string_content))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (string_literal + (string_content))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (string_literal + (string_content))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (string_literal + (string_content))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (raw_string_literal + (raw_string_content))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (bytes_expr + (bytes_literal))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (bytes_expr + (bytes_literal)))) + +================================================================================ +Compound literals +================================================================================ + +fn void main() { + Foo { 1, 2.0, .field = "string" }; + LinkedList { .allocator = alloc }; + LinkedList a = { .allocator = alloc, .size = 0 }; + *list = { .allocator = alloc, .size = 0 }; +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (expr_stmt + (type + (base_type + (type_ident))) + (initializer_list + (arg + (integer_literal)) + (arg + (real_literal)) + (arg + (param_path + (param_path_element + (ident))) + (string_literal + (string_content))))) + (expr_stmt + (type + (base_type + (type_ident))) + (initializer_list + (arg + (param_path + (param_path_element + (ident))) + (ident)))) + (declaration_stmt + (type + (base_type + (type_ident))) + (local_decl_after_type + (ident) + (initializer_list + (arg + (param_path + (param_path_element + (ident))) + (ident)) + (arg + (param_path + (param_path_element + (ident))) + (integer_literal))))) + (expr_stmt + (assignment_expr + (unary_expr + (ident)) + (initializer_list + (arg + (param_path + (param_path_element + (ident))) + (ident)) + (arg + (param_path + (param_path_element + (ident))) + (integer_literal))))))))) + +================================================================================ +Lambda expressions +================================================================================ + +fn void main() { + CallbackFn f = fn (i) => i + 1; + apply(x, fn (i) => i * i); + apply(x, fn int(int i) { return i * i; }); +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (declaration_stmt + (type + (base_type + (type_ident))) + (local_decl_after_type + (ident) + (lambda_expr + (lambda_declaration + (fn_parameter_list + (parameter + (ident)))) + (implies_body + (binary_expr + (ident) + (integer_literal)))))) + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (ident)) + (arg + (lambda_expr + (lambda_declaration + (fn_parameter_list + (parameter + (ident)))) + (implies_body + (binary_expr + (ident) + (ident)))))))) + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (ident)) + (arg + (lambda_declaration + (type + (base_type + (base_type_name))) + (fn_parameter_list + (parameter + (type + (base_type + (base_type_name))) + (ident)))) + (compound_stmt + (return_stmt + (binary_expr + (ident) + (ident)))))))))))) + +================================================================================ +Assignment expressions +================================================================================ + +fn void main() { + x = y; + x += 1; + x += y; + *s = &p; +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + return_type: (type + (base_type + (base_type_name))) + name: (ident)) + (fn_parameter_list) + body: (macro_func_body + (compound_stmt + (expr_stmt + (assignment_expr + left: (ident) + right: (ident))) + (expr_stmt + (assignment_expr + left: (ident) + right: (integer_literal))) + (expr_stmt + (assignment_expr + left: (ident) + right: (ident))) + (expr_stmt + (assignment_expr + left: (unary_expr + argument: (ident)) + right: (unary_expr + argument: (ident)))))))) + +================================================================================ +Unary expressions +================================================================================ + +fn void main() { + -a; + !b; + *c; + &d; + &&12; + &(*(&(e))); +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (expr_stmt + (unary_expr + (ident))) + (expr_stmt + (unary_expr + (ident))) + (expr_stmt + (unary_expr + (ident))) + (expr_stmt + (unary_expr + (ident))) + (expr_stmt + (unary_expr + (integer_literal))) + (expr_stmt + (unary_expr + (paren_expr + (unary_expr + (paren_expr + (unary_expr + (paren_expr + (ident)))))))))))) + +================================================================================ +Binary expressions +================================================================================ + +fn void main() { + a - a; + !b && c; + c || !b; + 12 + 12; + 100 * d; + 2 * (10 + 4); + (2) * (10 + (4 << 2)); +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (expr_stmt + (binary_expr + (ident) + (ident))) + (expr_stmt + (binary_expr + (unary_expr + (ident)) + (ident))) + (expr_stmt + (binary_expr + (ident) + (unary_expr + (ident)))) + (expr_stmt + (binary_expr + (integer_literal) + (integer_literal))) + (expr_stmt + (binary_expr + (integer_literal) + (ident))) + (expr_stmt + (binary_expr + (integer_literal) + (paren_expr + (binary_expr + (integer_literal) + (integer_literal))))) + (expr_stmt + (binary_expr + (paren_expr + (integer_literal)) + (paren_expr + (binary_expr + (integer_literal) + (paren_expr + (binary_expr + (integer_literal) + (integer_literal))))))))))) + +================================================================================ +Subscript expressions +================================================================================ + +fn void main() { + a[0]; + b[:]; + b[..]; + c[1:]; + c[1..]; + d[1:2]; + d[1..2]; + e[a]; +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (expr_stmt + (subscript_expr + (ident) + (integer_literal))) + (expr_stmt + (subscript_expr + (ident) + (range_expr + (MISSING ident)))) + (expr_stmt + (subscript_expr + (ident) + (range_expr))) + (expr_stmt + (subscript_expr + (ident) + (range_expr + (integer_literal)))) + (expr_stmt + (subscript_expr + (ident) + (range_expr + (integer_literal)))) + (expr_stmt + (subscript_expr + (ident) + (range_expr + (integer_literal) + (integer_literal)))) + (expr_stmt + (subscript_expr + (ident) + (range_expr + (integer_literal) + (integer_literal)))) + (expr_stmt + (subscript_expr + (ident) + (ident))))))) + +================================================================================ +Call expressions +================================================================================ + +fn void main() { + a(); + b(c, d); + e.f(g); + i.j.k(); + l::m::n(); + o::p(); +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation))) + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (ident)) + (arg + (ident))))) + (expr_stmt + (call_expr + (field_expr + (ident) + (access_ident + (ident))) + (call_invocation + (arg + (ident))))) + (expr_stmt + (call_expr + (field_expr + (field_expr + (ident) + (access_ident + (ident))) + (access_ident + (ident))) + (call_invocation))) + (expr_stmt + (call_expr + (module_ident_expr + (module_resolution + (ident)) + (module_resolution + (ident)) + (ident)) + (call_invocation))) + (expr_stmt + (call_expr + (module_ident_expr + (module_resolution + (ident)) + (ident)) + (call_invocation))))))) + +================================================================================ +Field expressions +================================================================================ + +fn void main() { + a.b; + *a.b; + &a.b; + a.b.c; + a.b.c.d; + a::b::c.d.e; + a::b::c.d.e(); +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (expr_stmt + (field_expr + (ident) + (access_ident + (ident)))) + (expr_stmt + (unary_expr + (field_expr + (ident) + (access_ident + (ident))))) + (expr_stmt + (unary_expr + (field_expr + (ident) + (access_ident + (ident))))) + (expr_stmt + (field_expr + (field_expr + (ident) + (access_ident + (ident))) + (access_ident + (ident)))) + (expr_stmt + (field_expr + (field_expr + (field_expr + (ident) + (access_ident + (ident))) + (access_ident + (ident))) + (access_ident + (ident)))) + (expr_stmt + (field_expr + (field_expr + (module_ident_expr + (module_resolution + (ident)) + (module_resolution + (ident)) + (ident)) + (access_ident + (ident))) + (access_ident + (ident)))) + (expr_stmt + (call_expr + (field_expr + (field_expr + (module_ident_expr + (module_resolution + (ident)) + (module_resolution + (ident)) + (ident)) + (access_ident + (ident))) + (access_ident + (ident))) + (call_invocation))))))) + +================================================================================ +Try expressions +================================================================================ + +fn void main() { + File*! res = findFile(); + if (try res) + { + printf("I found the file\n"); + } +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (declaration_stmt + (optional_type + (type + (base_type + (type_ident)) + (type_suffix))) + (local_decl_after_type + (ident) + (call_expr + (ident) + (call_invocation)))) + (if_stmt + (paren_cond + (try_unwrap + (ident))) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (string_literal + (string_content) + (escape_sequence)))))))))))) + +================================================================================ +Catch expressions +================================================================================ + +fn void main() { + File*! res = findFile(); + if (catch res) + { + printf("An error occurred!\n"); + } +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (declaration_stmt + (optional_type + (type + (base_type + (type_ident)) + (type_suffix))) + (local_decl_after_type + (ident) + (call_expr + (ident) + (call_invocation)))) + (if_stmt + (paren_cond + (catch_unwrap + (catch_unwrap_list + (ident)))) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (string_literal + (string_content) + (escape_sequence)))))))))))) + +================================================================================ +Block expressions +================================================================================ + +fn void main() { + {| + io::printf("Hello "); + |}; + + int a = {| + if (x > 0) return x * 2; + if (x == 0) return 100; + return -x; + |}; +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (expr_stmt + (expr_block + (expr_stmt + (call_expr + (module_ident_expr + (module_resolution + (ident)) + (ident)) + (call_invocation + (arg + (string_literal + (string_content)))))))) + (declaration_stmt + (type + (base_type + (base_type_name))) + (local_decl_after_type + (ident) + (expr_block + (if_stmt + (paren_cond + (binary_expr + (ident) + (integer_literal))) + (return_stmt + (binary_expr + (ident) + (integer_literal)))) + (if_stmt + (paren_cond + (binary_expr + (ident) + (integer_literal))) + (return_stmt + (integer_literal))) + (return_stmt + (unary_expr + (ident)))))))))) diff --git a/test/corpus/statement.txt b/test/corpus/statement.txt new file mode 100644 index 0000000..1853a0b --- /dev/null +++ b/test/corpus/statement.txt @@ -0,0 +1,600 @@ +================================================================================ +If statements +================================================================================ + +fn void main() { + if (true) false; + if (1) { + 0; + } + if (true) { + 0; + } else { + 1; + } + if (a) { + 0; + } else if (b) { + 1; + } else { + 2; + } +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (if_stmt + (paren_cond) + (expr_stmt)) + (if_stmt + (paren_cond + (integer_literal)) + (compound_stmt + (expr_stmt + (integer_literal)))) + (if_stmt + (paren_cond) + (compound_stmt + (expr_stmt + (integer_literal))) + (else_part + (compound_stmt + (expr_stmt + (integer_literal))))) + (if_stmt + (paren_cond + (ident)) + (compound_stmt + (expr_stmt + (integer_literal))) + (else_part + (if_stmt + (paren_cond + (ident)) + (compound_stmt + (expr_stmt + (integer_literal))) + (else_part + (compound_stmt + (expr_stmt + (integer_literal))))))))))) + +================================================================================ +While statements +================================================================================ + +fn void main() { + int i = 0; + while (i < 10) { + i += 1; + i++; + } + while (1) { + print(1); + } + while (Point* p = getPoint()) + { + print(p.calculate()); + } +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (declaration_stmt + (type + (base_type + (base_type_name))) + (local_decl_after_type + (ident) + (integer_literal))) + (while_stmt + (paren_cond + (binary_expr + (ident) + (integer_literal))) + (compound_stmt + (expr_stmt + (assignment_expr + (ident) + (integer_literal))) + (expr_stmt + (update_expr + (ident))))) + (while_stmt + (paren_cond + (integer_literal)) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (integer_literal))))))) + (while_stmt + (paren_cond + (type + (base_type + (type_ident)) + (type_suffix)) + (local_decl_after_type + (ident) + (call_expr + (ident) + (call_invocation)))) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (call_expr + (field_expr + (ident) + (access_ident + (ident))) + (call_invocation)))))))))))) + +================================================================================ +Do statements +================================================================================ + +fn void main() { + do + { + print(i); + }; +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (do_stmt + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (ident))))))))))) + +================================================================================ +For statements +================================================================================ + +fn void main() { + for (int i = 0; i < 10; i++) { + print(i); + } + for (i = 0; i < 10; i++) { + print(i); + } + for (;;) { + print(1); + } + for (; i < 10;) { + print(1); + } + for (;; i++) { + print(1); + } +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (for_stmt + (for_cond + (comma_decl_or_expr + (type + (base_type + (base_type_name))) + (local_decl_after_type + (ident) + (integer_literal))) + (binary_expr + (ident) + (integer_literal)) + (comma_decl_or_expr + (update_expr + (ident)))) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (ident))))))) + (for_stmt + (for_cond + (comma_decl_or_expr + (assignment_expr + (ident) + (integer_literal))) + (binary_expr + (ident) + (integer_literal)) + (comma_decl_or_expr + (update_expr + (ident)))) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (ident))))))) + (for_stmt + (for_cond) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (integer_literal))))))) + (for_stmt + (for_cond + (binary_expr + (ident) + (integer_literal))) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (integer_literal))))))) + (for_stmt + (for_cond + (comma_decl_or_expr + (update_expr + (ident)))) + (compound_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation + (arg + (integer_literal))))))))))) + +================================================================================ +Foreach statements +================================================================================ + +fn void main() { + foreach (index, value : values) + { + io::printfn("%d: %f", index, value); + } +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (foreach_stmt + (foreach_cond + (foreach_var + (ident)) + (foreach_var + (ident)) + (ident)) + (compound_stmt + (expr_stmt + (call_expr + (module_ident_expr + (module_resolution + (ident)) + (ident)) + (call_invocation + (arg + (string_literal + (string_content))) + (arg + (ident)) + (arg + (ident))))))))))) + +================================================================================ +Defer statements +================================================================================ + +fn void main() { + defer free(); + defer { + i = i + 1; + break; + } + defer { + defer test1(); + test2(); + defer test3(); + } +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (defer_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation)))) + (defer_stmt + (compound_stmt + (expr_stmt + (assignment_expr + (ident) + (binary_expr + (ident) + (integer_literal)))) + (break_stmt))) + (defer_stmt + (compound_stmt + (defer_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation)))) + (expr_stmt + (call_expr + (ident) + (call_invocation))) + (defer_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation)))))))))) + +================================================================================ +Switch statements +================================================================================ + +fn void main() { + switch { + case 1: + case 2: + a(); + nextcase; + case 3: + b(); + default: + c(); + break; + } + + switch { + case true: + return; + } + + switch (i) { + case 1: + doSomething(); + nextcase; + case 2: + doSomethingElse(); + } + + switch (true) + { + case x < 0: + xless(); + case x > 0: + xgreater(); + default: + xequals(); + } + + switch { + case foo() > 0: + bar(); + case test() == 1: + baz(); + } + + switch (i) { + case 1: + doSomething(); + nextcase 3; + case 2: + doSomethingElse(); + case 3: + nextcase rand(); + default: + io::println("Ended"); + } +} + +-------------------------------------------------------------------------------- + +(source_file + (func_definition + (func_header + (type + (base_type + (base_type_name))) + (ident)) + (fn_parameter_list) + (macro_func_body + (compound_stmt + (switch_stmt + (switch_body + (case_stmt + (integer_literal)) + (case_stmt + (integer_literal) + (expr_stmt + (call_expr + (ident) + (call_invocation))) + (nextcase_stmt)) + (case_stmt + (integer_literal) + (expr_stmt + (call_expr + (ident) + (call_invocation)))) + (default_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation))) + (break_stmt)))) + (switch_stmt + (switch_body + (case_stmt + (return_stmt)))) + (switch_stmt + (paren_cond + (ident)) + (switch_body + (case_stmt + (integer_literal) + (expr_stmt + (call_expr + (ident) + (call_invocation))) + (nextcase_stmt)) + (case_stmt + (integer_literal) + (expr_stmt + (call_expr + (ident) + (call_invocation)))))) + (switch_stmt + (paren_cond) + (switch_body + (case_stmt + (binary_expr + (ident) + (integer_literal)) + (expr_stmt + (call_expr + (ident) + (call_invocation)))) + (case_stmt + (binary_expr + (ident) + (integer_literal)) + (expr_stmt + (call_expr + (ident) + (call_invocation)))) + (default_stmt + (expr_stmt + (call_expr + (ident) + (call_invocation)))))) + (switch_stmt + (switch_body + (case_stmt + (binary_expr + (call_expr + (ident) + (call_invocation)) + (integer_literal)) + (expr_stmt + (call_expr + (ident) + (call_invocation)))) + (case_stmt + (binary_expr + (call_expr + (ident) + (call_invocation)) + (integer_literal)) + (expr_stmt + (call_expr + (ident) + (call_invocation)))))) + (switch_stmt + (paren_cond + (ident)) + (switch_body + (case_stmt + (integer_literal) + (expr_stmt + (call_expr + (ident) + (call_invocation))) + (nextcase_stmt + (integer_literal))) + (case_stmt + (integer_literal) + (expr_stmt + (call_expr + (ident) + (call_invocation)))) + (case_stmt + (integer_literal) + (nextcase_stmt + (call_expr + (ident) + (call_invocation)))) + (default_stmt + (expr_stmt + (call_expr + (module_ident_expr + (module_resolution + (ident)) + (ident)) + (call_invocation + (arg + (string_literal + (string_content))))))))))))) diff --git a/test/corpus/types.txt b/test/corpus/types.txt new file mode 100644 index 0000000..e2dc434 --- /dev/null +++ b/test/corpus/types.txt @@ -0,0 +1,258 @@ +================================================================================ +Primitive types +================================================================================ + +int a; +char a; +ushort a; +uint a; +ulong a; +uptr a; +usz a; + +ichar a; +short a; +int a; +long a; +iptr a; +isz a; + +-------------------------------------------------------------------------------- + +(source_file + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident)) + (global_declaration + (type + (base_type + (base_type_name))) + (ident))) + +================================================================================ +Array types +================================================================================ + +int[*] a = { 1, 2, 3, 4, 5 }; +int[5] a = { 1, 2, 3, 4, 5 }; + +-------------------------------------------------------------------------------- + +(source_file + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix)) + (ident) + (initializer_list + (arg + (integer_literal)) + (arg + (integer_literal)) + (arg + (integer_literal)) + (arg + (integer_literal)) + (arg + (integer_literal)))) + (global_declaration + (type + (base_type + (base_type_name)) + (type_suffix + (integer_literal))) + (ident) + (initializer_list + (arg + (integer_literal)) + (arg + (integer_literal)) + (arg + (integer_literal)) + (arg + (integer_literal)) + (arg + (integer_literal))))) + +================================================================================ +Type identifiers +================================================================================ + +Foo a; +foo::Bar a; +foo::bar::Baz a; +Foo[10] b; +foo::Bar[10] b; +foo::bar::Baz[10] b; +Foo! c; +foo::Bar! c; +foo::bar::Baz! c; +Foo* c; +foo::Bar* c; +foo::bar::Baz* c; + +-------------------------------------------------------------------------------- + +(source_file + (global_declaration + (type + (base_type + (type_ident))) + (ident)) + (global_declaration + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (type_ident)))) + (ident)) + (global_declaration + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (module_resolution + (ident)) + (type_ident)))) + (ident)) + (global_declaration + (type + (base_type + (type_ident)) + (type_suffix + (integer_literal))) + (ident)) + (global_declaration + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (type_ident))) + (type_suffix + (integer_literal))) + (ident)) + (global_declaration + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (module_resolution + (ident)) + (type_ident))) + (type_suffix + (integer_literal))) + (ident)) + (global_declaration + (optional_type + (type + (base_type + (type_ident)))) + (ident)) + (global_declaration + (optional_type + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (type_ident))))) + (ident)) + (global_declaration + (optional_type + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (module_resolution + (ident)) + (type_ident))))) + (ident)) + (global_declaration + (type + (base_type + (type_ident)) + (type_suffix)) + (ident)) + (global_declaration + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (type_ident))) + (type_suffix)) + (ident)) + (global_declaration + (type + (base_type + (module_type_ident + (module_resolution + (ident)) + (module_resolution + (ident)) + (type_ident))) + (type_suffix)) + (ident)))