Skip to content

Commit

Permalink
Fix compilation errors and spurious grammar ambiguity
Browse files Browse the repository at this point in the history
  • Loading branch information
yannham committed Nov 20, 2024
1 parent 3694dca commit f1da1dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
22 changes: 0 additions & 22 deletions core/src/bytecode/ast/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,6 @@ pub struct FieldPattern<'ast> {
pub pos: TermPos,
}

/// The last match in a data structure pattern. This can either be a normal match, or an ellipsis
/// which can capture the rest of the data structure. The type parameter `P` is the type of the
/// pattern of the data structure: currently, ellipsis matches are only supported for record, but
/// we'll probably support them for arrays as well.
///
/// This enum is mostly used during parsing.
///
/// # Example
///
/// - In `{foo={}, bar}`, the last match is an normal match.
/// - In `{foo={}, bar, ..}`, the last match is a non-capturing ellipsis.
/// - In `{foo={}, bar, ..rest}`, the last match is a capturing ellipsis.
#[derive(Debug, PartialEq, Clone)]
pub enum PatternTail<'ast, P> {
/// The last field is a normal match. In this case the pattern is "closed" so every record
/// fields should be matched.
Normal(&'ast P),
/// The pattern is "open" `, ..}`. Optionally you can bind a record containing the remaining
/// fields to an `Identifier` using the syntax `, ..y}`.
Ellipsis(Option<LocIdent>),
}

/// A record pattern.
#[derive(Debug, PartialEq, Clone)]
pub struct RecordPattern<'ast> {
Expand Down
20 changes: 11 additions & 9 deletions core/src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ RepeatSep<Rule, Sep>: Vec<Rule> = <mut elems: (<Rule> Sep)*> <last: Rule?> => {
elems
};

// Same as `RepeatSep`, but repeat the rule at least once (one or more), instead of zero or
// more.
RepeatSep1<Rule, Sep>: Vec<Rule> = <mut elems: (Sep <Rule>)*> <last: Rule> Sep? => {
// Same as `RepeatSep`, but repeat the rule at least once (one or more), instead
// of zero or more.
RepeatSep1<Rule, Sep>: Vec<Rule> = <mut elems: (<Rule> Sep)*> <last: Rule> Sep? => {
elems.push(last);
elems
};
Expand Down Expand Up @@ -934,12 +934,14 @@ FieldPattern: FieldPattern<'ast> = {
},
};

// Last field pattern of a record pattern. We need this rule (together with
// `LastElemPat`) combining both a field and a potential ellipsis because
// putting the ellipsis in a separate rule AND handling the case of zero fields
// (`{..}`) isn't possible: the fact that the ellipsis will need a "," separator
// before it will depend on the presence of zero or more fields. A stand-alone
// ellipsis rule would have no way to know that.
// Last field pattern of a record pattern.
//
// We need this rule (together with `LastElemPat`) combining both a last field
// or a potential ellipsis because putting the ellipsis in a separate rule AND
// handling the case of zero fields (`{..}`) isn't possible: the fact that the
// ellipsis will need a "," separator before depends on the presence of zero or
// more fields, but a stand-alone ellipsis rule has no way to get this
// information about previous match.
LastFieldPat: LastPattern<FieldPattern<'ast>> = {
FieldPattern => LastPattern::Normal(<>),
".." <Ident?> => LastPattern::Ellipsis(<>),
Expand Down
19 changes: 19 additions & 0 deletions core/src/parser/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ pub enum RecordLastField<'ast> {
Ellipsis,
}

/// The last match in a data structure pattern. This can either be a normal match, or an ellipsis
/// which can capture the rest of the data structure. The type parameter `P` is the type of the
/// pattern of the data structure (ellipsis are supported for both array and record patterns).
///
/// # Example
///
/// - In `{foo={}, bar}`, the last match is an normal match.
/// - In `{foo={}, bar, ..}`, the last match is a non-capturing ellipsis.
/// - In `{foo={}, bar, ..rest}`, the last match is a capturing ellipsis.
#[derive(Debug, PartialEq, Clone)]
pub enum LastPattern<P> {
/// The last field is a normal match. In this case the pattern is "closed" so every record
/// fields should be matched.
Normal(P),
/// The pattern is "open" `, ..}`. Optionally you can bind a record containing the remaining
/// fields to an `Identifier` using the syntax `, ..y}`.
Ellipsis(Option<LocIdent>),
}

/// Trait for operators that can be eta-expanded to a function.
pub(super) trait EtaExpand {
/// Eta-expand an operator. This wraps an operator, for example `==`, as a function `fun x1 x2
Expand Down

0 comments on commit f1da1dd

Please sign in to comment.