1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use core::fmt;
use std::fs;
use std::path::{PathBuf, Path};
use std::io;
use crate::utils::{self, ReadChars};

use crate::lexer::LexerBuilder;
use crate::parser::{Parser, ParserError};
use crate::parser::stmt::StmtMeta;
use crate::runtime::strings::StringInterner;

type ReadFileChars = ReadChars<io::BufReader<fs::File>>;

#[derive(Debug, Hash)]
pub enum ModuleSource {
    String(String),
    File(PathBuf),
}

impl ModuleSource {
    // Load the source text
    pub fn read_text(&self) -> io::Result<SourceText> {
        match self {
            Self::String(string) => Ok(SourceText::String(string.clone())),
            Self::File(ref path) => Ok(SourceText::File(Self::read_source_file(path)?)),
        }
    }
    
    fn read_source_file(path: &Path) -> io::Result<ReadFileChars> {
        let file = fs::File::open(path)?;
        let reader = io::BufReader::new(file);
        Ok(ReadChars::new(reader))
    }
}

impl fmt::Display for ModuleSource {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::File(path) => write!(fmt, "file \"{}\"", path.display()),
            Self::String(string) => write!(fmt, "source text \"{}\"", utils::trim_str(string, 16)),
        }
    }
}


#[derive(Debug)]
pub enum SourceText {
    String(String),
    File(ReadFileChars),
}

impl<S> From<S> for SourceText where S: ToString {
    fn from(text: S) -> Self { SourceText::String(text.to_string()) }
}



/// High-level Parsing Interface
///
/// Contains the state required for parsing, and deals with the separate code paths taken for different SourceTypes
pub struct ParseContext<'f, 's> {
    lexer_factory: &'f LexerBuilder,
    interner: &'s mut StringInterner,
}

impl<'f, 's> ParseContext<'f, 's> {
    pub fn new(lexer_factory: &'f LexerBuilder, interner: &'s mut StringInterner) -> Self {
        ParseContext {
            lexer_factory,
            interner,
        }
    }
    
    // Returns a Vec of parsed Stmts (if no error occurred) or a Vec or errors
    pub fn parse_ast(&mut self, source: SourceText) -> Result<Vec<StmtMeta>, Vec<ParserError>> {
        
        let output = self.collect_parser_output(source);
        
        if output.iter().any(|r| r.is_err()) {
            Err(output.into_iter().filter_map(|r| r.err()).collect())
        } else {
            Ok(output.into_iter().filter_map(|r| r.ok()).collect())
        }
    }

    // Helper to deal with the separate branches for parsing SourceText
    fn collect_parser_output(&mut self, source: SourceText) -> Vec<Result<StmtMeta, ParserError>> {
        match source {
            SourceText::String(text) => {
                let mut chars = Vec::with_capacity(text.len());
                chars.extend(text.chars().map(Ok));
                
                let lexer = self.lexer_factory.build(chars.into_iter());
                let parser = Parser::new(self.interner, lexer);
                parser.collect()
            }
            SourceText::File(text) => {
                let lexer = self.lexer_factory.build(text);
                let parser = Parser::new(self.interner, lexer);
                parser.collect()
            },
        }
    }
}