Fix and document some Anchor Syntax corner cases

This commit is contained in:
Juno Takano 2025-12-24 12:20:28 -03:00
commit 51047ad11c
4 changed files with 99 additions and 14 deletions

View file

@ -1,7 +1,6 @@
use std::collections::{HashMap};
use crate::{formats::populate_graph, types::Config};
use super::{Parseable as _, Token, LexMap};
use token::{
anchor::Anchor, linebreak::LineBreak, paragraph::Paragraph, header::Header,
@ -113,8 +112,13 @@ fn lex(text: &str, map: LexMap) -> Vec<Token> {
} else if candidate.destination.is_none() {
// candidate is leading and we found the second pipe
if candidate.leading && lexeme.text() == "|" {
// whitespace after pipe: flanking node anchor
if lexeme.is_next_whitespace() {
// third pipe immediately after second: forcing flanking
if lexeme.match_next_first_char('|') {
continue;
// whitespace or punctuation after pipe: flanking anchor
} else if lexeme.is_next_whitespace()
|| lexeme.is_next_punctuation()
{
candidate.destination =
Some(candidate.text.clone());
let token = Token::Anchor(candidate.clone());
@ -186,6 +190,11 @@ struct State {
buffers: Buffers,
}
struct Context {
block: BlockContext,
inline: InlineContext,
}
struct Buffers {
anchor: AnchorBuffer,
}
@ -224,11 +233,6 @@ impl State {
}
}
struct Context {
block: BlockContext,
inline: InlineContext,
}
fn parse(tokens: &[Token]) -> String {
tokens.iter().map(Token::render).collect::<String>()
}

View file

@ -24,6 +24,15 @@ impl Lexeme {
self.next == " " || self.next == "\n"
}
pub fn is_next_punctuation(&self) -> bool {
let punctuation = [",", ".", ":", ";", "?", "!", "(", ")", "\"", "'"];
punctuation.contains(&self.next.as_str())
}
pub fn next_first_char(&self) -> Option<char> {
self.next.chars().nth(0)
}
pub fn match_first_char(&self, query: char) -> bool {
if let Some(first) = self.text.chars().nth(0) {
first == query
@ -32,8 +41,12 @@ impl Lexeme {
}
}
pub fn next_first_char(&self) -> Option<char> {
self.next.chars().nth(0)
pub fn match_next_first_char(&self, query: char) -> bool {
if let Some(first) = self.next.chars().nth(0) {
first == query
} else {
false
}
}
/// # Panics