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

Fix parsing of angle brackets for sorts and bit vectors #940

Merged
merged 1 commit into from
Dec 12, 2024
Merged
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
39 changes: 22 additions & 17 deletions crates/flux-syntax/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,10 @@ ParamMode: surface::ParamMode = {
}

BaseSort: surface::BaseSort = {
"bitvec" "<" <lo:@L> <lit:Lit> <hi:@R> ">" =>? {
if let Ok(width) = lit.symbol.as_str().parse::<usize>() {
Ok(surface::BaseSort::BitVec(width))
} else {
Err(ParseError::User { error: UserParseError::UnexpectedToken(lo, hi) })
}
"bitvec" <width: AngleBrackets<BitVecWidth>> => {
surface::BaseSort::BitVec(width)
},
<segments:Sep1<"::", Ident>> <args:("<" <Comma<BaseSort>> ">")?> => {
<segments:Sep1<"::", Ident>> <args:AngleBrackets<Comma<BaseSort>>?> => {
let path = surface::SortPath {
segments,
args: args.unwrap_or_default(),
Expand All @@ -177,6 +173,16 @@ BaseSort: surface::BaseSort = {
},
}

BitVecWidth: usize = {
<lo:@L> <lit:Lit> <hi:@R> =>? {
if let Ok(width) = lit.symbol.as_str().parse::<usize>() {
Ok(width)
} else {
Err(ParseError::User { error: UserParseError::UnexpectedToken(lo, hi) })
}
}
}

Sort: surface::Sort = {
<base: BaseSort> => surface::Sort::Base(base),
"(" <inputs:Comma<BaseSort>> ")" "->" <output:BaseSort> => surface::Sort::Func { <> },
Expand Down Expand Up @@ -432,15 +438,7 @@ BaseTyKind: surface::BaseTyKind = {
}
}

GenericArgTys: Vec<surface::Ty> = {
"<" <Comma<Ty>> ">",
"<" <Comma<Ty>> ">(?=>)",
}

GenericArgs: Vec<surface::GenericArg> = {
"<" <Comma<GenericArg>> ">",
"<" <Comma<GenericArg>> ">(?=>)",
}
GenericArgs: Vec<surface::GenericArg> = AngleBrackets<Comma<GenericArg>>;

GenericArg: surface::GenericArg = {
<bind:Ident> "=" <ty:Ty> => surface::GenericArg {
Expand Down Expand Up @@ -690,6 +688,13 @@ Ident: surface::Ident = {
}
}

// Something enclosed by angle brackets. This handles the case where there are two adjacent
// angle brackets which we tokenize differently to account for the right shift operator.
AngleBrackets<T>: T = {
"<" <T> ">",
"<" <T> ">(?=>)",
}

Sep<S, T>: Vec<T> = {
<mut v:(<T> S)*> <e:T?> => {
if let Some(e) = e { v.push(e); }
Expand Down Expand Up @@ -781,6 +786,6 @@ extern {
"else" => Token::Else,
"::" => Token::PathSep,
"_" => Token::Underscore,
".." => Token::DotDot,
".." => Token::DotDot,
}
}
Loading