From bba433c8086444ce2c5e7fc4ec168e9bcc646cba Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 20 Nov 2021 22:55:11 -0800 Subject: [PATCH] Cleanup warnings --- schala-lang/src/parsing/combinator.rs | 111 ++++++++++++++----------- schala-lang/src/parsing/mod.rs | 20 +---- schala-lang/src/parsing/test.rs | 14 ++-- schala-lang/src/tree_walk_eval/test.rs | 3 +- 4 files changed, 73 insertions(+), 75 deletions(-) diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 484ca2f..57d4c8c 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -1,19 +1,17 @@ -use std::{cell::RefCell, rc::Rc}; +use std::rc::Rc; use nom::{ branch::alt, - bytes::complete::{escaped_transform, tag, take_till, take_while}, + bytes::complete::{escaped_transform, tag, take_while}, character::{ - complete::{ - alpha1, alphanumeric0, anychar, char, line_ending, none_of, not_line_ending, one_of, space1, - }, + complete::{alpha1, char, line_ending, none_of, not_line_ending, one_of, space1}, is_alphanumeric, }, combinator::{cut, eof, map, not, opt, peek, recognize, value, verify}, error::{context, ErrorKind, ParseError, VerboseError}, multi::{many0, many1, separated_list0, separated_list1}, 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}; @@ -102,12 +100,12 @@ fn block_comment(input: Span) -> ParseResult<()> { fn inner_parser(mut input: Span) -> ParseResult<()> { loop { 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() { return Err(nom::Err::Failure(VerboseError::from_error_kind(input, ErrorKind::Verify))); } 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)?; input = rest; @@ -115,7 +113,7 @@ fn block_comment(input: Span) -> ParseResult<()> { } if input.slice(idx..idx + 2).fragment() == &"*/" { - let (rest, seen) = input.take_split(idx); + let (rest, _seen) = input.take_split(idx); return Ok((rest, ())); } } @@ -304,49 +302,64 @@ fn formal_param(input: Span) -> ParseResult { } fn type_decl(input: Span) -> ParseResult { - context("type-decl", - alt(( - map( - tuple((kw("type"), kw("alias"), identifier, tok(char('=')), identifier)), - |(_, _, alias, _, name)| Declaration::TypeAlias { - alias: rc_string(alias.fragment()), - original: rc_string(name.fragment()), - }, - ), - map( - tuple((kw("type"), opt(kw("mut")), type_singleton_name, tok(char('=')), type_body)), - |(_, mutable, name, _, body)| Declaration::TypeDecl { name, body, mutable: mutable.is_some() }, - ), - )))(input) + context( + "type-decl", + alt(( + map( + tuple((kw("type"), kw("alias"), identifier, tok(char('=')), identifier)), + |(_, _, alias, _, name)| Declaration::TypeAlias { + alias: rc_string(alias.fragment()), + original: rc_string(name.fragment()), + }, + ), + map( + tuple((kw("type"), opt(kw("mut")), type_singleton_name, tok(char('=')), type_body)), + |(_, mutable, name, _, body)| Declaration::TypeDecl { + name, + body, + mutable: mutable.is_some(), + }, + ), + )), + )(input) } fn type_body(input: Span) -> ParseResult { let id = fresh_id(&input); - context("type-body", - alt(( - map( - delimited(tok(char('{')), separated_list1(tok(char(',')), record_variant_item), tok(char('}'))), - move |items| TypeBody::ImmediateRecord { id, fields: items }, - ), - map(separated_list0(tok(char('|')), variant_spec), TypeBody::Variants), - )))(input) + context( + "type-body", + alt(( + map( + delimited( + tok(char('{')), + separated_list1(tok(char(',')), record_variant_item), + tok(char('}')), + ), + move |items| TypeBody::ImmediateRecord { id, fields: items }, + ), + map(separated_list0(tok(char('|')), variant_spec), TypeBody::Variants), + )), + )(input) } fn record_variant(input: Span) -> ParseResult { - context("record-variant", - map( - delimited( - pair(tok(char('{')), many0(statement_delimiter)), - terminated(separated_list1(pair(tok(char(',')), many0(statement_delimiter)), record_variant_item), - opt(tok(char(',')))), - pair(many0(statement_delimiter), tok(char('}'))) + context( + "record-variant", + map( + delimited( + pair(tok(char('{')), many0(statement_delimiter)), + terminated( + separated_list1(pair(tok(char(',')), many0(statement_delimiter)), record_variant_item), + opt(tok(char(','))), + ), + pair(many0(statement_delimiter), tok(char('}'))), + ), + VariantKind::Record, ), - VariantKind::Record, - ))(input) + )(input) } fn variant_spec(input: Span) -> ParseResult { - fn tuple_variant(input: Span) -> ParseResult { map( delimited(tok(char('(')), separated_list1(tok(char(',')), type_identifier), tok(char(')'))), @@ -365,10 +378,12 @@ fn variant_spec(input: Span) -> ParseResult { } fn record_variant_item(input: Span) -> ParseResult<(Rc, TypeIdentifier)> { - context("record-variant-item", - map(tuple((identifier, tok(char(':')), type_identifier)), |(name, _, ty)| { - (rc_string(name.fragment()), ty) - }))(input) + context( + "record-variant-item", + map(tuple((identifier, tok(char(':')), type_identifier)), |(name, _, ty)| { + (rc_string(name.fragment()), ty) + }), + )(input) } fn binding(input: Span) -> ParseResult { @@ -622,7 +637,6 @@ fn lambda_expr(input: Span) -> ParseResult { } fn while_expr(input: Span) -> ParseResult { - let id = fresh_id(&input); map(preceded(kw("while"), pair(opt(expression_no_struct), block)), move |(condition, body)| { ExpressionKind::WhileExpression { condition: condition.map(Box::new), body } })(input) @@ -818,8 +832,7 @@ fn qualified_identifier(input: Span) -> ParseResult { } fn identifier(input: Span) -> ParseResult { - context("identifier", - tok(identifier_span))(input) + context("identifier", tok(identifier_span))(input) } fn identifier_span(input: Span) -> ParseResult { @@ -973,6 +986,8 @@ impl BinopSequence { #[cfg(test)] mod test { + use std::cell::RefCell; + use pretty_assertions::assert_eq; use super::*; diff --git a/schala-lang/src/parsing/mod.rs b/schala-lang/src/parsing/mod.rs index 182c81d..0a0255e 100644 --- a/schala-lang/src/parsing/mod.rs +++ b/schala-lang/src/parsing/mod.rs @@ -71,7 +71,7 @@ impl Parser { } fn convert<'a, O>(input: &'a str, result: combinator::ParseResult<'a, O>) -> Result { - use nom::{error::VerboseError, Err, Finish}; + use nom::{error::VerboseError, Finish}; match result.finish() { 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>>, -) -> 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 { diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index a0bc7bc..daaa19a 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -95,7 +95,7 @@ macro_rules! assert_ast { Err(err) => { println!("Parse error: {}", err.msg); panic!(); - }, + } Ok(ast) => ast, }; assert_eq!(ast, ast2.unwrap()); @@ -106,7 +106,7 @@ macro_rules! assert_ast { macro_rules! assert_fail { ($input:expr) => { 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) => { let mut parser = Parser::new(); @@ -125,7 +125,7 @@ macro_rules! assert_expr { Err(err) => { println!("Expression parse error: {}", err.msg); panic!(); - }, + } Ok(expr) => expr, }; assert_eq!(expr, expr2.unwrap()); @@ -1396,20 +1396,20 @@ fn comments() { use ExpressionKind::*; 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 let source = "1 + /* hella /* bro */ 2"; - //assert_fail_expr!(source, "foo"); + assert_fail_expr!(source, "foo"); let source = "1 + /* hella */ bro */ 2"; assert_fail_expr!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2)))); let source = "5//no man\n"; - //assert_ast!(source, vec![exst(NatLiteral(5))]); + assert_ast!(source, vec![exst(NatLiteral(5))]); 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 diff --git a/schala-lang/src/tree_walk_eval/test.rs b/schala-lang/src/tree_walk_eval/test.rs index 47ce87f..954691c 100644 --- a/schala-lang/src/tree_walk_eval/test.rs +++ b/schala-lang/src/tree_walk_eval/test.rs @@ -95,8 +95,9 @@ trad()"#, "30", ); + //TODO this shouldn't depend on details of id assignment 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( r#"