Some type anno parsing work

This commit is contained in:
greg 2017-09-28 23:55:10 -07:00
parent c20f93e18c
commit bb93d29beb

View File

@ -290,8 +290,10 @@ param_list := (IDENTIFIER type_anno+ ',')*
binding_declaration: 'var' IDENTIFIER '=' expression binding_declaration: 'var' IDENTIFIER '=' expression
| 'const' IDENTIFIER '=' expression | 'const' IDENTIFIER '=' expression
type_anno := ':' type type_anno := (':' type_name)+
type := IDENTIFIER (LAngleBracket type RAngleBracket)* type_name := IDENTIFIER (type_params)* | '(' type_names ')'
type_names := ε | type_name (, type_name)*
type_params := '<' type_name (, type_name)* '>'
expression := precedence_expr type_anno+ expression := precedence_expr type_anno+
@ -423,7 +425,13 @@ pub enum Variant {
pub struct Expression(ExpressionType, Option<TypeAnno>); pub struct Expression(ExpressionType, Option<TypeAnno>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct TypeAnno(Rc<String>); pub enum TypeAnno {
Tuple(Vec<TypeAnno>),
Singleton {
name: Rc<String>,
params: Vec<TypeAnno>,
}
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ExpressionType { pub enum ExpressionType {
@ -493,6 +501,15 @@ macro_rules! parse_method {
}; };
} }
macro_rules! delimited {
($self:expr, $parse_fn:expr, $delim:pat, $end:pat) => {
{
let mut acc = vec![];
acc
}
}
}
impl Parser { impl Parser {
parse_method!(program(&mut self) -> ParseResult<AST> { parse_method!(program(&mut self) -> ParseResult<AST> {
let mut statements = Vec::new(); let mut statements = Vec::new();
@ -574,10 +591,7 @@ impl Parser {
parse_method!(expression(&mut self) -> ParseResult<Expression> { parse_method!(expression(&mut self) -> ParseResult<Expression> {
let mut expr_body = self.precedence_expr(Operation::min_precedence())?; let mut expr_body = self.precedence_expr(Operation::min_precedence())?;
let type_anno = match self.peek() { let type_anno = match self.peek() {
Colon => { Colon => Some(self.type_anno()?),
self.next();
Some(self.type_anno()?)
},
_ => None _ => None
}; };
if let Some(a) = expr_body.1 { if let Some(a) = expr_body.1 {
@ -588,17 +602,23 @@ impl Parser {
}); });
parse_method!(type_anno(&mut self) -> ParseResult<TypeAnno> { parse_method!(type_anno(&mut self) -> ParseResult<TypeAnno> {
let type_name = self.identifier()?; expect!(self, Colon, "Expected ':'");
let param = match self.peek() { Ok(match self.peek() {
LAngleBracket => { LParen => {
self.next(); unimplemented!("Not done with tuple types yet")
let param = self.type_anno()?;
expect!(self, RAngleBracket, "Expected '>'");
Some(param)
}, },
_ => None _ => {
}; let type_name = self.identifier()?;
Ok(TypeAnno(type_name)) let params = match self.peek() {
LAngleBracket => delimited!(self, self.type_anno, Comma, RAngleBracket),
_ => vec![],
};
TypeAnno::Singleton {
name: type_name,
params: params,
}
}
})
}); });
// this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/ // this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/