Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zero_or_more_groups: Add more details to results #302

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions wrausmt-format/src/text/parse/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ impl<R: Read> Parser<R> {
/// Attempts to parse a series of items using the provided parse method.
/// The parse method should return 0 or more of the item type.
/// Returns the results as a flattened vector of items.
pub fn zero_or_more_groups<T>(&mut self, parse: ParseGroupFn<Self, T>) -> Result<Vec<T>> {
pub fn zero_or_more_groups<T>(
&mut self,
parse: ParseGroupFn<Self, T>,
) -> Result<(Vec<T>, bool)> {
pctx!(self, "zero or more groups");
let mut result: Vec<T> = vec![];
let mut any = false;
while let Some(t) = parse(self)? {
any = true;
result.extend(t);
}
Ok(result)
Ok((result, any))
}

/// Attempts to parse a series of items using the provided parse method.
Expand Down
14 changes: 7 additions & 7 deletions wrausmt-format/src/text/parse/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
impl<R: Read> Parser<R> {
pub fn parse_instructions(&mut self) -> Result<Vec<Instruction<Unresolved>>> {
pctx!(self, "parse instructiosn");
self.zero_or_more_groups(Self::try_instruction)
Ok(self.zero_or_more_groups(Self::try_instruction)?.0)
}

/// Called at a point where we expect an instruction name keyword
Expand Down Expand Up @@ -63,8 +63,8 @@ impl<R: Read> Parser<R> {
syntax::Operands::BrTable(idxs, last)
}
Operands::Select => {
let results = self.zero_or_more_groups(Self::try_parse_fresult)?;
if results.is_empty() {
let (results, any) = self.zero_or_more_groups(Self::try_parse_fresult)?;
if !any {
syntax::Operands::None
} else {
syntax::Operands::SelectT(results)
Expand Down Expand Up @@ -244,17 +244,17 @@ impl<R: Read> Parser<R> {
let location = self.location();
let label = self.try_id()?;
let typeuse = self.parse_type_use(super::module::FParamId::Forbidden)?;
let condition = self.zero_or_more_groups(Self::try_folded_instruction)?;
let (condition, _) = self.zero_or_more_groups(Self::try_folded_instruction)?;
let mut unfolded = condition;
let thexpr = if self.try_expr_start("then")? {
let instr = self.zero_or_more_groups(Self::try_instruction)?;
let (instr, _) = self.zero_or_more_groups(Self::try_instruction)?;
self.expect_close()?;
UncompiledExpr { instr }
} else {
Err(self.unexpected_token("then"))?
};
let elexpr = if self.try_expr_start("else")? {
let instr = self.zero_or_more_groups(Self::try_instruction)?;
let (instr, _) = self.zero_or_more_groups(Self::try_instruction)?;
self.expect_close()?;
UncompiledExpr { instr }
} else {
Expand Down Expand Up @@ -321,7 +321,7 @@ impl<R: Read> Parser<R> {
None => return Ok(None),
};

let mut rest = self.zero_or_more_groups(Self::try_folded_instruction)?;
let (mut rest, _) = self.zero_or_more_groups(Self::try_folded_instruction)?;

rest.push(first);
self.expect_close()?;
Expand Down
20 changes: 11 additions & 9 deletions wrausmt-format/src/text/parse/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,16 @@ impl<R: Read> Parser<R> {
pub fn try_function_type(&mut self, fparam_id: FParamId) -> Result<FunctionType> {
pctx!(self, "try function type");
Ok(FunctionType {
params: self.zero_or_more_groups(match fparam_id {
// Using closures makes the combinators a lot more complicated.
// For this use case, it's simpler to just create variants for the
// two types of FParam variants.
FParamId::Allowed => Self::try_parse_fparam_id_allowed,
FParamId::Forbidden => Self::try_parse_fparam_id_forbidden,
})?,
results: self.zero_or_more_groups(Self::try_parse_fresult)?,
params: self
.zero_or_more_groups(match fparam_id {
// Using closures makes the combinators a lot more complicated.
// For this use case, it's simpler to just create variants for the
// two types of FParam variants.
FParamId::Allowed => Self::try_parse_fparam_id_allowed,
FParamId::Forbidden => Self::try_parse_fparam_id_forbidden,
})?
.0,
results: self.zero_or_more_groups(Self::try_parse_fresult)?.0,
})
}

Expand Down Expand Up @@ -227,7 +229,7 @@ impl<R: Read> Parser<R> {
})));
}

let locals = self.zero_or_more_groups(Self::try_locals)?;
let (locals, _) = self.zero_or_more_groups(Self::try_locals)?;

let instr = self.parse_instructions()?;
self.expect_close()?;
Expand Down
Loading