fix: properly lex new token and adjust parsed spans

This commit is contained in:
rvcas 2023-02-15 12:56:52 -05:00 committed by Lucas
parent e647330433
commit a88a193383
4 changed files with 66 additions and 16 deletions

View File

@ -235,14 +235,14 @@ pub fn type_alias_parser() -> impl Parser<Token, ast::UntypedDefinition, Error =
pub fn validator_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> { pub fn validator_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
just(Token::Validator) just(Token::Validator)
.ignore_then(select! {Token::Name {name} => name}) .ignore_then(select! {Token::Name {name} => name}.map_with_span(|name, span| (name, span)))
.then( .then(
fn_param_parser() fn_param_parser()
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.allow_trailing() .allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen)) .delimited_by(just(Token::LeftParen), just(Token::RightParen))
.or_not() .map_with_span(|arguments, span| (arguments, span))
.map_with_span(|arguments, span| (arguments.unwrap_or_default(), span)), .or_not(),
) )
.then( .then(
just(Token::Fn) just(Token::Fn)
@ -284,8 +284,10 @@ pub fn validator_parser() -> impl Parser<Token, ast::UntypedDefinition, Error =
) )
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace)), .delimited_by(just(Token::LeftBrace), just(Token::RightBrace)),
) )
.map_with_span(|((name, (params, params_span)), mut fun), span| { .map_with_span(|((name, opt_extra_params), mut fun), span| {
fun.name = name; fun.name = name.0;
let (params, params_span) = opt_extra_params.unwrap_or((vec![], name.1));
ast::UntypedDefinition::Validator(ast::Validator { ast::UntypedDefinition::Validator(ast::Validator {
doc: None, doc: None,

View File

@ -102,6 +102,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = ParseError> {
"todo" => Token::Todo, "todo" => Token::Todo,
"type" => Token::Type, "type" => Token::Type,
"when" => Token::When, "when" => Token::When,
"validator" => Token::Validator,
_ => { _ => {
if s.chars().next().map_or(false, |c| c.is_uppercase()) { if s.chars().next().map_or(false, |c| c.is_uppercase()) {
Token::UpName { Token::UpName {

View File

@ -50,12 +50,56 @@ fn validator() {
assert_definitions( assert_definitions(
code, code,
vec![ast::UntypedDefinition::Use(Use { vec![ast::UntypedDefinition::Validator(ast::Validator {
location: Span::new((), 0..12), doc: None,
module: vec!["std".to_string(), "list".to_string()], end_position: 54,
as_name: None, fun: Function {
unqualified: vec![], arguments: vec![
package: (), ast::Arg {
arg_name: ast::ArgName::Named {
name: "datum".to_string(),
label: "datum".to_string(),
location: Span::new((), 21..26),
},
location: Span::new((), 21..26),
annotation: None,
tipo: (),
},
ast::Arg {
arg_name: ast::ArgName::Named {
name: "rdmr".to_string(),
label: "rdmr".to_string(),
location: Span::new((), 28..32),
},
location: Span::new((), 28..32),
annotation: None,
tipo: (),
},
ast::Arg {
arg_name: ast::ArgName::Named {
name: "ctx".to_string(),
label: "ctx".to_string(),
location: Span::new((), 34..37),
},
location: Span::new((), 34..37),
annotation: None,
tipo: (),
},
],
body: expr::UntypedExpr::Var {
location: Span::new((), 45..49),
name: "True".to_string(),
},
doc: None,
location: Span::new((), 18..38),
name: "foo".to_string(),
public: false,
return_annotation: None,
return_type: (),
end_position: 52,
},
location: Span::new((), 0..13),
params: vec![],
})], })],
) )
} }

View File

@ -12,11 +12,14 @@ type Redeemer {
msg: ByteArray, msg: ByteArray,
} }
fn spend(datum: Datum, redeemer: Redeemer, context: ScriptContext) -> Bool { validator spend {
fn(datum: Datum, redeemer: Redeemer, context: ScriptContext) -> Bool {
let must_say_hello = string.from_bytearray(redeemer.msg) == "Hello, World!" let must_say_hello = string.from_bytearray(redeemer.msg) == "Hello, World!"
let must_be_signed = let must_be_signed =
list.has(context.transaction.extra_signatories, datum.owner) list.has(context.transaction.extra_signatories, datum.owner)
must_say_hello && must_be_signed must_say_hello && must_be_signed
}
} }