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

;&, ;|, ;;& in case command #430

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Parse all types of CaseContinuation in a case item
Previously, the parser only recognized the `;;` terminator for a case
item. This commit changes the parser to recognize all types of
CaseContinuation, including `;&` and `;;&`.
magicant committed Nov 12, 2024
commit c0f55b143cf3ffe9c17b40ff5845f49797fe9800
3 changes: 2 additions & 1 deletion yash-syntax/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -13,7 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `SemicolonAnd`, `SemicolonSemicolonAnd`, and `SemicolonBar` variants
are added to the `parser::lex::Operator` enum.
- The `parser::Parser::case_item` and `syntax::CaseItem::from_str` methods
now consume a trailing terminator token, if any.
now consume a trailing terminator token, if any. The terminator can be
not only `;;`, but also `;&`, `;|`, or `;;&`.

## [0.12.1] - 2024-11-10

29 changes: 25 additions & 4 deletions yash-syntax/src/parser/case.rs
Original file line number Diff line number Diff line change
@@ -22,9 +22,8 @@ use super::core::Result;
use super::error::Error;
use super::error::SyntaxError;
use super::lex::Keyword::{Case, Esac, In};
use super::lex::Operator::{Bar, CloseParen, Newline, OpenParen, SemicolonSemicolon};
use super::lex::Operator::{Bar, CloseParen, Newline, OpenParen};
use super::lex::TokenId::{self, EndOfInput, Operator, Token};
use crate::syntax::CaseContinuation;
use crate::syntax::CaseItem;
use crate::syntax::CompoundCommand;

@@ -102,11 +101,16 @@ impl Parser<'_, '_> {
}

let body = self.maybe_compound_list_boxed().await?;
let continuation = CaseContinuation::default();

let continued = self.peek_token().await?.id == Operator(SemicolonSemicolon);
let continuation = match self.peek_token().await?.id {
Operator(op) => op.try_into().ok(),
_ => None,
};
let continued = continuation.is_some();
let continuation = continuation.unwrap_or_default();
if continued {
self.take_token_raw().await?;
// TODO Reject Continue in strict POSIX mode
}

Ok(Some((
@@ -190,6 +194,7 @@ mod tests {
use crate::alias::{AliasSet, EmptyGlossary, HashEntry};
use crate::source::Location;
use crate::source::Source;
use crate::syntax::CaseContinuation;
use assert_matches::assert_matches;
use futures_util::FutureExt;

@@ -321,6 +326,22 @@ mod tests {
assert_eq!(next.id, EndOfInput);
}

#[test]
fn parser_case_item_with_semicolon_and() {
let mut lexer = Lexer::from_memory("foo);&", Source::Unknown);
let mut parser = Parser::new(&mut lexer, &EmptyGlossary);

let (item, continued) = parser.case_item().now_or_never().unwrap().unwrap().unwrap();
assert_eq!(item.patterns.len(), 1);
assert_eq!(item.patterns[0].to_string(), "foo");
assert_eq!(item.body.0, []);
assert_eq!(item.continuation, CaseContinuation::FallThrough);
assert!(continued);

let next = parser.peek_token().now_or_never().unwrap().unwrap();
assert_eq!(next.id, EndOfInput);
}

#[test]
fn parser_case_item_missing_pattern_without_open_paren() {
let mut lexer = Lexer::from_memory(")", Source::Unknown);