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
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Parsing pipes protocols from token trees.
use ast_util;
use ext::pipes::proto::*;
use parse::common::SeqSep;
use parse::parser;
use parse::token;
pub trait proto_parser {
fn parse_proto(&self, id: ~str) -> protocol;
fn parse_state(&self, proto: protocol);
fn parse_message(&self, state: state);
}
impl proto_parser for parser::Parser {
fn parse_proto(&self, id: ~str) -> protocol {
let proto = protocol(id, *self.span);
self.parse_seq_to_before_end(
&token::EOF,
SeqSep {
sep: None,
trailing_sep_allowed: false,
},
|self| self.parse_state(proto)
);
return proto;
}
fn parse_state(&self, proto: protocol) {
let id = self.parse_ident();
let name = copy *self.interner.get(id);
self.expect(&token::COLON);
let dir = match copy *self.token {
token::IDENT(n, _) => self.interner.get(n),
_ => fail!()
};
self.bump();
let dir = match dir {
@~"send" => send,
@~"recv" => recv,
_ => fail!()
};
let generics = if *self.token == token::LT {
self.parse_generics()
} else {
ast_util::empty_generics()
};
let state = proto.add_state_poly(name, id, dir, generics);
// parse the messages
self.parse_unspanned_seq(
&token::LBRACE,
&token::RBRACE,
SeqSep {
sep: Some(token::COMMA),
trailing_sep_allowed: true,
},
|self| self.parse_message(state)
);
}
fn parse_message(&self, state: state) {
let mname = copy *self.interner.get(self.parse_ident());
let args = if *self.token == token::LPAREN {
self.parse_unspanned_seq(
&token::LPAREN,
&token::RPAREN,
SeqSep {
sep: Some(token::COMMA),
trailing_sep_allowed: true,
},
|p| p.parse_ty(false)
)
}
else { ~[] };
self.expect(&token::RARROW);
let next = match *self.token {
token::IDENT(_, _) => {
let name = copy *self.interner.get(self.parse_ident());
let ntys = if *self.token == token::LT {
self.parse_unspanned_seq(
&token::LT,
&token::GT,
SeqSep {
sep: Some(token::COMMA),
trailing_sep_allowed: true,
},
|p| p.parse_ty(false)
)
}
else { ~[] };
Some(next_state {state: name, tys: ntys})
}
token::NOT => {
// -> !
self.bump();
None
}
_ => self.fatal(~"invalid next state")
};
state.add_message(mname, *self.span, args, next);
}
}
|