From d5bc8da87f3fed99a010d867a3e34d2768caf768 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 27 Feb 2019 15:35:04 +0100 Subject: Try to update to latest nom --- Cargo.lock | 34 ++++++++++++++++----------------- Cargo.toml | 2 +- src/grammar.lalrpop | 2 +- src/lexer.rs | 54 ++++++++++++++++++++++++++--------------------------- src/main.rs | 2 +- src/parser.rs | 2 +- 6 files changed, 46 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9ff727a..1b26f61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,7 +101,7 @@ dependencies = [ "bytecount 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "lalrpop 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)", "lalrpop-util 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)", - "nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 4.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "term-painter 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -123,7 +123,7 @@ name = "docopt" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", @@ -214,7 +214,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "lazy_static" -version = "1.3.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -230,14 +230,6 @@ dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "memchr" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "memchr" version = "2.2.0" @@ -250,10 +242,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "nom" -version = "3.2.1" +version = "4.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -486,7 +479,7 @@ name = "string_cache" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "new_debug_unreachable 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", "precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -559,7 +552,7 @@ name = "thread_local" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -582,6 +575,11 @@ name = "utf8-ranges" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "version_check" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.2.8" @@ -639,13 +637,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lalrpop 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4e2e80bee40b22bca46665b4ef1f3cd88ed0fb043c971407eac17a0712c02572" "checksum lalrpop-util 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)" = "33b27d8490dbe1f9704b0088d61e8d46edc10d5673a8829836c6ded26a9912c7" -"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" +"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)" = "413f3dfc802c5dc91dc570b05125b6cda9855edfaa9825c9849807876376e70e" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" -"checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a" "checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39" "checksum new_debug_unreachable 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f40f005c60db6e03bae699e414c58bf9aa7ea02a2d0b9bfbcf19286cc4c82b30" -"checksum nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b" +"checksum nom 4.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4836e9d6036552017e107edc598c97b2dee245161ff1b1ad4af215004774b354" "checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409" "checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063" "checksum petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3659d1ee90221741f65dd128d9998311b0e40c5d3c23a62445938214abce4f" @@ -686,6 +683,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" +"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index f781e03..e6fb36c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,5 @@ lalrpop = "0.16.3" [dependencies] bytecount = "0.5.1" lalrpop-util = "0.16.3" -nom = "3.0.0" +nom = "4.0.0" term-painter = "0.2.3" diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop index 35716fc..1e4d323 100644 --- a/src/grammar.lalrpop +++ b/src/grammar.lalrpop @@ -13,7 +13,7 @@ grammar<'input>; extern { type Location = usize; - type Error = LexicalError; + type Error = LexicalError<'input>; enum Tok<'input> { Pi => Tok::Pi, diff --git a/src/lexer.rs b/src/lexer.rs index 0b58293..8617f4b 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -64,10 +64,7 @@ pub enum Tok<'i> { } #[derive(Debug)] -pub enum LexicalError { - Error(usize, nom::simple_errors::Err), - Incomplete(nom::Needed), -} +pub struct LexicalError<'a>(pub usize, pub nom::Err<&'a str>); pub type Spanned = Result<(Loc, Tok, Loc), Error>; @@ -109,11 +106,11 @@ named!(identifier<&str, &str>, recognize!(preceded!( macro_rules! ident_tag { ($i:expr, $tag:expr) => { match identifier($i) { - nom::IResult::Done(i, s) => { + Ok((i, s)) => { if s == $tag { - nom::IResult::Done(i, s) + Ok((i, s)) } else { - nom::IResult::Error(error_position!(nom::ErrorKind::Tag, $i)) + Err(nom::Err::Error(error_position!($i, nom::ErrorKind::Tag))) } } r => r, @@ -146,20 +143,20 @@ named!(string_escape_numeric<&str, char>, map_opt!(alt!( ), ::std::char::from_u32)); fn string_lit_inner(input: &str) -> nom::IResult<&str, String> { - use nom::IResult::*;; + use nom::Err; use nom::ErrorKind; let mut s = String::new(); let mut cs = input.char_indices().peekable(); while let Some((i, c)) = cs.next() { match c { - '"' => return nom::IResult::Done(&input[i..], s), + '"' => return Ok((&input[i..], s)), '\\' => match cs.next() { Some((_, s)) if s.is_whitespace() => { while cs.peek().map(|&(_, s)| s.is_whitespace()) == Some(true) { let _ = cs.next(); } if cs.next().map(|p| p.1) != Some('\\') { - return Error(error_position!(ErrorKind::Custom(4 /* FIXME */), input)); + return Err(Err::Error(error_position!(input, ErrorKind::Custom(4 /* FIXME */)))); } } Some((j, ec)) => { @@ -168,24 +165,23 @@ fn string_lit_inner(input: &str) -> nom::IResult<&str, String> { // FIXME Named ASCII escapes and control character escapes } else { match string_escape_numeric(&input[j..]) { - Done(rest, esc) => { + Ok((rest, esc)) => { let &(k, _) = cs.peek().unwrap(); // digits are always single byte ASCII characters let consumed = input[k..].len() - rest.len(); for _ in 0..consumed { let _ = cs.next(); } s.push(esc); } - Incomplete(s) => return Incomplete(s), - Error(e) => return Error(e), + Err(e) => return Err(e), } } }, - _ => return Error(error_position!(ErrorKind::Custom(5 /* FIXME */), input)), + _ => return Err(Err::Error(error_position!(input, ErrorKind::Custom(5 /* FIXME */)))), }, _ => s.push(c), } } - Error(error_position!(ErrorKind::Custom(3 /* FIXME */), input)) + Err(Err::Error(error_position!(input, ErrorKind::Custom(3 /* FIXME */)))) } named!(string_lit<&str, String>, delimited!(tag!("\""), string_lit_inner, tag!("\""))); @@ -322,30 +318,26 @@ impl<'input> Lexer<'input> { } impl<'input> Iterator for Lexer<'input> { - type Item = Spanned, usize, LexicalError>; + type Item = Spanned, usize, LexicalError<'input>>; fn next(&mut self) -> Option { - use nom::IResult::*; self.skip_comments_and_whitespace(); let input = self.current_input(); if input.is_empty() { return None; } match token(input) { - Done(rest, t) => { + Ok((rest, t)) => { let parsed_len = input.len() - rest.len(); //println!("parsed {} bytes => {:?}", parsed_len, t); let start = self.offset; self.offset += parsed_len; Some(Ok((start, t, self.offset))) } - Error(e) => { + Err(e) => { let offset = self.offset; - self.offset = self.input.len(); - Some(Err(LexicalError::Error(offset, e))) - } - Incomplete(needed) => { - Some(Err(LexicalError::Incomplete(needed))) + // self.offset = self.input.len(); + Some(Err(LexicalError(offset, e))) } } } @@ -354,6 +346,12 @@ impl<'input> Iterator for Lexer<'input> { #[test] fn test_lex() { use self::Tok::*; + let s = "22"; + let expected = [Lambda, Bool(false)]; + let lexer = Lexer::new(s); + let tokens = lexer.map(|r| r.unwrap().1).collect::>(); + assert_eq!(&tokens, &expected); + let s = "λ(b : Bool) → b == False"; let expected = [Lambda, ParenL, @@ -369,9 +367,9 @@ fn test_lex() { let tokens = lexer.map(|r| r.unwrap().1).collect::>(); assert_eq!(&tokens, &expected); - assert_eq!(string_lit(r#""a\&b""#).to_result(), Ok("ab".to_owned())); - assert_eq!(string_lit(r#""a\ \b""#).to_result(), Ok("ab".to_owned())); + assert_eq!(string_lit(r#""a\&b""#), Ok(("", "ab".to_owned()))); + assert_eq!(string_lit(r#""a\ \b""#), Ok(("", "ab".to_owned()))); assert!(string_lit(r#""a\ b""#).is_err()); - assert_eq!(string_lit(r#""a\nb""#).to_result(), Ok("a\nb".to_owned())); - assert_eq!(string_lit(r#""\o141\x62\99""#).to_result(), Ok("abc".to_owned())); + assert_eq!(string_lit(r#""a\nb""#), Ok(("", "a\nb".to_owned()))); + assert_eq!(string_lit(r#""\o141\x62\99""#), Ok(("", "abd".to_owned()))); } diff --git a/src/main.rs b/src/main.rs index fde3978..2a1eb11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,7 +71,7 @@ fn main() { io::stdin().read_to_string(&mut buffer).unwrap(); let expr = match parser::parse_expr(&buffer) { Ok(e) => e, - Err(lalrpop_util::ParseError::User { error: lexer::LexicalError::Error(pos, e) }) => { + Err(lalrpop_util::ParseError::User { error: lexer::LexicalError(pos, e) }) => { print_error(&format!("Unexpected token {:?}", e), &buffer, pos, pos); return; } diff --git a/src/parser.rs b/src/parser.rs index 3ad7ffc..1127d86 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,7 +4,7 @@ use grammar; use grammar_util::BoxExpr; use lexer::{Lexer, LexicalError, Tok}; -pub type ParseError<'i> = lalrpop_util::ParseError, LexicalError>; +pub type ParseError<'i> = lalrpop_util::ParseError, LexicalError<'i>>; pub fn parse_expr(s: &str) -> Result { grammar::ExprParser::new().parse(Lexer::new(s)) -- cgit v1.2.3