diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 80e3ad8..fb249e2 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -1,11 +1,13 @@ use serde::Serialize; #[derive(Debug, Clone, Serialize)] +#[serde(tag = "type", content = "value")] pub enum TypeExpr { Identifier(String), } #[derive(Debug, Clone, Serialize)] +#[serde(tag = "type", content = "value")] pub enum TopLevel { Import(String), StructDecl { @@ -21,11 +23,13 @@ pub enum TopLevel { } #[derive(Debug, Clone, Serialize)] +#[serde(tag = "type", content = "value")] pub enum Statement { Expression(String), } #[derive(Debug, Clone, Serialize)] +#[serde(tag = "type", content = "value")] pub enum Expression { Identifier(String), IntLiteral(i64), diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 1b3f177..02c4a55 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -12,12 +12,14 @@ pub struct MistParser; pub type ParseError = pest::error::Error; pub fn parse(source: &str) -> Result, ParseError> { - let pairs = MistParser::parse(Rule::program, source)?; + let mut pairs = MistParser::parse(Rule::program, source)?; let mut statements = vec![]; - for pair in pairs { - statements.push(TopLevel::from_pair(pair)); + for pair in pairs.next().unwrap().into_inner() { + if let Some(stmt) = TopLevel::from_pair(pair) { + statements.push(stmt); + } } Ok(statements) @@ -37,15 +39,11 @@ impl TypeExpr { } impl TopLevel { - pub fn from_pair(pair: pest::iterators::Pair) -> Self { + pub fn from_pair(pair: pest::iterators::Pair) -> Option { match pair.as_rule() { - Rule::program => { - let inner = pair.into_inner().next().unwrap(); - TopLevel::from_pair(inner) - } Rule::import => { let path = pair.into_inner().next().unwrap().as_str().to_string(); - TopLevel::Import(path) + Some(TopLevel::Import(path)) } Rule::function_decl => { let mut inner = pair.into_inner(); @@ -83,14 +81,17 @@ impl TopLevel { } else { None }; + // For now, we'll just ignore the function body and return an empty vector - TopLevel::FunctionDecl { + Some(TopLevel::FunctionDecl { name, params, return_type, body: vec![], - } + }) } + + Rule::EOI => None, _ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()), } } diff --git a/test/src/main.ms b/test/src/main.ms index bcc1da0..9e8fa56 100644 --- a/test/src/main.ms +++ b/test/src/main.ms @@ -1,5 +1,3 @@ import "fmt"; -function add(a: int, b: int): int { - hello; -} +function add(a: int, b: int): int {}