feat: finish moving definitions and start exprs

This commit is contained in:
rvcas
2023-06-21 16:54:13 -04:00
parent fc580d4fa0
commit 3339d41fdd
12 changed files with 417 additions and 387 deletions

View File

@@ -2,7 +2,7 @@ use chumsky::prelude::*;
use crate::{
ast,
parser::{error::ParseError, token::Token, utils},
parser::{annotation, error::ParseError, token::Token, utils},
};
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
@@ -10,9 +10,13 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
.or_not()
.then_ignore(just(Token::Const))
.then(select! {Token::Name{name} => name})
.then(just(Token::Colon).ignore_then(type_parser()).or_not())
.then(
just(Token::Colon)
.ignore_then(annotation::parser())
.or_not(),
)
.then_ignore(just(Token::Equal))
.then(constant_value_parser())
.then(value())
.map_with_span(|(((public, name), annotation), value), span| {
ast::UntypedDefinition::ModuleConstant(ast::ModuleConstant {
doc: None,
@@ -25,3 +29,37 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
})
})
}
pub fn value() -> impl Parser<Token, ast::Constant, Error = ParseError> {
let constant_string_parser =
select! {Token::String {value} => value}.map_with_span(|value, span| {
ast::Constant::String {
location: span,
value,
}
});
let constant_int_parser =
select! {Token::Int {value, base} => (value, base)}.map_with_span(|(value, base), span| {
ast::Constant::Int {
location: span,
value,
base,
}
});
let constant_bytearray_parser =
utils::bytearray().map_with_span(|(preferred_format, bytes), span| {
ast::Constant::ByteArray {
location: span,
bytes,
preferred_format,
}
});
choice((
constant_string_parser,
constant_int_parser,
constant_bytearray_parser,
))
}

View File

@@ -2,11 +2,11 @@ use chumsky::prelude::*;
use crate::{
ast,
parser::{error::ParseError, token::Token, utils},
parser::{annotation, error::ParseError, token::Token, utils},
};
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
let unlabeled_constructor_type_args = type_parser()
let unlabeled_constructor_type_args = annotation()
.map_with_span(|annotation, span| ast::RecordConstructorArg {
label: None,
annotation,
@@ -49,7 +49,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
utils::public()
.then(just(Token::Opaque).ignored().or_not())
.or_not()
.then(type_name_with_args())
.then(utils::type_name_with_args())
.then(choice((constructors, record_sugar)))
.map_with_span(|((pub_opaque, (name, parameters)), constructors), span| {
ast::UntypedDefinition::DataType(ast::DataType {
@@ -75,3 +75,20 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
})
})
}
fn labeled_constructor_type_args(
) -> impl Parser<Token, Vec<ast::RecordConstructorArg<()>>, Error = ParseError> {
select! {Token::Name {name} => name}
.then_ignore(just(Token::Colon))
.then(annotation())
.map_with_span(|(name, annotation), span| ast::RecordConstructorArg {
label: Some(name),
annotation,
tipo: (),
doc: None,
location: span,
})
.separated_by(just(Token::Comma))
.allow_trailing()
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace))
}

View File

@@ -1,8 +1,9 @@
use chumsky::prelude::*;
use crate::{
ast, expr,
parser::{error::ParseError, token::Token, utils},
ast,
expr::UntypedExpr,
parser::{annotation, error::ParseError, expr, token::Token, utils},
};
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
@@ -11,15 +12,15 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
.then_ignore(just(Token::Fn))
.then(select! {Token::Name {name} => name})
.then(
fn_param_parser(false)
param(false)
.separated_by(just(Token::Comma))
.allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen))
.map_with_span(|arguments, span| (arguments, span)),
)
.then(just(Token::RArrow).ignore_then(type_parser()).or_not())
.then(just(Token::RArrow).ignore_then(annotation()).or_not())
.then(
expr_seq_parser()
expr::sequence()
.or_not()
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace)),
)
@@ -27,7 +28,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
|((((opt_pub, name), (arguments, args_span)), return_annotation), body), span| {
ast::UntypedDefinition::Fn(ast::Function {
arguments,
body: body.unwrap_or_else(|| expr::UntypedExpr::todo(span, None)),
body: body.unwrap_or_else(|| UntypedExpr::todo(span, None)),
doc: None,
location: ast::Span {
start: span.start,
@@ -46,3 +47,43 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
},
)
}
pub fn param(is_validator_param: bool) -> impl Parser<Token, ast::UntypedArg, Error = ParseError> {
choice((
select! {Token::Name {name} => name}
.then(select! {Token::DiscardName {name} => name})
.map_with_span(|(label, name), span| ast::ArgName::Discarded {
label,
name,
location: span,
}),
select! {Token::DiscardName {name} => name}.map_with_span(|name, span| {
ast::ArgName::Discarded {
label: name.clone(),
name,
location: span,
}
}),
select! {Token::Name {name} => name}
.then(select! {Token::Name {name} => name})
.map_with_span(move |(label, name), span| ast::ArgName::Named {
label,
name,
location: span,
is_validator_param,
}),
select! {Token::Name {name} => name}.map_with_span(move |name, span| ast::ArgName::Named {
label: name.clone(),
name,
location: span,
is_validator_param,
}),
))
.then(just(Token::Colon).ignore_then(annotation()).or_not())
.map_with_span(|(arg_name, annotation), span| ast::Arg {
location: span,
annotation,
tipo: (),
arg_name,
})
}

View File

@@ -1,4 +1,4 @@
mod constant;
pub mod constant;
mod data_type;
mod function;
mod import;

View File

@@ -1,8 +1,9 @@
use chumsky::prelude::*;
use crate::{
ast, expr,
parser::{error::ParseError, token::Token},
ast,
expr::UntypedExpr,
parser::{error::ParseError, expr, token::Token},
};
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
@@ -15,14 +16,14 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
.then_ignore(just(Token::RightParen))
.map_with_span(|name, span| (name, span))
.then(
expr_seq_parser()
expr::sequence()
.or_not()
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace)),
)
.map_with_span(|(((fail, name), span_end), body), span| {
ast::UntypedDefinition::Test(ast::Function {
arguments: vec![],
body: body.unwrap_or_else(|| expr::UntypedExpr::todo(span, None)),
body: body.unwrap_or_else(|| UntypedExpr::todo(span, None)),
doc: None,
location: span_end,
end_position: span.end - 1,

View File

@@ -2,15 +2,15 @@ use chumsky::prelude::*;
use crate::{
ast,
parser::{error::ParseError, token::Token, utils},
parser::{annotation, error::ParseError, token::Token, utils},
};
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
utils::public()
.or_not()
.then(type_name_with_args())
.then(utils::type_name_with_args())
.then_ignore(just(Token::Equal))
.then(type_parser())
.then(annotation())
.map_with_span(|((opt_pub, (alias, parameters)), annotation), span| {
ast::UntypedDefinition::TypeAlias(ast::TypeAlias {
alias,

View File

@@ -5,10 +5,12 @@ use crate::{
parser::{error::ParseError, token::Token},
};
use super::function;
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
just(Token::Validator)
.ignore_then(
fn_param_parser(true)
function::param(true)
.separated_by(just(Token::Comma))
.allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen))
@@ -16,7 +18,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
.or_not(),
)
.then(
super::function()
function()
.repeated()
.at_least(1)
.at_most(2)