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

chore(codegen): error to prevent undesired behavior #303

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions logos-codegen/src/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ impl TryFrom<Hir> for Mir {
}

let kind = repetition.kind;
let is_dot = *repetition.hir == Hir::dot(!repetition.hir.is_always_utf8());
maciejhirsz marked this conversation as resolved.
Show resolved Hide resolved
let mir = Mir::try_from(*repetition.hir)?;

match kind {
RepetitionKind::ZeroOrMore | RepetitionKind::OneOrMore if is_dot => {
Err(r#"#[regex]: ".+" and ".*" regexes will always match everything, which is most propably not what you want. If you are looking to match everything until a specific character, you should use a capturing group. E.g., use regex r"\([^\}]\)" to match anything in between two parentheses. Read more about that here: https://github.com/maciejhirsz/logos/issues/302#issuecomment-1521342541."#.into())
maciejhirsz marked this conversation as resolved.
Show resolved Hide resolved
}
maciejhirsz marked this conversation as resolved.
Show resolved Hide resolved
RepetitionKind::ZeroOrOne => Ok(Mir::Maybe(Box::new(mir))),
RepetitionKind::ZeroOrMore => Ok(Mir::Loop(Box::new(mir))),
RepetitionKind::OneOrMore => {
Expand Down
84 changes: 84 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,87 @@
//! ```compile_fail
//! use logos::Logos;
//! use logos_derive::Logos;
//!
//! #[derive(Logos)]
//! enum Token {
//! #[token(b"\xFF")]
//! NonUtf8,
//! }
//!
//! fn main() {
//! Token::lexer("This shouldn't work with a string literal!");
//! }
//! ```
//!
//! Same, but with regex:
//!
//! ```compile_fail
//! use logos::Logos;
//! use logos_derive::Logos;
//!
//! #[derive(Logos)]
//! enum Token {
//! #[regex(b"\xFF")]
//! NonUtf8,
//! }
//!
//! fn main() {
//! Token::lexer("This shouldn't work with a string literal!");
//! }
//! ```
//!
//! Matching against .* (or .+) should fail to compile:
//!
//! ```compile_fail
//! use logos::Logos;
//! use logos_derive::Logos;
//!
//! #[derive(Logos, Debug, PartialEq)]
//! enum Token {
//! #[regex(r"\(.*\)")]
//! BetweenParen,
//!
//! }
//! ```
//!
//! ```compile_fail
//! use logos::Logos;
//! use logos_derive::Logos;
//!
//! #[derive(Logos, Debug, PartialEq)]
//! enum Token {
//! #[regex(r"\(.+\)")]
//! BetweenParen,
//!
//! }
//! ```
//!
//! And also when working with bytes:
//!
//! ```compile_fail
//! use logos::Logos;
//! use logos_derive::Logos;
//!
//! #[derive(Logos, Debug, PartialEq)]
//! enum Token {
//! #[regex(b"\x00.*")]
//! NonUtf8,
//!
//! }
//! ```
//!
//! ```compile_fail
//! use logos::Logos;
//! use logos_derive::Logos;
//!
//! #[derive(Logos, Debug, PartialEq)]
//! enum Token {
//! #[regex(b"\x00.+")]
//! NonUtf8,
//!
//! }
//! ```

use logos::source::Source;
use logos::Logos;

Expand Down
31 changes: 0 additions & 31 deletions tests/tests/binary.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,3 @@
//! ```compile_fail
//! use logos::Logos;
//! use logos_derive::Logos;
//!
//! #[derive(Logos)]
//! enum Token {
//! #[token(b"\xFF")]
//! NonUtf8,
//! }
//!
//! fn main() {
//! Token::lexer("This shouldn't work with a string literal!");
//! }
//! ```
//! Same, but with regex:
//!
//! ```compile_fail
//! use logos::Logos;
//! use logos_derive::Logos;
//!
//! #[derive(Logos)]
//! enum Token {
//! #[regex(b"\xFF")]
//! NonUtf8,
//! }
//!
//! fn main() {
//! Token::lexer("This shouldn't work with a string literal!");
//! }
//! ```

use logos_derive::Logos;
use tests::assert_lex;

Expand Down