Cleanup warnings

This commit is contained in:
Greg Shuflin 2021-11-20 22:55:11 -08:00
parent 194cb2202a
commit bba433c808
4 changed files with 73 additions and 75 deletions

View File

@ -1,19 +1,17 @@
use std::{cell::RefCell, rc::Rc}; use std::rc::Rc;
use nom::{ use nom::{
branch::alt, branch::alt,
bytes::complete::{escaped_transform, tag, take_till, take_while}, bytes::complete::{escaped_transform, tag, take_while},
character::{ character::{
complete::{ complete::{alpha1, char, line_ending, none_of, not_line_ending, one_of, space1},
alpha1, alphanumeric0, anychar, char, line_ending, none_of, not_line_ending, one_of, space1,
},
is_alphanumeric, is_alphanumeric,
}, },
combinator::{cut, eof, map, not, opt, peek, recognize, value, verify}, combinator::{cut, eof, map, not, opt, peek, recognize, value, verify},
error::{context, ErrorKind, ParseError, VerboseError}, error::{context, ErrorKind, ParseError, VerboseError},
multi::{many0, many1, separated_list0, separated_list1}, multi::{many0, many1, separated_list0, separated_list1},
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple}, sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
Finish, IResult, InputIter, InputLength, InputTake, Parser, Slice, IResult, InputIter, InputLength, InputTake, Parser, Slice,
}; };
use nom_locate::{position, LocatedSpan}; use nom_locate::{position, LocatedSpan};
@ -102,12 +100,12 @@ fn block_comment(input: Span) -> ParseResult<()> {
fn inner_parser(mut input: Span) -> ParseResult<()> { fn inner_parser(mut input: Span) -> ParseResult<()> {
loop { loop {
let mut iter = input.iter_indices(); let mut iter = input.iter_indices();
while let Some((idx, ch)) = iter.next() { while let Some((idx, _ch)) = iter.next() {
if idx + 2 > input.input_len() { if idx + 2 > input.input_len() {
return Err(nom::Err::Failure(VerboseError::from_error_kind(input, ErrorKind::Verify))); return Err(nom::Err::Failure(VerboseError::from_error_kind(input, ErrorKind::Verify)));
} }
if input.slice(idx..idx + 2).fragment() == &"/*" { if input.slice(idx..idx + 2).fragment() == &"/*" {
let (rest, seen) = input.take_split(idx); let (rest, _seen) = input.take_split(idx);
let (rest, ()) = block_comment(rest)?; let (rest, ()) = block_comment(rest)?;
input = rest; input = rest;
@ -115,7 +113,7 @@ fn block_comment(input: Span) -> ParseResult<()> {
} }
if input.slice(idx..idx + 2).fragment() == &"*/" { if input.slice(idx..idx + 2).fragment() == &"*/" {
let (rest, seen) = input.take_split(idx); let (rest, _seen) = input.take_split(idx);
return Ok((rest, ())); return Ok((rest, ()));
} }
} }
@ -304,7 +302,8 @@ fn formal_param(input: Span) -> ParseResult<FormalParam> {
} }
fn type_decl(input: Span) -> ParseResult<Declaration> { fn type_decl(input: Span) -> ParseResult<Declaration> {
context("type-decl", context(
"type-decl",
alt(( alt((
map( map(
tuple((kw("type"), kw("alias"), identifier, tok(char('=')), identifier)), tuple((kw("type"), kw("alias"), identifier, tok(char('=')), identifier)),
@ -315,38 +314,52 @@ fn type_decl(input: Span) -> ParseResult<Declaration> {
), ),
map( map(
tuple((kw("type"), opt(kw("mut")), type_singleton_name, tok(char('=')), type_body)), tuple((kw("type"), opt(kw("mut")), type_singleton_name, tok(char('=')), type_body)),
|(_, mutable, name, _, body)| Declaration::TypeDecl { name, body, mutable: mutable.is_some() }, |(_, mutable, name, _, body)| Declaration::TypeDecl {
name,
body,
mutable: mutable.is_some(),
},
), ),
)))(input) )),
)(input)
} }
fn type_body(input: Span) -> ParseResult<TypeBody> { fn type_body(input: Span) -> ParseResult<TypeBody> {
let id = fresh_id(&input); let id = fresh_id(&input);
context("type-body", context(
"type-body",
alt(( alt((
map( map(
delimited(tok(char('{')), separated_list1(tok(char(',')), record_variant_item), tok(char('}'))), delimited(
tok(char('{')),
separated_list1(tok(char(',')), record_variant_item),
tok(char('}')),
),
move |items| TypeBody::ImmediateRecord { id, fields: items }, move |items| TypeBody::ImmediateRecord { id, fields: items },
), ),
map(separated_list0(tok(char('|')), variant_spec), TypeBody::Variants), map(separated_list0(tok(char('|')), variant_spec), TypeBody::Variants),
)))(input) )),
)(input)
} }
fn record_variant(input: Span) -> ParseResult<VariantKind> { fn record_variant(input: Span) -> ParseResult<VariantKind> {
context("record-variant", context(
"record-variant",
map( map(
delimited( delimited(
pair(tok(char('{')), many0(statement_delimiter)), pair(tok(char('{')), many0(statement_delimiter)),
terminated(separated_list1(pair(tok(char(',')), many0(statement_delimiter)), record_variant_item), terminated(
opt(tok(char(',')))), separated_list1(pair(tok(char(',')), many0(statement_delimiter)), record_variant_item),
pair(many0(statement_delimiter), tok(char('}'))) opt(tok(char(','))),
),
pair(many0(statement_delimiter), tok(char('}'))),
), ),
VariantKind::Record, VariantKind::Record,
))(input) ),
)(input)
} }
fn variant_spec(input: Span) -> ParseResult<Variant> { fn variant_spec(input: Span) -> ParseResult<Variant> {
fn tuple_variant(input: Span) -> ParseResult<VariantKind> { fn tuple_variant(input: Span) -> ParseResult<VariantKind> {
map( map(
delimited(tok(char('(')), separated_list1(tok(char(',')), type_identifier), tok(char(')'))), delimited(tok(char('(')), separated_list1(tok(char(',')), type_identifier), tok(char(')'))),
@ -365,10 +378,12 @@ fn variant_spec(input: Span) -> ParseResult<Variant> {
} }
fn record_variant_item(input: Span) -> ParseResult<(Rc<String>, TypeIdentifier)> { fn record_variant_item(input: Span) -> ParseResult<(Rc<String>, TypeIdentifier)> {
context("record-variant-item", context(
"record-variant-item",
map(tuple((identifier, tok(char(':')), type_identifier)), |(name, _, ty)| { map(tuple((identifier, tok(char(':')), type_identifier)), |(name, _, ty)| {
(rc_string(name.fragment()), ty) (rc_string(name.fragment()), ty)
}))(input) }),
)(input)
} }
fn binding(input: Span) -> ParseResult<Declaration> { fn binding(input: Span) -> ParseResult<Declaration> {
@ -622,7 +637,6 @@ fn lambda_expr(input: Span) -> ParseResult<ExpressionKind> {
} }
fn while_expr(input: Span) -> ParseResult<ExpressionKind> { fn while_expr(input: Span) -> ParseResult<ExpressionKind> {
let id = fresh_id(&input);
map(preceded(kw("while"), pair(opt(expression_no_struct), block)), move |(condition, body)| { map(preceded(kw("while"), pair(opt(expression_no_struct), block)), move |(condition, body)| {
ExpressionKind::WhileExpression { condition: condition.map(Box::new), body } ExpressionKind::WhileExpression { condition: condition.map(Box::new), body }
})(input) })(input)
@ -818,8 +832,7 @@ fn qualified_identifier(input: Span) -> ParseResult<QualifiedName> {
} }
fn identifier(input: Span) -> ParseResult<Span> { fn identifier(input: Span) -> ParseResult<Span> {
context("identifier", context("identifier", tok(identifier_span))(input)
tok(identifier_span))(input)
} }
fn identifier_span(input: Span) -> ParseResult<Span> { fn identifier_span(input: Span) -> ParseResult<Span> {
@ -973,6 +986,8 @@ impl BinopSequence {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::cell::RefCell;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use super::*; use super::*;

View File

@ -71,7 +71,7 @@ impl Parser {
} }
fn convert<'a, O>(input: &'a str, result: combinator::ParseResult<'a, O>) -> Result<O, ParseError> { fn convert<'a, O>(input: &'a str, result: combinator::ParseResult<'a, O>) -> Result<O, ParseError> {
use nom::{error::VerboseError, Err, Finish}; use nom::{error::VerboseError, Finish};
match result.finish() { match result.finish() {
Ok((rest, output)) => { Ok((rest, output)) => {
@ -94,24 +94,6 @@ fn convert<'a, O>(input: &'a str, result: combinator::ParseResult<'a, O>) -> Res
} }
} }
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 /// Represents a parsing error
#[derive(Debug)] #[derive(Debug)]
pub struct ParseError { pub struct ParseError {

View File

@ -95,7 +95,7 @@ macro_rules! assert_ast {
Err(err) => { Err(err) => {
println!("Parse error: {}", err.msg); println!("Parse error: {}", err.msg);
panic!(); panic!();
}, }
Ok(ast) => ast, Ok(ast) => ast,
}; };
assert_eq!(ast, ast2.unwrap()); assert_eq!(ast, ast2.unwrap());
@ -106,7 +106,7 @@ macro_rules! assert_ast {
macro_rules! assert_fail { macro_rules! assert_fail {
($input:expr) => { ($input:expr) => {
let mut parser = Parser::new(); let mut parser = Parser::new();
let err = parser.parse_comb($input).unwrap_err(); let _err = parser.parse_comb($input).unwrap_err();
}; };
($input:expr, $failure:expr) => { ($input:expr, $failure:expr) => {
let mut parser = Parser::new(); let mut parser = Parser::new();
@ -125,7 +125,7 @@ macro_rules! assert_expr {
Err(err) => { Err(err) => {
println!("Expression parse error: {}", err.msg); println!("Expression parse error: {}", err.msg);
panic!(); panic!();
}, }
Ok(expr) => expr, Ok(expr) => expr,
}; };
assert_eq!(expr, expr2.unwrap()); assert_eq!(expr, expr2.unwrap());
@ -1396,20 +1396,20 @@ fn comments() {
use ExpressionKind::*; use ExpressionKind::*;
let source = "1 + /* hella /* bro */ */ 2"; let source = "1 + /* hella /* bro */ */ 2";
//assert_expr!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2)))); assert_expr!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2))));
//TODO make sure this error message makes sense //TODO make sure this error message makes sense
let source = "1 + /* hella /* bro */ 2"; let source = "1 + /* hella /* bro */ 2";
//assert_fail_expr!(source, "foo"); assert_fail_expr!(source, "foo");
let source = "1 + /* hella */ bro */ 2"; let source = "1 + /* hella */ bro */ 2";
assert_fail_expr!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2)))); assert_fail_expr!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2))));
let source = "5//no man\n"; let source = "5//no man\n";
//assert_ast!(source, vec![exst(NatLiteral(5))]); assert_ast!(source, vec![exst(NatLiteral(5))]);
let source = " /*yolo*/ barnaby"; let source = " /*yolo*/ barnaby";
//assert_ast!(source, exst(ExpressionKind::Value(qn!(barnaby)))); assert_ast!(source, exst(ExpressionKind::Value(qn!(barnaby))));
} }
//TODO support backtick operators like this //TODO support backtick operators like this

View File

@ -95,8 +95,9 @@ trad()"#,
"30", "30",
); );
//TODO this shouldn't depend on details of id assignment
let err = let err =
"No symbol found for name: QualifiedName { id: Id { idx: 25, t: PhantomData }, components: [\"a\"] }"; "No symbol found for name: QualifiedName { id: Id { idx: 22, t: PhantomData }, components: [\"a\"] }";
eval_assert_failure( eval_assert_failure(
r#" r#"