microcad_lang_parse/
lib.rs1pub mod ast;
13
14pub mod token;
16
17mod parse;
19
20mod lex;
22
23use microcad_lang_base::virtual_url;
24pub use parse::{ParseContext, ParseError, ParseErrors, parsers};
25
26pub trait Parse: Sized {
28 fn parse(context: &ParseContext) -> Result<Self, ParseErrors>;
32}
33
34impl Parse for ast::Source {
35 fn parse(context: &ParseContext) -> Result<Self, ParseErrors> {
36 match context {
37 ParseContext::Element(code) => {
38 let ast = crate::parse(code)?;
39 let src_ref = context.src_ref(&ast.span);
40
41 Ok(Self {
42 url: virtual_url("virtual"),
43 ast: microcad_lang_base::Refer::new(ast, src_ref),
44 line_offset: 0,
45 code: code.clone().map(|s| s.to_string()),
46 })
47 }
48 ParseContext::Source {
49 url,
50 line_offset,
51 code,
52 ..
53 } => {
54 let ast = crate::parse(code.value())?;
55 let src_ref = context.src_ref(&ast.span);
56
57 Ok(Self {
58 url: url.clone(),
59 ast: microcad_lang_base::Refer::new(ast, src_ref),
60 line_offset: *line_offset,
61 code: code.clone().map(|s| s.to_string()),
62 })
63 }
64 }
65 }
66}
67
68pub use lex::lex;
69
70pub fn parse(source: &str) -> Result<ast::Program, ParseErrors> {
72 parse::parse(&lex(source).collect::<Vec<_>>())
73}