diff --git a/Cargo.lock b/Cargo.lock index f0823962..f3ff2c34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -824,7 +824,7 @@ dependencies = [ [[package]] name = "pilota-thrift-parser" -version = "0.11.0" +version = "0.11.1" dependencies = [ "nom", ] diff --git a/pilota-thrift-parser/Cargo.toml b/pilota-thrift-parser/Cargo.toml index 9bbde99a..6a682ab8 100644 --- a/pilota-thrift-parser/Cargo.toml +++ b/pilota-thrift-parser/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pilota-thrift-parser" -version = "0.11.0" +version = "0.11.1" edition = "2021" description = "Pilota thrift Parser." documentation = "https://docs.rs/pilota" diff --git a/pilota-thrift-parser/src/parser/mod.rs b/pilota-thrift-parser/src/parser/mod.rs index c5a0abb7..da1e5578 100644 --- a/pilota-thrift-parser/src/parser/mod.rs +++ b/pilota-thrift-parser/src/parser/mod.rs @@ -22,7 +22,7 @@ use std::sync::Arc; use nom::{ branch::alt, bytes::complete::{tag, take_till, take_until}, - character::complete::{multispace1, one_of}, + character::complete::{multispace1, one_of, satisfy}, combinator::{map, opt}, multi::{many0, many1, separated_list1}, sequence::{preceded, terminated, tuple}, @@ -63,3 +63,7 @@ fn comment(input: &str) -> IResult<&str, &str> { pub(crate) fn blank(input: &str) -> IResult<&str, ()> { map(many1(alt((comment, multispace1))), |_| ())(input) } + +pub(crate) fn alphanumeric_or_underscore(input: &str) -> IResult<&str, char> { + satisfy(|c: char| c.is_alphanumeric() || c == '_')(input) +} diff --git a/pilota-thrift-parser/src/parser/namespace.rs b/pilota-thrift-parser/src/parser/namespace.rs index e9681361..9540ca16 100644 --- a/pilota-thrift-parser/src/parser/namespace.rs +++ b/pilota-thrift-parser/src/parser/namespace.rs @@ -46,8 +46,8 @@ impl Parser for Scope { tag("netstd"), tag("perl"), tag("php"), - tag("py"), tag("py.twisted"), + tag("py"), tag("rb"), tag("st"), tag("xsd"), diff --git a/pilota-thrift-parser/src/parser/struct_.rs b/pilota-thrift-parser/src/parser/struct_.rs index c1921858..ca13ec21 100644 --- a/pilota-thrift-parser/src/parser/struct_.rs +++ b/pilota-thrift-parser/src/parser/struct_.rs @@ -98,4 +98,13 @@ mod tests { }"#; Struct::parse(str).unwrap(); } + + #[test] + fn test_ty() { + let str = r#"struct Test { + 1: required string(pilota.annotation="test") Service, // required service + 2: required bytet_i.Injection Injection, + }"#; + Struct::parse(str).unwrap(); + } } diff --git a/pilota-thrift-parser/src/parser/ty.rs b/pilota-thrift-parser/src/parser/ty.rs index 318eb4f0..b2f1d7bd 100644 --- a/pilota-thrift-parser/src/parser/ty.rs +++ b/pilota-thrift-parser/src/parser/ty.rs @@ -1,9 +1,10 @@ use std::sync::Arc; use nom::{ + self, branch::{alt, permutation}, bytes::complete::tag, - combinator::{map, opt}, + combinator::{map, not, opt, peek}, sequence::{preceded, tuple}, IResult, }; @@ -40,17 +41,50 @@ impl Parser for CppType { impl Parser for Ty { fn parse(input: &str) -> IResult<&str, Ty> { alt(( - map(tag("string"), |_| Ty::String), - map(tag("void"), |_| Ty::Void), - map(tag("byte"), |_| Ty::Byte), - map(tag("bool"), |_| Ty::Bool), - map(tag("binary"), |_| Ty::Binary), - map(tag("i8"), |_| Ty::I8), - map(tag("i16"), |_| Ty::I16), - map(tag("i32"), |_| Ty::I32), - map(tag("i64"), |_| Ty::I64), - map(tag("double"), |_| Ty::Double), - map(tag("uuid"), |_| Ty::Uuid), + map( + tuple((tag("string"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::String, + ), + map( + tuple((tag("void"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::Void, + ), + map( + tuple((tag("byte"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::Byte, + ), + map( + tuple((tag("bool"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::Bool, + ), + map( + tuple((tag("binary"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::Binary, + ), + map( + tuple((tag("i8"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::I8, + ), + map( + tuple((tag("i16"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::I16, + ), + map( + tuple((tag("i32"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::I32, + ), + map( + tuple((tag("i64"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::I64, + ), + map( + tuple((tag("double"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::Double, + ), + map( + tuple((tag("uuid"), peek(not(alphanumeric_or_underscore)))), + |_| Ty::Uuid, + ), map( tuple(( tag("list"),