diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index be4e62d..040b59c 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -789,6 +789,27 @@ mod tests { ); } + // https://github.com/alexfertel/bulloak/issues/54 + #[test] + fn parses_top_level_actions() { + assert_eq!( + parse( + r#"Foo +└── It reverts when X."# + ) + .unwrap(), + Ast::Root(Root { + contract_name: String::from("Foo"), + span: s(p(0, 1, 1), p(31, 2, 22)), + children: vec![Ast::Action(Action { + title: String::from("It reverts when X."), + span: s(p(4, 2, 1), p(31, 2, 22)), + children: vec![] + })], + }) + ); + } + #[test] fn unsanitized_input() { assert_eq!( diff --git a/src/syntax/tokenizer.rs b/src/syntax/tokenizer.rs index d93ff93..edaf5d7 100644 --- a/src/syntax/tokenizer.rs +++ b/src/syntax/tokenizer.rs @@ -83,6 +83,15 @@ pub struct Token { pub lexeme: String, } +impl Token { + fn is_branch(&self) -> bool { + match self.kind { + TokenKind::Tee | TokenKind::Corner => true, + TokenKind::Word | TokenKind::When | TokenKind::Given | TokenKind::It => false, + } + } +} + impl fmt::Debug for Token { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -329,7 +338,10 @@ impl<'s, T: Borrow> TokenizerI<'s, T> { } _ => { let token = self.scan_word()?; - if token.kind == TokenKind::When || token.kind == TokenKind::Given { + let last_is_branch = tokens.last().is_some_and(Token::is_branch); + if last_is_branch + && (token.kind == TokenKind::When || token.kind == TokenKind::Given) + { self.enter_identifier_mode(); }; tokens.push(token);