More test work

This commit is contained in:
Greg Shuflin 2021-11-18 01:37:05 -08:00
parent 8ace37c5cf
commit fc088923c0
3 changed files with 75 additions and 76 deletions

View File

@ -78,7 +78,7 @@ fn statement_delimiter(input: Span) -> ParseResult<()> {
tok(alt((value((), line_ending), value((), char(';')))))(input) tok(alt((value((), line_ending), value((), char(';')))))(input)
} }
fn block(input: Span) -> ParseResult<Block> { pub fn block(input: Span) -> ParseResult<Block> {
context( context(
"block", "block",
map( map(
@ -376,61 +376,4 @@ mod test {
) )
); );
} }
#[test]
fn combinator_test3() {
let source = "{}";
assert_eq!(span!(block, source).unwrap().1, vec![].into());
let source = r#"{
//hella
4_5 //bog
11; /*chutney*/0xf
}"#;
let parsed = span!(block, source).map_err(|err| match err {
Err::Error(err) | Err::Failure(err) => {
let err = VerboseError {
errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(),
};
nom::error::convert_error(source, err)
}
_ => panic!(),
});
if let Err(err) = parsed {
println!("{}", err);
panic!("parse error desu!");
}
assert_eq!(
parsed.unwrap().1,
vec![
Statement {
id: Default::default(),
location: Default::default(),
kind: StatementKind::Expression(Expression::new(
Default::default(),
ExpressionKind::NatLiteral(45)
))
},
Statement {
id: Default::default(),
location: Default::default(),
kind: StatementKind::Expression(Expression::new(
Default::default(),
ExpressionKind::NatLiteral(11)
))
},
Statement {
id: Default::default(),
location: Default::default(),
kind: StatementKind::Expression(Expression::new(
Default::default(),
ExpressionKind::NatLiteral(15)
))
},
]
.into()
);
}
} }

View File

@ -4,7 +4,9 @@ pub mod combinator;
mod peg_parser; mod peg_parser;
mod test; mod test;
use std::fmt; use std::{cell::RefCell, fmt, rc::Rc};
use combinator::Span;
#[cfg(test)] #[cfg(test)]
use crate::ast::{Block, Expression}; use crate::ast::{Block, Expression};
@ -33,15 +35,37 @@ impl Parser {
#[cfg(test)] #[cfg(test)]
fn expression_comb(&mut self, input: &str) -> Result<Expression, ParseError> { fn expression_comb(&mut self, input: &str) -> Result<Expression, ParseError> {
use std::{cell::RefCell, rc::Rc};
use combinator::Span;
use nom::{error::VerboseError, Err};
let id_store: IdStore<ASTItem> = IdStore::new(); let id_store: IdStore<ASTItem> = IdStore::new();
let span = Span::new_extra(input, Rc::new(RefCell::new(id_store))); let span = Span::new_extra(input, Rc::new(RefCell::new(id_store)));
combinator::expression(span)
.map_err(|err| match err { combinator::expression(span).map_err(|err| convert_err(input, err)).map(|(_, output)| output)
}
#[cfg(test)]
fn block(&mut self, input: &str) -> Result<Block, ParseError> {
peg_parser::schala_parser::block(input, self).map_err(ParseError::from_peg)
}
#[cfg(test)]
fn block_comb(&mut self, input: &str) -> Result<Block, ParseError> {
let id_store: IdStore<ASTItem> = IdStore::new();
let span = Span::new_extra(input, Rc::new(RefCell::new(id_store)));
combinator::block(span).map_err(|err| convert_err(input, err)).map(|(_, output)| output)
}
fn fresh(&mut self) -> Id<ASTItem> {
self.id_store.fresh()
}
}
fn convert_err<'a>(
input: &'a str,
err: nom::Err<nom::error::VerboseError<combinator::Span<'a>>>,
) -> ParseError {
use nom::{error::VerboseError, Err};
match err {
Err::Error(err) | Err::Failure(err) => { Err::Error(err) | Err::Failure(err) => {
let err = VerboseError { let err = VerboseError {
errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(), errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(),
@ -50,17 +74,6 @@ impl Parser {
ParseError { msg, location: (0).into() } ParseError { msg, location: (0).into() }
} }
_ => panic!(), _ => panic!(),
})
.map(|(_, output)| output)
}
#[cfg(test)]
fn block(&mut self, input: &str) -> Result<Block, ParseError> {
peg_parser::schala_parser::block(input, self).map_err(ParseError::from_peg)
}
fn fresh(&mut self) -> Id<ASTItem> {
self.id_store.fresh()
} }
} }

View File

@ -1336,6 +1336,49 @@ fn blocks() {
))] ))]
.into() .into()
); );
let block = parser.block_comb("{}");
assert_eq!(block.unwrap(), vec![].into());
let source = r#"{
//hella
4_5 //bog
11; /*chutney*/0xf
}"#;
let block = parser.block_comb(source);
assert_eq!(
block.unwrap(),
vec![
Statement {
id: Default::default(),
location: Default::default(),
kind: StatementKind::Expression(Expression::new(
Default::default(),
ExpressionKind::NatLiteral(45)
))
},
Statement {
id: Default::default(),
location: Default::default(),
kind: StatementKind::Expression(Expression::new(
Default::default(),
ExpressionKind::NatLiteral(11)
))
},
Statement {
id: Default::default(),
location: Default::default(),
kind: StatementKind::Expression(Expression::new(
Default::default(),
ExpressionKind::NatLiteral(15)
))
},
]
.into()
);
} }
#[test] #[test]