Implement module loading and imports
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use alloc::{string::String, vec::Vec};
|
||||
|
||||
use crate::{
|
||||
Span,
|
||||
SourceId, Span,
|
||||
ast::{Ast, BinaryOp, CompareOp, Expr, ExprId, Field, Literal, MatchArm, Param},
|
||||
diagnostic::{Diagnostic, Result},
|
||||
lexer::{Lexer, Token, TokenKind},
|
||||
@@ -11,10 +11,21 @@ use crate::{
|
||||
pub struct ParseOutput {
|
||||
pub ast: Ast,
|
||||
pub root: ExprId,
|
||||
pub source_form: SourceForm,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SourceForm {
|
||||
Expr,
|
||||
Fields,
|
||||
}
|
||||
|
||||
pub fn parse_source(source: &str) -> Result<ParseOutput> {
|
||||
let tokens = Lexer::new(source).tokenize()?;
|
||||
parse_source_with_source_id(SourceId(0), source)
|
||||
}
|
||||
|
||||
pub fn parse_source_with_source_id(source_id: SourceId, source: &str) -> Result<ParseOutput> {
|
||||
let tokens = Lexer::with_source_id(source_id, source).tokenize()?;
|
||||
Parser::new(tokens).parse()
|
||||
}
|
||||
|
||||
@@ -34,7 +45,7 @@ impl Parser {
|
||||
}
|
||||
|
||||
pub fn parse(mut self) -> Result<ParseOutput> {
|
||||
let root = if self.starts_field() {
|
||||
let (root, source_form) = if self.starts_field() {
|
||||
let fields = self.parse_fields_until_eof()?;
|
||||
let span = fields
|
||||
.first()
|
||||
@@ -43,16 +54,20 @@ impl Parser {
|
||||
.iter()
|
||||
.fold(f.span, |acc, field| acc.join(field.span))
|
||||
})
|
||||
.unwrap_or_else(|| Span::empty(0));
|
||||
self.ast.push(Expr::Object(fields), span)
|
||||
.unwrap_or_else(|| self.peek().span);
|
||||
(
|
||||
self.ast.push(Expr::Object(fields), span),
|
||||
SourceForm::Fields,
|
||||
)
|
||||
} else {
|
||||
let expr = self.parse_expr(0)?;
|
||||
self.expect_eof()?;
|
||||
expr
|
||||
(expr, SourceForm::Expr)
|
||||
};
|
||||
Ok(ParseOutput {
|
||||
ast: self.ast,
|
||||
root,
|
||||
source_form,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user