rust/src/libsyntax/ext/pipes/parse_proto.rs

92 lines
2.6 KiB
Rust
Raw Normal View History

// Parsing pipes protocols from token trees.
import parse::parser;
import ast::ident;
import parse::token;
import pipec::*;
trait proto_parser {
fn parse_proto(id: ident) -> protocol;
fn parse_state(proto: protocol);
}
2012-08-07 18:10:06 -07:00
impl parser: proto_parser {
fn parse_proto(id: ident) -> protocol {
let proto = protocol(id, self.span);
self.parse_seq_to_before_end(token::EOF,
{sep: none, trailing_sep_allowed: false},
|self| self.parse_state(proto));
2012-08-01 17:30:05 -07:00
return proto;
}
fn parse_state(proto: protocol) {
let id = self.parse_ident();
self.expect(token::COLON);
2012-08-06 12:34:08 -07:00
let dir = match copy self.token {
2012-08-03 19:59:04 -07:00
token::IDENT(n, _) => self.get_str(n),
_ => fail
};
self.bump();
2012-08-06 12:34:08 -07:00
let dir = match dir {
2012-08-03 19:59:04 -07:00
@~"send" => send,
@~"recv" => recv,
_ => fail
};
2012-07-05 23:14:27 -07:00
let typarms = if self.token == token::LT {
self.parse_ty_params()
}
else { ~[] };
let state = proto.add_state_poly(id, dir, typarms);
2012-07-05 23:14:27 -07:00
// parse the messages
self.parse_unspanned_seq(
token::LBRACE, token::RBRACE,
{sep: some(token::COMMA), trailing_sep_allowed: true},
|self| self.parse_message(state));
}
fn parse_message(state: state) {
let mname = self.parse_ident();
let args = if self.token == token::LPAREN {
self.parse_unspanned_seq(token::LPAREN,
token::RPAREN,
{sep: some(token::COMMA),
trailing_sep_allowed: true},
|p| p.parse_ty(false))
}
else { ~[] };
self.expect(token::RARROW);
2012-08-06 12:34:08 -07:00
let next = match copy self.token {
2012-08-03 19:59:04 -07:00
token::IDENT(_, _) => {
let name = self.parse_ident();
let ntys = if self.token == token::LT {
self.parse_unspanned_seq(token::LT,
token::GT,
{sep: some(token::COMMA),
trailing_sep_allowed: true},
|p| p.parse_ty(false))
}
else { ~[] };
some({state: name, tys: ntys})
}
2012-08-03 19:59:04 -07:00
token::NOT => {
// -> !
self.bump();
none
}
2012-08-03 19:59:04 -07:00
_ => self.fatal(~"invalid next state")
};
state.add_message(mname, copy self.span, args, next);
}
}