Skip to content

Commit

Permalink
test: extending GLR build tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igordejanovic committed Oct 8, 2023
1 parent 1c6a26f commit fb8085f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 4 deletions.
54 changes: 50 additions & 4 deletions tests/src/glr/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::BorrowMut;

use rustemo::rustemo_mod;
use rustemo::{lr::builder::TreeBuilder, rustemo_mod, glr::gss::GssHead};
use rustemo_compiler::output_cmp;

rustemo_mod!(calc, "/src/glr/build");
Expand All @@ -10,20 +10,66 @@ use self::calc::CalcParser;
use rustemo::parser::Parser;

#[test]
fn glr_tree_build() {
fn glr_tree_build_default() {
let forest = CalcParser::new().parse("1 + 4 * 9").unwrap();
assert_eq!(forest.solutions(), 2);

let mut builder = calc::DefaultBuilder::new();
output_cmp!(
"src/glr/build/tree_build_1.ast",
"src/glr/build/tree_build_default_1.ast",
format!(
"{:#?}",
forest.get_first_tree().unwrap().build(&mut builder)
)
);
output_cmp!(
"src/glr/build/tree_build_2.ast",
"src/glr/build/tree_build_default_2.ast",
format!("{:#?}", forest.get_tree(1).unwrap().build(&mut builder))
);
}

#[test]
fn glr_tree_build_generic() {
let forest = CalcParser::new().parse("1 + 4 * 9").unwrap();
assert_eq!(forest.solutions(), 2);

let mut builder = TreeBuilder::new();
output_cmp!(
"src/glr/build/tree_build_generic_1.ast",
format!(
"{:#?}",
forest.get_first_tree().unwrap().build::<TreeBuilder<
'_,
str,
calc::ProdKind,
calc::TokenKind,
>, GssHead<
'_,
str,
calc::State,
calc::TokenKind,
>, calc::State>(
&mut builder
)
)
);
output_cmp!(
"src/glr/build/tree_build_generic_2.ast",
format!(
"{:#?}",
forest.get_tree(1).unwrap().build::<TreeBuilder<
'_,
str,
calc::ProdKind,
calc::TokenKind,
>, GssHead<
'_,
str,
calc::State,
calc::TokenKind,
>, calc::State>(
&mut builder
)
)
);
}
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions tests/src/glr/build/tree_build_generic_1.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
NonTermNode {
prod: E: E Mul E,
location: [1,0],
children: [
NonTermNode {
prod: E: E Plus E,
location: [1,0],
children: [
NonTermNode {
prod: E: Num,
location: [1,0],
children: [
TermNode {
token: Num("\"1\"" [1,0-1,1]),
layout: None,
},
],
layout: None,
},
TermNode {
token: Plus("\"+\"" [1,2-1,3]),
layout: None,
},
NonTermNode {
prod: E: Num,
location: [1,0],
children: [
TermNode {
token: Num("\"4\"" [1,4-1,5]),
layout: None,
},
],
layout: None,
},
],
layout: None,
},
TermNode {
token: Mul("\"*\"" [1,6-1,7]),
layout: None,
},
NonTermNode {
prod: E: Num,
location: [1,0],
children: [
TermNode {
token: Num("\"9\"" [1,8-1,9]),
layout: None,
},
],
layout: None,
},
],
layout: None,
}
55 changes: 55 additions & 0 deletions tests/src/glr/build/tree_build_generic_2.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
NonTermNode {
prod: E: E Plus E,
location: [1,0],
children: [
NonTermNode {
prod: E: Num,
location: [1,0],
children: [
TermNode {
token: Num("\"1\"" [1,0-1,1]),
layout: None,
},
],
layout: None,
},
TermNode {
token: Plus("\"+\"" [1,2-1,3]),
layout: None,
},
NonTermNode {
prod: E: E Mul E,
location: [1,0],
children: [
NonTermNode {
prod: E: Num,
location: [1,0],
children: [
TermNode {
token: Num("\"4\"" [1,4-1,5]),
layout: None,
},
],
layout: None,
},
TermNode {
token: Mul("\"*\"" [1,6-1,7]),
layout: None,
},
NonTermNode {
prod: E: Num,
location: [1,0],
children: [
TermNode {
token: Num("\"9\"" [1,8-1,9]),
layout: None,
},
],
layout: None,
},
],
layout: None,
},
],
layout: None,
}

0 comments on commit fb8085f

Please sign in to comment.