Implement list token

This commit is contained in:
Juno Takano 2026-01-06 05:56:20 -03:00
commit 0ec784034a
5 changed files with 166 additions and 9 deletions

View file

@ -0,0 +1,41 @@
use crate::syntax::content::{Parseable, Lexeme};
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Item {
open: bool,
}
impl Parseable for Item {
fn probe(lexeme: &Lexeme) -> bool {
(lexeme.match_as_char('-') || lexeme.match_as_char('+'))
&& lexeme.match_next_as_char(' ')
}
fn lex(_lexeme: &Lexeme) -> Item {
Item { open: false }
}
fn render(&self) -> String {
if self.open {
String::from("<li>")
} else {
String::from("</li>")
}
}
}
impl Item {
pub fn new(open: bool) -> Item {
Item { open }
}
pub fn probe_end(lexeme: &Lexeme) -> bool {
lexeme.match_as_char('\n')
}
}
impl std::fmt::Display for Item {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Item [{}]", if self.open { "open" } else { "closed" })
}
}

View file

@ -0,0 +1,47 @@
use crate::syntax::content::{Parseable, Lexeme};
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct List {
open: bool,
ordered: bool,
}
impl Parseable for List {
fn probe(lexeme: &Lexeme) -> bool {
(lexeme.match_as_char('-') || lexeme.match_as_char('+'))
&& lexeme.match_next_as_char(' ')
}
fn lex(_lexeme: &Lexeme) -> List {
panic!("Attempt to lex a List directly from a lexeme")
}
fn render(&self) -> String {
let bar = if self.open { "" } else { "/" };
let tag = if self.ordered { "ol" } else { "ul" };
format!("<{bar}{tag}>")
}
}
impl List {
pub fn new(open: bool, ordered: bool) -> List {
List { open, ordered }
}
pub fn probe_end(lexeme: &Lexeme) -> bool {
lexeme.match_as_char('\n')
}
}
impl std::fmt::Display for List {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"List [{} {}]",
if self.open { "open" } else { "closed" },
if self.ordered { "ordered" } else { "unordered" },
)
}
}