From 8c6feb7e805d84fd3107dab126f9a57f4c73ed7c Mon Sep 17 00:00:00 2001 From: JT Date: Fri, 23 Jul 2021 08:45:23 +1200 Subject: [PATCH] Fix up global span logic --- src/main.rs | 5 ++++- src/parser.rs | 34 ++++++++++++++++++---------------- src/parser_state.rs | 4 ++++ src/span.rs | 7 +++++++ 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2193b3afda..b227107dee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,7 +110,10 @@ fn main() -> std::io::Result<()> { use reedline::{DefaultPrompt, FileBackedHistory, Reedline, Signal}; let mut line_editor = Reedline::new() - .with_history(Box::new(FileBackedHistory::new(1000)))? + .with_history(Box::new(FileBackedHistory::with_file( + 1000, + "history.txt".into(), + )?))? .with_highlighter(Box::new(NuHighlighter { parser_state: parser_state.clone(), })); diff --git a/src/parser.rs b/src/parser.rs index acc75f796a..947eda8dd8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -994,7 +994,7 @@ impl<'a> ParserWorkingSet<'a> { } let span = Span { start, end }; - let source = &self.delta.file_contents[..span.end]; + let source = self.get_span_contents(span); let (output, err) = lex(&source, span.start, &[b'\n', b','], &[b':']); error = error.or(err); @@ -1008,7 +1008,8 @@ impl<'a> ParserWorkingSet<'a> { contents: crate::TokenContents::Item, span, } => { - let contents = &self.delta.file_contents[span.start..span.end]; + let span = *span; + let contents = self.get_span_contents(span); if contents == b":" { match parse_mode { @@ -1017,7 +1018,7 @@ impl<'a> ParserWorkingSet<'a> { } ParseMode::TypeMode => { // We're seeing two types for the same thing for some reason, error - error = error.or(Some(ParseError::Mismatch("type".into(), *span))); + error = error.or(Some(ParseError::Mismatch("type".into(), span))); } } } else { @@ -1042,7 +1043,7 @@ impl<'a> ParserWorkingSet<'a> { { error = error.or(Some(ParseError::Mismatch( "short flag".into(), - *span, + span, ))); short_flag } else { @@ -1064,7 +1065,7 @@ impl<'a> ParserWorkingSet<'a> { } else { error = error.or(Some(ParseError::Mismatch( "short flag".into(), - *span, + span, ))); } } @@ -1079,7 +1080,7 @@ impl<'a> ParserWorkingSet<'a> { if chars.len() > 1 { error = error.or(Some(ParseError::Mismatch( "short flag".into(), - *span, + span, ))); args.push(Arg::Flag(Flag { @@ -1104,7 +1105,7 @@ impl<'a> ParserWorkingSet<'a> { let short_flag = if !short_flag.ends_with(b")") { error = error.or(Some(ParseError::Mismatch( "short flag".into(), - *span, + span, ))); short_flag } else { @@ -1121,7 +1122,7 @@ impl<'a> ParserWorkingSet<'a> { if flag.short.is_some() { error = error.or(Some(ParseError::Mismatch( "one short flag".into(), - *span, + span, ))); } else { flag.short = Some(chars[0]); @@ -1130,14 +1131,14 @@ impl<'a> ParserWorkingSet<'a> { _ => { error = error.or(Some(ParseError::Mismatch( "unknown flag".into(), - *span, + span, ))); } } } else { error = error.or(Some(ParseError::Mismatch( "short flag".into(), - *span, + span, ))); } } else { @@ -1186,7 +1187,10 @@ impl<'a> ParserWorkingSet<'a> { contents: crate::TokenContents::Comment, span, } => { - let contents = &self.delta.file_contents[span.start + 1..span.end]; + let contents = self.get_span_contents(Span { + start: span.start + 1, + end: span.end, + }); let mut contents = String::from_utf8_lossy(contents).to_string(); contents = contents.trim().into(); @@ -1276,7 +1280,7 @@ impl<'a> ParserWorkingSet<'a> { } let span = Span { start, end }; - let source = &self.delta.file_contents[..span.end]; + let source = self.get_span_contents(span); let (output, err) = lex(&source, span.start, &[b'\n', b','], &[]); error = error.or(err); @@ -1340,7 +1344,7 @@ impl<'a> ParserWorkingSet<'a> { let span = Span { start, end }; - let source = &self.delta.file_contents[..end]; + let source = self.get_span_contents(span); let (output, err) = lex(&source, start, &[b'\n', b','], &[]); error = error.or(err); @@ -1430,7 +1434,7 @@ impl<'a> ParserWorkingSet<'a> { let span = Span { start, end }; - let source = &self.delta.file_contents[..end]; + let source = self.get_span_contents(span); let (output, err) = lex(&source, start, &[], &[]); error = error.or(err); @@ -1441,8 +1445,6 @@ impl<'a> ParserWorkingSet<'a> { let (output, err) = self.parse_block(&output, true); error = error.or(err); - println!("{:?} {:?}", output, error); - let block_id = self.add_block(output); ( diff --git a/src/parser_state.rs b/src/parser_state.rs index de618e9bf0..17cc03c9c0 100644 --- a/src/parser_state.rs +++ b/src/parser_state.rs @@ -210,6 +210,10 @@ impl<'a> ParserWorkingSet<'a> { self.permanent_state.next_span_start() + self.delta.file_contents.len() } + pub fn global_span_offset(&self) -> usize { + self.permanent_state.next_span_start() + } + pub fn add_file(&mut self, filename: String, contents: &[u8]) -> usize { let next_span_start = self.next_span_start(); diff --git a/src/span.rs b/src/span.rs index 4d436245d0..0777afef69 100644 --- a/src/span.rs +++ b/src/span.rs @@ -12,4 +12,11 @@ impl Span { pub fn unknown() -> Span { Span { start: 0, end: 0 } } + + pub fn offset(&self, offset: usize) -> Span { + Span { + start: self.start - offset, + end: self.end - offset, + } + } }