Skip to main content

moonlift/
lib.rs

1use std::{convert::Infallible, io};
2
3use ast::Block;
4use lexer::{Lexer, Position};
5use parser::Parser;
6
7mod ast;
8pub mod jit;
9mod lexer;
10mod parser;
11
12#[derive(thiserror::Error, Debug)]
13pub enum Error<E = Infallible> {
14    #[error("Parse error at {1}: {0}")]
15    ParseError(parser::ParseError<E>, Position),
16    #[error(transparent)]
17    ModuleError(#[from] ModuleError),
18}
19
20#[derive(thiserror::Error, Debug)]
21pub enum ModuleError {}
22
23pub struct Source {
24    block: Block,
25}
26
27impl Source {
28    pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, Error> {
29        let mut lexer = Lexer::from_bytes(bytes);
30        let mut parser = Parser::new(&mut lexer);
31        match parser.parse() {
32            Ok(block) => Ok(Self::from_block(block)?),
33            Err(e) => Err(Error::ParseError(e, lexer.position())),
34        }
35    }
36    pub fn read(read: impl io::Read) -> Result<Self, Error<io::Error>> {
37        let mut lexer = lexer::Lexer::read(read);
38        let mut parser = Parser::new(&mut lexer);
39        match parser.parse() {
40            Ok(block) => Ok(Self::from_block(block)?),
41            Err(e) => Err(Error::ParseError(e, lexer.position())),
42        }
43    }
44    fn from_block(block: Block) -> Result<Self, ModuleError> {
45        Ok(Self { block })
46    }
47}