From 4a4d78e55cbb4f6a79efaf35721351205a644e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Fri, 26 Jan 2024 15:30:17 +0100 Subject: [PATCH] Add a constructor to allow specifying initial position --- crates/lexgen/src/dfa/codegen.rs | 4 ++++ crates/lexgen_util/src/lib.rs | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/lexgen/src/dfa/codegen.rs b/crates/lexgen/src/dfa/codegen.rs index 3cb9add..bed245e 100644 --- a/crates/lexgen/src/dfa/codegen.rs +++ b/crates/lexgen/src/dfa/codegen.rs @@ -270,6 +270,10 @@ pub fn reify( #visibility fn new_from_iter(iter: I) -> Self { #lexer_struct_name(::lexgen_util::Lexer::new_from_iter(iter)) } + + #visibility fn new_from_iter_with_loc(iter: I, loc: ::lexgen_util::Loc) -> Self { + #lexer_struct_name(::lexgen_util::Lexer::new_from_iter_with_loc(iter, loc)) + } } impl<#(#user_state_lifetimes,)* I: Iterator + Clone> diff --git a/crates/lexgen_util/src/lib.rs b/crates/lexgen_util/src/lib.rs index f3504f7..ce8d913 100644 --- a/crates/lexgen_util/src/lib.rs +++ b/crates/lexgen_util/src/lib.rs @@ -112,19 +112,27 @@ impl + Clone, T, S: Default, E, W> Lexer pub fn new_from_iter(iter: I) -> Self { Self::new_from_iter_with_state(iter, Default::default()) } + + pub fn new_from_iter_with_loc(iter: I, loc: Loc) -> Self { + Self::new_from_iter_with_state_and_loc(iter, Default::default(), loc) + } } impl + Clone, T, S, E, W> Lexer { pub fn new_from_iter_with_state(iter: I, state: S) -> Self { + Self::new_from_iter_with_state_and_loc(iter, state, Loc::ZERO) + } + + pub fn new_from_iter_with_state_and_loc(iter: I, state: S, loc: Loc) -> Self { Self { __state: 0, __done: false, __initial_state: 0, user_state: state, - iter_loc: Loc::ZERO, + iter_loc: loc, __iter: iter.peekable(), - current_match_start: Loc::ZERO, - current_match_end: Loc::ZERO, + current_match_start: loc, + current_match_end: loc, last_match: None, __match_buffer: String::with_capacity(100), }