From 7721f210c16e19b1c2af90f69130386b89bb5104 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 29 Jun 2022 10:26:39 +0200 Subject: Add support for sharing identifiers, references before definitions --- src/compiler.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index bb7aedc..6f4d1a6 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -91,6 +91,14 @@ struct Media { title: Option, } +/// To do. +#[derive(Debug, Clone, PartialEq)] +struct DefinitionInfo { + id: Option, + destination: Option, + title: Option, +} + /// Configuration (optional). #[derive(Default, Debug)] pub struct Options { @@ -226,17 +234,39 @@ pub fn compile(events: &[Event], codes: &[Code], options: &Options) -> String { let mut media_stack: Vec = vec![]; // let mut slurp_all_line_endings = false; + let mut definition: Option = None; + + // To do: actually do a compile pass, so that `buffer`, `resume`, etc can be used. while index < events.len() { let event = &events[index]; - if event.event_type == EventType::Exit + // Find the used line ending style. + if line_ending_inferred.is_none() + && event.event_type == EventType::Exit && (event.token_type == TokenType::BlankLineEnding || event.token_type == TokenType::CodeTextLineEnding || event.token_type == TokenType::LineEnding) { let codes = codes_from_span(codes, &from_exit_event(events, index)); line_ending_inferred = Some(LineEnding::from_code(*codes.first().unwrap())); - break; + } + + if event.event_type == EventType::Enter { + if event.token_type == TokenType::Definition { + definition = Some(DefinitionInfo { + id: None, + destination: None, + title: None, + }); + } + } else if event.token_type == TokenType::Definition { + definition = None; + } else if event.token_type == TokenType::DefinitionLabelString + || event.token_type == TokenType::DefinitionDestinationString + || event.token_type == TokenType::DefinitionTitleString + { + let slice = serialize(codes, &from_exit_event(events, index), false); + println!("set: {:?} {:?}", slice, definition); } index += 1; @@ -250,7 +280,7 @@ pub fn compile(events: &[Event], codes: &[Code], options: &Options) -> String { LineEnding::LineFeed }; - index = 0; + let mut index = 0; while index < events.len() { let event = &events[index]; @@ -483,7 +513,7 @@ pub fn compile(events: &[Event], codes: &[Code], options: &Options) -> String { let label = media.label.unwrap(); let buf = buf_tail_mut(buffers); // To do: get from definition. - let destination = media.destination.unwrap(); + let destination = media.destination.unwrap_or_else(|| "".to_string()); let title = if let Some(title) = media.title { format!(" title=\"{}\"", title) } else { -- cgit