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 unicode dot #376

Merged
merged 9 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 12 additions & 14 deletions logos-codegen/src/graph/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::mir::{Class, ClassUnicode, Literal, Mir};

impl<Leaf: Disambiguate + Debug> Graph<Leaf> {
pub fn regex(&mut self, mir: Mir, then: NodeId) -> NodeId {
self.parse_mir(mir, then, None, None)
self.parse_mir(mir, then, None, None, false)
}

fn parse_mir(
Expand All @@ -16,6 +16,7 @@ impl<Leaf: Disambiguate + Debug> Graph<Leaf> {
then: NodeId,
miss: Option<NodeId>,
reserved: Option<ReservedId>,
repeated: bool,
jeertmans marked this conversation as resolved.
Show resolved Hide resolved
) -> NodeId {
match mir {
Mir::Empty => then,
Expand All @@ -29,21 +30,21 @@ impl<Leaf: Disambiguate + Debug> Graph<Leaf> {
None => self.reserve(),
};

self.parse_mir(*mir, this.get(), Some(miss), Some(this))
self.parse_mir(*mir, this.get(), Some(miss), Some(this), true)
}
Mir::Maybe(mir) => {
let miss = match miss {
Some(id) => self.merge(id, then),
None => then,
};

self.parse_mir(*mir, then, Some(miss), reserved)
self.parse_mir(*mir, then, Some(miss), reserved, false)
}
Mir::Alternation(alternation) => {
let mut fork = Fork::new().miss(miss);

for mir in alternation {
let id = self.parse_mir(mir, then, None, None);
let id = self.parse_mir(mir, then, None, None, repeated);
let alt = self.fork_off(id);

fork.merge(alt, self);
Expand Down Expand Up @@ -97,7 +98,7 @@ impl<Leaf: Disambiguate + Debug> Graph<Leaf> {

for mir in concat.drain(1..).rev() {
if let Some(mir) = handle_bytes(self, mir, &mut then) {
then = self.parse_mir(mir, then, None, None);
then = self.parse_mir(mir, then, None, None, false);
}
}

Expand All @@ -107,10 +108,10 @@ impl<Leaf: Disambiguate + Debug> Graph<Leaf> {

self.insert_or_push(reserved, rope)
}
Some(mir) => self.parse_mir(mir, then, miss, reserved),
Some(mir) => self.parse_mir(mir, then, miss, reserved, false),
}
}
Mir::Class(Class::Unicode(class)) if !is_ascii(&class) => {
Mir::Class(Class::Unicode(class)) if !is_ascii(&class, repeated) => {
let mut ropes = class
.iter()
.flat_map(|range| Utf8Sequences::new(range.start(), range.end()))
Expand Down Expand Up @@ -160,12 +161,10 @@ impl<Leaf: Disambiguate + Debug> Graph<Leaf> {
}
}

fn is_ascii(class: &ClassUnicode) -> bool {
class.iter().all(|range| {
let start = range.start() as u32;
fn is_ascii(class: &ClassUnicode, repeated: bool) -> bool {
class.iter().last().map_or(true, |range| {
let end = range.end() as u32;

start < 128 && (end < 128 || end == 0x0010_FFFF)
end < 128 || (repeated && end == 0x0010_FFFF)
})
}

Expand All @@ -175,10 +174,9 @@ fn is_one_ascii(class: &ClassUnicode) -> bool {
}

let range = &class.ranges()[0];
let start = range.start() as u32;
let end = range.end() as u32;

start < 128 && (end < 128 || end == 0x0010_FFFF)
end < 128 || end == 0x0010_FFFF
jeertmans marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extern crate core as std;
pub use logos_derive::Logos;
use std::fmt::Debug;

#[cfg(test)]
mod tests;

mod lexer;
pub mod source;

Expand Down
57 changes: 57 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::Logos;

#[derive(Logos, Debug, PartialEq)]
#[logos(crate = crate)]
enum TestUnicodeDot {
#[regex(".")]
Dot,
}

#[test]
fn test_unicode_dot_str_ascii() {
let mut lexer = TestUnicodeDot::lexer("a");
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDot::Dot)));
assert_eq!(lexer.remainder(), "");
assert_eq!(lexer.next(), None);
}

#[test]
fn test_unicode_dot_str_unicode() {
let mut lexer = TestUnicodeDot::lexer("");
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDot::Dot)));
assert_eq!(lexer.remainder(), "");
assert_eq!(lexer.next(), None);
}

#[derive(Logos, Debug, PartialEq)]
#[logos(crate = crate)]
enum TestUnicodeDotBytes {
#[regex(".", priority = 100)]
Dot,
#[regex(b".", priority = 0)]
InvalidUtf8,
}

#[test]
fn test_unicode_dot_bytes_ascii() {
let mut lexer = TestUnicodeDotBytes::lexer(b"a");
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDotBytes::Dot)));
assert_eq!(lexer.remainder(), b"");
assert_eq!(lexer.next(), None);
}

#[test]
fn test_unicode_dot_bytes_unicode() {
let mut lexer = TestUnicodeDotBytes::lexer("".as_bytes());
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDotBytes::Dot)));
assert_eq!(lexer.remainder(), b"");
assert_eq!(lexer.next(), None);
}

#[test]
fn test_unicode_dot_bytes_invalid_utf8() {
let mut lexer = TestUnicodeDotBytes::lexer(b"\xff");
assert_eq!(lexer.next(), Some(Ok(TestUnicodeDotBytes::InvalidUtf8)));
assert_eq!(lexer.remainder(), b"");
assert_eq!(lexer.next(), None);
}
Loading