2012-07-16 17:27:04 -07:00
|
|
|
import to_str::to_str;
|
|
|
|
import dvec::{dvec, extensions};
|
|
|
|
|
|
|
|
import ast::{ident};
|
|
|
|
|
2012-07-17 16:49:54 -07:00
|
|
|
import ast_builder::{path, methods, ast_builder, append_types};
|
2012-07-16 17:27:04 -07:00
|
|
|
|
|
|
|
enum direction {
|
|
|
|
send, recv
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_str for direction {
|
|
|
|
fn to_str() -> ~str {
|
|
|
|
alt self {
|
|
|
|
send { ~"send" }
|
|
|
|
recv { ~"recv" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl methods for direction {
|
|
|
|
fn reverse() -> direction {
|
|
|
|
alt self {
|
|
|
|
send { recv }
|
|
|
|
recv { send }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type next_state = option<{state: ident, tys: ~[@ast::ty]}>;
|
|
|
|
|
|
|
|
enum message {
|
2012-07-24 16:58:48 -07:00
|
|
|
// name, span, data, current state, next state
|
|
|
|
message(ident, span, ~[@ast::ty], state, next_state)
|
2012-07-16 17:27:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl methods for message {
|
|
|
|
fn name() -> ident {
|
|
|
|
alt self {
|
2012-07-24 16:58:48 -07:00
|
|
|
message(id, _, _, _, _) {
|
2012-07-16 17:27:04 -07:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 16:58:48 -07:00
|
|
|
fn span() -> span {
|
|
|
|
alt self {
|
|
|
|
message(_, span, _, _, _) {
|
|
|
|
span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-16 17:27:04 -07:00
|
|
|
/// Return the type parameters actually used by this message
|
|
|
|
fn get_params() -> ~[ast::ty_param] {
|
|
|
|
alt self {
|
2012-07-24 16:58:48 -07:00
|
|
|
message(_, _, _, this, _) {
|
2012-07-16 17:27:04 -07:00
|
|
|
this.ty_params
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum state {
|
|
|
|
state_(@{
|
2012-07-17 17:03:27 -07:00
|
|
|
id: uint,
|
2012-07-16 17:27:04 -07:00
|
|
|
name: ident,
|
2012-07-24 16:58:48 -07:00
|
|
|
span: span,
|
2012-07-16 17:27:04 -07:00
|
|
|
dir: direction,
|
|
|
|
ty_params: ~[ast::ty_param],
|
|
|
|
messages: dvec<message>,
|
|
|
|
proto: protocol,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl methods for state {
|
2012-07-24 16:58:48 -07:00
|
|
|
fn add_message(name: ident, span: span,
|
|
|
|
+data: ~[@ast::ty], next: next_state) {
|
|
|
|
self.messages.push(message(name, span, data, self,
|
2012-07-16 17:27:04 -07:00
|
|
|
next));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filename() -> ~str {
|
|
|
|
(*self).proto.filename()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data_name() -> ident {
|
|
|
|
self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_ty(cx: ext_ctxt) -> @ast::ty {
|
2012-07-17 16:49:54 -07:00
|
|
|
cx.ty_path_ast_builder
|
2012-07-24 16:58:48 -07:00
|
|
|
(path(self.name, self.span).add_tys(cx.ty_vars(self.ty_params)))
|
2012-07-16 17:27:04 -07:00
|
|
|
}
|
2012-07-17 17:03:27 -07:00
|
|
|
|
|
|
|
/// Iterate over the states that can be reached in one message
|
|
|
|
/// from this state.
|
|
|
|
fn reachable(f: fn(state) -> bool) {
|
|
|
|
for self.messages.each |m| {
|
|
|
|
alt m {
|
2012-07-24 16:58:48 -07:00
|
|
|
message(_, _, _, _, some({state: id, _})) {
|
2012-07-17 17:03:27 -07:00
|
|
|
let state = self.proto.get_state(id);
|
|
|
|
if !f(state) { break }
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-16 17:27:04 -07:00
|
|
|
}
|
|
|
|
|
2012-07-23 13:50:12 -07:00
|
|
|
type protocol = @protocol_;
|
2012-07-16 17:27:04 -07:00
|
|
|
|
2012-07-24 16:58:48 -07:00
|
|
|
fn protocol(name: ident, +span: span) -> protocol {
|
|
|
|
@protocol_(name, span)
|
|
|
|
}
|
2012-07-16 17:27:04 -07:00
|
|
|
|
2012-07-23 13:50:12 -07:00
|
|
|
class protocol_ {
|
|
|
|
let name: ident;
|
2012-07-24 16:58:48 -07:00
|
|
|
let span: span;
|
2012-07-23 13:50:12 -07:00
|
|
|
let states: dvec<state>;
|
|
|
|
|
|
|
|
let mut bounded: option<bool>;
|
|
|
|
|
2012-07-24 16:58:48 -07:00
|
|
|
new(name: ident, span: span) {
|
2012-07-23 13:50:12 -07:00
|
|
|
self.name = name;
|
2012-07-24 16:58:48 -07:00
|
|
|
self.span = span;
|
2012-07-23 13:50:12 -07:00
|
|
|
self.states = dvec();
|
|
|
|
self.bounded = none;
|
2012-07-16 17:27:04 -07:00
|
|
|
}
|
|
|
|
|
2012-07-23 13:50:12 -07:00
|
|
|
/// Get a state.
|
2012-07-16 17:27:04 -07:00
|
|
|
fn get_state(name: ident) -> state {
|
|
|
|
self.states.find(|i| i.name == name).get()
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:03:27 -07:00
|
|
|
fn get_state_by_id(id: uint) -> state { self.states[id] }
|
|
|
|
|
2012-07-16 17:27:04 -07:00
|
|
|
fn has_state(name: ident) -> bool {
|
|
|
|
self.states.find(|i| i.name == name) != none
|
|
|
|
}
|
|
|
|
|
2012-07-23 13:50:12 -07:00
|
|
|
fn filename() -> ~str {
|
|
|
|
~"proto://" + *self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn num_states() -> uint { self.states.len() }
|
|
|
|
|
2012-07-23 18:50:53 -07:00
|
|
|
fn has_ty_params() -> bool {
|
|
|
|
for self.states.each |s| {
|
|
|
|
if s.ty_params.len() > 0 {
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
fn is_bounded() -> bool {
|
|
|
|
let bounded = self.bounded.get();
|
2012-07-24 16:58:48 -07:00
|
|
|
bounded
|
|
|
|
//if bounded && self.has_ty_params() {
|
|
|
|
// #debug("protocol %s has is bounded, but type parameters\
|
|
|
|
// are not yet supported.",
|
|
|
|
// *self.name);
|
|
|
|
// false
|
|
|
|
//}
|
|
|
|
//else { bounded }
|
2012-07-23 18:50:53 -07:00
|
|
|
}
|
2012-07-23 13:50:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl methods for protocol {
|
|
|
|
fn add_state(name: ident, dir: direction) -> state {
|
|
|
|
self.add_state_poly(name, dir, ~[])
|
|
|
|
}
|
|
|
|
|
2012-07-16 17:27:04 -07:00
|
|
|
fn add_state_poly(name: ident, dir: direction,
|
|
|
|
+ty_params: ~[ast::ty_param]) -> state {
|
|
|
|
let messages = dvec();
|
|
|
|
|
|
|
|
let state = state_(@{
|
2012-07-17 17:03:27 -07:00
|
|
|
id: self.states.len(),
|
2012-07-16 17:27:04 -07:00
|
|
|
name: name,
|
2012-07-24 16:58:48 -07:00
|
|
|
span: self.span,
|
2012-07-16 17:27:04 -07:00
|
|
|
dir: dir,
|
|
|
|
ty_params: ty_params,
|
|
|
|
messages: messages,
|
|
|
|
proto: self
|
|
|
|
});
|
|
|
|
|
|
|
|
self.states.push(state);
|
|
|
|
state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait visitor<Tproto, Tstate, Tmessage> {
|
|
|
|
fn visit_proto(proto: protocol, st: &[Tstate]) -> Tproto;
|
|
|
|
fn visit_state(state: state, m: &[Tmessage]) -> Tstate;
|
2012-07-24 16:58:48 -07:00
|
|
|
fn visit_message(name: ident, spane: span, tys: &[@ast::ty],
|
2012-07-16 17:27:04 -07:00
|
|
|
this: state, next: next_state) -> Tmessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit<Tproto, Tstate, Tmessage, V: visitor<Tproto, Tstate, Tmessage>>(
|
|
|
|
proto: protocol, visitor: V) -> Tproto {
|
|
|
|
|
|
|
|
// the copy keywords prevent recursive use of dvec
|
|
|
|
let states = do (copy proto.states).map_to_vec |s| {
|
|
|
|
let messages = do (copy s.messages).map_to_vec |m| {
|
2012-07-24 16:58:48 -07:00
|
|
|
let message(name, span, tys, this, next) = m;
|
|
|
|
visitor.visit_message(name, span, tys, this, next)
|
2012-07-16 17:27:04 -07:00
|
|
|
};
|
|
|
|
visitor.visit_state(s, messages)
|
|
|
|
};
|
|
|
|
visitor.visit_proto(proto, states)
|
|
|
|
}
|