summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNanoTech2016-12-06 04:12:17 -0600
committerNanoTech2017-03-10 23:48:27 -0600
commit7e76e6745ca2eb1dbe78c7503371094e6694fcdf (patch)
tree214db303b947c51dbb8f3011d9a94db6445e1622 /src
parent7d0f38c1ff244f47bd3619515567246b498b493e (diff)
Separate keywords and builtins
Diffstat (limited to '')
-rw-r--r--src/grammar.lalrpop16
-rw-r--r--src/lexer.rs63
2 files changed, 44 insertions, 35 deletions
diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop
index 3d4603d..73d1675 100644
--- a/src/grammar.lalrpop
+++ b/src/grammar.lalrpop
@@ -1,7 +1,7 @@
use core;
use core::Expr::*;
use grammar_util::*;
-use lexer::{Keyword, LexicalError, Tok};
+use lexer::{Builtin, Keyword, LexicalError, Tok};
grammar;
@@ -19,12 +19,12 @@ extern {
Nat => Tok::Natural(<usize>),
Bool => Tok::Bool(<bool>),
Label => Tok::Identifier(<String>),
- Let => Tok::Reserved(Keyword::Let),
- In => Tok::Reserved(Keyword::In),
- If => Tok::Reserved(Keyword::If),
- Then => Tok::Reserved(Keyword::Then),
- Else => Tok::Reserved(Keyword::Else),
- Reserved => Tok::Reserved(<Keyword>),
+ Let => Tok::Keyword(Keyword::Let),
+ In => Tok::Keyword(Keyword::In),
+ If => Tok::Keyword(Keyword::If),
+ Then => Tok::Keyword(Keyword::Then),
+ Else => Tok::Keyword(Keyword::Else),
+ Builtin => Tok::Builtin(<Builtin>),
"(" => Tok::ParenL,
")" => Tok::ParenR,
@@ -97,7 +97,7 @@ ExprF: BoxExpr = {
Nat => bx(NaturalLit(<>)),
Int => bx(IntegerLit(<>)),
Label => bx(Var(core::Var(<>, 0))), // FIXME support var@n syntax
- Reserved => bx(Bool), // FIXME
+ Builtin => bx(Bool), // FIXME
Bool => bx(BoolLit(<>)),
"(" <Expr> ")",
};
diff --git a/src/lexer.rs b/src/lexer.rs
index 23457ac..37268a8 100644
--- a/src/lexer.rs
+++ b/src/lexer.rs
@@ -4,6 +4,15 @@ use std::str::FromStr;
#[derive(Debug, PartialEq, Eq)]
pub enum Keyword {
+ Let,
+ In,
+ If,
+ Then,
+ Else,
+}
+
+#[derive(Debug, PartialEq, Eq)]
+pub enum Builtin {
Natural,
NaturalFold,
NaturalBuild,
@@ -24,17 +33,13 @@ pub enum Keyword {
Optional,
OptionalFold,
Bool,
- Let,
- In,
- If,
- Then,
- Else,
}
#[derive(Debug, PartialEq, Eq)]
pub enum Tok {
Identifier(String),
- Reserved(Keyword),
+ Keyword(Keyword),
+ Builtin(Builtin),
Bool(bool),
Integer(isize),
Natural(usize),
@@ -124,26 +129,6 @@ named!(boolean<&str, bool>, alt!(
));
named!(keyword<&str, Keyword>, alt!(
- value!(Keyword::NaturalFold, tag!("Natural/fold")) |
- value!(Keyword::NaturalBuild, tag!("Natural/build")) |
- value!(Keyword::NaturalIsZero, tag!("Natural/isZero")) |
- value!(Keyword::NaturalEven, tag!("Natural/even")) |
- value!(Keyword::NaturalOdd, tag!("Natural/odd")) |
- value!(Keyword::Natural, tag!("Natural")) |
- value!(Keyword::Integer, tag!("Integer")) |
- value!(Keyword::Double, tag!("Double")) |
- value!(Keyword::Text, tag!("Text")) |
- value!(Keyword::ListBuild, tag!("List/build")) |
- value!(Keyword::ListFold, tag!("List/fold")) |
- value!(Keyword::ListLength, tag!("List/length")) |
- value!(Keyword::ListHead, tag!("List/head")) |
- value!(Keyword::ListLast, tag!("List/last")) |
- value!(Keyword::ListIndexed, tag!("List/indexed")) |
- value!(Keyword::ListReverse, tag!("List/reverse")) |
- value!(Keyword::List, tag!("List")) |
- value!(Keyword::OptionalFold, tag!("Optional/fold")) |
- value!(Keyword::Optional, tag!("Optional")) |
- value!(Keyword::Bool, tag!("Bool")) |
value!(Keyword::Let, tag!("let")) |
value!(Keyword::In, tag!("in")) |
value!(Keyword::If, tag!("if")) |
@@ -151,6 +136,29 @@ named!(keyword<&str, Keyword>, alt!(
value!(Keyword::Else, tag!("else"))
));
+named!(builtin<&str, Builtin>, alt!(
+ value!(Builtin::NaturalFold, tag!("Natural/fold")) |
+ value!(Builtin::NaturalBuild, tag!("Natural/build")) |
+ value!(Builtin::NaturalIsZero, tag!("Natural/isZero")) |
+ value!(Builtin::NaturalEven, tag!("Natural/even")) |
+ value!(Builtin::NaturalOdd, tag!("Natural/odd")) |
+ value!(Builtin::Natural, tag!("Natural")) |
+ value!(Builtin::Integer, tag!("Integer")) |
+ value!(Builtin::Double, tag!("Double")) |
+ value!(Builtin::Text, tag!("Text")) |
+ value!(Builtin::ListBuild, tag!("List/build")) |
+ value!(Builtin::ListFold, tag!("List/fold")) |
+ value!(Builtin::ListLength, tag!("List/length")) |
+ value!(Builtin::ListHead, tag!("List/head")) |
+ value!(Builtin::ListLast, tag!("List/last")) |
+ value!(Builtin::ListIndexed, tag!("List/indexed")) |
+ value!(Builtin::ListReverse, tag!("List/reverse")) |
+ value!(Builtin::List, tag!("List")) |
+ value!(Builtin::OptionalFold, tag!("Optional/fold")) |
+ value!(Builtin::Optional, tag!("Optional")) |
+ value!(Builtin::Bool, tag!("Bool"))
+));
+
named!(token<&str, Tok>, alt!(
value!(Tok::Pi, tag!("forall")) |
value!(Tok::Pi, tag!("∀")) |
@@ -162,7 +170,8 @@ named!(token<&str, Tok>, alt!(
value!(Tok::Arrow, tag!("→")) |
map!(boolean, Tok::Bool) |
- map!(keyword, Tok::Reserved) |
+ map!(keyword, Tok::Keyword) |
+ map!(builtin, Tok::Builtin) |
map_opt!(natural, |s| usize::from_str(s).ok().map(|n| Tok::Natural(n))) |
map!(integer, Tok::Integer) |
map!(identifier, |s: &str| Tok::Identifier(s.to_owned())) |