summaryrefslogtreecommitdiff
path: root/src/taiga.lalrpop
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2017-11-05 21:12:44 -0500
committerDavid Li <li.davidm96@gmail.com>2017-11-05 21:12:44 -0500
commite5c6400f75029124474ee989388bf2c0abdc7e0a (patch)
treec713adc4c05559bb082375218a9eca9badffc531 /src/taiga.lalrpop
parent44570e6b300961cdcf080b82e7cc1d89fc553b0a (diff)
Parse type and variable declarations
Diffstat (limited to 'src/taiga.lalrpop')
-rw-r--r--src/taiga.lalrpop34
1 files changed, 29 insertions, 5 deletions
diff --git a/src/taiga.lalrpop b/src/taiga.lalrpop
index 851cb24..aa5e50e 100644
--- a/src/taiga.lalrpop
+++ b/src/taiga.lalrpop
@@ -7,17 +7,39 @@ pub Program: ast::Program = {
Expression => ast::Program(<>),
};
-VarDec: Box<WithLocation<ast::VarDec>> =
+RecordFields: Vec<WithLocation<ast::RecordField>> = {
+ <l: @L> <n:Name> ":" <v:Name> <r: @R> "," <rest: RecordFields> => {
+ let mut rest = rest;
+ rest.push(WithLocation::new(ast::RecordField::new(n, v), l, r));
+ rest
+ },
+ <l: @L> <n:Name> ":" <v:Name> <r: @R> "," => vec![WithLocation::new(ast::RecordField::new(n, v), l, r)],
+ <l: @L> <n:Name> ":" <v:Name> <r: @R> => vec![WithLocation::new(ast::RecordField::new(n, v), l, r)],
+ "" => vec![],
+};
+
+Ty: WithLocation<ast::Ty> = {
+ <Name> => <>.map(|v| ast::Ty::Name(v)),
+ "array" "of" <Name> => <>.map(|v| ast::Ty::Array(Box::new(ast::Ty::Name(v)))),
+ <l: @L> "{" <v: RecordFields> "}" <r: @R> => WithLocation::new(ast::Ty::Record(v), l, r),
+};
+
+Declaration: Box<WithLocation<ast::Declaration>> = {
+ <l: @L> "var" <name: Name> ":" <ty: Ty> "=" <exp: Expression> <r: @R> =>
+ Box::new(WithLocation::new(ast::Declaration::new_var(name, Some(ty), exp), l, r)),
<l: @L> "var" <name: Name> "=" <exp: Expression> <r: @R> =>
- Box::new(WithLocation::new(ast::VarDec::new(name, None, exp), l, r));
+ Box::new(WithLocation::new(ast::Declaration::new_var(name, None, exp), l, r)),
+ <l: @L> "type" <name: Name> "=" <ty: Ty> <r: @R> =>
+ Box::new(WithLocation::new(ast::Declaration::new_ty(name, ty), l, r)),
+};
-DeclarationsList: Vec<WithLocation<ast::VarDec>> = {
- <h: DeclarationsList> <t: VarDec> => {
+DeclarationsList: Vec<WithLocation<ast::Declaration>> = {
+ <h: DeclarationsList> <t: Declaration> => {
let mut h = h;
h.push(*t);
h
},
- <VarDec> => vec![*<>],
+ <Declaration> => vec![*<>],
};
Expression: Box<ast::WithLocation<ast::Expression>> = {
@@ -121,6 +143,8 @@ match {
"let",
"in",
"end",
+ "type",
+ "function",
}
else {
r"[[:alpha:]_][[:alpha:]_0-9]*",