Skip to content

Instantly share code, notes, and snippets.

@MaikKlein
Created May 5, 2018 18:34
Show Gist options
  • Save MaikKlein/c28a82e75141bcf303effb62ebcc354f to your computer and use it in GitHub Desktop.
Save MaikKlein/c28a82e75141bcf303effb62ebcc354f to your computer and use it in GitHub Desktop.
pub struct Types {
pub types: Vec<Type>,
}
impl Types {
pub fn parse(parser: &mut Parser) -> Option<Self> {
{
let event = parser.peek(0)?;
let _ = event.as_start_named("types")?;
}
let _ = parser.consume_start_named("types");
let mut types = Vec::new();
loop {
// loop until we reach the end tag
if parser.is_end_named("types") {
break;
}
// order matters, alias need to parse first
// or_else is used because we only want to have one successful parser
// per iteration
let ty = Alias::parse(parser)
.map(Type::Alias)
.or_else(|| Bitmask::parse(parser).map(Type::Bitmask))
.or_else(|| Primitive::parse(parser).map(Type::Primitive))
.or_else(|| Include::parse(parser).map(Type::Include))
.or_else(|| Enum::parse(parser).map(Type::Enum))
.or_else(|| FunctionPointer::parse(parser).map(Type::FunctionPointer))
.or_else(|| Handle::parse(parser).map(Type::Handle));
if let Some(ty) = ty {
types.push(ty);
} else {
// if none of the parsers parsed anything, and we haven't reached the end
// then we move the parser forward
let _ = parser.next();
}
}
println!("{:#?}", types);
Some(Types { types })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment