aboutsummaryrefslogblamecommitdiffstats
path: root/src/parser.rs
blob: f11f0d15f13a5ff7febf0669bba61d739c444fe9 (plain) (tree)
1
2
3
4
5
6
                                          
 
                                                                   
                               
                                                     
 







                                                                    
                       
                      
                         

                                     

 
                                          
   

                                                                
                                      
                               
                                    

      
                      
                         






                      


                                            
 
//! Turn a string of markdown into events.

// To do: this should start with `containers`, when they’re done.
use crate::content::flow::flow;
use crate::tokenizer::{as_codes, Code, Event, Point};

/// To do: could we do without `HashSet`, so we don’t need `std`?
use std::collections::HashSet;

/// Information needed, in all content types, when parsing markdown.
///
/// Importantly, this contains a set of known definitions.
/// It also references the input value as [`Code`][]s.
#[derive(Debug)]
pub struct ParseState {
    /// List of codes.
    pub codes: Vec<Code>,
    /// Set of defined identifiers.
    pub definitions: HashSet<String>,
}

/// Turn a string of markdown into events.
///
/// Passes the codes back so the compiler can access the source.
pub fn parse(value: &str) -> (Vec<Event>, Vec<Code>) {
    let mut parse_state = ParseState {
        codes: as_codes(value),
        definitions: HashSet::new(),
    };

    let events = flow(
        &mut parse_state,
        Point {
            line: 1,
            column: 1,
            offset: 0,
        },
        0,
    );

    // To do: pass whole `parse_state` back?
    (events, parse_state.codes)
}