aboutsummaryrefslogtreecommitdiff
path: root/src/parser/grammar/params.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-31 21:13:08 +0100
committerAleksey Kladov <[email protected]>2018-07-31 21:13:08 +0100
commitd82a21ab202b2f5e8c96847802d806735ec74ad3 (patch)
treeaea23d0bb9760a6104f5994a73b37c869cbaf4bb /src/parser/grammar/params.rs
parent1af8eb9c08f974a1b3beecfebadeb03144ef337d (diff)
lambda expressions
Diffstat (limited to 'src/parser/grammar/params.rs')
-rw-r--r--src/parser/grammar/params.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/parser/grammar/params.rs b/src/parser/grammar/params.rs
new file mode 100644
index 000000000..1ef2cea88
--- /dev/null
+++ b/src/parser/grammar/params.rs
@@ -0,0 +1,71 @@
1use super::*;
2
3// test param_list
4// fn a() {}
5// fn b(x: i32) {}
6// fn c(x: i32, ) {}
7// fn d(x: i32, y: ()) {}
8pub(super) fn list(p: &mut Parser) {
9 list_(p, true)
10}
11
12pub(super) fn list_opt_types(p: &mut Parser) {
13 list_(p, false)
14}
15
16fn list_(p: &mut Parser, require_types: bool) {
17 assert!(p.at(if require_types { L_PAREN } else { PIPE }));
18 let m = p.start();
19 p.bump();
20 if require_types {
21 self_param(p);
22 }
23 let terminator = if require_types { R_PAREN } else { PIPE };
24 while !p.at(EOF) && !p.at(terminator) {
25 value_parameter(p, require_types);
26 if !p.at(terminator) {
27 p.expect(COMMA);
28 }
29 }
30 p.expect(terminator);
31 m.complete(p, PARAM_LIST);
32}
33
34fn value_parameter(p: &mut Parser, require_type: bool) {
35 let m = p.start();
36 patterns::pattern(p);
37 if p.at(COLON) || require_type {
38 types::ascription(p)
39 }
40 m.complete(p, VALUE_PARAMETER);
41}
42
43// test self_param
44// impl S {
45// fn a(self) {}
46// fn b(&self,) {}
47// fn c(&'a self,) {}
48// fn d(&'a mut self, x: i32) {}
49// }
50fn self_param(p: &mut Parser) {
51 let la1 = p.nth(1);
52 let la2 = p.nth(2);
53 let la3 = p.nth(3);
54 let n_toks = match (p.current(), la1, la2, la3) {
55 (SELF_KW, _, _, _) => 1,
56 (AMPERSAND, SELF_KW, _, _) => 2,
57 (AMPERSAND, MUT_KW, SELF_KW, _) => 3,
58 (AMPERSAND, LIFETIME, SELF_KW, _) => 3,
59 (AMPERSAND, LIFETIME, MUT_KW, SELF_KW) => 4,
60 _ => return,
61 };
62 let m = p.start();
63 for _ in 0..n_toks {
64 p.bump();
65 }
66 m.complete(p, SELF_PARAM);
67 if !p.at(R_PAREN) {
68 p.expect(COMMA);
69 }
70}
71