lang: first test and stuff

This commit is contained in:
Schrottkatze 2024-04-24 21:09:55 +02:00
parent ba0da33509
commit 4df0118aa4
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
5 changed files with 69 additions and 5 deletions

7
Cargo.lock generated
View file

@ -504,6 +504,12 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "indoc"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5"
[[package]] [[package]]
name = "ir" name = "ir"
version = "0.1.0" version = "0.1.0"
@ -538,6 +544,7 @@ dependencies = [
"ego-tree", "ego-tree",
"enumset", "enumset",
"indexmap", "indexmap",
"indoc",
"logos", "logos",
"petgraph", "petgraph",
"rowan", "rowan",

View file

@ -15,6 +15,7 @@ ego-tree = "0.6.2"
rowan = "0.15.15" rowan = "0.15.15"
drop_bomb = "0.1.5" drop_bomb = "0.1.5"
enumset = "1.1.3" enumset = "1.1.3"
indoc = "2"
[lints] [lints]
workspace = true workspace = true

View file

@ -1,6 +1,13 @@
use std::fmt::Debug;
use crate::parser::syntax_kind::SyntaxKind::*; use crate::parser::syntax_kind::SyntaxKind::*;
use super::Parser; use super::{
input::Input,
output::Output,
syntax_kind::{self, lex},
Parser,
};
mod expression; mod expression;
@ -12,3 +19,16 @@ pub fn source_file(p: &mut Parser) {
root.complete(p, ROOT); root.complete(p, ROOT);
} }
fn check_parser(input: &str, parser_fn: fn(&mut Parser), output: &str) {
let toks = lex(input);
let mut parser = Parser::new(Input::new(&toks));
parser_fn(&mut parser);
let p_out = dbg!(parser.finish());
let o = Output::from_parser_output(toks, p_out);
let s = format!("{o:?}");
assert_eq!(&s, output);
}

View file

@ -1,6 +1,8 @@
use enumset::enum_set; use enumset::enum_set;
use indoc::indoc;
use crate::parser::{ use crate::parser::{
grammar::check_parser,
syntax_kind::{SyntaxKind::*, TokenSet}, syntax_kind::{SyntaxKind::*, TokenSet},
CompletedMarker, Parser, CompletedMarker, Parser,
}; };
@ -18,3 +20,40 @@ pub fn literal(p: &mut Parser) -> Option<CompletedMarker> {
Some(lit.complete(p, LITERAL)) Some(lit.complete(p, LITERAL))
} }
#[test]
fn test_parse_lst_lit() {
check_parser(
"42",
|p| {
literal(p);
},
indoc! {r#"
LITERAL {
INT_NUM "42";
}
"#},
);
check_parser(
"3.14",
|p| {
literal(p);
},
indoc! {r#"
LITERAL {
FLOAT_NUM "3.14";
}
"#},
);
check_parser(
r#""Meow""#,
|p| {
literal(p);
},
indoc! {r#"
LITERAL {
STRING "\"Meow\"";
}
"#},
);
}

View file

@ -1,4 +1 @@
mat 2x2 [ 42
1 2, 2;
3, 4
]