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 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
1 change: 1 addition & 0 deletions logos-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ syn = { version = "2.0.13", features = ["full"] }
quote = "1.0.3"
proc-macro2 = "1.0.9"
regex-syntax = "0.6"
lazy_static = "1.4.0"

[dev-dependencies]
pretty_assertions = "0.6.1"
26 changes: 26 additions & 0 deletions logos-codegen/src/mir.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use std::convert::TryFrom;

use lazy_static::lazy_static;
use regex_syntax::hir::{Hir, HirKind, RepetitionKind, RepetitionRange};
use regex_syntax::ParserBuilder;

pub use regex_syntax::hir::{Class, ClassUnicode, Literal};

use crate::error::{Error, Result};

lazy_static! {
/// DOT regex that matches utf8 only.
static ref DOT_UTF8: Hir = Hir::dot(false);

/// DOT regex that matches any byte.
static ref DOT_BYTES: Hir = Hir::dot(true);
}

/// Middle Intermediate Representation of the regex, built from
/// `regex_syntax`'s `Hir`. The goal here is to strip and canonicalize
/// the tree, so that we don't have to do transformations later on the
Expand Down Expand Up @@ -110,9 +119,26 @@ impl TryFrom<Hir> for Mir {
}

let kind = repetition.kind;
let is_dot = if repetition.hir.is_always_utf8() {
*repetition.hir == *DOT_UTF8
} else {
*repetition.hir == *DOT_BYTES
};
let mir = Mir::try_from(*repetition.hir)?;

match kind {
RepetitionKind::ZeroOrMore | RepetitionKind::OneOrMore if is_dot => {
Err(
"#[regex]: \".+\" and \".*\" patterns will greedily consume \
the entire source till the end as Logos does not allow \
backtracking. If you are looking to match everything until \
a specific character, you should use a negative character \
class. E.g., use regex r\"'[^']*'\" to match anything in \
between two quotes. Read more about that here: \
https://github.com/maciejhirsz/logos/issues/302#issuecomment-1521342541."
.into()
)
}
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