From 315956e20dd885107a49d3d1f9fc166d421a03e2 Mon Sep 17 00:00:00 2001 From: jutty Date: Mon, 5 Jan 2026 12:52:48 -0300 Subject: [PATCH] Make Oblique no longer a context, add a point module --- src/syntax/content/parser.rs | 26 +++++++----------- src/syntax/content/parser/context.rs | 1 - src/syntax/content/parser/point.rs | 40 ++++++++++++++++++++++++++++ src/syntax/content/parser/state.rs | 11 ++++++-- 4 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 src/syntax/content/parser/point.rs diff --git a/src/syntax/content/parser.rs b/src/syntax/content/parser.rs index 14e2b75..7a94f22 100644 --- a/src/syntax/content/parser.rs +++ b/src/syntax/content/parser.rs @@ -2,7 +2,7 @@ use crate::{prelude::*, types::Config}; use super::{Parseable as _, Token, LexMap}; use token::{ anchor::Anchor, linebreak::LineBreak, paragraph::Paragraph, header::Header, - preformat::PreFormat, literal::Literal, code::Code, oblique::Oblique, + preformat::PreFormat, literal::Literal, code::Code, }; use lexeme::Lexeme; use context::{Block, Inline}; @@ -11,6 +11,7 @@ pub mod token; pub mod lexeme; pub mod segment; pub mod context; +pub mod point; pub mod state; const LEXMAP: LexMap = &[ @@ -24,7 +25,7 @@ const LEXMAP: LexMap = &[ fn lex(text: &str, map: LexMap, config: &Config) -> Vec { let mut tokens: Vec = Vec::new(); - let mut state = state::State::new(); + let mut state = state::State::default(); let segments = segment::segment(text); let lexemes = Lexeme::collect(&segments); @@ -79,17 +80,16 @@ fn lex(text: &str, map: LexMap, config: &Config) -> Vec { }, } + if point::puncture(lexeme, &mut state, &mut tokens) { + continue; + } + match state.context.inline { Inline::None => { if Code::probe(lexeme) { state.context.inline = Inline::Code; tokens.push(Token::Code(Code::new(true))); continue; - } else if Oblique::probe(lexeme) { - log!("Inline Context: None -> Oblique on {lexeme}"); - state.context.inline = Inline::Oblique; - tokens.push(Token::Oblique(Oblique::new(true))); - continue; } else if Anchor::probe(lexeme) { state.context.inline = Inline::Anchor; state.buffers.anchor.clear(); @@ -112,14 +112,6 @@ fn lex(text: &str, map: LexMap, config: &Config) -> Vec { continue; } }, - Inline::Oblique => { - if Oblique::probe(lexeme) { - log!("Inline Context: Oblique -> None on {lexeme}"); - state.context.inline = Inline::None; - tokens.push(Token::Oblique(Oblique::new(false))); - continue; - } - }, Inline::Anchor => { if context::anchor::parse(lexeme, &mut state, &mut tokens) { continue; @@ -390,7 +382,7 @@ mod tests { #[test] #[should_panic(expected = "End of input with open header")] fn end_with_open_header() { - let mut state = State::new(); + let mut state = State::default(); state.context.block = Block::Header(1); context::close(&state, &mut vec![]); @@ -398,7 +390,7 @@ mod tests { #[test] fn end_with_open_preformat() { - let mut state = State::new(); + let mut state = State::default(); state.context.block = Block::PreFormat; let mut vec: Vec = vec![]; diff --git a/src/syntax/content/parser/context.rs b/src/syntax/content/parser/context.rs index 17060ae..8273e5d 100644 --- a/src/syntax/content/parser/context.rs +++ b/src/syntax/content/parser/context.rs @@ -23,7 +23,6 @@ pub enum Block { pub enum Inline { Anchor, Code, - Oblique, None, } diff --git a/src/syntax/content/parser/point.rs b/src/syntax/content/parser/point.rs new file mode 100644 index 0000000..82bb847 --- /dev/null +++ b/src/syntax/content/parser/point.rs @@ -0,0 +1,40 @@ +use crate::syntax::content::{ + Parseable as _, + parser::{ + lexeme::Lexeme, + token::{Token, oblique::Oblique}, + state::State, + }, +}; + +pub fn puncture( + lexeme: &Lexeme, + state: &mut State, + tokens: &mut Vec, +) -> bool { + if Oblique::probe(lexeme) { + tokens.push(Token::Oblique(Oblique::new(!state.switches.oblique))); + state.switches.oblique = !state.switches.oblique; + return true; + } + false +} + +#[cfg(test)] +mod tests { + use crate::{syntax::content::parser, types::Graph}; + + fn read_noconfig(input: &str) -> String { + parser::read(input, &Graph::new(None).meta.config) + } + + #[test] + fn oblique() { + assert_eq!( + read_noconfig( + "_|this anchor is oblique|o as are these literals_ but not these _just these_, not this _and these with an |anchor| again_" + ), + r#"

this anchor is oblique as are these literals but not these just these, not this and these with an anchor again

"# + ); + } +} diff --git a/src/syntax/content/parser/state.rs b/src/syntax/content/parser/state.rs index c237b29..5680b96 100644 --- a/src/syntax/content/parser/state.rs +++ b/src/syntax/content/parser/state.rs @@ -9,9 +9,15 @@ use crate::syntax::content::parser::{ pub struct State { pub context: Context, pub dom_ids: HashMap>, + pub switches: Switches, pub buffers: Buffers, } +#[derive(Clone, Debug)] +pub struct Switches { + pub oblique: bool, +} + #[derive(Clone, Debug)] pub struct Buffers { pub anchor: AnchorBuffer, @@ -56,14 +62,15 @@ impl std::fmt::Display for AnchorBuffer { } } -impl State { - pub fn new() -> State { +impl Default for State { + fn default() -> State { State { context: Context { inline: Inline::None, block: Block::None, }, dom_ids: HashMap::new(), + switches: Switches { oblique: false }, buffers: Buffers { anchor: AnchorBuffer { candidate: Anchor::default(),