Skip to content

Commit 9580e7d

Browse files
Fully refactored the type system
1 parent 46864ca commit 9580e7d

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

thrust-parser/src/lib.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern crate syntax;
1010

1111
use std::char;
1212

13-
#[derive(Debug, PartialEq, Eq)]
13+
#[derive(Debug, PartialEq, Eq, Clone)]
1414
pub enum Ty {
1515
String,
1616
Void,
@@ -24,6 +24,7 @@ pub enum Ty {
2424
List(Box<Ty>),
2525
Set(Box<Ty>),
2626
Map(Box<Ty>, Box<Ty>),
27+
Option(Box<Ty>),
2728
// User-defined type.
2829
Ident(String)
2930
}
@@ -57,6 +58,10 @@ impl Ty {
5758
&Ty::I32 => quote_ty!(cx, i32),
5859
&Ty::I64 => quote_ty!(cx, i64),
5960
&Ty::Double => quote_ty!(cx, f64),
61+
&Ty::Option(ref t) => {
62+
let inner = t.to_ast(cx);
63+
quote_ty!(cx, Option<$inner>)
64+
},
6065
&Ty::List(ref s) => {
6166
let inner = s.to_ast(cx);
6267
quote_ty!(cx, Vec<$inner>)
@@ -114,20 +119,13 @@ impl Ast for Service {
114119
let mut inputs = vec![
115120
ast::Arg::new_self(span, ast::Mutability::Immutable, self_ident.clone())
116121
];
117-
let ty = cx.ty_ident(span, token::str_to_ident(match &*method.ty {
118-
"void" => "()",
119-
"string" => "String",
120-
ty => ty
121-
}));
122+
let ty = method.ty.to_ast(cx);
122123

123124
for arg in method.args.iter() {
124125
let arg_ident = token::str_to_ident(&arg.ident);
126+
let arg_ty = arg.ty.to_ast(cx);
125127
inputs.push(
126-
cx.arg(span, arg_ident, cx.ty_ident(span, token::str_to_ident(match &*method.ty {
127-
"void" => "()",
128-
"string" => "String",
129-
ty => ty
130-
})))
128+
cx.arg(span, arg_ident, arg_ty)
131129
);
132130
}
133131

@@ -179,7 +177,7 @@ impl Ast for Service {
179177
#[derive(Debug, PartialEq, Eq)]
180178
pub struct ServiceMethod {
181179
ident: String,
182-
ty: String,
180+
ty: Ty,
183181
attr: FieldAttribute,
184182
args: Vec<StructField>
185183
}
@@ -257,20 +255,20 @@ impl Ast for Struct {
257255

258256
for node in self.fields.iter() {
259257
let span = cx.call_site();
260-
let mut ty = map_ty(&node.ty);
258+
let mut ty = node.ty.clone();
261259

262260
match node.attr {
263261
FieldAttribute::Required => {},
264262
// XXX: We need to map the inner `node.ty` to a proper Rust type.
265-
FieldAttribute::Optional => ty = map_ty(&format!("Option<{}>", node.ty)),
263+
FieldAttribute::Optional => ty = Ty::Option(Box::new(ty)),
266264
_ => panic!("Oneway is not supported for struct fields.")
267265
}
268266

269267
let field = ast::StructField {
270268
node: ast::StructField_ {
271269
kind: ast::StructFieldKind::NamedField(token::str_to_ident(&node.ident), ast::Visibility::Public),
272270
id: ast::DUMMY_NODE_ID,
273-
ty: cx.ty_ident(span, ty),
271+
ty: ty.to_ast(cx),
274272
attrs: Vec::new()
275273
},
276274
span: span
@@ -322,7 +320,7 @@ pub enum FieldAttribute {
322320
pub struct StructField {
323321
seq: i16,
324322
attr: FieldAttribute,
325-
ty: String,
323+
ty: Ty,
326324
ident: String
327325
}
328326

@@ -472,7 +470,7 @@ impl<'a> Parser<'a> {
472470
return Err(Error::MissingFieldAttribute);
473471
};
474472

475-
let ty = self.parse_ident()?;
473+
let ty = self.parse_ty()?;
476474
let ident = self.parse_ident()?;
477475

478476
Ok(StructField {
@@ -576,7 +574,7 @@ impl<'a> Parser<'a> {
576574
FieldAttribute::Required
577575
};
578576

579-
let method_ty = self.parse_ident()?;
577+
let method_ty = self.parse_ty()?;
580578
let method_ident = self.parse_ident()?;
581579
let mut method_fields = Vec::new();
582580

@@ -589,7 +587,7 @@ impl<'a> Parser<'a> {
589587

590588
let seq = self.parse_number()?;
591589
self.expect(&Token::Colon)?;
592-
let field_ty = self.parse_ident()?;
590+
let field_ty = self.parse_ty()?;
593591
let field_ident = self.parse_ident()?;
594592

595593
method_fields.push(StructField {
@@ -1186,7 +1184,7 @@ mod tests {
11861184
assert_eq!(&*def.ident, "Flock");
11871185
assert_eq!(def.methods.len(), 1);
11881186
assert_eq!(&*def.methods[0].ident, "ping");
1189-
assert_eq!(&*def.methods[0].ty, "void");
1187+
assert_eq!(def.methods[0].ty, Ty::Void);
11901188
assert_eq!(def.methods[0].attr, FieldAttribute::Required);
11911189
assert_eq!(def.methods[0].args.len(), 0);
11921190
}
@@ -1200,13 +1198,13 @@ mod tests {
12001198
assert_eq!(&*def.ident, "Beans");
12011199
assert_eq!(def.methods.len(), 1);
12021200
assert_eq!(&*def.methods[0].ident, "poutine");
1203-
assert_eq!(&*def.methods[0].ty, "void");
1201+
assert_eq!(def.methods[0].ty, Ty::Void);
12041202
assert_eq!(def.methods[0].attr, FieldAttribute::Required);
12051203
assert_eq!(def.methods[0].args.len(), 1);
12061204
assert_eq!(def.methods[0].args[0], StructField {
12071205
seq: 1,
12081206
attr: FieldAttribute::Required,
1209-
ty: "string".to_string(),
1207+
ty: Ty::String,
12101208
ident: "firstName".to_string()
12111209
});
12121210
}
@@ -1220,7 +1218,7 @@ mod tests {
12201218
assert_eq!(&*def.ident, "Flock");
12211219
assert_eq!(def.methods.len(), 1);
12221220
assert_eq!(&*def.methods[0].ident, "ping");
1223-
assert_eq!(&*def.methods[0].ty, "void");
1221+
assert!(def.methods[0].ty == Ty::Void);
12241222
assert_eq!(def.methods[0].attr, FieldAttribute::Oneway);
12251223
assert_eq!(def.methods[0].args.len(), 0);
12261224
}
@@ -1264,7 +1262,7 @@ mod tests {
12641262
let mut p = Parser::new("1: optional i32 foobar");
12651263
let def = p.parse_struct_field().unwrap();
12661264
assert_eq!(&*def.ident, "foobar");
1267-
assert_eq!(&*def.ty, "i32");
1265+
assert_eq!(def.ty, Ty::I32);
12681266
assert_eq!(def.seq, 1);
12691267
assert_eq!(def.attr, FieldAttribute::Optional);
12701268
}
@@ -1274,7 +1272,7 @@ mod tests {
12741272
let mut p = Parser::new("1: required i32 foobar");
12751273
let def = p.parse_struct_field().unwrap();
12761274
assert_eq!(&*def.ident, "foobar");
1277-
assert_eq!(&*def.ty, "i32");
1275+
assert_eq!(def.ty, Ty::I32);
12781276
assert_eq!(def.seq, 1);
12791277
assert_eq!(def.attr, FieldAttribute::Required);
12801278
}

0 commit comments

Comments
 (0)