2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
2012-07-05 23:04:56 -07:00
|
|
|
// Parsing pipes protocols from token trees.
|
|
|
|
|
2013-01-08 19:37:25 -08:00
|
|
|
use ext::pipes::pipec::*;
|
2013-02-21 00:16:31 -08:00
|
|
|
use parse::common::SeqSep;
|
2012-09-04 11:37:29 -07:00
|
|
|
use parse::parser;
|
|
|
|
use parse::token;
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2013-01-08 19:37:25 -08:00
|
|
|
use core::prelude::*;
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2013-01-29 13:54:06 -08:00
|
|
|
pub trait proto_parser {
|
2013-02-17 02:51:55 -05:00
|
|
|
fn parse_proto(&self, id: ~str) -> protocol;
|
|
|
|
fn parse_state(&self, proto: protocol);
|
|
|
|
fn parse_message(&self, state: state);
|
2012-07-11 15:00:40 -07:00
|
|
|
}
|
|
|
|
|
2013-02-14 21:17:26 -08:00
|
|
|
pub impl proto_parser for parser::Parser {
|
2013-02-17 02:51:55 -05:00
|
|
|
fn parse_proto(&self, id: ~str) -> protocol {
|
2013-02-21 18:12:13 -08:00
|
|
|
let proto = protocol(id, *self.span);
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2013-02-24 18:32:02 -08:00
|
|
|
self.parse_seq_to_before_end(
|
|
|
|
token::EOF,
|
|
|
|
SeqSep {
|
|
|
|
sep: None,
|
|
|
|
trailing_sep_allowed: false,
|
|
|
|
},
|
|
|
|
|self| self.parse_state(proto)
|
|
|
|
);
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2012-08-01 17:30:05 -07:00
|
|
|
return proto;
|
2012-07-05 23:04:56 -07:00
|
|
|
}
|
|
|
|
|
2013-02-17 02:51:55 -05:00
|
|
|
fn parse_state(&self, proto: protocol) {
|
2012-07-05 23:04:56 -07:00
|
|
|
let id = self.parse_ident();
|
2012-07-18 16:18:02 -07:00
|
|
|
let name = *self.interner.get(id);
|
|
|
|
|
2013-02-24 10:20:24 -08:00
|
|
|
self.expect(&token::COLON);
|
2013-02-24 18:32:02 -08:00
|
|
|
let dir = match copy *self.token {
|
|
|
|
token::IDENT(n, _) => self.interner.get(n),
|
|
|
|
_ => fail!()
|
2012-07-05 23:04:56 -07:00
|
|
|
};
|
|
|
|
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,
|
2013-02-11 19:26:38 -08:00
|
|
|
_ => fail!()
|
2012-07-05 23:04:56 -07:00
|
|
|
};
|
|
|
|
|
2013-02-21 18:12:13 -08:00
|
|
|
let typarms = if *self.token == token::LT {
|
2012-07-05 23:14:27 -07:00
|
|
|
self.parse_ty_params()
|
2013-02-21 18:12:13 -08:00
|
|
|
} else {
|
|
|
|
~[]
|
|
|
|
};
|
2012-07-05 23:14:27 -07:00
|
|
|
|
2012-07-18 16:18:02 -07:00
|
|
|
let state = proto.add_state_poly(name, id, dir, typarms);
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2012-07-05 23:14:27 -07:00
|
|
|
// parse the messages
|
|
|
|
self.parse_unspanned_seq(
|
2013-02-24 18:32:02 -08:00
|
|
|
token::LBRACE,
|
|
|
|
token::RBRACE,
|
|
|
|
SeqSep {
|
2013-02-21 00:16:31 -08:00
|
|
|
sep: Some(token::COMMA),
|
2013-02-24 18:32:02 -08:00
|
|
|
trailing_sep_allowed: true,
|
|
|
|
},
|
|
|
|
|self| self.parse_message(state)
|
|
|
|
);
|
2012-07-16 14:44:27 -07:00
|
|
|
}
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2013-02-17 02:51:55 -05:00
|
|
|
fn parse_message(&self, state: state) {
|
2012-07-18 16:18:02 -07:00
|
|
|
let mname = *self.interner.get(self.parse_ident());
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2013-02-21 18:12:13 -08:00
|
|
|
let args = if *self.token == token::LPAREN {
|
2013-02-24 18:32:02 -08:00
|
|
|
self.parse_unspanned_seq(
|
|
|
|
token::LPAREN,
|
|
|
|
token::RPAREN,
|
|
|
|
SeqSep {
|
|
|
|
sep: Some(token::COMMA),
|
|
|
|
trailing_sep_allowed: true,
|
|
|
|
},
|
|
|
|
|p| p.parse_ty(false)
|
|
|
|
)
|
2012-07-16 14:44:27 -07:00
|
|
|
}
|
|
|
|
else { ~[] };
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2013-02-24 10:20:24 -08:00
|
|
|
self.expect(&token::RARROW);
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2013-02-21 18:12:13 -08:00
|
|
|
let next = match *self.token {
|
2012-08-03 19:59:04 -07:00
|
|
|
token::IDENT(_, _) => {
|
2012-07-18 16:18:02 -07:00
|
|
|
let name = *self.interner.get(self.parse_ident());
|
2013-02-21 18:12:13 -08:00
|
|
|
let ntys = if *self.token == token::LT {
|
2013-02-24 18:32:02 -08:00
|
|
|
self.parse_unspanned_seq(
|
|
|
|
token::LT,
|
|
|
|
token::GT,
|
|
|
|
SeqSep {
|
|
|
|
sep: Some(token::COMMA),
|
|
|
|
trailing_sep_allowed: true,
|
|
|
|
},
|
|
|
|
|p| p.parse_ty(false)
|
|
|
|
)
|
2012-07-16 14:44:27 -07:00
|
|
|
}
|
|
|
|
else { ~[] };
|
2013-01-17 08:55:28 -08:00
|
|
|
Some(next_state {state: name, tys: ntys})
|
2012-07-16 14:44:27 -07:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
token::NOT => {
|
2012-07-16 14:44:27 -07:00
|
|
|
// -> !
|
|
|
|
self.bump();
|
2012-08-20 12:23:37 -07:00
|
|
|
None
|
2012-07-16 14:44:27 -07:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => self.fatal(~"invalid next state")
|
2012-07-16 14:44:27 -07:00
|
|
|
};
|
2012-07-05 23:04:56 -07:00
|
|
|
|
2013-02-21 18:12:13 -08:00
|
|
|
state.add_message(mname, *self.span, args, next);
|
2012-07-05 23:04:56 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|