From 5ee6edf222658e73ee97871972b6fbeef444451b Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 25 Jul 2022 17:42:20 +0200 Subject: Refactor to not pass codes around --- src/construct/attention.rs | 14 +- src/construct/autolink.rs | 90 +++--- src/construct/blank_line.rs | 8 +- src/construct/block_quote.rs | 28 +- src/construct/character_escape.rs | 12 +- src/construct/character_reference.rs | 36 +-- src/construct/code_fenced.rs | 145 +++++----- src/construct/code_indented.rs | 44 +-- src/construct/code_text.rs | 50 ++-- src/construct/definition.rs | 57 ++-- src/construct/hard_break_escape.rs | 10 +- src/construct/hard_break_trailing.rs | 16 +- src/construct/heading_atx.rs | 50 ++-- src/construct/heading_setext.rs | 24 +- src/construct/html_flow.rs | 362 ++++++++++++------------- src/construct/html_text.rs | 280 +++++++++---------- src/construct/label_end.rs | 119 ++++---- src/construct/label_start_image.rs | 12 +- src/construct/label_start_link.rs | 6 +- src/construct/list.rs | 79 +++--- src/construct/paragraph.rs | 12 +- src/construct/partial_data.rs | 34 +-- src/construct/partial_destination.rs | 72 ++--- src/construct/partial_label.rs | 52 ++-- src/construct/partial_non_lazy_continuation.rs | 8 +- src/construct/partial_space_or_tab.rs | 40 +-- src/construct/partial_title.rs | 58 ++-- src/construct/partial_whitespace.rs | 12 +- src/construct/thematic_break.rs | 27 +- 29 files changed, 861 insertions(+), 896 deletions(-) (limited to 'src/construct') diff --git a/src/construct/attention.rs b/src/construct/attention.rs index a53a328..27d7544 100644 --- a/src/construct/attention.rs +++ b/src/construct/attention.rs @@ -168,11 +168,11 @@ struct Sequence { /// > | ** /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('*' | '_') if tokenizer.parse_state.constructs.attention => { tokenizer.enter(Token::AttentionSequence); - inside(tokenizer, code, MarkerKind::from_code(code)) + inside(tokenizer, MarkerKind::from_code(tokenizer.current)) } _ => State::Nok, } @@ -184,11 +184,11 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ** /// ^^ /// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code, marker: MarkerKind) -> State { - match code { +fn inside(tokenizer: &mut Tokenizer, marker: MarkerKind) -> State { + match tokenizer.current { Code::Char(char) if char == marker.as_char() => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| inside(t, c, marker))) + tokenizer.consume(); + State::Fn(Box::new(move |t| inside(t, marker))) } _ => { tokenizer.exit(Token::AttentionSequence); diff --git a/src/construct/autolink.rs b/src/construct/autolink.rs index 0c5a328..3933596 100644 --- a/src/construct/autolink.rs +++ b/src/construct/autolink.rs @@ -113,12 +113,12 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// > | ab /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('<') if tokenizer.parse_state.constructs.autolink => { tokenizer.enter(Token::Autolink); tokenizer.enter(Token::AutolinkMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::AutolinkMarker); tokenizer.enter(Token::AutolinkProtocol); State::Fn(Box::new(open)) @@ -135,13 +135,13 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ab /// ^ /// ``` -fn open(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn open(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char(char) if char.is_ascii_alphabetic() => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(scheme_or_email_atext)) } - Code::Char(char) if is_ascii_atext(char) => email_atext(tokenizer, code), + Code::Char(char) if is_ascii_atext(char) => email_atext(tokenizer), _ => State::Nok, } } @@ -154,12 +154,12 @@ fn open(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ab /// ^ /// ``` -fn scheme_or_email_atext(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn scheme_or_email_atext(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('+' | '-' | '.' | '0'..='9' | 'A'..='Z' | 'a'..='z') => { - scheme_inside_or_email_atext(tokenizer, code, 1) + scheme_inside_or_email_atext(tokenizer, 1) } - _ => email_atext(tokenizer, code), + _ => email_atext(tokenizer), } } @@ -171,21 +171,19 @@ fn scheme_or_email_atext(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ab /// ^ /// ``` -fn scheme_inside_or_email_atext(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { - match code { +fn scheme_inside_or_email_atext(tokenizer: &mut Tokenizer, size: usize) -> State { + match tokenizer.current { Code::Char(':') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(url_inside)) } Code::Char('+' | '-' | '.' | '0'..='9' | 'A'..='Z' | 'a'..='z') if size < AUTOLINK_SCHEME_SIZE_MAX => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| { - scheme_inside_or_email_atext(t, c, size + 1) - })) + tokenizer.consume(); + State::Fn(Box::new(move |t| scheme_inside_or_email_atext(t, size + 1))) } - _ => email_atext(tokenizer, code), + _ => email_atext(tokenizer), } } @@ -195,18 +193,18 @@ fn scheme_inside_or_email_atext(tokenizer: &mut Tokenizer, code: Code, size: usi /// > | ab /// ^ /// ``` -fn url_inside(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn url_inside(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('>') => { tokenizer.exit(Token::AutolinkProtocol); - end(tokenizer, code) + end(tokenizer) } Code::Char(char) if char.is_ascii_control() => State::Nok, Code::None | Code::CarriageReturnLineFeed | Code::VirtualSpace | Code::Char(' ') => { State::Nok } Code::Char(_) => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(url_inside)) } } @@ -218,14 +216,14 @@ fn url_inside(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ab /// ^ /// ``` -fn email_atext(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn email_atext(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('@') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| email_at_sign_or_dot(t, c, 0))) + tokenizer.consume(); + State::Fn(Box::new(|t| email_at_sign_or_dot(t, 0))) } Code::Char(char) if is_ascii_atext(char) => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(email_atext)) } _ => State::Nok, @@ -238,9 +236,9 @@ fn email_atext(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ab /// ^ ^ /// ``` -fn email_at_sign_or_dot(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { - match code { - Code::Char(char) if char.is_ascii_alphanumeric() => email_value(tokenizer, code, size), +fn email_at_sign_or_dot(tokenizer: &mut Tokenizer, size: usize) -> State { + match tokenizer.current { + Code::Char(char) if char.is_ascii_alphanumeric() => email_value(tokenizer, size), _ => State::Nok, } } @@ -251,11 +249,11 @@ fn email_at_sign_or_dot(tokenizer: &mut Tokenizer, code: Code, size: usize) -> S /// > | ab /// ^ /// ``` -fn email_label(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { - match code { +fn email_label(tokenizer: &mut Tokenizer, size: usize) -> State { + match tokenizer.current { Code::Char('.') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| email_at_sign_or_dot(t, c, 0))) + tokenizer.consume(); + State::Fn(Box::new(|t| email_at_sign_or_dot(t, 0))) } Code::Char('>') => { let index = tokenizer.events.len(); @@ -263,9 +261,9 @@ fn email_label(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { // Change the token type. tokenizer.events[index - 1].token_type = Token::AutolinkEmail; tokenizer.events[index].token_type = Token::AutolinkEmail; - end(tokenizer, code) + end(tokenizer) } - _ => email_value(tokenizer, code, size), + _ => email_value(tokenizer, size), } } @@ -277,15 +275,15 @@ fn email_label(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { /// > | ab /// ^ /// ``` -fn email_value(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { - match code { +fn email_value(tokenizer: &mut Tokenizer, size: usize) -> State { + match tokenizer.current { Code::Char('-') if size < AUTOLINK_DOMAIN_SIZE_MAX => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| email_value(t, c, size + 1))) + tokenizer.consume(); + State::Fn(Box::new(move |t| email_value(t, size + 1))) } Code::Char(char) if char.is_ascii_alphanumeric() && size < AUTOLINK_DOMAIN_SIZE_MAX => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| email_label(t, c, size + 1))) + tokenizer.consume(); + State::Fn(Box::new(move |t| email_label(t, size + 1))) } _ => State::Nok, } @@ -299,11 +297,11 @@ fn email_value(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { /// > | ab /// ^ /// ``` -fn end(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn end(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('>') => { tokenizer.enter(Token::AutolinkMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::AutolinkMarker); tokenizer.exit(Token::Autolink); State::Ok diff --git a/src/construct/blank_line.rs b/src/construct/blank_line.rs index 48cf2e6..537ffc1 100644 --- a/src/construct/blank_line.rs +++ b/src/construct/blank_line.rs @@ -45,8 +45,8 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// > | /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.attempt_opt(space_or_tab(), after)(tokenizer, code) +pub fn start(tokenizer: &mut Tokenizer) -> State { + tokenizer.attempt_opt(space_or_tab(), after)(tokenizer) } /// After zero or more spaces or tabs, before a line ending or EOF. @@ -57,8 +57,8 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | /// ^ /// ``` -fn after(_tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn after(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => State::Ok, _ => State::Nok, } diff --git a/src/construct/block_quote.rs b/src/construct/block_quote.rs index b99665f..3bb4b8b 100644 --- a/src/construct/block_quote.rs +++ b/src/construct/block_quote.rs @@ -44,14 +44,14 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// > | > a /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { usize::MAX }; if tokenizer.parse_state.constructs.block_quote { - tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer) } else { State::Nok } @@ -63,13 +63,13 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | > a /// ^ /// ``` -fn before(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn before(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('>') => { tokenizer.enter(Token::BlockQuote); - cont_before(tokenizer, code) + cont_before(tokenizer) } - _ => cont_before(tokenizer, code), + _ => cont_before(tokenizer), } } @@ -80,13 +80,13 @@ fn before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | > b /// ^ /// ``` -pub fn cont(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn cont(tokenizer: &mut Tokenizer) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { usize::MAX }; - tokenizer.go(space_or_tab_min_max(0, max), cont_before)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, max), cont_before)(tokenizer) } /// After whitespace, before `>`. @@ -96,12 +96,12 @@ pub fn cont(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | > b /// ^ /// ``` -fn cont_before(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn cont_before(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('>') => { tokenizer.enter(Token::BlockQuotePrefix); tokenizer.enter(Token::BlockQuoteMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::BlockQuoteMarker); State::Fn(Box::new(cont_after)) } @@ -117,11 +117,11 @@ fn cont_before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | >b /// ^ /// ``` -fn cont_after(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn cont_after(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::VirtualSpace | Code::Char('\t' | ' ') => { tokenizer.enter(Token::SpaceOrTab); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::SpaceOrTab); tokenizer.exit(Token::BlockQuotePrefix); State::Ok diff --git a/src/construct/character_escape.rs b/src/construct/character_escape.rs index f317854..9e9b713 100644 --- a/src/construct/character_escape.rs +++ b/src/construct/character_escape.rs @@ -42,12 +42,12 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// > | a\*b /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('\\') if tokenizer.parse_state.constructs.character_escape => { tokenizer.enter(Token::CharacterEscape); tokenizer.enter(Token::CharacterEscapeMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::CharacterEscapeMarker); State::Fn(Box::new(inside)) } @@ -61,11 +61,11 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a\*b /// ^ /// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn inside(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char(char) if char.is_ascii_punctuation() => { tokenizer.enter(Token::CharacterEscapeValue); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::CharacterEscapeValue); tokenizer.exit(Token::CharacterEscape); State::Ok diff --git a/src/construct/character_reference.rs b/src/construct/character_reference.rs index 6e44502..8521f15 100644 --- a/src/construct/character_reference.rs +++ b/src/construct/character_reference.rs @@ -136,12 +136,12 @@ struct Info { /// > | a b /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('&') if tokenizer.parse_state.constructs.character_reference => { tokenizer.enter(Token::CharacterReference); tokenizer.enter(Token::CharacterReferenceMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::CharacterReferenceMarker); State::Fn(Box::new(open)) } @@ -160,19 +160,19 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a b /// ^ /// ``` -fn open(tokenizer: &mut Tokenizer, code: Code) -> State { +fn open(tokenizer: &mut Tokenizer) -> State { let info = Info { buffer: String::new(), kind: Kind::Named, }; - if let Code::Char('#') = code { + if let Code::Char('#') = tokenizer.current { tokenizer.enter(Token::CharacterReferenceMarkerNumeric); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::CharacterReferenceMarkerNumeric); - State::Fn(Box::new(|t, c| numeric(t, c, info))) + State::Fn(Box::new(|t| numeric(t, info))) } else { tokenizer.enter(Token::CharacterReferenceValue); - value(tokenizer, code, info) + value(tokenizer, info) } } @@ -185,18 +185,18 @@ fn open(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a b /// ^ /// ``` -fn numeric(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - if let Code::Char('x' | 'X') = code { +fn numeric(tokenizer: &mut Tokenizer, mut info: Info) -> State { + if let Code::Char('x' | 'X') = tokenizer.current { tokenizer.enter(Token::CharacterReferenceMarkerHexadecimal); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::CharacterReferenceMarkerHexadecimal); tokenizer.enter(Token::CharacterReferenceValue); info.kind = Kind::Hexadecimal; - State::Fn(Box::new(|t, c| value(t, c, info))) + State::Fn(Box::new(|t| value(t, info))) } else { tokenizer.enter(Token::CharacterReferenceValue); info.kind = Kind::Decimal; - value(tokenizer, code, info) + value(tokenizer, info) } } @@ -213,8 +213,8 @@ fn numeric(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { /// > | a b /// ^ /// ``` -fn value(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn value(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char(';') if !info.buffer.is_empty() => { let unknown_named = Kind::Named == info.kind && !CHARACTER_REFERENCES.iter().any(|d| d.0 == info.buffer); @@ -224,7 +224,7 @@ fn value(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { } else { tokenizer.exit(Token::CharacterReferenceValue); tokenizer.enter(Token::CharacterReferenceMarkerSemi); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::CharacterReferenceMarkerSemi); tokenizer.exit(Token::CharacterReference); State::Ok @@ -233,8 +233,8 @@ fn value(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { Code::Char(char) => { if info.buffer.len() < info.kind.max() && info.kind.allowed(char) { info.buffer.push(char); - tokenizer.consume(code); - State::Fn(Box::new(|t, c| value(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| value(t, info))) } else { State::Nok } diff --git a/src/construct/code_fenced.rs b/src/construct/code_fenced.rs index 10968c2..2fea95e 100644 --- a/src/construct/code_fenced.rs +++ b/src/construct/code_fenced.rs @@ -188,7 +188,7 @@ struct Info { /// | console.log(1) /// | ~~~ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { @@ -197,7 +197,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { if tokenizer.parse_state.constructs.code_fenced { tokenizer.enter(Token::CodeFenced); tokenizer.enter(Token::CodeFencedFence); - tokenizer.go(space_or_tab_min_max(0, max), before_sequence_open)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, max), before_sequence_open)(tokenizer) } else { State::Nok } @@ -211,7 +211,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// | console.log(1) /// | ~~~ /// ``` -fn before_sequence_open(tokenizer: &mut Tokenizer, code: Code) -> State { +fn before_sequence_open(tokenizer: &mut Tokenizer) -> State { let tail = tokenizer.events.last(); let mut prefix = 0; @@ -222,16 +222,15 @@ fn before_sequence_open(tokenizer: &mut Tokenizer, code: Code) -> State { } } - match code { + match tokenizer.current { Code::Char('`' | '~') => { tokenizer.enter(Token::CodeFencedFenceSequence); sequence_open( tokenizer, - code, Info { prefix, size: 0, - kind: Kind::from_code(code), + kind: Kind::from_code(tokenizer.current), }, ) } @@ -247,18 +246,18 @@ fn before_sequence_open(tokenizer: &mut Tokenizer, code: Code) -> State { /// | console.log(1) /// | ~~~ /// ``` -fn sequence_open(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn sequence_open(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char(char) if char == info.kind.as_char() => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| { + tokenizer.consume(); + State::Fn(Box::new(|t| { info.size += 1; - sequence_open(t, c, info) + sequence_open(t, info) })) } _ if info.size >= CODE_FENCED_SEQUENCE_SIZE_MIN => { tokenizer.exit(Token::CodeFencedFenceSequence); - tokenizer.attempt_opt(space_or_tab(), |t, c| info_before(t, c, info))(tokenizer, code) + tokenizer.attempt_opt(space_or_tab(), |t| info_before(t, info))(tokenizer) } _ => State::Nok, } @@ -272,18 +271,18 @@ fn sequence_open(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State /// | console.log(1) /// | ~~~ /// ``` -fn info_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn info_before(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::CodeFencedFence); // Do not form containers. tokenizer.concrete = true; - at_break(tokenizer, code, info) + at_break(tokenizer, info) } _ => { tokenizer.enter(Token::CodeFencedFenceInfo); tokenizer.enter_with_content(Token::Data, Some(ContentType::String)); - info_inside(tokenizer, code, info, vec![]) + info_inside(tokenizer, info, vec![]) } } } @@ -296,26 +295,26 @@ fn info_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// | console.log(1) /// | ~~~ /// ``` -fn info_inside(tokenizer: &mut Tokenizer, code: Code, info: Info, mut codes: Vec) -> State { - match code { +fn info_inside(tokenizer: &mut Tokenizer, info: Info, mut codes: Vec) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::Data); tokenizer.exit(Token::CodeFencedFenceInfo); tokenizer.exit(Token::CodeFencedFence); // Do not form containers. tokenizer.concrete = true; - at_break(tokenizer, code, info) + at_break(tokenizer, info) } Code::VirtualSpace | Code::Char('\t' | ' ') => { tokenizer.exit(Token::Data); tokenizer.exit(Token::CodeFencedFenceInfo); - tokenizer.attempt_opt(space_or_tab(), |t, c| meta_before(t, c, info))(tokenizer, code) + tokenizer.attempt_opt(space_or_tab(), |t| meta_before(t, info))(tokenizer) } Code::Char('`') if info.kind == Kind::GraveAccent => State::Nok, Code::Char(_) => { - codes.push(code); - tokenizer.consume(code); - State::Fn(Box::new(|t, c| info_inside(t, c, info, codes))) + codes.push(tokenizer.current); + tokenizer.consume(); + State::Fn(Box::new(|t| info_inside(t, info, codes))) } } } @@ -328,18 +327,18 @@ fn info_inside(tokenizer: &mut Tokenizer, code: Code, info: Info, mut codes: Vec /// | console.log(1) /// | ~~~ /// ``` -fn meta_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn meta_before(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::CodeFencedFence); // Do not form containers. tokenizer.concrete = true; - at_break(tokenizer, code, info) + at_break(tokenizer, info) } _ => { tokenizer.enter(Token::CodeFencedFenceMeta); tokenizer.enter_with_content(Token::Data, Some(ContentType::String)); - meta(tokenizer, code, info) + meta(tokenizer, info) } } } @@ -352,20 +351,20 @@ fn meta_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// | console.log(1) /// | ~~~ /// ``` -fn meta(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn meta(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::Data); tokenizer.exit(Token::CodeFencedFenceMeta); tokenizer.exit(Token::CodeFencedFence); // Do not form containers. tokenizer.concrete = true; - at_break(tokenizer, code, info) + at_break(tokenizer, info) } Code::Char('`') if info.kind == Kind::GraveAccent => State::Nok, _ => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| meta(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| meta(t, info))) } } } @@ -379,14 +378,14 @@ fn meta(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// ^ /// | ~~~ /// ``` -fn at_break(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { +fn at_break(tokenizer: &mut Tokenizer, info: Info) -> State { tokenizer.check(partial_non_lazy_continuation, |ok| { if ok { - Box::new(move |t, c| at_non_lazy_break(t, c, info)) + Box::new(move |t| at_non_lazy_break(t, info)) } else { Box::new(after) } - })(tokenizer, code) + })(tokenizer) } /// At an eol/eof in code, before a non-lazy closing fence or content. @@ -398,19 +397,19 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// ^ /// | ~~~ /// ``` -fn at_non_lazy_break(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { +fn at_non_lazy_break(tokenizer: &mut Tokenizer, info: Info) -> State { let clone = info.clone(); tokenizer.attempt( - |t, c| close_begin(t, c, info), + |t| close_begin(t, info), |ok| { if ok { Box::new(after) } else { - Box::new(|t, c| content_before(t, c, clone)) + Box::new(|t| content_before(t, clone)) } }, - )(tokenizer, code) + )(tokenizer) } /// Before a closing fence, at the line ending. @@ -421,13 +420,13 @@ fn at_non_lazy_break(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State /// ^ /// | ~~~ /// ``` -fn close_begin(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn close_begin(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); - State::Fn(Box::new(|t, c| close_start(t, c, info))) + State::Fn(Box::new(|t| close_start(t, info))) } _ => unreachable!("expected eol"), } @@ -441,7 +440,7 @@ fn close_begin(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | ~~~ /// ^ /// ``` -fn close_start(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { +fn close_start(tokenizer: &mut Tokenizer, info: Info) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { @@ -449,9 +448,7 @@ fn close_start(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { }; tokenizer.enter(Token::CodeFencedFence); - tokenizer.go(space_or_tab_min_max(0, max), |t, c| { - close_before(t, c, info) - })(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, max), |t| close_before(t, info))(tokenizer) } /// In a closing fence, after optional whitespace, before sequence. @@ -462,11 +459,11 @@ fn close_start(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | ~~~ /// ^ /// ``` -fn close_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn close_before(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char(char) if char == info.kind.as_char() => { tokenizer.enter(Token::CodeFencedFenceSequence); - close_sequence(tokenizer, code, info, 0) + close_sequence(tokenizer, info, 0) } _ => State::Nok, } @@ -480,15 +477,15 @@ fn close_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | ~~~ /// ^ /// ``` -fn close_sequence(tokenizer: &mut Tokenizer, code: Code, info: Info, size: usize) -> State { - match code { +fn close_sequence(tokenizer: &mut Tokenizer, info: Info, size: usize) -> State { + match tokenizer.current { Code::Char(char) if char == info.kind.as_char() => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| close_sequence(t, c, info, size + 1))) + tokenizer.consume(); + State::Fn(Box::new(move |t| close_sequence(t, info, size + 1))) } _ if size >= CODE_FENCED_SEQUENCE_SIZE_MIN && size >= info.size => { tokenizer.exit(Token::CodeFencedFenceSequence); - tokenizer.attempt_opt(space_or_tab(), close_sequence_after)(tokenizer, code) + tokenizer.attempt_opt(space_or_tab(), close_sequence_after)(tokenizer) } _ => State::Nok, } @@ -502,8 +499,8 @@ fn close_sequence(tokenizer: &mut Tokenizer, code: Code, info: Info, size: usize /// > | ~~~ /// ^ /// ``` -fn close_sequence_after(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn close_sequence_after(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::CodeFencedFence); State::Ok @@ -520,11 +517,11 @@ fn close_sequence_after(tokenizer: &mut Tokenizer, code: Code) -> State { /// ^ /// | ~~~ /// ``` -fn content_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { +fn content_before(tokenizer: &mut Tokenizer, info: Info) -> State { tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); - State::Fn(Box::new(|t, c| content_start(t, c, info))) + State::Fn(Box::new(|t| content_start(t, info))) } /// Before code content, definitely not before a closing fence. /// @@ -534,10 +531,10 @@ fn content_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// ^ /// | ~~~ /// ``` -fn content_start(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - tokenizer.go(space_or_tab_min_max(0, info.prefix), |t, c| { - content_begin(t, c, info) - })(tokenizer, code) +fn content_start(tokenizer: &mut Tokenizer, info: Info) -> State { + tokenizer.go(space_or_tab_min_max(0, info.prefix), |t| { + content_begin(t, info) + })(tokenizer) } /// Before code content, after a prefix. @@ -548,14 +545,14 @@ fn content_start(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// ^ /// | ~~~ /// ``` -fn content_begin(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn content_begin(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_break(tokenizer, code, info) + at_break(tokenizer, info) } _ => { tokenizer.enter(Token::CodeFlowChunk); - content_continue(tokenizer, code, info) + content_continue(tokenizer, info) } } } @@ -568,15 +565,15 @@ fn content_begin(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// ^^^^^^^^^^^^^^ /// | ~~~ /// ``` -fn content_continue(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn content_continue(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::CodeFlowChunk); - at_break(tokenizer, code, info) + at_break(tokenizer, info) } _ => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| content_continue(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| content_continue(t, info))) } } } @@ -589,7 +586,7 @@ fn content_continue(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State /// > | ~~~ /// ^ /// ``` -fn after(tokenizer: &mut Tokenizer, _code: Code) -> State { +fn after(tokenizer: &mut Tokenizer) -> State { tokenizer.exit(Token::CodeFenced); // Feel free to interrupt. tokenizer.interrupt = false; diff --git a/src/construct/code_indented.rs b/src/construct/code_indented.rs index f223633..015c4a0 100644 --- a/src/construct/code_indented.rs +++ b/src/construct/code_indented.rs @@ -60,13 +60,13 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// > | aaa /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { // Do not interrupt paragraphs. if tokenizer.interrupt || !tokenizer.parse_state.constructs.code_indented { State::Nok } else { tokenizer.enter(Token::CodeIndented); - tokenizer.go(space_or_tab_min_max(TAB_SIZE, TAB_SIZE), at_break)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(TAB_SIZE, TAB_SIZE), at_break)(tokenizer) } } @@ -76,16 +76,16 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | aaa /// ^ ^ /// ``` -fn at_break(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { - Code::None => after(tokenizer, code), +fn at_break(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { + Code::None => after(tokenizer), Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => tokenizer .attempt(further_start, |ok| { Box::new(if ok { at_break } else { after }) - })(tokenizer, code), + })(tokenizer), _ => { tokenizer.enter(Token::CodeFlowChunk); - content(tokenizer, code) + content(tokenizer) } } } @@ -96,14 +96,14 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | aaa /// ^^^^ /// ``` -fn content(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn content(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::CodeFlowChunk); - at_break(tokenizer, code) + at_break(tokenizer) } _ => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(content)) } } @@ -115,7 +115,7 @@ fn content(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | aaa /// ^ /// ``` -fn after(tokenizer: &mut Tokenizer, _code: Code) -> State { +fn after(tokenizer: &mut Tokenizer) -> State { tokenizer.exit(Token::CodeIndented); // Feel free to interrupt. tokenizer.interrupt = false; @@ -129,20 +129,20 @@ fn after(tokenizer: &mut Tokenizer, _code: Code) -> State { /// ^ /// | bbb /// ``` -fn further_start(tokenizer: &mut Tokenizer, code: Code) -> State { +fn further_start(tokenizer: &mut Tokenizer) -> State { if tokenizer.lazy { State::Nok } else { - match code { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); State::Fn(Box::new(further_start)) } _ => tokenizer.attempt(space_or_tab_min_max(TAB_SIZE, TAB_SIZE), |ok| { Box::new(if ok { further_end } else { further_begin }) - })(tokenizer, code), + })(tokenizer), } } } @@ -154,7 +154,7 @@ fn further_start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | bbb /// ^ /// ``` -fn further_end(_tokenizer: &mut Tokenizer, _code: Code) -> State { +fn further_end(_tokenizer: &mut Tokenizer) -> State { State::Ok } @@ -165,8 +165,8 @@ fn further_end(_tokenizer: &mut Tokenizer, _code: Code) -> State { /// > | bbb /// ^ /// ``` -fn further_begin(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.attempt_opt(space_or_tab(), further_after)(tokenizer, code) +fn further_begin(tokenizer: &mut Tokenizer) -> State { + tokenizer.attempt_opt(space_or_tab(), further_after)(tokenizer) } /// After whitespace, not indented enough. @@ -176,9 +176,9 @@ fn further_begin(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | bbb /// ^ /// ``` -fn further_after(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { - Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => further_start(tokenizer, code), +fn further_after(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { + Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => further_start(tokenizer), _ => State::Nok, } } diff --git a/src/construct/code_text.rs b/src/construct/code_text.rs index 978ad01..f5f92fc 100644 --- a/src/construct/code_text.rs +++ b/src/construct/code_text.rs @@ -94,10 +94,10 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// > | \`a` /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let len = tokenizer.events.len(); - match code { + match tokenizer.current { Code::Char('`') if tokenizer.parse_state.constructs.code_text && (tokenizer.previous != Code::Char('`') @@ -106,7 +106,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { { tokenizer.enter(Token::CodeText); tokenizer.enter(Token::CodeTextSequence); - sequence_open(tokenizer, code, 0) + sequence_open(tokenizer, 0) } _ => State::Nok, } @@ -118,13 +118,13 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | `a` /// ^ /// ``` -fn sequence_open(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { - if let Code::Char('`') = code { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| sequence_open(t, c, size + 1))) +fn sequence_open(tokenizer: &mut Tokenizer, size: usize) -> State { + if let Code::Char('`') = tokenizer.current { + tokenizer.consume(); + State::Fn(Box::new(move |t| sequence_open(t, size + 1))) } else { tokenizer.exit(Token::CodeTextSequence); - between(tokenizer, code, size) + between(tokenizer, size) } } @@ -134,22 +134,22 @@ fn sequence_open(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { /// > | `a` /// ^^ /// ``` -fn between(tokenizer: &mut Tokenizer, code: Code, size_open: usize) -> State { - match code { +fn between(tokenizer: &mut Tokenizer, size_open: usize) -> State { + match tokenizer.current { Code::None => State::Nok, Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); - State::Fn(Box::new(move |t, c| between(t, c, size_open))) + State::Fn(Box::new(move |t| between(t, size_open))) } Code::Char('`') => { tokenizer.enter(Token::CodeTextSequence); - sequence_close(tokenizer, code, size_open, 0) + sequence_close(tokenizer, size_open, 0) } _ => { tokenizer.enter(Token::CodeTextData); - data(tokenizer, code, size_open) + data(tokenizer, size_open) } } } @@ -160,15 +160,15 @@ fn between(tokenizer: &mut Tokenizer, code: Code, size_open: usize) -> State { /// > | `a` /// ^ /// ``` -fn data(tokenizer: &mut Tokenizer, code: Code, size_open: usize) -> State { - match code { +fn data(tokenizer: &mut Tokenizer, size_open: usize) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r' | '`') => { tokenizer.exit(Token::CodeTextData); - between(tokenizer, code, size_open) + between(tokenizer, size_open) } _ => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| data(t, c, size_open))) + tokenizer.consume(); + State::Fn(Box::new(move |t| data(t, size_open))) } } } @@ -179,13 +179,11 @@ fn data(tokenizer: &mut Tokenizer, code: Code, size_open: usize) -> State { /// > | `a` /// ^ /// ``` -fn sequence_close(tokenizer: &mut Tokenizer, code: Code, size_open: usize, size: usize) -> State { - match code { +fn sequence_close(tokenizer: &mut Tokenizer, size_open: usize, size: usize) -> State { + match tokenizer.current { Code::Char('`') => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| { - sequence_close(t, c, size_open, size + 1) - })) + tokenizer.consume(); + State::Fn(Box::new(move |t| sequence_close(t, size_open, size + 1))) } _ if size_open == size => { tokenizer.exit(Token::CodeTextSequence); @@ -198,7 +196,7 @@ fn sequence_close(tokenizer: &mut Tokenizer, code: Code, size_open: usize, size: // Change the token type. tokenizer.events[index - 1].token_type = Token::CodeTextData; tokenizer.events[index].token_type = Token::CodeTextData; - between(tokenizer, code, size_open) + between(tokenizer, size_open) } } } diff --git a/src/construct/definition.rs b/src/construct/definition.rs index 093b9e5..ffaaa98 100644 --- a/src/construct/definition.rs +++ b/src/construct/definition.rs @@ -109,7 +109,7 @@ use crate::util::skip::opt_back as skip_opt_back; /// > | [a]: b "c" /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let definition_before = !tokenizer.events.is_empty() && tokenizer.events[skip_opt_back( &tokenizer.events, @@ -123,7 +123,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { if (!tokenizer.interrupt || definition_before) && tokenizer.parse_state.constructs.definition { tokenizer.enter(Token::Definition); // Note: arbitrary whitespace allowed even if code (indented) is on. - tokenizer.attempt_opt(space_or_tab(), before)(tokenizer, code) + tokenizer.attempt_opt(space_or_tab(), before)(tokenizer) } else { State::Nok } @@ -135,13 +135,12 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn before(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn before(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('[') => tokenizer.go( - |t, c| { + |t| { label( t, - c, LabelOptions { label: Token::DefinitionLabel, marker: Token::DefinitionLabelMarker, @@ -150,7 +149,7 @@ fn before(tokenizer: &mut Tokenizer, code: Code) -> State { ) }, label_after, - )(tokenizer, code), + )(tokenizer), _ => State::Nok, } } @@ -161,11 +160,11 @@ fn before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn label_after(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn label_after(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char(':') => { tokenizer.enter(Token::DefinitionMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::DefinitionMarker); State::Fn(Box::new( tokenizer.attempt_opt(space_or_tab_eol(), destination_before), @@ -181,12 +180,11 @@ fn label_after(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn destination_before(tokenizer: &mut Tokenizer, code: Code) -> State { +fn destination_before(tokenizer: &mut Tokenizer) -> State { tokenizer.go( - |t, c| { + |t| { destination( t, - c, DestinationOptions { limit: usize::MAX, destination: Token::DefinitionDestination, @@ -198,7 +196,7 @@ fn destination_before(tokenizer: &mut Tokenizer, code: Code) -> State { ) }, destination_after, - )(tokenizer, code) + )(tokenizer) } /// After a destination. @@ -207,8 +205,8 @@ fn destination_before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn destination_after(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.attempt_opt(title_before, after)(tokenizer, code) +fn destination_after(tokenizer: &mut Tokenizer) -> State { + tokenizer.attempt_opt(title_before, after)(tokenizer) } /// After a definition. @@ -219,8 +217,8 @@ fn destination_after(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn after(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.attempt_opt(space_or_tab(), after_whitespace)(tokenizer, code) +fn after(tokenizer: &mut Tokenizer) -> State { + tokenizer.attempt_opt(space_or_tab(), after_whitespace)(tokenizer) } /// After a definition, after optional whitespace. @@ -231,8 +229,8 @@ fn after(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn after_whitespace(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn after_whitespace(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::Definition); // You’d be interrupting. @@ -251,8 +249,8 @@ fn after_whitespace(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn title_before(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.go(space_or_tab_eol(), title_before_marker)(tokenizer, code) +fn title_before(tokenizer: &mut Tokenizer) -> State { + tokenizer.go(space_or_tab_eol(), title_before_marker)(tokenizer) } /// Before a title, after a line ending. @@ -262,12 +260,11 @@ fn title_before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | "c" /// ^ /// ``` -fn title_before_marker(tokenizer: &mut Tokenizer, code: Code) -> State { +fn title_before_marker(tokenizer: &mut Tokenizer) -> State { tokenizer.go( - |t, c| { + |t| { title( t, - c, TitleOptions { title: Token::DefinitionTitle, marker: Token::DefinitionTitleMarker, @@ -276,7 +273,7 @@ fn title_before_marker(tokenizer: &mut Tokenizer, code: Code) -> State { ) }, title_after, - )(tokenizer, code) + )(tokenizer) } /// After a title. @@ -285,8 +282,8 @@ fn title_before_marker(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn title_after(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.attempt_opt(space_or_tab(), title_after_after_optional_whitespace)(tokenizer, code) +fn title_after(tokenizer: &mut Tokenizer) -> State { + tokenizer.attempt_opt(space_or_tab(), title_after_after_optional_whitespace)(tokenizer) } /// After a title, after optional whitespace. @@ -295,8 +292,8 @@ fn title_after(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a]: b "c" /// ^ /// ``` -fn title_after_after_optional_whitespace(_tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn title_after_after_optional_whitespace(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => State::Ok, _ => State::Nok, } diff --git a/src/construct/hard_break_escape.rs b/src/construct/hard_break_escape.rs index 85a64f7..d45d685 100644 --- a/src/construct/hard_break_escape.rs +++ b/src/construct/hard_break_escape.rs @@ -50,12 +50,12 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// ^ /// | b /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('\\') if tokenizer.parse_state.constructs.hard_break_escape => { tokenizer.enter(Token::HardBreakEscape); tokenizer.enter(Token::HardBreakEscapeMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::HardBreakEscapeMarker); State::Fn(Box::new(inside)) } @@ -70,8 +70,8 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// ^ /// | b /// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn inside(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::HardBreakEscape); State::Ok diff --git a/src/construct/hard_break_trailing.rs b/src/construct/hard_break_trailing.rs index 1d1f10f..f0ef83b 100644 --- a/src/construct/hard_break_trailing.rs +++ b/src/construct/hard_break_trailing.rs @@ -51,13 +51,13 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// ^ /// | b /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char(' ') if tokenizer.parse_state.constructs.hard_break_trailing => { tokenizer.enter(Token::HardBreakTrailing); tokenizer.enter(Token::HardBreakTrailingSpace); - tokenizer.consume(code); - State::Fn(Box::new(|t, c| inside(t, c, 1))) + tokenizer.consume(); + State::Fn(Box::new(|t| inside(t, 1))) } _ => State::Nok, } @@ -70,11 +70,11 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// ^ /// | b /// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { - match code { +fn inside(tokenizer: &mut Tokenizer, size: usize) -> State { + match tokenizer.current { Code::Char(' ') => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| inside(t, c, size + 1))) + tokenizer.consume(); + State::Fn(Box::new(move |t| inside(t, size + 1))) } Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') if size >= HARD_BREAK_PREFIX_SIZE_MIN => diff --git a/src/construct/heading_atx.rs b/src/construct/heading_atx.rs index 65022d5..5de9a80 100644 --- a/src/construct/heading_atx.rs +++ b/src/construct/heading_atx.rs @@ -65,7 +65,7 @@ use crate::tokenizer::{Code, ContentType, Event, EventType, State, Tokenizer}; /// > | ## aa /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { @@ -74,7 +74,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { if tokenizer.parse_state.constructs.heading_atx { tokenizer.enter(Token::HeadingAtx); - tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer) } else { State::Nok } @@ -86,10 +86,10 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ## aa /// ^ /// ``` -fn before(tokenizer: &mut Tokenizer, code: Code) -> State { - if Code::Char('#') == code { +fn before(tokenizer: &mut Tokenizer) -> State { + if Code::Char('#') == tokenizer.current { tokenizer.enter(Token::HeadingAtxSequence); - sequence_open(tokenizer, code, 0) + sequence_open(tokenizer, 0) } else { State::Nok } @@ -101,21 +101,21 @@ fn before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ## aa /// ^ /// ``` -fn sequence_open(tokenizer: &mut Tokenizer, code: Code, rank: usize) -> State { - match code { +fn sequence_open(tokenizer: &mut Tokenizer, rank: usize) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') if rank > 0 => { tokenizer.exit(Token::HeadingAtxSequence); - at_break(tokenizer, code) + at_break(tokenizer) } Code::Char('#') if rank < HEADING_ATX_OPENING_FENCE_SIZE_MAX => { - tokenizer.consume(code); - State::Fn(Box::new(move |tokenizer, code| { - sequence_open(tokenizer, code, rank + 1) + tokenizer.consume(); + State::Fn(Box::new(move |tokenizer| { + sequence_open(tokenizer, rank + 1) })) } _ if rank > 0 => { tokenizer.exit(Token::HeadingAtxSequence); - tokenizer.go(space_or_tab(), at_break)(tokenizer, code) + tokenizer.go(space_or_tab(), at_break)(tokenizer) } _ => State::Nok, } @@ -127,8 +127,8 @@ fn sequence_open(tokenizer: &mut Tokenizer, code: Code, rank: usize) -> State { /// > | ## aa /// ^ /// ``` -fn at_break(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn at_break(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::HeadingAtx); tokenizer.register_resolver("heading_atx".to_string(), Box::new(resolve)); @@ -137,15 +137,15 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code) -> State { State::Ok } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.go(space_or_tab(), at_break)(tokenizer, code) + tokenizer.go(space_or_tab(), at_break)(tokenizer) } Code::Char('#') => { tokenizer.enter(Token::HeadingAtxSequence); - further_sequence(tokenizer, code) + further_sequence(tokenizer) } Code::Char(_) => { tokenizer.enter_with_content(Token::Data, Some(ContentType::Text)); - data(tokenizer, code) + data(tokenizer) } } } @@ -158,13 +158,13 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ## aa ## /// ^ /// ``` -fn further_sequence(tokenizer: &mut Tokenizer, code: Code) -> State { - if let Code::Char('#') = code { - tokenizer.consume(code); +fn further_sequence(tokenizer: &mut Tokenizer) -> State { + if let Code::Char('#') = tokenizer.current { + tokenizer.consume(); State::Fn(Box::new(further_sequence)) } else { tokenizer.exit(Token::HeadingAtxSequence); - at_break(tokenizer, code) + at_break(tokenizer) } } @@ -174,15 +174,15 @@ fn further_sequence(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | ## aa /// ^ /// ``` -fn data(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn data(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { // Note: `#` for closing sequence must be preceded by whitespace, otherwise it’s just text. Code::None | Code::CarriageReturnLineFeed | Code::Char('\t' | '\n' | '\r' | ' ') => { tokenizer.exit(Token::Data); - at_break(tokenizer, code) + at_break(tokenizer) } _ => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(data)) } } diff --git a/src/construct/heading_setext.rs b/src/construct/heading_setext.rs index 7ac2eb6..a0f7545 100644 --- a/src/construct/heading_setext.rs +++ b/src/construct/heading_setext.rs @@ -116,7 +116,7 @@ impl Kind { /// > | == /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { @@ -133,7 +133,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { // Require a paragraph before and do not allow on a lazy line. if paragraph_before && !tokenizer.lazy && tokenizer.parse_state.constructs.heading_setext { - tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer) } else { State::Nok } @@ -146,11 +146,11 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | == /// ^ /// ``` -fn before(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn before(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char(char) if char == '-' || char == '=' => { tokenizer.enter(Token::HeadingSetextUnderline); - inside(tokenizer, code, Kind::from_char(char)) + inside(tokenizer, Kind::from_char(char)) } _ => State::Nok, } @@ -163,15 +163,15 @@ fn before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | == /// ^ /// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code, kind: Kind) -> State { - match code { +fn inside(tokenizer: &mut Tokenizer, kind: Kind) -> State { + match tokenizer.current { Code::Char(char) if char == kind.as_char() => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| inside(t, c, kind))) + tokenizer.consume(); + State::Fn(Box::new(move |t| inside(t, kind))) } _ => { tokenizer.exit(Token::HeadingSetextUnderline); - tokenizer.attempt_opt(space_or_tab(), after)(tokenizer, code) + tokenizer.attempt_opt(space_or_tab(), after)(tokenizer) } } } @@ -183,8 +183,8 @@ fn inside(tokenizer: &mut Tokenizer, code: Code, kind: Kind) -> State { /// > | == /// ^ /// ``` -fn after(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn after(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { // Feel free to interrupt. tokenizer.interrupt = false; diff --git a/src/construct/html_flow.rs b/src/construct/html_flow.rs index 30c14a1..24d6f98 100644 --- a/src/construct/html_flow.rs +++ b/src/construct/html_flow.rs @@ -203,7 +203,7 @@ struct Info { /// > | /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { @@ -221,7 +221,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { content_type: None, }), before, - )(tokenizer, code) + )(tokenizer) } else { State::Nok } @@ -233,10 +233,10 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | /// ^ /// ``` -fn before(tokenizer: &mut Tokenizer, code: Code) -> State { - if Code::Char('<') == code { +fn before(tokenizer: &mut Tokenizer) -> State { + if Code::Char('<') == tokenizer.current { tokenizer.enter(Token::HtmlFlowData); - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(open)) } else { State::Nok @@ -253,7 +253,7 @@ fn before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | /// ^ /// ``` -fn open(tokenizer: &mut Tokenizer, code: Code) -> State { +fn open(tokenizer: &mut Tokenizer) -> State { let mut info = Info { // Assume basic. kind: Kind::Basic, @@ -264,27 +264,27 @@ fn open(tokenizer: &mut Tokenizer, code: Code) -> State { quote: None, }; - match code { + match tokenizer.current { Code::Char('!') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| declaration_open(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| declaration_open(t, info))) } Code::Char('/') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| tag_close_start(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| tag_close_start(t, info))) } Code::Char('?') => { info.kind = Kind::Instruction; - tokenizer.consume(code); + tokenizer.consume(); // Do not form containers. tokenizer.concrete = true; // While we’re in an instruction instead of a declaration, we’re on a `?` // right now, so we do need to search for `>`, similar to declarations. - State::Fn(Box::new(|t, c| continuation_declaration_inside(t, c, info))) + State::Fn(Box::new(|t| continuation_declaration_inside(t, info))) } Code::Char('A'..='Z' | 'a'..='z') => { info.start_tag = true; - tag_name(tokenizer, code, info) + tag_name(tokenizer, info) } _ => State::Nok, } @@ -300,26 +300,26 @@ fn open(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | &<]]> /// ^ /// ``` -fn declaration_open(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn declaration_open(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char('-') => { - tokenizer.consume(code); + tokenizer.consume(); info.kind = Kind::Comment; - State::Fn(Box::new(|t, c| comment_open_inside(t, c, info))) + State::Fn(Box::new(|t| comment_open_inside(t, info))) } Code::Char('[') => { - tokenizer.consume(code); + tokenizer.consume(); info.kind = Kind::Cdata; info.buffer = parse("CDATA["); info.index = 0; - State::Fn(Box::new(|t, c| cdata_open_inside(t, c, info))) + State::Fn(Box::new(|t| cdata_open_inside(t, info))) } Code::Char('A'..='Z' | 'a'..='z') => { - tokenizer.consume(code); + tokenizer.consume(); info.kind = Kind::Declaration; // Do not form containers. tokenizer.concrete = true; - State::Fn(Box::new(|t, c| continuation_declaration_inside(t, c, info))) + State::Fn(Box::new(|t| continuation_declaration_inside(t, info))) } _ => State::Nok, } @@ -331,13 +331,13 @@ fn declaration_open(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> St /// > | /// ^ /// ``` -fn comment_open_inside(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn comment_open_inside(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('-') => { - tokenizer.consume(code); + tokenizer.consume(); // Do not form containers. tokenizer.concrete = true; - State::Fn(Box::new(|t, c| continuation_declaration_inside(t, c, info))) + State::Fn(Box::new(|t| continuation_declaration_inside(t, info))) } _ => State::Nok, } @@ -349,18 +349,18 @@ fn comment_open_inside(tokenizer: &mut Tokenizer, code: Code, info: Info) -> Sta /// > | &<]]> /// ^^^^^^ /// ``` -fn cdata_open_inside(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - if code == info.buffer[info.index] { +fn cdata_open_inside(tokenizer: &mut Tokenizer, mut info: Info) -> State { + if tokenizer.current == info.buffer[info.index] { info.index += 1; - tokenizer.consume(code); + tokenizer.consume(); if info.index == info.buffer.len() { info.buffer.clear(); // Do not form containers. tokenizer.concrete = true; - State::Fn(Box::new(|t, c| continuation(t, c, info))) + State::Fn(Box::new(|t| continuation(t, info))) } else { - State::Fn(Box::new(|t, c| cdata_open_inside(t, c, info))) + State::Fn(Box::new(|t| cdata_open_inside(t, info))) } } else { State::Nok @@ -373,12 +373,12 @@ fn cdata_open_inside(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> S /// > | /// ^ /// ``` -fn tag_close_start(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn tag_close_start(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char('A'..='Z' | 'a'..='z') => { - tokenizer.consume(code); - info.buffer.push(code); - State::Fn(Box::new(|t, c| tag_name(t, c, info))) + tokenizer.consume(); + info.buffer.push(tokenizer.current); + State::Fn(Box::new(|t| tag_name(t, info))) } _ => State::Nok, } @@ -392,15 +392,15 @@ fn tag_close_start(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> Sta /// > | /// ^^ /// ``` -fn tag_name(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn tag_name(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::VirtualSpace | Code::Char('\t' | '\n' | '\r' | ' ' | '/' | '>') => { let tag_name_buffer = serialize(&info.buffer, false).to_lowercase(); let name = tag_name_buffer.as_str(); - let slash = matches!(code, Code::Char('/')); + let slash = matches!(tokenizer.current, Code::Char('/')); info.buffer.clear(); @@ -408,16 +408,16 @@ fn tag_name(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { info.kind = Kind::Raw; // Do not form containers. tokenizer.concrete = true; - continuation(tokenizer, code, info) + continuation(tokenizer, info) } else if HTML_BLOCK_NAMES.contains(&name) { // Basic is assumed, no need to set `kind`. if slash { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| basic_self_closing(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| basic_self_closing(t, info))) } else { // Do not form containers. tokenizer.concrete = true; - continuation(tokenizer, code, info) + continuation(tokenizer, info) } } else { info.kind = Kind::Complete; @@ -426,16 +426,16 @@ fn tag_name(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { if tokenizer.interrupt && !tokenizer.lazy { State::Nok } else if info.start_tag { - complete_attribute_name_before(tokenizer, code, info) + complete_attribute_name_before(tokenizer, info) } else { - complete_closing_tag_after(tokenizer, code, info) + complete_closing_tag_after(tokenizer, info) } } } Code::Char('-' | '0'..='9' | 'A'..='Z' | 'a'..='z') => { - tokenizer.consume(code); - info.buffer.push(code); - State::Fn(Box::new(|t, c| tag_name(t, c, info))) + tokenizer.consume(); + info.buffer.push(tokenizer.current); + State::Fn(Box::new(|t| tag_name(t, info))) } Code::Char(_) => State::Nok, } @@ -447,13 +447,13 @@ fn tag_name(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { /// > |
/// ^ /// ``` -fn basic_self_closing(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn basic_self_closing(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('>') => { - tokenizer.consume(code); + tokenizer.consume(); // Do not form containers. tokenizer.concrete = true; - State::Fn(Box::new(|t, c| continuation(t, c, info))) + State::Fn(Box::new(|t| continuation(t, info))) } _ => State::Nok, } @@ -465,13 +465,13 @@ fn basic_self_closing(tokenizer: &mut Tokenizer, code: Code, info: Info) -> Stat /// > | /// ^ /// ``` -fn complete_closing_tag_after(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn complete_closing_tag_after(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_closing_tag_after(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_closing_tag_after(t, info))) } - _ => complete_end(tokenizer, code, info), + _ => complete_end(tokenizer, info), } } @@ -494,21 +494,21 @@ fn complete_closing_tag_after(tokenizer: &mut Tokenizer, code: Code, info: Info) /// > | /// ^ /// ``` -fn complete_attribute_name_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn complete_attribute_name_before(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('/') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_end(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_end(t, info))) } Code::Char('0'..='9' | ':' | 'A'..='Z' | '_' | 'a'..='z') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_attribute_name(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_name(t, info))) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_attribute_name_before(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_name_before(t, info))) } - _ => complete_end(tokenizer, code, info), + _ => complete_end(tokenizer, info), } } @@ -522,13 +522,13 @@ fn complete_attribute_name_before(tokenizer: &mut Tokenizer, code: Code, info: I /// > | /// ^ /// ``` -fn complete_attribute_name(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn complete_attribute_name(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('-' | '.' | '0'..='9' | ':' | 'A'..='Z' | '_' | 'a'..='z') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_attribute_name(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_name(t, info))) } - _ => complete_attribute_name_after(tokenizer, code, info), + _ => complete_attribute_name_after(tokenizer, info), } } @@ -541,17 +541,17 @@ fn complete_attribute_name(tokenizer: &mut Tokenizer, code: Code, info: Info) -> /// > | /// ^ /// ``` -fn complete_attribute_name_after(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn complete_attribute_name_after(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('=') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_attribute_value_before(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_value_before(t, info))) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_attribute_name_after(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_name_after(t, info))) } - _ => complete_attribute_name_before(tokenizer, code, info), + _ => complete_attribute_name_before(tokenizer, info), } } @@ -564,19 +564,19 @@ fn complete_attribute_name_after(tokenizer: &mut Tokenizer, code: Code, info: In /// > | /// ^ /// ``` -fn complete_attribute_value_before(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn complete_attribute_value_before(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::None | Code::Char('<' | '=' | '>' | '`') => State::Nok, Code::Char('"' | '\'') => { - tokenizer.consume(code); - info.quote = Some(QuoteKind::from_code(code)); - State::Fn(Box::new(|t, c| complete_attribute_value_quoted(t, c, info))) + tokenizer.consume(); + info.quote = Some(QuoteKind::from_code(tokenizer.current)); + State::Fn(Box::new(|t| complete_attribute_value_quoted(t, info))) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_attribute_value_before(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_value_before(t, info))) } - _ => complete_attribute_value_unquoted(tokenizer, code, info), + _ => complete_attribute_value_unquoted(tokenizer, info), } } @@ -588,18 +588,16 @@ fn complete_attribute_value_before(tokenizer: &mut Tokenizer, code: Code, mut in /// > | /// ^ /// ``` -fn complete_attribute_value_quoted(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn complete_attribute_value_quoted(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => State::Nok, Code::Char(char) if char == info.quote.as_ref().unwrap().as_char() => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| { - complete_attribute_value_quoted_after(t, c, info) - })) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_value_quoted_after(t, info))) } _ => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_attribute_value_quoted(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_value_quoted(t, info))) } } } @@ -610,19 +608,17 @@ fn complete_attribute_value_quoted(tokenizer: &mut Tokenizer, code: Code, info: /// > | /// ^ /// ``` -fn complete_attribute_value_unquoted(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn complete_attribute_value_unquoted(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::VirtualSpace | Code::Char('\t' | '\n' | '\r' | ' ' | '"' | '\'' | '/' | '<' | '=' | '>' | '`') => { - complete_attribute_name_after(tokenizer, code, info) + complete_attribute_name_after(tokenizer, info) } Code::Char(_) => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| { - complete_attribute_value_unquoted(t, c, info) - })) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_attribute_value_unquoted(t, info))) } } } @@ -634,14 +630,10 @@ fn complete_attribute_value_unquoted(tokenizer: &mut Tokenizer, code: Code, info /// > | /// ^ /// ``` -fn complete_attribute_value_quoted_after( - tokenizer: &mut Tokenizer, - code: Code, - info: Info, -) -> State { - match code { +fn complete_attribute_value_quoted_after(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::VirtualSpace | Code::Char('\t' | ' ' | '/' | '>') => { - complete_attribute_name_before(tokenizer, code, info) + complete_attribute_name_before(tokenizer, info) } _ => State::Nok, } @@ -653,11 +645,11 @@ fn complete_attribute_value_quoted_after( /// > | /// ^ /// ``` -fn complete_end(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn complete_end(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('>') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_after(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_after(t, info))) } _ => State::Nok, } @@ -669,16 +661,16 @@ fn complete_end(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | /// ^ /// ``` -fn complete_after(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn complete_after(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { // Do not form containers. tokenizer.concrete = true; - continuation(tokenizer, code, info) + continuation(tokenizer, info) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| complete_after(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| complete_after(t, info))) } Code::Char(_) => State::Nok, } @@ -690,29 +682,27 @@ fn complete_after(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | /// ^ /// ``` -fn continuation(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn continuation(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('-') if info.kind == Kind::Comment => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_comment_inside(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_comment_inside(t, info))) } Code::Char('<') if info.kind == Kind::Raw => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_raw_tag_open(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_raw_tag_open(t, info))) } Code::Char('>') if info.kind == Kind::Declaration => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_close(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_close(t, info))) } Code::Char('?') if info.kind == Kind::Instruction => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_declaration_inside(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_declaration_inside(t, info))) } Code::Char(']') if info.kind == Kind::Cdata => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| { - continuation_character_data_inside(t, c, info) - })) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_character_data_inside(t, info))) } Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') if info.kind == Kind::Basic || info.kind == Kind::Complete => @@ -722,17 +712,17 @@ fn continuation(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { if ok { Box::new(continuation_after) } else { - Box::new(move |t, c| continuation_start(t, c, info)) + Box::new(move |t| continuation_start(t, info)) } - })(tokenizer, code) + })(tokenizer) } Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::HtmlFlowData); - continuation_start(tokenizer, code, info) + continuation_start(tokenizer, info) } _ => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation(t, info))) } } } @@ -744,14 +734,14 @@ fn continuation(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// ^ /// | asd /// ``` -fn continuation_start(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { +fn continuation_start(tokenizer: &mut Tokenizer, info: Info) -> State { tokenizer.check(partial_non_lazy_continuation, |ok| { if ok { - Box::new(move |t, c| continuation_start_non_lazy(t, c, info)) + Box::new(move |t| continuation_start_non_lazy(t, info)) } else { Box::new(continuation_after) } - })(tokenizer, code) + })(tokenizer) } /// In continuation, at an eol, before non-lazy content. @@ -761,13 +751,13 @@ fn continuation_start(tokenizer: &mut Tokenizer, code: Code, info: Info) -> Stat /// ^ /// | asd /// ``` -fn continuation_start_non_lazy(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn continuation_start_non_lazy(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); - State::Fn(Box::new(|t, c| continuation_before(t, c, info))) + State::Fn(Box::new(|t| continuation_before(t, info))) } _ => unreachable!("expected eol"), } @@ -780,14 +770,14 @@ fn continuation_start_non_lazy(tokenizer: &mut Tokenizer, code: Code, info: Info /// > | asd /// ^ /// ``` -fn continuation_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn continuation_before(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - continuation_start(tokenizer, code, info) + continuation_start(tokenizer, info) } _ => { tokenizer.enter(Token::HtmlFlowData); - continuation(tokenizer, code, info) + continuation(tokenizer, info) } } } @@ -798,13 +788,13 @@ fn continuation_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> Sta /// > | /// ^ /// ``` -fn continuation_comment_inside(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn continuation_comment_inside(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('-') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_declaration_inside(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_declaration_inside(t, info))) } - _ => continuation(tokenizer, code, info), + _ => continuation(tokenizer, info), } } @@ -814,13 +804,13 @@ fn continuation_comment_inside(tokenizer: &mut Tokenizer, code: Code, info: Info /// > | /// ^ /// ``` -fn continuation_raw_tag_open(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn continuation_raw_tag_open(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('/') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_raw_end_tag(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_raw_end_tag(t, info))) } - _ => continuation(tokenizer, code, info), + _ => continuation(tokenizer, info), } } @@ -830,27 +820,27 @@ fn continuation_raw_tag_open(tokenizer: &mut Tokenizer, code: Code, info: Info) /// > | /// ^^^^^^ /// ``` -fn continuation_raw_end_tag(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn continuation_raw_end_tag(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char('>') => { let tag_name_buffer = serialize(&info.buffer, false).to_lowercase(); info.buffer.clear(); if HTML_RAW_NAMES.contains(&tag_name_buffer.as_str()) { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_close(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_close(t, info))) } else { - continuation(tokenizer, code, info) + continuation(tokenizer, info) } } Code::Char('A'..='Z' | 'a'..='z') if info.buffer.len() < HTML_RAW_SIZE_MAX => { - tokenizer.consume(code); - info.buffer.push(code); - State::Fn(Box::new(|t, c| continuation_raw_end_tag(t, c, info))) + tokenizer.consume(); + info.buffer.push(tokenizer.current); + State::Fn(Box::new(|t| continuation_raw_end_tag(t, info))) } _ => { info.buffer.clear(); - continuation(tokenizer, code, info) + continuation(tokenizer, info) } } } @@ -861,13 +851,13 @@ fn continuation_raw_end_tag(tokenizer: &mut Tokenizer, code: Code, mut info: Inf /// > | &<]]> /// ^ /// ``` -fn continuation_character_data_inside(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn continuation_character_data_inside(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char(']') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_declaration_inside(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_declaration_inside(t, info))) } - _ => continuation(tokenizer, code, info), + _ => continuation(tokenizer, info), } } @@ -885,17 +875,17 @@ fn continuation_character_data_inside(tokenizer: &mut Tokenizer, code: Code, inf /// > | &<]]> /// ^ /// ``` -fn continuation_declaration_inside(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn continuation_declaration_inside(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('>') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_close(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_close(t, info))) } Code::Char('-') if info.kind == Kind::Comment => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_declaration_inside(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_declaration_inside(t, info))) } - _ => continuation(tokenizer, code, info), + _ => continuation(tokenizer, info), } } @@ -905,15 +895,15 @@ fn continuation_declaration_inside(tokenizer: &mut Tokenizer, code: Code, info: /// > | /// ^ /// ``` -fn continuation_close(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn continuation_close(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::HtmlFlowData); - continuation_after(tokenizer, code) + continuation_after(tokenizer) } _ => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| continuation_close(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| continuation_close(t, info))) } } } @@ -924,7 +914,7 @@ fn continuation_close(tokenizer: &mut Tokenizer, code: Code, info: Info) -> Stat /// > | /// ^ /// ``` -fn continuation_after(tokenizer: &mut Tokenizer, _code: Code) -> State { +fn continuation_after(tokenizer: &mut Tokenizer) -> State { tokenizer.exit(Token::HtmlFlow); // Feel free to interrupt. tokenizer.interrupt = false; @@ -940,9 +930,9 @@ fn continuation_after(tokenizer: &mut Tokenizer, _code: Code) -> State { /// ^ /// | /// ``` -fn blank_line_before(tokenizer: &mut Tokenizer, code: Code) -> State { +fn blank_line_before(tokenizer: &mut Tokenizer) -> State { tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); State::Fn(Box::new(blank_line)) } diff --git a/src/construct/html_text.rs b/src/construct/html_text.rs index 8fbe862..3ac8d71 100644 --- a/src/construct/html_text.rs +++ b/src/construct/html_text.rs @@ -65,11 +65,11 @@ use crate::util::codes::parse; /// > | a c /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - if Code::Char('<') == code && tokenizer.parse_state.constructs.html_text { +pub fn start(tokenizer: &mut Tokenizer) -> State { + if Code::Char('<') == tokenizer.current && tokenizer.parse_state.constructs.html_text { tokenizer.enter(Token::HtmlText); tokenizer.enter(Token::HtmlTextData); - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(open)) } else { State::Nok @@ -86,22 +86,22 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn open(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn open(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('!') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(declaration_open)) } Code::Char('/') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_close_start)) } Code::Char('?') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(instruction)) } Code::Char('A'..='Z' | 'a'..='z') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open)) } _ => State::Nok, @@ -118,19 +118,19 @@ fn open(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a &<]]> c /// ^ /// ``` -fn declaration_open(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn declaration_open(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('-') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(comment_open_inside)) } Code::Char('[') => { - tokenizer.consume(code); + tokenizer.consume(); let buffer = parse("CDATA["); - State::Fn(Box::new(|t, c| cdata_open_inside(t, c, buffer, 0))) + State::Fn(Box::new(|t| cdata_open_inside(t, buffer, 0))) } Code::Char('A'..='Z' | 'a'..='z') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(declaration)) } _ => State::Nok, @@ -143,10 +143,10 @@ fn declaration_open(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn comment_open_inside(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn comment_open_inside(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('-') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(comment_start)) } _ => State::Nok, @@ -166,14 +166,14 @@ fn comment_open_inside(tokenizer: &mut Tokenizer, code: Code) -> State { /// ``` /// /// [html_flow]: crate::construct::html_flow -fn comment_start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn comment_start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::Char('>') => State::Nok, Code::Char('-') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(comment_start_dash)) } - _ => comment(tokenizer, code), + _ => comment(tokenizer), } } @@ -190,10 +190,10 @@ fn comment_start(tokenizer: &mut Tokenizer, code: Code) -> State { /// ``` /// /// [html_flow]: crate::construct::html_flow -fn comment_start_dash(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn comment_start_dash(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::Char('>') => State::Nok, - _ => comment(tokenizer, code), + _ => comment(tokenizer), } } @@ -203,18 +203,18 @@ fn comment_start_dash(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn comment(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn comment(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None => State::Nok, Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_line_ending(tokenizer, code, Box::new(comment)) + at_line_ending(tokenizer, Box::new(comment)) } Code::Char('-') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(comment_close)) } _ => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(comment)) } } @@ -226,13 +226,13 @@ fn comment(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn comment_close(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn comment_close(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('-') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(end)) } - _ => comment(tokenizer, code), + _ => comment(tokenizer), } } @@ -242,21 +242,14 @@ fn comment_close(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a &<]]> b /// ^^^^^^ /// ``` -fn cdata_open_inside( - tokenizer: &mut Tokenizer, - code: Code, - buffer: Vec, - index: usize, -) -> State { - if code == buffer[index] { - tokenizer.consume(code); +fn cdata_open_inside(tokenizer: &mut Tokenizer, buffer: Vec, index: usize) -> State { + if tokenizer.current == buffer[index] { + tokenizer.consume(); if index + 1 == buffer.len() { State::Fn(Box::new(cdata)) } else { - State::Fn(Box::new(move |t, c| { - cdata_open_inside(t, c, buffer, index + 1) - })) + State::Fn(Box::new(move |t| cdata_open_inside(t, buffer, index + 1))) } } else { State::Nok @@ -269,18 +262,18 @@ fn cdata_open_inside( /// > | a &<]]> b /// ^^^ /// ``` -fn cdata(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn cdata(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None => State::Nok, Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_line_ending(tokenizer, code, Box::new(cdata)) + at_line_ending(tokenizer, Box::new(cdata)) } Code::Char(']') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(cdata_close)) } _ => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(cdata)) } } @@ -292,13 +285,13 @@ fn cdata(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a &<]]> b /// ^ /// ``` -fn cdata_close(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn cdata_close(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char(']') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(cdata_end)) } - _ => cdata(tokenizer, code), + _ => cdata(tokenizer), } } @@ -308,11 +301,11 @@ fn cdata_close(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a &<]]> b /// ^ /// ``` -fn cdata_end(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { - Code::Char('>') => end(tokenizer, code), - Code::Char(']') => cdata_close(tokenizer, code), - _ => cdata(tokenizer, code), +fn cdata_end(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { + Code::Char('>') => end(tokenizer), + Code::Char(']') => cdata_close(tokenizer), + _ => cdata(tokenizer), } } @@ -322,14 +315,14 @@ fn cdata_end(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn declaration(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { - Code::None | Code::Char('>') => end(tokenizer, code), +fn declaration(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { + Code::None | Code::Char('>') => end(tokenizer), Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_line_ending(tokenizer, code, Box::new(declaration)) + at_line_ending(tokenizer, Box::new(declaration)) } _ => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(declaration)) } } @@ -341,18 +334,18 @@ fn declaration(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn instruction(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn instruction(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None => State::Nok, Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_line_ending(tokenizer, code, Box::new(instruction)) + at_line_ending(tokenizer, Box::new(instruction)) } Code::Char('?') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(instruction_close)) } _ => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(instruction)) } } @@ -364,10 +357,10 @@ fn instruction(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn instruction_close(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { - Code::Char('>') => end(tokenizer, code), - _ => instruction(tokenizer, code), +fn instruction_close(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { + Code::Char('>') => end(tokenizer), + _ => instruction(tokenizer), } } @@ -377,10 +370,10 @@ fn instruction_close(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn tag_close_start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_close_start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('A'..='Z' | 'a'..='z') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_close)) } _ => State::Nok, @@ -393,13 +386,13 @@ fn tag_close_start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn tag_close(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_close(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('-' | '0'..='9' | 'A'..='Z' | 'a'..='z') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_close)) } - _ => tag_close_between(tokenizer, code), + _ => tag_close_between(tokenizer), } } @@ -409,16 +402,16 @@ fn tag_close(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn tag_close_between(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_close_between(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_line_ending(tokenizer, code, Box::new(tag_close_between)) + at_line_ending(tokenizer, Box::new(tag_close_between)) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_close_between)) } - _ => end(tokenizer, code), + _ => end(tokenizer), } } @@ -428,15 +421,15 @@ fn tag_close_between(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn tag_open(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_open(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('-' | '0'..='9' | 'A'..='Z' | 'a'..='z') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open)) } Code::CarriageReturnLineFeed | Code::VirtualSpace - | Code::Char('\t' | '\n' | '\r' | ' ' | '/' | '>') => tag_open_between(tokenizer, code), + | Code::Char('\t' | '\n' | '\r' | ' ' | '/' | '>') => tag_open_between(tokenizer), _ => State::Nok, } } @@ -447,24 +440,24 @@ fn tag_open(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a c /// ^ /// ``` -fn tag_open_between(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_open_between(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_line_ending(tokenizer, code, Box::new(tag_open_between)) + at_line_ending(tokenizer, Box::new(tag_open_between)) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_between)) } Code::Char('/') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(end)) } Code::Char(':' | 'A'..='Z' | '_' | 'a'..='z') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_attribute_name)) } - _ => end(tokenizer, code), + _ => end(tokenizer), } } @@ -474,13 +467,13 @@ fn tag_open_between(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a d /// ^ /// ``` -fn tag_open_attribute_name(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_open_attribute_name(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('-' | '.' | '0'..='9' | ':' | 'A'..='Z' | '_' | 'a'..='z') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_attribute_name)) } - _ => tag_open_attribute_name_after(tokenizer, code), + _ => tag_open_attribute_name_after(tokenizer), } } @@ -491,20 +484,20 @@ fn tag_open_attribute_name(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a d /// ^ /// ``` -fn tag_open_attribute_name_after(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_open_attribute_name_after(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_line_ending(tokenizer, code, Box::new(tag_open_attribute_name_after)) + at_line_ending(tokenizer, Box::new(tag_open_attribute_name_after)) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_attribute_name_after)) } Code::Char('=') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_attribute_value_before)) } - _ => tag_open_between(tokenizer, code), + _ => tag_open_between(tokenizer), } } @@ -515,24 +508,22 @@ fn tag_open_attribute_name_after(tokenizer: &mut Tokenizer, code: Code) -> State /// > | a e /// ^ /// ``` -fn tag_open_attribute_value_before(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_open_attribute_value_before(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::Char('<' | '=' | '>' | '`') => State::Nok, Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { - at_line_ending(tokenizer, code, Box::new(tag_open_attribute_value_before)) + at_line_ending(tokenizer, Box::new(tag_open_attribute_value_before)) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_attribute_value_before)) } Code::Char(char) if char == '"' || char == '\'' => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| { - tag_open_attribute_value_quoted(t, c, char) - })) + tokenizer.consume(); + State::Fn(Box::new(move |t| tag_open_attribute_value_quoted(t, char))) } Code::Char(_) => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_attribute_value_unquoted)) } } @@ -544,22 +535,21 @@ fn tag_open_attribute_value_before(tokenizer: &mut Tokenizer, code: Code) -> Sta /// > | a e /// ^ /// ``` -fn tag_open_attribute_value_quoted(tokenizer: &mut Tokenizer, code: Code, marker: char) -> State { - match code { +fn tag_open_attribute_value_quoted(tokenizer: &mut Tokenizer, marker: char) -> State { + match tokenizer.current { Code::None => State::Nok, Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => at_line_ending( tokenizer, - code, - Box::new(move |t, c| tag_open_attribute_value_quoted(t, c, marker)), + Box::new(move |t| tag_open_attribute_value_quoted(t, marker)), ), Code::Char(char) if char == marker => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_attribute_value_quoted_after)) } _ => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| { - tag_open_attribute_value_quoted(t, c, marker) + tokenizer.consume(); + State::Fn(Box::new(move |t| { + tag_open_attribute_value_quoted(t, marker) })) } } @@ -571,14 +561,14 @@ fn tag_open_attribute_value_quoted(tokenizer: &mut Tokenizer, code: Code, marker /// > | a e /// ^ /// ``` -fn tag_open_attribute_value_unquoted(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_open_attribute_value_unquoted(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::Char('"' | '\'' | '<' | '=' | '`') => State::Nok, Code::CarriageReturnLineFeed | Code::VirtualSpace - | Code::Char('\t' | '\n' | '\r' | ' ' | '/' | '>') => tag_open_between(tokenizer, code), + | Code::Char('\t' | '\n' | '\r' | ' ' | '/' | '>') => tag_open_between(tokenizer), Code::Char(_) => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(tag_open_attribute_value_unquoted)) } } @@ -591,11 +581,11 @@ fn tag_open_attribute_value_unquoted(tokenizer: &mut Tokenizer, code: Code) -> S /// > | a e /// ^ /// ``` -fn tag_open_attribute_value_quoted_after(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn tag_open_attribute_value_quoted_after(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::VirtualSpace - | Code::Char('\t' | '\n' | '\r' | ' ' | '>' | '/') => tag_open_between(tokenizer, code), + | Code::Char('\t' | '\n' | '\r' | ' ' | '>' | '/') => tag_open_between(tokenizer), _ => State::Nok, } } @@ -606,10 +596,10 @@ fn tag_open_attribute_value_quoted_after(tokenizer: &mut Tokenizer, code: Code) /// > | a e /// ^ /// ``` -fn end(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn end(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('>') => { - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::HtmlTextData); tokenizer.exit(Token::HtmlText); State::Ok @@ -628,14 +618,14 @@ fn end(tokenizer: &mut Tokenizer, code: Code) -> State { /// ^ /// | b--> /// ``` -fn at_line_ending(tokenizer: &mut Tokenizer, code: Code, return_state: Box) -> State { - match code { +fn at_line_ending(tokenizer: &mut Tokenizer, return_state: Box) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::HtmlTextData); tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); - State::Fn(Box::new(|t, c| after_line_ending(t, c, return_state))) + State::Fn(Box::new(|t| after_line_ending(t, return_state))) } _ => unreachable!("expected eol"), } @@ -651,10 +641,10 @@ fn at_line_ending(tokenizer: &mut Tokenizer, code: Code, return_state: Box | b--> /// ^ /// ``` -fn after_line_ending(tokenizer: &mut Tokenizer, code: Code, return_state: Box) -> State { - tokenizer.attempt_opt(space_or_tab(), |t, c| { - after_line_ending_prefix(t, c, return_state) - })(tokenizer, code) +fn after_line_ending(tokenizer: &mut Tokenizer, return_state: Box) -> State { + tokenizer.attempt_opt(space_or_tab(), |t| { + after_line_ending_prefix(t, return_state) + })(tokenizer) } /// After a line ending, after indent. @@ -667,11 +657,7 @@ fn after_line_ending(tokenizer: &mut Tokenizer, code: Code, return_state: Box | b--> /// ^ /// ``` -fn after_line_ending_prefix( - tokenizer: &mut Tokenizer, - code: Code, - return_state: Box, -) -> State { +fn after_line_ending_prefix(tokenizer: &mut Tokenizer, return_state: Box) -> State { tokenizer.enter(Token::HtmlTextData); - return_state(tokenizer, code) + return_state(tokenizer) } diff --git a/src/construct/label_end.rs b/src/construct/label_end.rs index 960e95e..6f0a707 100644 --- a/src/construct/label_end.rs +++ b/src/construct/label_end.rs @@ -180,8 +180,8 @@ struct Info { /// ^ /// > | [a] b /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - if Code::Char(']') == code && tokenizer.parse_state.constructs.label_end { +pub fn start(tokenizer: &mut Tokenizer) -> State { + if Code::Char(']') == tokenizer.current && tokenizer.parse_state.constructs.label_end { let mut label_start_index = None; let mut index = tokenizer.label_start_stack.len(); @@ -203,7 +203,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { // Mark as balanced if the info is inactive. if label_start.inactive { - return nok(tokenizer, code, label_start_index); + return nok(tokenizer, label_start_index); } let label_end_start = tokenizer.events.len(); @@ -225,11 +225,11 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { tokenizer.enter(Token::LabelEnd); tokenizer.enter(Token::LabelMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LabelMarker); tokenizer.exit(Token::LabelEnd); - return State::Fn(Box::new(move |t, c| after(t, c, info))); + return State::Fn(Box::new(move |t| after(t, info))); } } @@ -248,39 +248,39 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a] b /// ^ /// ``` -fn after(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { +fn after(tokenizer: &mut Tokenizer, info: Info) -> State { let defined = tokenizer.parse_state.definitions.contains(&info.media.id); - match code { + match tokenizer.current { // Resource (`[asd](fgh)`)? Code::Char('(') => tokenizer.attempt(resource, move |is_ok| { - Box::new(move |t, c| { + Box::new(move |t| { // Also fine if `defined`, as then it’s a valid shortcut. if is_ok || defined { - ok(t, c, info) + ok(t, info) } else { - nok(t, c, info.label_start_index) + nok(t, info.label_start_index) } }) - })(tokenizer, code), + })(tokenizer), // Full (`[asd][fgh]`) or collapsed (`[asd][]`) reference? Code::Char('[') => tokenizer.attempt(full_reference, move |is_ok| { - Box::new(move |t, c| { + Box::new(move |t| { if is_ok { - ok(t, c, info) + ok(t, info) } else if defined { - reference_not_full(t, c, info) + reference_not_full(t, info) } else { - nok(t, c, info.label_start_index) + nok(t, info.label_start_index) } }) - })(tokenizer, code), + })(tokenizer), // Shortcut reference: `[asd]`? _ => { if defined { - ok(tokenizer, code, info) + ok(tokenizer, info) } else { - nok(tokenizer, code, info.label_start_index) + nok(tokenizer, info.label_start_index) } } } @@ -296,16 +296,16 @@ fn after(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | [a] b /// ^ /// ``` -fn reference_not_full(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { +fn reference_not_full(tokenizer: &mut Tokenizer, info: Info) -> State { tokenizer.attempt(collapsed_reference, move |is_ok| { - Box::new(move |t, c| { + Box::new(move |t| { if is_ok { - ok(t, c, info) + ok(t, info) } else { - nok(t, c, info.label_start_index) + nok(t, info.label_start_index) } }) - })(tokenizer, code) + })(tokenizer) } /// Done, we found something. @@ -320,7 +320,7 @@ fn reference_not_full(tokenizer: &mut Tokenizer, code: Code, info: Info) -> Stat /// > | [a] b /// ^ /// ``` -fn ok(tokenizer: &mut Tokenizer, _code: Code, mut info: Info) -> State { +fn ok(tokenizer: &mut Tokenizer, mut info: Info) -> State { // Remove this one and everything after it. let mut left = tokenizer .label_start_stack @@ -360,7 +360,7 @@ fn ok(tokenizer: &mut Tokenizer, _code: Code, mut info: Info) -> State { /// > | [a] b /// ^ /// ``` -fn nok(tokenizer: &mut Tokenizer, _code: Code, label_start_index: usize) -> State { +fn nok(tokenizer: &mut Tokenizer, label_start_index: usize) -> State { let label_start = tokenizer .label_start_stack .get_mut(label_start_index) @@ -375,12 +375,12 @@ fn nok(tokenizer: &mut Tokenizer, _code: Code, label_start_index: usize) -> Stat /// > | [a](b) c /// ^ /// ``` -fn resource(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn resource(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('(') => { tokenizer.enter(Token::Resource); tokenizer.enter(Token::ResourceMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::ResourceMarker); State::Fn(Box::new(resource_start)) } @@ -394,8 +394,8 @@ fn resource(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a](b) c /// ^ /// ``` -fn resource_start(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.attempt_opt(space_or_tab_eol(), resource_open)(tokenizer, code) +fn resource_start(tokenizer: &mut Tokenizer) -> State { + tokenizer.attempt_opt(space_or_tab_eol(), resource_open)(tokenizer) } /// At the start of a resource, after optional whitespace. @@ -404,14 +404,13 @@ fn resource_start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a](b) c /// ^ /// ``` -fn resource_open(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { - Code::Char(')') => resource_end(tokenizer, code), +fn resource_open(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { + Code::Char(')') => resource_end(tokenizer), _ => tokenizer.go( - |t, c| { + |t| { destination( t, - c, DestinationOptions { limit: RESOURCE_DESTINATION_BALANCE_MAX, destination: Token::ResourceDestination, @@ -423,7 +422,7 @@ fn resource_open(tokenizer: &mut Tokenizer, code: Code) -> State { ) }, destination_after, - )(tokenizer, code), + )(tokenizer), } } @@ -433,10 +432,10 @@ fn resource_open(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a](b) c /// ^ /// ``` -fn destination_after(tokenizer: &mut Tokenizer, code: Code) -> State { +fn destination_after(tokenizer: &mut Tokenizer) -> State { tokenizer.attempt(space_or_tab_eol(), |ok| { Box::new(if ok { resource_between } else { resource_end }) - })(tokenizer, code) + })(tokenizer) } /// In a resource, after a destination, after whitespace. @@ -445,13 +444,12 @@ fn destination_after(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a](b ) c /// ^ /// ``` -fn resource_between(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn resource_between(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('"' | '\'' | '(') => tokenizer.go( - |t, c| { + |t| { title( t, - c, TitleOptions { title: Token::ResourceTitle, marker: Token::ResourceTitleMarker, @@ -460,8 +458,8 @@ fn resource_between(tokenizer: &mut Tokenizer, code: Code) -> State { ) }, title_after, - )(tokenizer, code), - _ => resource_end(tokenizer, code), + )(tokenizer), + _ => resource_end(tokenizer), } } @@ -471,8 +469,8 @@ fn resource_between(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a](b "c") d /// ^ /// ``` -fn title_after(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.attempt_opt(space_or_tab_eol(), resource_end)(tokenizer, code) +fn title_after(tokenizer: &mut Tokenizer) -> State { + tokenizer.attempt_opt(space_or_tab_eol(), resource_end)(tokenizer) } /// In a resource, at the `)`. @@ -481,11 +479,11 @@ fn title_after(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a](b) d /// ^ /// ``` -fn resource_end(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn resource_end(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char(')') => { tokenizer.enter(Token::ResourceMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::ResourceMarker); tokenizer.exit(Token::Resource); State::Ok @@ -500,13 +498,12 @@ fn resource_end(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a][b] d /// ^ /// ``` -fn full_reference(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn full_reference(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('[') => tokenizer.go( - |t, c| { + |t| { label( t, - c, LabelOptions { label: Token::Reference, marker: Token::ReferenceMarker, @@ -515,7 +512,7 @@ fn full_reference(tokenizer: &mut Tokenizer, code: Code) -> State { ) }, full_reference_after, - )(tokenizer, code), + )(tokenizer), _ => unreachable!("expected `[`"), } } @@ -526,7 +523,7 @@ fn full_reference(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a][b] d /// ^ /// ``` -fn full_reference_after(tokenizer: &mut Tokenizer, _code: Code) -> State { +fn full_reference_after(tokenizer: &mut Tokenizer) -> State { let events = &tokenizer.events; let mut index = events.len() - 1; let mut start: Option = None; @@ -572,12 +569,12 @@ fn full_reference_after(tokenizer: &mut Tokenizer, _code: Code) -> State { /// > | [a][] d /// ^ /// ``` -fn collapsed_reference(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn collapsed_reference(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('[') => { tokenizer.enter(Token::Reference); tokenizer.enter(Token::ReferenceMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::ReferenceMarker); State::Fn(Box::new(collapsed_reference_open)) } @@ -593,11 +590,11 @@ fn collapsed_reference(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | [a][] d /// ^ /// ``` -fn collapsed_reference_open(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn collapsed_reference_open(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char(']') => { tokenizer.enter(Token::ReferenceMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::ReferenceMarker); tokenizer.exit(Token::Reference); State::Ok diff --git a/src/construct/label_start_image.rs b/src/construct/label_start_image.rs index 54521d4..8c12ffe 100644 --- a/src/construct/label_start_image.rs +++ b/src/construct/label_start_image.rs @@ -38,12 +38,12 @@ use crate::tokenizer::{Code, LabelStart, State, Tokenizer}; /// > | a ![b] c /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('!') if tokenizer.parse_state.constructs.label_start_image => { tokenizer.enter(Token::LabelImage); tokenizer.enter(Token::LabelImageMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LabelImageMarker); State::Fn(Box::new(open)) } @@ -57,11 +57,11 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | a ![b] c /// ^ /// ``` -pub fn open(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn open(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('[') => { tokenizer.enter(Token::LabelMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LabelMarker); tokenizer.exit(Token::LabelImage); let end = tokenizer.events.len() - 1; diff --git a/src/construct/label_start_link.rs b/src/construct/label_start_link.rs index f4c2ce8..e13cd77 100644 --- a/src/construct/label_start_link.rs +++ b/src/construct/label_start_link.rs @@ -37,13 +37,13 @@ use crate::tokenizer::{Code, LabelStart, State, Tokenizer}; /// > | a [b] c /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('[') if tokenizer.parse_state.constructs.label_start_link => { let start = tokenizer.events.len(); tokenizer.enter(Token::LabelLink); tokenizer.enter(Token::LabelMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LabelMarker); tokenizer.exit(Token::LabelLink); tokenizer.label_start_stack.push(LabelStart { diff --git a/src/construct/list.rs b/src/construct/list.rs index 4dfd202..f5bb0ce 100644 --- a/src/construct/list.rs +++ b/src/construct/list.rs @@ -136,7 +136,7 @@ impl Kind { /// > | * a /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { @@ -145,7 +145,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { if tokenizer.parse_state.constructs.list { tokenizer.enter(Token::ListItem); - tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer) } else { State::Nok } @@ -157,17 +157,17 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | * a /// ^ /// ``` -fn before(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn before(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { // Unordered. Code::Char('*' | '+' | '-') => tokenizer.check(thematic_break, |ok| { Box::new(if ok { nok } else { before_unordered }) - })(tokenizer, code), + })(tokenizer), // Ordered. Code::Char(char) if char.is_ascii_digit() && (!tokenizer.interrupt || char == '1') => { tokenizer.enter(Token::ListItemPrefix); tokenizer.enter(Token::ListItemValue); - inside(tokenizer, code, 0) + inside(tokenizer, 0) } _ => State::Nok, } @@ -181,9 +181,9 @@ fn before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | * a /// ^ /// ``` -fn before_unordered(tokenizer: &mut Tokenizer, code: Code) -> State { +fn before_unordered(tokenizer: &mut Tokenizer) -> State { tokenizer.enter(Token::ListItemPrefix); - marker(tokenizer, code) + marker(tokenizer) } /// In an ordered list item value. @@ -192,15 +192,15 @@ fn before_unordered(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | 1. a /// ^ /// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { - match code { +fn inside(tokenizer: &mut Tokenizer, size: usize) -> State { + match tokenizer.current { Code::Char(char) if char.is_ascii_digit() && size + 1 < LIST_ITEM_VALUE_SIZE_MAX => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| inside(t, c, size + 1))) + tokenizer.consume(); + State::Fn(Box::new(move |t| inside(t, size + 1))) } Code::Char('.' | ')') if !tokenizer.interrupt || size < 2 => { tokenizer.exit(Token::ListItemValue); - marker(tokenizer, code) + marker(tokenizer) } _ => State::Nok, } @@ -214,9 +214,9 @@ fn inside(tokenizer: &mut Tokenizer, code: Code, size: usize) -> State { /// > | 1. b /// ^ /// ``` -fn marker(tokenizer: &mut Tokenizer, code: Code) -> State { +fn marker(tokenizer: &mut Tokenizer) -> State { tokenizer.enter(Token::ListItemMarker); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::ListItemMarker); State::Fn(Box::new(marker_after)) } @@ -229,14 +229,14 @@ fn marker(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | 1. b /// ^ /// ``` -fn marker_after(tokenizer: &mut Tokenizer, code: Code) -> State { +fn marker_after(tokenizer: &mut Tokenizer) -> State { tokenizer.check(blank_line, move |ok| { if ok { - Box::new(|t, c| after(t, c, true)) + Box::new(|t| after(t, true)) } else { Box::new(marker_after_not_blank) } - })(tokenizer, code) + })(tokenizer) } /// After a list item marker, not followed by a blank line. @@ -245,15 +245,15 @@ fn marker_after(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | * a /// ^ /// ``` -fn marker_after_not_blank(tokenizer: &mut Tokenizer, code: Code) -> State { +fn marker_after_not_blank(tokenizer: &mut Tokenizer) -> State { // Attempt to parse up to the largest allowed indent, `nok` if there is more whitespace. tokenizer.attempt(whitespace, move |ok| { if ok { - Box::new(|t, c| after(t, c, false)) + Box::new(|t| after(t, false)) } else { Box::new(prefix_other) } - })(tokenizer, code) + })(tokenizer) } /// In whitespace after a marker. @@ -262,8 +262,8 @@ fn marker_after_not_blank(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | * a /// ^ /// ``` -fn whitespace(tokenizer: &mut Tokenizer, code: Code) -> State { - tokenizer.go(space_or_tab_min_max(1, TAB_SIZE), whitespace_after)(tokenizer, code) +fn whitespace(tokenizer: &mut Tokenizer) -> State { + tokenizer.go(space_or_tab_min_max(1, TAB_SIZE), whitespace_after)(tokenizer) } /// After acceptable whitespace. @@ -272,8 +272,11 @@ fn whitespace(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | * a /// ^ /// ``` -fn whitespace_after(_tokenizer: &mut Tokenizer, code: Code) -> State { - if matches!(code, Code::VirtualSpace | Code::Char('\t' | ' ')) { +fn whitespace_after(tokenizer: &mut Tokenizer) -> State { + if matches!( + tokenizer.current, + Code::VirtualSpace | Code::Char('\t' | ' ') + ) { State::Nok } else { State::Ok @@ -286,13 +289,13 @@ fn whitespace_after(_tokenizer: &mut Tokenizer, code: Code) -> State { /// > | * a /// ^ /// ``` -fn prefix_other(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn prefix_other(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::VirtualSpace | Code::Char('\t' | ' ') => { tokenizer.enter(Token::SpaceOrTab); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::SpaceOrTab); - State::Fn(Box::new(|t, c| after(t, c, false))) + State::Fn(Box::new(|t| after(t, false))) } _ => State::Nok, } @@ -304,7 +307,7 @@ fn prefix_other(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | * a /// ^ /// ``` -fn after(tokenizer: &mut Tokenizer, _code: Code, blank: bool) -> State { +fn after(tokenizer: &mut Tokenizer, blank: bool) -> State { if blank && tokenizer.interrupt { State::Nok } else { @@ -333,10 +336,10 @@ fn after(tokenizer: &mut Tokenizer, _code: Code, blank: bool) -> State { /// > | b /// ^ /// ``` -pub fn cont(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn cont(tokenizer: &mut Tokenizer) -> State { tokenizer.check(blank_line, |ok| { Box::new(if ok { blank_cont } else { not_blank_cont }) - })(tokenizer, code) + })(tokenizer) } /// Start of blank list item continuation. @@ -347,7 +350,7 @@ pub fn cont(tokenizer: &mut Tokenizer, code: Code) -> State { /// ^ /// | b /// ``` -pub fn blank_cont(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn blank_cont(tokenizer: &mut Tokenizer) -> State { let container = tokenizer.container.as_ref().unwrap(); let size = container.size; @@ -355,7 +358,7 @@ pub fn blank_cont(tokenizer: &mut Tokenizer, code: Code) -> State { State::Nok } else { // Consume, optionally, at most `size`. - tokenizer.go(space_or_tab_min_max(0, size), ok)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, size), ok)(tokenizer) } } @@ -366,23 +369,23 @@ pub fn blank_cont(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | b /// ^ /// ``` -pub fn not_blank_cont(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn not_blank_cont(tokenizer: &mut Tokenizer) -> State { let container = tokenizer.container.as_mut().unwrap(); let size = container.size; container.blank_initial = false; // Consume exactly `size`. - tokenizer.go(space_or_tab_min_max(size, size), ok)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(size, size), ok)(tokenizer) } /// A state fn to yield [`State::Ok`]. -pub fn ok(_tokenizer: &mut Tokenizer, _code: Code) -> State { +pub fn ok(_tokenizer: &mut Tokenizer) -> State { State::Ok } /// A state fn to yield [`State::Nok`]. -fn nok(_tokenizer: &mut Tokenizer, _code: Code) -> State { +fn nok(_tokenizer: &mut Tokenizer) -> State { State::Nok } diff --git a/src/construct/paragraph.rs b/src/construct/paragraph.rs index d320779..4bce6a4 100644 --- a/src/construct/paragraph.rs +++ b/src/construct/paragraph.rs @@ -42,15 +42,15 @@ use crate::util::skip::opt as skip_opt; /// > | abc /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { unreachable!("unexpected eol/eof") } _ => { tokenizer.enter(Token::Paragraph); tokenizer.enter_with_content(Token::Data, Some(ContentType::Text)); - inside(tokenizer, code) + inside(tokenizer) } } } @@ -61,8 +61,8 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | abc /// ^^^ /// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn inside(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::Data); tokenizer.exit(Token::Paragraph); @@ -72,7 +72,7 @@ fn inside(tokenizer: &mut Tokenizer, code: Code) -> State { State::Ok } _ => { - tokenizer.consume(code); + tokenizer.consume(); State::Fn(Box::new(inside)) } } diff --git a/src/construct/partial_data.rs b/src/construct/partial_data.rs index f399bac..86492b5 100644 --- a/src/construct/partial_data.rs +++ b/src/construct/partial_data.rs @@ -15,13 +15,13 @@ use crate::tokenizer::{Code, EventType, State, Tokenizer}; /// > | abc /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code, stop: &'static [Code]) -> State { - if stop.contains(&code) { +pub fn start(tokenizer: &mut Tokenizer, stop: &'static [Code]) -> State { + if stop.contains(&tokenizer.current) { tokenizer.enter(Token::Data); - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| data(t, c, stop))) + tokenizer.consume(); + State::Fn(Box::new(move |t| data(t, stop))) } else { - at_break(tokenizer, code, stop) + at_break(tokenizer, stop) } } @@ -31,22 +31,22 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code, stop: &'static [Code]) -> St /// > | abc /// ^ /// ``` -fn at_break(tokenizer: &mut Tokenizer, code: Code, stop: &'static [Code]) -> State { - match code { +fn at_break(tokenizer: &mut Tokenizer, stop: &'static [Code]) -> State { + match tokenizer.current { Code::None => State::Ok, Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); - State::Fn(Box::new(move |t, c| at_break(t, c, stop))) + State::Fn(Box::new(move |t| at_break(t, stop))) } - _ if stop.contains(&code) => { + _ if stop.contains(&tokenizer.current) => { tokenizer.register_resolver("data".to_string(), Box::new(resolve_data)); State::Ok } _ => { tokenizer.enter(Token::Data); - data(tokenizer, code, stop) + data(tokenizer, stop) } } } @@ -57,19 +57,19 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, stop: &'static [Code]) -> Sta /// > | abc /// ^^^ /// ``` -fn data(tokenizer: &mut Tokenizer, code: Code, stop: &'static [Code]) -> State { - let done = match code { +fn data(tokenizer: &mut Tokenizer, stop: &'static [Code]) -> State { + let done = match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => true, - _ if stop.contains(&code) => true, + _ if stop.contains(&tokenizer.current) => true, _ => false, }; if done { tokenizer.exit(Token::Data); - at_break(tokenizer, code, stop) + at_break(tokenizer, stop) } else { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| data(t, c, stop))) + tokenizer.consume(); + State::Fn(Box::new(move |t| data(t, stop))) } } diff --git a/src/construct/partial_destination.rs b/src/construct/partial_destination.rs index d567876..6a984e2 100644 --- a/src/construct/partial_destination.rs +++ b/src/construct/partial_destination.rs @@ -110,20 +110,20 @@ struct Info { /// > | aa /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code, options: Options) -> State { +pub fn start(tokenizer: &mut Tokenizer, options: Options) -> State { let info = Info { balance: 0, options, }; - match code { + match tokenizer.current { Code::Char('<') => { tokenizer.enter(info.options.destination.clone()); tokenizer.enter(info.options.literal.clone()); tokenizer.enter(info.options.marker.clone()); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(info.options.marker.clone()); - State::Fn(Box::new(|t, c| enclosed_before(t, c, info))) + State::Fn(Box::new(|t| enclosed_before(t, info))) } Code::None | Code::CarriageReturnLineFeed | Code::VirtualSpace | Code::Char(' ' | ')') => { State::Nok @@ -134,7 +134,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code, options: Options) -> State { tokenizer.enter(info.options.raw.clone()); tokenizer.enter(info.options.string.clone()); tokenizer.enter_with_content(Token::Data, Some(ContentType::String)); - raw(tokenizer, code, info) + raw(tokenizer, info) } } } @@ -145,10 +145,10 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code, options: Options) -> State { /// > | /// ^ /// ``` -fn enclosed_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - if let Code::Char('>') = code { +fn enclosed_before(tokenizer: &mut Tokenizer, info: Info) -> State { + if let Code::Char('>') = tokenizer.current { tokenizer.enter(info.options.marker.clone()); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(info.options.marker.clone()); tokenizer.exit(info.options.literal.clone()); tokenizer.exit(info.options.destination); @@ -156,7 +156,7 @@ fn enclosed_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { } else { tokenizer.enter(info.options.string.clone()); tokenizer.enter_with_content(Token::Data, Some(ContentType::String)); - enclosed(tokenizer, code, info) + enclosed(tokenizer, info) } } @@ -166,21 +166,21 @@ fn enclosed_before(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | /// ^ /// ``` -fn enclosed(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn enclosed(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('>') => { tokenizer.exit(Token::Data); tokenizer.exit(info.options.string.clone()); - enclosed_before(tokenizer, code, info) + enclosed_before(tokenizer, info) } Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r' | '<') => State::Nok, Code::Char('\\') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| enclosed_escape(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| enclosed_escape(t, info))) } _ => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| enclosed(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| enclosed(t, info))) } } } @@ -191,13 +191,13 @@ fn enclosed(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | /// ^ /// ``` -fn enclosed_escape(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn enclosed_escape(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('<' | '>' | '\\') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| enclosed(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| enclosed(t, info))) } - _ => enclosed(tokenizer, code, info), + _ => enclosed(tokenizer, info), } } @@ -207,15 +207,15 @@ fn enclosed_escape(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | aa /// ^ /// ``` -fn raw(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn raw(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char('(') => { if info.balance >= info.options.limit { State::Nok } else { - tokenizer.consume(code); + tokenizer.consume(); info.balance += 1; - State::Fn(Box::new(move |t, c| raw(t, c, info))) + State::Fn(Box::new(move |t| raw(t, info))) } } Code::Char(')') => { @@ -226,9 +226,9 @@ fn raw(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { tokenizer.exit(info.options.destination); State::Ok } else { - tokenizer.consume(code); + tokenizer.consume(); info.balance -= 1; - State::Fn(Box::new(move |t, c| raw(t, c, info))) + State::Fn(Box::new(move |t| raw(t, info))) } } Code::None @@ -247,12 +247,12 @@ fn raw(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { } Code::Char(char) if char.is_ascii_control() => State::Nok, Code::Char('\\') => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| raw_escape(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(move |t| raw_escape(t, info))) } Code::Char(_) => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| raw(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(move |t| raw(t, info))) } } } @@ -263,12 +263,12 @@ fn raw(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { /// > | a\*a /// ^ /// ``` -fn raw_escape(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn raw_escape(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char('(' | ')' | '\\') => { - tokenizer.consume(code); - State::Fn(Box::new(move |t, c| raw(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(move |t| raw(t, info))) } - _ => raw(tokenizer, code, info), + _ => raw(tokenizer, info), } } diff --git a/src/construct/partial_label.rs b/src/construct/partial_label.rs index 3bb3abf..91a0e26 100644 --- a/src/construct/partial_label.rs +++ b/src/construct/partial_label.rs @@ -96,8 +96,8 @@ struct Info { /// > | [a] /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code, options: Options) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer, options: Options) -> State { + match tokenizer.current { Code::Char('[') => { let info = Info { connect: false, @@ -107,10 +107,10 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code, options: Options) -> State { }; tokenizer.enter(info.options.label.clone()); tokenizer.enter(info.options.marker.clone()); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(info.options.marker.clone()); tokenizer.enter(info.options.string.clone()); - State::Fn(Box::new(|t, c| at_break(t, c, info))) + State::Fn(Box::new(|t| at_break(t, info))) } _ => State::Nok, } @@ -122,15 +122,15 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code, options: Options) -> State { /// > | [a] /// ^ /// ``` -fn at_break(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn at_break(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::None | Code::Char('[') => State::Nok, Code::Char(']') if !info.data => State::Nok, _ if info.size > LINK_REFERENCE_SIZE_MAX => State::Nok, Code::Char(']') => { tokenizer.exit(info.options.string.clone()); tokenizer.enter(info.options.marker.clone()); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(info.options.marker.clone()); tokenizer.exit(info.options.label); State::Ok @@ -140,11 +140,11 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { content_type: Some(ContentType::String), connect: info.connect, }), - |t, c| { + |t| { info.connect = true; - at_break(t, c, info) + at_break(t, info) }, - )(tokenizer, code), + )(tokenizer), _ => { tokenizer.enter_with_content(Token::Data, Some(ContentType::String)); @@ -155,7 +155,7 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { info.connect = true; } - label(tokenizer, code, info) + label(tokenizer, info) } } } @@ -166,36 +166,36 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { /// > | [a] /// ^ /// ``` -fn label(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn label(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r' | '[' | ']') => { tokenizer.exit(Token::Data); - at_break(tokenizer, code, info) + at_break(tokenizer, info) } _ if info.size > LINK_REFERENCE_SIZE_MAX => { tokenizer.exit(Token::Data); - at_break(tokenizer, code, info) + at_break(tokenizer, info) } Code::VirtualSpace | Code::Char('\t' | ' ') => { - tokenizer.consume(code); + tokenizer.consume(); info.size += 1; - State::Fn(Box::new(|t, c| label(t, c, info))) + State::Fn(Box::new(|t| label(t, info))) } Code::Char('\\') => { - tokenizer.consume(code); + tokenizer.consume(); info.size += 1; if !info.data { info.data = true; } - State::Fn(Box::new(|t, c| escape(t, c, info))) + State::Fn(Box::new(|t| escape(t, info))) } Code::Char(_) => { - tokenizer.consume(code); + tokenizer.consume(); info.size += 1; if !info.data { info.data = true; } - State::Fn(Box::new(|t, c| label(t, c, info))) + State::Fn(Box::new(|t| label(t, info))) } } } @@ -206,13 +206,13 @@ fn label(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { /// > | [a\*a] /// ^ /// ``` -fn escape(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn escape(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char('[' | '\\' | ']') => { - tokenizer.consume(code); + tokenizer.consume(); info.size += 1; - State::Fn(Box::new(|t, c| label(t, c, info))) + State::Fn(Box::new(|t| label(t, info))) } - _ => label(tokenizer, code, info), + _ => label(tokenizer, info), } } diff --git a/src/construct/partial_non_lazy_continuation.rs b/src/construct/partial_non_lazy_continuation.rs index 5835848..bdc22e4 100644 --- a/src/construct/partial_non_lazy_continuation.rs +++ b/src/construct/partial_non_lazy_continuation.rs @@ -20,11 +20,11 @@ use crate::tokenizer::{Code, State, Tokenizer}; /// ^ /// | b /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.enter(Token::LineEnding); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); State::Fn(Box::new(after)) } @@ -39,7 +39,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | b /// ^ /// ``` -fn after(tokenizer: &mut Tokenizer, _code: Code) -> State { +fn after(tokenizer: &mut Tokenizer) -> State { if tokenizer.lazy { State::Nok } else { diff --git a/src/construct/partial_space_or_tab.rs b/src/construct/partial_space_or_tab.rs index 30c63e0..a97ac29 100644 --- a/src/construct/partial_space_or_tab.rs +++ b/src/construct/partial_space_or_tab.rs @@ -78,7 +78,7 @@ pub fn space_or_tab_min_max(min: usize, max: usize) -> Box { /// `space_or_tab`, with the given options. pub fn space_or_tab_with_options(options: Options) -> Box { - Box::new(|t, c| start(t, c, Info { size: 0, options })) + Box::new(|t| start(t, Info { size: 0, options })) } /// `space_or_tab`, or optionally `space_or_tab`, one `eol`, and @@ -96,7 +96,7 @@ pub fn space_or_tab_eol() -> Box { /// `space_or_tab_eol`, with the given options. pub fn space_or_tab_eol_with_options(options: EolOptions) -> Box { - Box::new(move |tokenizer, code| { + Box::new(move |tokenizer| { let mut info = EolInfo { connect: false, ok: false, @@ -120,9 +120,9 @@ pub fn space_or_tab_eol_with_options(options: EolOptions) -> Box { } } - Box::new(|t, c| after_space_or_tab(t, c, info)) + Box::new(|t| after_space_or_tab(t, info)) }, - )(tokenizer, code) + )(tokenizer) }) } @@ -132,8 +132,8 @@ pub fn space_or_tab_eol_with_options(options: EolOptions) -> Box { /// > | a␠␠b /// ^ /// ``` -fn start(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn start(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::VirtualSpace | Code::Char('\t' | ' ') if info.options.max > 0 => { tokenizer .enter_with_content(info.options.kind.clone(), info.options.content_type.clone()); @@ -143,9 +143,9 @@ fn start(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { link(&mut tokenizer.events, index); } - tokenizer.consume(code); + tokenizer.consume(); info.size += 1; - State::Fn(Box::new(|t, c| inside(t, c, info))) + State::Fn(Box::new(|t| inside(t, info))) } _ => { if info.options.min == 0 { @@ -163,12 +163,12 @@ fn start(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { /// > | a␠␠b /// ^ /// ``` -fn inside(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn inside(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::VirtualSpace | Code::Char('\t' | ' ') if info.size < info.options.max => { - tokenizer.consume(code); + tokenizer.consume(); info.size += 1; - State::Fn(Box::new(|t, c| inside(t, c, info))) + State::Fn(Box::new(|t| inside(t, info))) } _ => { tokenizer.exit(info.options.kind.clone()); @@ -188,8 +188,8 @@ fn inside(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { /// ^ /// | b /// ``` -fn after_space_or_tab(tokenizer: &mut Tokenizer, code: Code, mut info: EolInfo) -> State { - match code { +fn after_space_or_tab(tokenizer: &mut Tokenizer, mut info: EolInfo) -> State { + match tokenizer.current { Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.enter_with_content(Token::LineEnding, info.options.content_type.clone()); @@ -200,9 +200,9 @@ fn after_space_or_tab(tokenizer: &mut Tokenizer, code: Code, mut info: EolInfo) info.connect = true; } - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(Token::LineEnding); - State::Fn(Box::new(|t, c| after_eol(t, c, info))) + State::Fn(Box::new(|t| after_eol(t, info))) } _ if info.ok => State::Ok, _ => State::Nok, @@ -217,7 +217,7 @@ fn after_space_or_tab(tokenizer: &mut Tokenizer, code: Code, mut info: EolInfo) /// ^ /// ``` #[allow(clippy::needless_pass_by_value)] -fn after_eol(tokenizer: &mut Tokenizer, code: Code, info: EolInfo) -> State { +fn after_eol(tokenizer: &mut Tokenizer, info: EolInfo) -> State { tokenizer.attempt_opt( space_or_tab_with_options(Options { kind: Token::SpaceOrTab, @@ -227,7 +227,7 @@ fn after_eol(tokenizer: &mut Tokenizer, code: Code, info: EolInfo) -> State { connect: info.connect, }), after_more_space_or_tab, - )(tokenizer, code) + )(tokenizer) } /// `space_or_tab_eol`: after more (optional) `space_or_tab`. @@ -237,10 +237,10 @@ fn after_eol(tokenizer: &mut Tokenizer, code: Code, info: EolInfo) -> State { /// > | b /// ^ /// ``` -fn after_more_space_or_tab(_tokenizer: &mut Tokenizer, code: Code) -> State { +fn after_more_space_or_tab(tokenizer: &mut Tokenizer) -> State { // Blank line not allowed. if matches!( - code, + tokenizer.current, Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') ) { State::Nok diff --git a/src/construct/partial_title.rs b/src/construct/partial_title.rs index 0483681..e9528fd 100644 --- a/src/construct/partial_title.rs +++ b/src/construct/partial_title.rs @@ -135,19 +135,19 @@ struct Info { /// > | "a" /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code, options: Options) -> State { - match code { +pub fn start(tokenizer: &mut Tokenizer, options: Options) -> State { + match tokenizer.current { Code::Char('"' | '\'' | '(') => { let info = Info { connect: false, - kind: Kind::from_code(code), + kind: Kind::from_code(tokenizer.current), options, }; tokenizer.enter(info.options.title.clone()); tokenizer.enter(info.options.marker.clone()); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(info.options.marker.clone()); - State::Fn(Box::new(|t, c| begin(t, c, info))) + State::Fn(Box::new(|t| begin(t, info))) } _ => State::Nok, } @@ -161,18 +161,18 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code, options: Options) -> State { /// > | "a" /// ^ /// ``` -fn begin(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn begin(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char(char) if char == info.kind.as_char() => { tokenizer.enter(info.options.marker.clone()); - tokenizer.consume(code); + tokenizer.consume(); tokenizer.exit(info.options.marker.clone()); tokenizer.exit(info.options.title); State::Ok } _ => { tokenizer.enter(info.options.string.clone()); - at_break(tokenizer, code, info) + at_break(tokenizer, info) } } } @@ -183,11 +183,11 @@ fn begin(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | "a" /// ^ /// ``` -fn at_break(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn at_break(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char(char) if char == info.kind.as_char() => { tokenizer.exit(info.options.string.clone()); - begin(tokenizer, code, info) + begin(tokenizer, info) } Code::None => State::Nok, Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => tokenizer.go( @@ -195,11 +195,11 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { content_type: Some(ContentType::String), connect: info.connect, }), - |t, c| { + |t| { info.connect = true; - at_break(t, c, info) + at_break(t, info) }, - )(tokenizer, code), + )(tokenizer), _ => { tokenizer.enter_with_content(Token::Data, Some(ContentType::String)); @@ -210,7 +210,7 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { info.connect = true; } - title(tokenizer, code, info) + title(tokenizer, info) } } } @@ -221,23 +221,23 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { /// > | "a" /// ^ /// ``` -fn title(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn title(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char(char) if char == info.kind.as_char() => { tokenizer.exit(Token::Data); - at_break(tokenizer, code, info) + at_break(tokenizer, info) } Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => { tokenizer.exit(Token::Data); - at_break(tokenizer, code, info) + at_break(tokenizer, info) } Code::Char('\\') => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| escape(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| escape(t, info))) } _ => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| title(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| title(t, info))) } } } @@ -248,12 +248,12 @@ fn title(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | "a\*b" /// ^ /// ``` -fn escape(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn escape(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::Char(char) if char == info.kind.as_char() => { - tokenizer.consume(code); - State::Fn(Box::new(|t, c| title(t, c, info))) + tokenizer.consume(); + State::Fn(Box::new(|t| title(t, info))) } - _ => title(tokenizer, code, info), + _ => title(tokenizer, info), } } diff --git a/src/construct/partial_whitespace.rs b/src/construct/partial_whitespace.rs index 023c52f..afff1c4 100644 --- a/src/construct/partial_whitespace.rs +++ b/src/construct/partial_whitespace.rs @@ -27,7 +27,7 @@ use super::partial_space_or_tab::space_or_tab; use crate::tokenizer::{Code, State, Tokenizer}; /// Parse initial or final whitespace. -pub fn whitespace(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn whitespace(tokenizer: &mut Tokenizer) -> State { tokenizer.go( // Nothing if there’s no whitespace. space_or_tab(), @@ -41,22 +41,22 @@ pub fn whitespace(tokenizer: &mut Tokenizer, code: Code) -> State { // If there’s whitespace, and we were not at an eol/eof, there must be one here. at_eol }, - )(tokenizer, code) + )(tokenizer) } /// After whitespace, at an eol/eof. -fn at_eol(tokenizer: &mut Tokenizer, code: Code) -> State { +fn at_eol(tokenizer: &mut Tokenizer) -> State { if matches!( - code, + tokenizer.current, Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') ) { - ok(tokenizer, code) + ok(tokenizer) } else { State::Nok } } /// Fine. -fn ok(_tokenizer: &mut Tokenizer, _code: Code) -> State { +fn ok(_tokenizer: &mut Tokenizer) -> State { State::Ok } diff --git a/src/construct/thematic_break.rs b/src/construct/thematic_break.rs index 3c9085a..41dc6ae 100644 --- a/src/construct/thematic_break.rs +++ b/src/construct/thematic_break.rs @@ -134,7 +134,7 @@ struct Info { /// > | *** /// ^ /// ``` -pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { +pub fn start(tokenizer: &mut Tokenizer) -> State { let max = if tokenizer.parse_state.constructs.code_indented { TAB_SIZE - 1 } else { @@ -143,7 +143,7 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { if tokenizer.parse_state.constructs.thematic_break { tokenizer.enter(Token::ThematicBreak); - tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer, code) + tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer) } else { State::Nok } @@ -155,13 +155,12 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | *** /// ^ /// ``` -fn before(tokenizer: &mut Tokenizer, code: Code) -> State { - match code { +fn before(tokenizer: &mut Tokenizer) -> State { + match tokenizer.current { Code::Char('*' | '-' | '_') => at_break( tokenizer, - code, Info { - kind: Kind::from_code(code), + kind: Kind::from_code(tokenizer.current), size: 0, }, ), @@ -175,8 +174,8 @@ fn before(tokenizer: &mut Tokenizer, code: Code) -> State { /// > | *** /// ^ /// ``` -fn at_break(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { - match code { +fn at_break(tokenizer: &mut Tokenizer, info: Info) -> State { + match tokenizer.current { Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') if info.size >= THEMATIC_BREAK_MARKER_COUNT_MIN => { @@ -187,7 +186,7 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { } Code::Char(char) if char == info.kind.as_char() => { tokenizer.enter(Token::ThematicBreakSequence); - sequence(tokenizer, code, info) + sequence(tokenizer, info) } _ => State::Nok, } @@ -199,16 +198,16 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code, info: Info) -> State { /// > | *** /// ^ /// ``` -fn sequence(tokenizer: &mut Tokenizer, code: Code, mut info: Info) -> State { - match code { +fn sequence(tokenizer: &mut Tokenizer, mut info: Info) -> State { + match tokenizer.current { Code::Char(char) if char == info.kind.as_char() => { - tokenizer.consume(code); + tokenizer.consume(); info.size += 1; - State::Fn(Box::new(|t, c| sequence(t, c, info))) + State::Fn(Box::new(|t| sequence(t, info))) } _ => { tokenizer.exit(Token::ThematicBreakSequence); - tokenizer.attempt_opt(space_or_tab(), |t, c| at_break(t, c, info))(tokenizer, code) + tokenizer.attempt_opt(space_or_tab(), |t| at_break(t, info))(tokenizer) } } } -- cgit