Skip to content

Commit

Permalink
fix: exact match type for the parser (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione authored May 20, 2024
1 parent 1af36fd commit 87fb28f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pilota-thrift-parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 5 additions & 1 deletion pilota-thrift-parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pilota-thrift-parser/src/parser/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
9 changes: 9 additions & 0 deletions pilota-thrift-parser/src/parser/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
58 changes: 46 additions & 12 deletions pilota-thrift-parser/src/parser/ty.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 87fb28f

Please sign in to comment.