Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 134 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ allocator-api2.workspace = true
ahash.workspace = true
ariadne = "0.6.0"

[dev-dependencies]
proptest = "1.6.0"

[lints]
workspace = true
6 changes: 5 additions & 1 deletion parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ mod diagnostics;
mod idempotency;
mod lex;
mod pratt;
#[cfg(test)]
mod prop_tests;
mod sexpr;
#[cfg(test)]
mod testing;
#[cfg(test)]
mod tests;

use std::{fmt::Debug, mem::replace};
Expand Down Expand Up @@ -738,7 +742,7 @@ impl<'a> Parser<'a> {
}

impl<'a> Ast<'a> {
fn new(arena: &'a Bump) -> Self {
pub(crate) fn new(arena: &'a Bump) -> Self {
Self {
loads: Vec::new_in(arena),
begin: Vec::new_in(arena),
Expand Down
107 changes: 107 additions & 0 deletions parser/src/prop_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// This file is part of the uutils awk package.
//
// For the full copyright and license information, please view the LICENSE
// files that was distributed with this source code.

use std::fmt::Write;

use bumpalo::Bump;
use proptest::prelude::*;

use crate::testing::{
ast_gen::{self, GenAtom, GenBody, GenExpr, GenPattern, GenProgram, GenRule, GenStatement},
roundtrip_ast, roundtrip_source,
};

proptest! {
#![proptest_config(ProptestConfig {
cases: 128,
.. ProptestConfig::default()
})]

#[test]
fn roundtrip_generated_program(program in ast_gen::gen_program()) {
let arena = Bump::new();
let ast = ast_gen::materialize(&program, &arena);
roundtrip_ast(&ast).map_err(|e| TestCaseError::fail(e))?;
}

#[test]
fn roundtrip_generated_source(program in ast_gen::gen_program()) {
let arena = Bump::new();
let ast = ast_gen::materialize(&program, &arena);
let mut source = String::new();
write!(source, "{ast}").map_err(|e| TestCaseError::fail(format!("display failed: {e}")))?;
roundtrip_source(&source).map_err(|e| TestCaseError::fail(e))?;
}
}

#[test]
fn roundtrip_smoke_cases() {
let cases = [
"{ print 1 + a }",
"BEGIN { print 1 }",
"{ if (a) print; else print 0 }",
"{ while (a < 10) a++ }",
"{ a = 1; b = a + 2 }",
"/pat/ { print $1, $2 }",
"BEGIN { print \"hi\" }",
];
for source in cases {
roundtrip_source(source)
.unwrap_or_else(|e| panic!("round-trip failed for {source:?}: {e}"));
}
}

#[test]
fn roundtrip_handwritten_ast() {
let program = GenProgram {
rules: vec![
GenRule {
pattern: Some(GenPattern::Regex(0)),
body: GenBody {
statements: vec![GenStatement::Print(vec![GenExpr::Atom(GenAtom::Var(0))])],
},
},
GenRule {
pattern: None,
body: GenBody {
statements: vec![
GenStatement::If {
condition: GenExpr::Binary(
crate::ast::BinaryOperator::Lt,
Box::new(GenExpr::Atom(GenAtom::Var(0))),
Box::new(GenExpr::Atom(GenAtom::SmallInt(10))),
),
then_body: GenBody {
statements: vec![GenStatement::Print(vec![GenExpr::Atom(
GenAtom::SmallInt(1),
)])],
},
else_body: None,
},
GenStatement::Print(vec![GenExpr::Binary(
crate::ast::BinaryOperator::Add,
Box::new(GenExpr::Atom(GenAtom::SmallInt(1))),
Box::new(GenExpr::Atom(GenAtom::Var(0))),
)]),
GenStatement::Expr(GenExpr::Binary(
crate::ast::BinaryOperator::Eq,
Box::new(GenExpr::Atom(GenAtom::SmallInt(0))),
Box::new(GenExpr::Atom(GenAtom::SmallInt(1))),
)),
],
},
},
],
begin: Some(GenBody {
statements: vec![GenStatement::Print(vec![GenExpr::Atom(GenAtom::SmallInt(
0,
))])],
}),
};

let arena = Bump::new();
let ast = ast_gen::materialize(&program, &arena);
roundtrip_ast(&ast).expect("hand-written AST should round-trip");
}
Loading
Loading