test(parser): rename definitions to definition and more tests

This commit is contained in:
rvcas
2023-07-03 14:58:04 -04:00
parent baf807ca2d
commit 6b05d6a91e
28 changed files with 337 additions and 362 deletions

View File

@@ -46,3 +46,25 @@ pub fn parser<'a>(
}
})
}
#[cfg(test)]
mod tests {
use chumsky::Parser;
use crate::assert_expr;
#[test]
fn if_else_basic() {
assert_expr!(
r#"
if True {
1 + 1
} else if a < 1 {
3
} else {
4
}
"#
);
}
}

View File

@@ -14,3 +14,38 @@ pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
}
})
}
#[cfg(test)]
mod tests {
use chumsky::Parser;
use crate::assert_expr;
#[test]
fn int_literal() {
assert_expr!("1");
}
#[test]
fn int_negative() {
assert_expr!("-1");
}
#[test]
fn int_numeric_underscore() {
assert_expr!(
r#"
{
let i = 1_234_567
let j = 1_000_000
let k = -10_000
}
"#
);
}
#[test]
fn int_hex_bytes() {
assert_expr!(r#"0x01"#);
}
}

View File

@@ -30,7 +30,6 @@ pub fn parser(
#[cfg(test)]
mod tests {
use chumsky::Parser;
use crate::assert_expr;

View File

@@ -0,0 +1,66 @@
---
source: crates/aiken-lang/src/parser/expr/if_else.rs
description: "Code:\n\nif True {\n 1 + 1\n} else if a < 1 {\n 3\n} else {\n 4\n}\n"
---
If {
location: 0..54,
branches: [
IfBranch {
condition: Var {
location: 3..7,
name: "True",
},
body: BinOp {
location: 12..17,
name: AddInt,
left: Int {
location: 12..13,
value: "1",
base: Decimal {
numeric_underscore: false,
},
},
right: Int {
location: 16..17,
value: "1",
base: Decimal {
numeric_underscore: false,
},
},
},
location: 3..19,
},
IfBranch {
condition: BinOp {
location: 28..33,
name: LtInt,
left: Var {
location: 28..29,
name: "a",
},
right: Int {
location: 32..33,
value: "1",
base: Decimal {
numeric_underscore: false,
},
},
},
body: Int {
location: 38..39,
value: "3",
base: Decimal {
numeric_underscore: false,
},
},
location: 28..41,
},
],
final_else: Int {
location: 51..52,
value: "4",
base: Decimal {
numeric_underscore: false,
},
},
}

View File

@@ -0,0 +1,9 @@
---
source: crates/aiken-lang/src/parser/expr/int.rs
description: "Code:\n\n0x01"
---
Int {
location: 0..4,
value: "1",
base: Hexadecimal,
}

View File

@@ -0,0 +1,11 @@
---
source: crates/aiken-lang/src/parser/expr/int.rs
description: "Code:\n\n1"
---
Int {
location: 0..1,
value: "1",
base: Decimal {
numeric_underscore: false,
},
}

View File

@@ -0,0 +1,15 @@
---
source: crates/aiken-lang/src/parser/expr/int.rs
description: "Code:\n\n-1"
---
UnOp {
op: Negate,
location: 0..2,
value: Int {
location: 1..2,
value: "1",
base: Decimal {
numeric_underscore: false,
},
},
}

View File

@@ -0,0 +1,61 @@
---
source: crates/aiken-lang/src/parser/expr/int.rs
description: "Code:\n\n{\n let i = 1_234_567\n let j = 1_000_000\n let k = -10_000\n}\n"
---
Sequence {
location: 4..59,
expressions: [
Assignment {
location: 4..21,
value: Int {
location: 12..21,
value: "1234567",
base: Decimal {
numeric_underscore: true,
},
},
pattern: Var {
location: 8..9,
name: "i",
},
kind: Let,
annotation: None,
},
Assignment {
location: 24..41,
value: Int {
location: 32..41,
value: "1000000",
base: Decimal {
numeric_underscore: true,
},
},
pattern: Var {
location: 28..29,
name: "j",
},
kind: Let,
annotation: None,
},
Assignment {
location: 44..59,
value: UnOp {
op: Negate,
location: 52..59,
value: Int {
location: 53..59,
value: "10000",
base: Decimal {
numeric_underscore: true,
},
},
},
pattern: Var {
location: 48..49,
name: "k",
},
kind: Let,
annotation: None,
},
],
}

View File

@@ -2,7 +2,7 @@ use chumsky::prelude::*;
use crate::{
ast,
parser::{definitions, error::ParseError, token::Token},
parser::{definition, error::ParseError, token::Token},
};
pub fn parser() -> impl Parser<Token, ast::UntypedClauseGuard, Error = ParseError> {
@@ -17,7 +17,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedClauseGuard, Error = ParseErro
location: span,
});
let constant_parser = definitions::constant::value().map(ast::ClauseGuard::Constant);
let constant_parser = definition::constant::value().map(ast::ClauseGuard::Constant);
let block_parser = r
.clone()