Fix up global span logic

This commit is contained in:
JT 2021-07-23 08:45:23 +12:00
parent 37f8ff0efc
commit 8c6feb7e80
4 changed files with 33 additions and 17 deletions

View file

@ -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(),
}));

View file

@ -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);
(

View file

@ -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();

View file

@ -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,
}
}
}