Skip to content

Commit

Permalink
parse (defunct?) decl version numbers if present
Browse files Browse the repository at this point in the history
  • Loading branch information
timbod7 committed Aug 15, 2022
1 parent aa5720c commit 9acb077
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
18 changes: 16 additions & 2 deletions rust/compiler/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use nom::{
character::{
complete::{
satisfy,
digit1,
}
},
multi::{
Expand All @@ -25,7 +26,7 @@ use nom::{
map,
value,
},
number::complete::{double},
number::complete::{double, u64},
sequence::{
pair,
delimited,
Expand Down Expand Up @@ -331,6 +332,7 @@ pub fn decl_type(i: Input) -> Res<Input,(Spanned<&str>,adlast::DeclType<TypeExpr
pub fn struct_(i: Input) -> Res<Input,(Spanned<&str>,adlast::Struct<TypeExpr0>)> {
let (i,_) = ws(tag("struct"))(i)?;
let (i,name) = ws(spanned(ident0))(i)?;
let (i,_) = oversion(i)?;
let (i,type_params) = type_params(i)?;
let (i,fields) =
delimited(
Expand All @@ -350,10 +352,10 @@ pub fn struct_(i: Input) -> Res<Input,(Spanned<&str>,adlast::Struct<TypeExpr0>)>
Ok((i,(name,struct_)))
}


pub fn union(i: Input) -> Res<Input,(Spanned<&str>,adlast::Union<TypeExpr0>)> {
let (i,_) = wtag("union")(i)?;
let (i,name) = ws(spanned(ident0))(i)?;
let (i,_) = oversion(i)?;
let (i,type_params) = type_params(i)?;
let (i,fields) =
delimited(
Expand All @@ -376,6 +378,7 @@ pub fn union(i: Input) -> Res<Input,(Spanned<&str>,adlast::Union<TypeExpr0>)> {
pub fn typedef(i: Input) -> Res<Input,(Spanned<&str>,adlast::TypeDef<TypeExpr0>)> {
let (i,_) = wtag("type")(i)?;
let (i,name) = ws(spanned(ident0))(i)?;
let (i,_) = oversion(i)?;
let (i,type_params) = type_params(i)?;
let (i,type_expr) =
preceded(
Expand All @@ -392,6 +395,7 @@ pub fn typedef(i: Input) -> Res<Input,(Spanned<&str>,adlast::TypeDef<TypeExpr0>)
pub fn newtype(i: Input) -> Res<Input,(Spanned<&str>,adlast::NewType<TypeExpr0>)> {
let (i,_) = ws(tag("newtype"))(i)?;
let (i,name) = ws(spanned(ident0))(i)?;
let (i,_) = oversion(i)?;
let (i,type_params) = type_params(i)?;
let (i,type_expr) = preceded(
wtag("="),
Expand All @@ -412,6 +416,16 @@ pub fn newtype(i: Input) -> Res<Input,(Spanned<&str>,adlast::NewType<TypeExpr0>)
Ok((i,(name,newtype)))
}

fn oversion(i: Input) -> Res<Input,Option<u64>> {
opt(oversion_)(i)
}

fn oversion_(i: Input) -> Res<Input,u64> {
let (i,_) = ws(tag("#"))(i)?;
let (i,ds) = ws(digit1)(i)?;
Ok((i, ds.parse::<u64>().unwrap()))
}

pub fn field(i: Input) -> Res<Input,adlast::Field<TypeExpr0>> {
let (i,annotations) = many0(prefix_annotation)(i)?;
let (i,texpr) = ws(type_expr)(i)?;
Expand Down
36 changes: 22 additions & 14 deletions rust/compiler/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ fn parse_json() {
assert_module_file_ok("../../haskell/compiler/tests/test6/input/test.adl");
assert_module_file_ok("../../haskell/compiler/tests/test7/input/test.adl");
assert_module_file_ok("../../haskell/compiler/tests/test8/input/test.adl");
// assert_module_file_ok("../../haskell/compiler/tests/test9/input/test.adl");
// assert_module_file_ok("../../haskell/compiler/tests/test10/input/test.adl");
assert_module_file_ok("../../haskell/compiler/tests/test9/input/test.adl");
assert_module_file_ok("../../haskell/compiler/tests/test10/input/test.adl");
}

fn inp (s: &str) -> Input {
Expand All @@ -286,22 +286,30 @@ fn parse_json() {

fn assert_parse_eq<T>(pr: Res<Input, T>, v:T)
where T: std::fmt::Debug+PartialEq {
if let Ok((i, pv)) = pr {
assert_eq!(pv, v);
assert!(i.is_empty());
} else {
panic!("Unexpected parse failure" );
match pr {
Ok((i,pv)) => {
assert_eq!(pv, v);
assert!(i.is_empty());
}
Err(e) => {
panic!("Unexpected parse failure: {}", e);

}
}
}

fn assert_parse_eq_2<T>(pr: Res<Input, T>, v:T, remaining: &str)
where T: std::fmt::Debug+PartialEq {
if let Ok((i, pv)) = pr {
assert_eq!(pv, v);
assert_eq!(*i.fragment(), remaining);
} else {
panic!("Unexpected parse failure" );
}
where T: std::fmt::Debug+PartialEq {
match pr {
Ok((i,pv)) => {
assert_eq!(pv, v);
assert_eq!(*i.fragment(), remaining);
}
Err(e) => {
panic!("Unexpected parse failure: {}", e);

}
}
}

fn assert_module_file_ok(path: &str) {
Expand Down

0 comments on commit 9acb077

Please sign in to comment.