Skip to content

Commit

Permalink
parser fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
timbod7 committed Aug 4, 2022
1 parent 92233db commit 9f5511f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
16 changes: 8 additions & 8 deletions rust/compiler/src/adlgen/sys/adlast2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<R> TypeExpr<R> {
}
}

#[derive(Deserialize,PartialEq,Serialize)]
#[derive(Debug,Deserialize,PartialEq,Serialize)]
pub struct Field<TE> {
pub name: Ident,

Expand All @@ -84,7 +84,7 @@ impl<TE> Field<TE> {
}
}

#[derive(Deserialize,PartialEq,Serialize)]
#[derive(Debug,Deserialize,PartialEq,Serialize)]
pub struct Struct<TE> {
#[serde(rename="typeParams")]
pub type_params: Vec<Ident>,
Expand All @@ -101,7 +101,7 @@ impl<TE> Struct<TE> {
}
}

#[derive(Deserialize,PartialEq,Serialize)]
#[derive(Debug,Deserialize,PartialEq,Serialize)]
pub struct Union<TE> {
#[serde(rename="typeParams")]
pub type_params: Vec<Ident>,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl<TE> TypeDef<TE> {
}
}

#[derive(Deserialize,PartialEq,Serialize)]
#[derive(Debug,Deserialize,PartialEq,Serialize)]
pub struct NewType<TE> {
#[serde(rename="typeParams")]
pub type_params: Vec<Ident>,
Expand All @@ -157,7 +157,7 @@ impl<TE> NewType<TE> {
}
}

#[derive(Deserialize,PartialEq,Serialize)]
#[derive(Debug,Deserialize,PartialEq,Serialize)]
pub enum DeclType<TE> {
#[serde(rename="struct_")]
Struct(Struct<TE>),
Expand All @@ -172,7 +172,7 @@ pub enum DeclType<TE> {
Newtype(NewType<TE>),
}

#[derive(Deserialize,PartialEq,Serialize)]
#[derive(Debug,Deserialize,PartialEq,Serialize)]
pub struct Decl<TE> {
pub name: Ident,

Expand All @@ -195,7 +195,7 @@ impl<TE> Decl<TE> {
}
}

#[derive(Deserialize,PartialEq,Serialize)]
#[derive(Debug,Deserialize,PartialEq,Serialize)]
pub struct ScopedDecl<TE> {
#[serde(rename="moduleName")]
pub module_name: ModuleName,
Expand Down Expand Up @@ -223,7 +223,7 @@ pub enum Import {
ScopedName(ScopedName),
}

#[derive(Deserialize,PartialEq,Serialize)]
#[derive(Debug,Deserialize,PartialEq,Serialize)]
pub struct Module<TE> {
pub name: ModuleName,

Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/adlrt/custom/sys/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::result;
use super::mapentry::MapEntry;


#[derive(Clone,Eq,PartialEq)]
#[derive(Clone,Eq,PartialEq, Debug)]
pub struct Map<K:Eq + Hash, V>(HashMap<K, V>);

impl<K:Eq + Hash, V> Map<K, V> {
Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/adlrt/custom/sys/types/mapentry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize};
use serde::{Serialize};

#[derive(Clone,Deserialize,Eq,Hash,PartialEq,Serialize)]
#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub struct MapEntry<K, V> {
#[serde(rename="k")]
pub key: K,
Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/adlrt/custom/sys/types/maybe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::result;
// Maybe<T> is a custom mapping to Option<T>,
// with ADL compatible serialization

#[derive(Clone,Eq,Hash,PartialEq)]
#[derive(Clone,Debug,Eq,Hash,PartialEq)]
pub struct Maybe<T> (Option<T>);

impl<T> Maybe<T> {
Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/adlrt/custom/sys/types/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Deserializer};
use serde::{Serialize, Serializer};
use std::result;

#[derive(Clone,Eq,Hash,PartialEq)]
#[derive(Clone,Debug,Eq,Hash,PartialEq)]
pub struct Pair<A,B> (pub (A,B));

impl<A, B> Pair<A, B> {
Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/adlrt/custom/sys/types/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Deserializer};
use serde::{Serialize, Serializer};


#[derive(Clone,Eq,Hash,PartialEq)]
#[derive(Clone,Debug,Eq,Hash,PartialEq)]
pub struct Result<T, E>(pub std::result::Result<T, E>);

impl<T, E> Result<T, E> {
Expand Down
53 changes: 40 additions & 13 deletions rust/compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ pub fn struct_(i: &str) -> Res<&str,(&str,adlast::Struct<TypeExpr0>)> {
let (i,fields) =
delimited(
wtag("{"),
many0(field),
many0(
terminated(
field,
wtag(";"),
)
),
wtag("}"),
)(i)?;
let struct_ = adlast::Struct{
Expand All @@ -162,7 +167,7 @@ pub fn struct_(i: &str) -> Res<&str,(&str,adlast::Struct<TypeExpr0>)> {


pub fn union(i: &str) -> Res<&str,(&str,adlast::Union<TypeExpr0>)> {
let (i,_) = wtag("struct")(i)?;
let (i,_) = wtag("union")(i)?;
let (i,name) = ws(ident0)(i)?;
let (i,type_params) = type_params(i)?;
let (i,fields) =
Expand Down Expand Up @@ -333,18 +338,15 @@ mod tests {

assert_eq!(
type_expr("a.X"),
Ok(("", adlast::TypeExpr{
type_ref: mk_scoped_name("a", "X"),
parameters: Vec::new()
}))
Ok(("", mk_typeexpr0( mk_scoped_name("a", "X"))))
);

assert_eq!(
type_expr("a.X<y.z.B>"),
Ok(("", adlast::TypeExpr{
type_ref: mk_scoped_name("a", "X"),
parameters: vec![
adlast::TypeExpr::new( mk_scoped_name("y.z", "B"), Vec::new())
mk_typeexpr0(mk_scoped_name("y.z", "B"))
]
}))
);
Expand All @@ -354,8 +356,8 @@ mod tests {
Ok(("", adlast::TypeExpr{
type_ref: mk_scoped_name("a", "X"),
parameters: vec![
adlast::TypeExpr::new( mk_scoped_name("y.z", "B"), Vec::new()),
adlast::TypeExpr::new( mk_scoped_name("", "C"), Vec::new()),
mk_typeexpr0( mk_scoped_name("y.z", "B")),
mk_typeexpr0(mk_scoped_name("", "C")),
]
}))
);
Expand All @@ -365,22 +367,36 @@ mod tests {
fn parse_decl() {

assert_eq!(
decl("struct A { F f1; A<B> f2; }"),
decl("struct A { F f1; G f2; }"),
Ok(("", adlast::Decl{
name: "A".to_string(),
version: crate::adlrt::custom::sys::types::maybe::Maybe::nothing(),
annotations: crate::adlrt::custom::sys::types::map::Map::new(Vec::new()),
version: mk_empty_maybe(),
annotations: mk_empty_annotations(),
r#type: adlast::DeclType::Struct(adlast::Struct{
type_params: Vec::new(),
fields: vec![
adlast::Field{
name: "f1".to_string(),
annotations: mk_empty_annotations(),
default: mk_empty_maybe(),
serialized_name: "f1".to_string(),
type_expr: mk_typeexpr0(mk_scoped_name("", "F")),
},
adlast::Field{
name: "f2".to_string(),
annotations: mk_empty_annotations(),
default: mk_empty_maybe(),
serialized_name: "f2".to_string(),
type_expr: mk_typeexpr0(mk_scoped_name("", "G")),
}
],
}),
})),
)
}

#[test]
fn parse_module() {
fn parse_empty_module() {
let pm = module("module x {\n}");
if let Ok((i, m)) = pm {
assert_eq!( m.name, "x".to_string());
Expand All @@ -392,4 +408,15 @@ mod tests {
fn mk_scoped_name(mname: &str, name: &str) -> adlast::ScopedName {
adlast::ScopedName::new(mname.to_string(), name.to_string())
}

fn mk_typeexpr0(type_ref: adlast::ScopedName) -> adlast::TypeExpr<adlast::ScopedName> {
adlast::TypeExpr{type_ref, parameters: vec![]}
}

fn mk_empty_annotations() -> crate::adlrt::custom::sys::types::map::Map<adlast::ScopedName,serde_json::Value> {
crate::adlrt::custom::sys::types::map::Map::new(Vec::new())
}
fn mk_empty_maybe<T>() -> crate::adlrt::custom::sys::types::maybe::Maybe<T> {
crate::adlrt::custom::sys::types::maybe::Maybe::nothing()
}
}

0 comments on commit 9f5511f

Please sign in to comment.