mirror of
https://github.com/nushell/nushell.git
synced 2025-04-21 11:48:28 +02:00
This commit contains two improvements: - Support for a Range syntax (and a corresponding Range value) - Work towards a signature syntax Implementing the Range syntax resulted in cleaning up how operators in the core syntax works. There are now two kinds of infix operators - tight operators (`.` and `..`) - loose operators Tight operators may not be interspersed (`$it.left..$it.right` is a syntax error). Loose operators require whitespace on both sides of the operator, and can be arbitrarily interspersed. Precedence is left to right in the core syntax. Note that delimited syntax (like `( ... )` or `[ ... ]`) is a single token node in the core syntax. A single token node can be parsed from beginning to end in a context-free manner. The rule for `.` is `<token node>.<member>`. The rule for `..` is `<token node>..<token node>`. Loose operators all have the same syntactic rule: `<token node><space><loose op><space><token node>`. The second aspect of this pull request is the beginning of support for a signature syntax. Before implementing signatures, a necessary prerequisite is for the core syntax to support multi-line programs. That work establishes a few things: - `;` and newlines are handled in the core grammar, and both count as "separators" - line comments begin with `#` and continue until the end of the line In this commit, multi-token productions in the core grammar can use separators interchangably with spaces. However, I think we will ultimately want a different rule preventing separators from occurring before an infix operator, so that the end of a line is always unambiguous. This would avoid gratuitous differences between modules and repl usage. We already effectively have this rule, because otherwise `x<newline> | y` would be a single pipeline, but of course that wouldn't work.
154 lines
4.6 KiB
Rust
154 lines
4.6 KiB
Rust
use derive_new::new;
|
|
use language_reporting::{FileName, Location};
|
|
use log::trace;
|
|
use nu_source::Span;
|
|
|
|
#[derive(new, Debug, Clone)]
|
|
pub struct Files {
|
|
snippet: String,
|
|
}
|
|
|
|
impl language_reporting::ReportingFiles for Files {
|
|
type Span = Span;
|
|
type FileId = usize;
|
|
|
|
fn byte_span(
|
|
&self,
|
|
_file: Self::FileId,
|
|
from_index: usize,
|
|
to_index: usize,
|
|
) -> Option<Self::Span> {
|
|
Some(Span::new(from_index, to_index))
|
|
}
|
|
|
|
fn file_id(&self, _tag: Self::Span) -> Self::FileId {
|
|
0
|
|
}
|
|
|
|
fn file_name(&self, _file: Self::FileId) -> FileName {
|
|
FileName::Verbatim(format!("shell"))
|
|
}
|
|
|
|
fn byte_index(&self, _file: Self::FileId, _line: usize, _column: usize) -> Option<usize> {
|
|
unimplemented!("byte_index")
|
|
}
|
|
|
|
fn location(&self, _file: Self::FileId, byte_index: usize) -> Option<Location> {
|
|
trace!("finding location for {}", byte_index);
|
|
|
|
let source = &self.snippet;
|
|
let mut seen_lines = 0;
|
|
let mut seen_bytes = 0;
|
|
|
|
for (pos, slice) in source.match_indices('\n') {
|
|
trace!(
|
|
"searching byte_index={} seen_bytes={} pos={} slice={:?} slice.len={} source={:?}",
|
|
byte_index,
|
|
seen_bytes,
|
|
pos,
|
|
slice,
|
|
source.len(),
|
|
source
|
|
);
|
|
|
|
if pos >= byte_index {
|
|
trace!(
|
|
"returning {}:{} seen_lines={} byte_index={} pos={} seen_bytes={}",
|
|
seen_lines,
|
|
byte_index,
|
|
pos,
|
|
seen_lines,
|
|
byte_index,
|
|
seen_bytes
|
|
);
|
|
|
|
return Some(language_reporting::Location::new(
|
|
seen_lines,
|
|
byte_index - pos,
|
|
));
|
|
} else {
|
|
seen_lines += 1;
|
|
seen_bytes = pos;
|
|
}
|
|
}
|
|
|
|
if seen_lines == 0 {
|
|
trace!("seen_lines=0 end={}", source.len() - 1);
|
|
|
|
// if we got here, there were no newlines in the source
|
|
Some(language_reporting::Location::new(0, source.len() - 1))
|
|
} else {
|
|
trace!(
|
|
"last line seen_lines={} end={}",
|
|
seen_lines,
|
|
source.len() - 1 - byte_index
|
|
);
|
|
|
|
// if we got here and we didn't return, it should mean that we're talking about
|
|
// the last line
|
|
Some(language_reporting::Location::new(
|
|
seen_lines,
|
|
source.len() - 1 - byte_index,
|
|
))
|
|
}
|
|
}
|
|
|
|
fn line_span(&self, _file: Self::FileId, lineno: usize) -> Option<Self::Span> {
|
|
trace!("finding line_span for {}", lineno);
|
|
|
|
let source = &self.snippet;
|
|
let mut seen_lines = 0;
|
|
let mut seen_bytes = 0;
|
|
|
|
for (pos, _) in source.match_indices('\n') {
|
|
trace!(
|
|
"lineno={} seen_lines={} seen_bytes={} pos={}",
|
|
lineno,
|
|
seen_lines,
|
|
seen_bytes,
|
|
pos
|
|
);
|
|
|
|
if seen_lines == lineno {
|
|
trace!("returning start={} end={}", seen_bytes, pos);
|
|
// If the number of seen lines is the lineno, seen_bytes is the start of the
|
|
// line and pos is the end of the line
|
|
return Some(Span::new(seen_bytes, pos));
|
|
} else {
|
|
// If it's not, increment seen_lines, and move seen_bytes to the beginning of
|
|
// the next line
|
|
seen_lines += 1;
|
|
seen_bytes = pos + 1;
|
|
}
|
|
}
|
|
|
|
if seen_lines == 0 {
|
|
trace!("returning start={} end={}", 0, self.snippet.len() - 1);
|
|
|
|
// if we got here, there were no newlines in the source
|
|
Some(Span::new(0, self.snippet.len() - 1))
|
|
} else {
|
|
trace!(
|
|
"returning start={} end={}",
|
|
seen_bytes,
|
|
self.snippet.len() - 1
|
|
);
|
|
|
|
// if we got here and we didn't return, it should mean that we're talking about
|
|
// the last line
|
|
Some(Span::new(seen_bytes, self.snippet.len() - 1))
|
|
}
|
|
}
|
|
|
|
fn source(&self, span: Self::Span) -> Option<String> {
|
|
trace!("source(tag={:?}) snippet={:?}", span, self.snippet);
|
|
|
|
if span.start() > span.end() {
|
|
return None;
|
|
} else if span.end() > self.snippet.len() {
|
|
return None;
|
|
}
|
|
Some(span.slice(&self.snippet).to_string())
|
|
}
|
|
}
|