forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_ast.rs
More file actions
210 lines (186 loc) · 8.49 KB
/
Copy pathparse_ast.rs
File metadata and controls
210 lines (186 loc) · 8.49 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use fe_common::diagnostics::print_diagnostics;
use fe_common::utils::ron::to_ron_string_pretty;
use fe_parser::grammar::{contracts, expressions, functions, module, types};
use fe_parser::{ParseResult, Parser};
use insta::assert_snapshot;
use serde::Serialize;
use wasm_bindgen_test::wasm_bindgen_test;
pub fn ast_string<F, T>(test_name: &str, mut parse_fn: F, src: &str) -> String
where
F: FnMut(&mut Parser) -> ParseResult<T>,
T: Serialize,
{
let mut files = fe_common::files::FileStore::new();
let id = files.add_file(test_name, src);
let mut parser = Parser::new(src, id);
if let Ok(ast) = parse_fn(&mut parser) {
if !parser.diagnostics.is_empty() {
print_diagnostics(&parser.diagnostics, &files);
panic!("parse error");
}
to_ron_string_pretty(&ast).unwrap()
} else {
if parser.diagnostics.is_empty() {
eprintln!("parsing failed, but no diagnostics were generated. this should be fixed.");
let next = parser.next();
if let Ok(tok) = next {
parser.error(
tok.span,
"this is the next token at time of parsing failure",
);
print_diagnostics(&parser.diagnostics, &files);
} else {
eprintln!("parser is at end of file");
}
} else {
print_diagnostics(&parser.diagnostics, &files);
}
panic!("parse failed");
}
}
// TODO: remove this when tests are moved into 'tests' module
macro_rules! assert_snapshot_wasm {
($module:ident, $name:ident, $actual:expr) => {
let snap = include_str!(concat!(
"snapshots/cases__",
stringify!($module),
"__",
stringify!($name),
".snap"
));
let (_, expected) = snap.rsplit_once("---\n").unwrap();
pretty_assertions::assert_eq!($actual.trim(), expected.trim());
};
}
macro_rules! test_parse {
($name:ident, $parse_fn:expr, $src:expr) => {
#[test]
#[wasm_bindgen_test]
fn $name() {
if cfg!(target_arch = "wasm32") {
assert_snapshot_wasm!(
parse_ast,
$name,
ast_string(stringify!($name), $parse_fn, $src)
);
} else {
assert_snapshot!(ast_string(stringify!($name), $parse_fn, $src));
}
}
};
}
test_parse! { expr_call1, expressions::parse_expr, "foo()" }
test_parse! { expr_call2, expressions::parse_expr, "foo(1,2,x=3)" }
test_parse! { expr_attr1, expressions::parse_expr, "foo.bar[0][y]" }
test_parse! { expr_attr2, expressions::parse_expr, "a[x].b[y](1)" }
test_parse! { expr_num1, expressions::parse_expr, "12345" }
test_parse! { expr_num2, expressions::parse_expr, "00001" }
test_parse! { expr_hex1, expressions::parse_expr, "0xbeefbeef" }
test_parse! { expr_hex2, expressions::parse_expr, "0xFEED1234" }
test_parse! { expr_string, expressions::parse_expr, r#""hi \tmom\n""# }
test_parse! { expr_list, expressions::parse_expr, "[]" }
test_parse! { expr_list2, expressions::parse_expr, "[x, y, z,]" }
test_parse! { expr_ternary, expressions::parse_expr, "x + 1 if y + 2 else z + 3" }
test_parse! { expr_group, expressions::parse_expr, "(1 + 2) * 3" }
test_parse! { expr_tuple1, expressions::parse_expr, "(1,)" }
test_parse! { expr_tuple2, expressions::parse_expr, "(1, 2, \n 3)" }
test_parse! { expr_tuple3, expressions::parse_expr, "(1, (2 + 3), (3 * 4, 5))" }
test_parse! { expr_unit, expressions::parse_expr, "()" }
test_parse! { ops_not, expressions::parse_expr, "x and not y" }
test_parse! { ops_math, expressions::parse_expr, "a + b * -c ** d / e % f" }
test_parse! { ops_neg, expressions::parse_expr, "-x" }
test_parse! { ops_bnot, expressions::parse_expr, "~x" }
// bitwise op precedence: shift > and > xor > or
test_parse! { ops_bit1, expressions::parse_expr, "a & b >> c" }
test_parse! { ops_bit2, expressions::parse_expr, "a ^ b & c" }
test_parse! { ops_bit3, expressions::parse_expr, "a | b ^ c" }
test_parse! { ops_shift, expressions::parse_expr, "a << b >> c" }
test_parse! { ops_bool, expressions::parse_expr, "a or b and c" }
test_parse! { stmt_assert_no_msg, functions::parse_stmt, "assert x == y" }
test_parse! { stmt_assert_msg, functions::parse_stmt, "assert x == y, z" }
test_parse! { stmt_aug_add, functions::parse_stmt, "x += y" }
test_parse! { stmt_aug_sub, functions::parse_stmt, "x -= y" }
test_parse! { stmt_aug_mul, functions::parse_stmt, "x *= y" }
test_parse! { stmt_aug_div, functions::parse_stmt, "x /= y" }
test_parse! { stmt_aug_mod, functions::parse_stmt, "x %= y" }
test_parse! { stmt_aug_and, functions::parse_stmt, "x &= y" }
test_parse! { stmt_aug_or, functions::parse_stmt, "x |= y" }
test_parse! { stmt_aug_xor, functions::parse_stmt, "x ^= y" }
test_parse! { stmt_aug_lsh, functions::parse_stmt, "x <<= y" }
test_parse! { stmt_aug_rsh, functions::parse_stmt, "x >>= y" }
test_parse! { stmt_aug_exp, functions::parse_stmt, "x **= y" }
test_parse! { stmt_emit1, functions::parse_stmt, "emit Foo()" }
test_parse! { stmt_emit2, functions::parse_stmt, "emit Foo(1, 2, x=y)" }
test_parse! { stmt_return1, functions::parse_stmt, "return" }
test_parse! { stmt_return2, functions::parse_stmt, "return x" }
test_parse! { stmt_return3, functions::parse_stmt, "return not x" }
test_parse! { stmt_revert, functions::parse_stmt, "revert" }
test_parse! { stmt_if, functions::parse_stmt, "if a:\n b" }
test_parse! { stmt_if2, functions::parse_stmt, "if a:\n b \nelif c:\n d \nelif e: \n f \nelse:\n g" }
test_parse! { stmt_while, functions::parse_stmt, "while a > 5:\n a -= 1" }
test_parse! { stmt_for, functions::parse_stmt, "for a in b[0]:\n pass" }
test_parse! { stmt_var_decl_name, functions::parse_stmt, "foo: u256" }
test_parse! { stmt_var_decl_tuple, functions::parse_stmt, "(foo, bar): (u256, u256) = (10, 10)" }
test_parse! { stmt_var_decl_tuples, functions::parse_stmt, "(a, (b, (c, d))): x" }
test_parse! { type_def, types::parse_type_def, "type X = Map<address, u256>" }
test_parse! { type_name, types::parse_type_desc, "MyType" }
test_parse! { type_array, types::parse_type_desc, "address[25]" }
test_parse! { type_3d, types::parse_type_desc, "u256[4][4][4]" }
test_parse! { type_string, types::parse_type_desc, "string<100>" }
test_parse! { type_generic, types::parse_type_desc, "foo<a, b<c>, d[10]>" }
test_parse! { type_generic_int, types::parse_type_desc, "foo<1, 2>" }
test_parse! { type_map1, types::parse_type_desc, "Map<address, u256>" }
test_parse! { type_map2, types::parse_type_desc, "Map<address, Map<u8, u256>>" }
test_parse! { type_map3, types::parse_type_desc, "Map<address, Map<u8, Map<u8, u8>>>" }
test_parse! { type_map4, types::parse_type_desc, "map < address , map < u8, u256 > >" }
test_parse! { type_tuple, types::parse_type_desc, "(u8, u16, address, Map<u8, u8>)" }
test_parse! { type_unit, types::parse_type_desc, "()" }
test_parse! { fn_def, |par| functions::parse_fn_def(par, None), "def foo21(x: bool, y: address,) -> bool:\n x"}
test_parse! { event_def, types::parse_event_def, "event Foo:\n x: address\n idx y: u8" }
test_parse! { empty_event_def, types::parse_event_def, "event Foo:\n pass" }
test_parse! { pragma1, module::parse_pragma, "pragma 0.1.0" }
test_parse! { pragma2, module::parse_pragma, "pragma 0.1.0-alpha" }
test_parse! { pragma3, module::parse_pragma, "pragma >= 1.2, < 1.5" }
test_parse! { import_simple, module::parse_simple_import, "import foo as bar, baz, bing as bop" }
test_parse! { struct_def, types::parse_struct_def, r#"struct S:
x: address
pub y: u8
const z: u8
pub const a: Map<u8, foo>
"# }
test_parse! { empty_struct_def, types::parse_struct_def, r#"struct S:
pass
"# }
test_parse! { contract_def, contracts::parse_contract_def, r#"contract Foo:
x: address
pub y: u8
pub const z: Map<u8, address>
pub def foo() -> u8:
return 10
event Bar:
idx from: address
"# }
test_parse! { empty_contract_def, contracts::parse_contract_def, r#"contract Foo:
pass
"# }
test_parse! { module_stmts, module::parse_module, r#"
pragma 0.5.0
import foo as bar, baz as bum
type X = Map<u8, u16>
contract A:
pub const x: u256 = 10
contract B:
pub x: X
"# }
test_parse! { guest_book, module::parse_module, r#"
type BookMsg = bytes[100]
contract GuestBook:
pub guest_book: Map<address, BookMsg>
event Signed:
idx book_msg: BookMsg
pub def sign(book_msg: BookMsg):
self.guest_book[msg.sender] = book_msg
emit Signed(book_msg=book_msg)
pub def get_msg(addr: address) -> BookMsg:
return self.guest_book[addr]
"# }