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)
}
fn block(input: Span) -> ParseResult<Block> {
pub fn block(input: Span) -> ParseResult<Block> {
context(
"block",
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 test;
use std::fmt;
use std::{cell::RefCell, fmt, rc::Rc};
use combinator::Span;
#[cfg(test)]
use crate::ast::{Block, Expression};
@ -33,25 +35,10 @@ impl Parser {
#[cfg(test)]
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 span = Span::new_extra(input, Rc::new(RefCell::new(id_store)));
combinator::expression(span)
.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(),
};
let msg = nom::error::convert_error(input, err);
ParseError { msg, location: (0).into() }
}
_ => panic!(),
})
.map(|(_, output)| output)
combinator::expression(span).map_err(|err| convert_err(input, err)).map(|(_, output)| output)
}
#[cfg(test)]
@ -59,11 +46,37 @@ impl Parser {
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) => {
let err = VerboseError {
errors: err.errors.into_iter().map(|(sp, kind)| (*sp.fragment(), kind)).collect(),
};
let msg = nom::error::convert_error(input, err);
ParseError { msg, location: (0).into() }
}
_ => panic!(),
}
}
/// Represents a parsing error
#[derive(Debug)]
pub struct ParseError {

View File

@ -1336,6 +1336,49 @@ fn blocks() {
))]
.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]