Skip to content

Commit

Permalink
Add a constructor to allow specifying initial position
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Jan 26, 2024
1 parent f82eee0 commit 4a4d78e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/lexgen/src/dfa/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = char> + Clone>
Expand Down
14 changes: 11 additions & 3 deletions crates/lexgen_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,27 @@ impl<I: Iterator<Item = char> + Clone, T, S: Default, E, W> Lexer<I, T, S, E, W>
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<I: Iterator<Item = char> + Clone, T, S, E, W> Lexer<I, T, S, E, W> {
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),
}
Expand Down

0 comments on commit 4a4d78e

Please sign in to comment.