Skip to content

Add line/column info to nimbleparses printing of HeaderError. #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cfgrammar/src/lib/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
yacc::{
parser::SpansKind, YaccGrammarError, YaccGrammarErrorKind, YaccKind, YaccOriginalActionKind,
},
Location, Span,
Location, Span, Spanned,
};
use lazy_static::lazy_static;
use regex::{Regex, RegexBuilder};
Expand Down Expand Up @@ -38,6 +38,15 @@ impl From<HeaderError<Span>> for YaccGrammarError {
}
}

impl Spanned for HeaderError<Span> {
fn spans(&self) -> &[Span] {
self.locations.as_slice()
}
fn spanskind(&self) -> SpansKind {
self.spanskind()
}
}

// This is essentially a tuple that needs a newtype so we can implement `From` for it.
// Thus we aren't worried about it being `pub`.
#[derive(Debug, PartialEq)]
Expand Down
56 changes: 32 additions & 24 deletions nimbleparse/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, fmt::Display, path::Path, str::FromStr};
use std::{cell::OnceCell, error::Error, fmt::Display, path::Path};

use cfgrammar::{
newlinecache::NewlineCache,
Expand All @@ -16,7 +16,7 @@ use unicode_width::UnicodeWidthStr;
pub struct SpannedDiagnosticFormatter<'a> {
src: &'a str,
path: &'a Path,
nlc: NewlineCache,
nlc: OnceCell<NewlineCache>,
}

fn pidx_prods_data<StorageT>(ast: &GrammarAST, pidx: PIdx<StorageT>) -> (Vec<String>, Vec<Span>)
Expand All @@ -36,11 +36,19 @@ where

impl<'a> SpannedDiagnosticFormatter<'a> {
#[allow(clippy::result_unit_err)]
pub fn new(src: &'a str, path: &'a Path) -> Result<Self, ()> {
Ok(Self {
pub fn new(src: &'a str, path: &'a Path) -> Self {
Self {
src,
path,
nlc: NewlineCache::from_str(src)?,
nlc: OnceCell::new(),
}
}

pub fn nlc(&self) -> &NewlineCache {
self.nlc.get_or_init(|| {
let mut nlc = NewlineCache::new();
nlc.feed(self.src);
nlc
})
}

Expand All @@ -60,7 +68,7 @@ impl<'a> SpannedDiagnosticFormatter<'a> {
pub fn file_location_msg(&self, msg: &str, span: Option<Span>) -> String {
if let Some(span) = span {
let (line, col) = self
.nlc
.nlc()
.byte_to_line_num_and_col_num(self.src, span.start())
.unwrap_or((0, 0));
format!("{} at {}:{line}:{col}", msg, self.path.display())
Expand All @@ -85,11 +93,11 @@ impl<'a> SpannedDiagnosticFormatter<'a> {
underline_c: char,
) -> String {
let mut out = String::new();
let (start_byte, end_byte) = self.nlc.span_line_bytes(span);
let (start_byte, end_byte) = self.nlc().span_line_bytes(span);
// Produce an underline underneath a span which may cover multiple lines, and a message on the last line.
let mut source_lines = self.src[start_byte..end_byte].lines().peekable();
while let Some(source_line) = source_lines.next() {
let (line_start_byte, _) = self.nlc.span_line_bytes(span);
let (line_start_byte, _) = self.nlc().span_line_bytes(span);
let span_offset_from_start = span.start() - line_start_byte;

// An underline bounded by the current line.
Expand All @@ -99,7 +107,7 @@ impl<'a> SpannedDiagnosticFormatter<'a> {
.min(span.start() + (source_line.len() - span_offset_from_start)),
);
let (line_num, _) = self
.nlc
.nlc()
.byte_to_line_num_and_col_num(self.src, span.start())
.expect("Span must correlate to a line in source");
// Print the line_num/source text for the line.
Expand Down Expand Up @@ -137,13 +145,13 @@ impl<'a> SpannedDiagnosticFormatter<'a> {
let mut spans = e.spans().iter().enumerate().peekable();
while let Some((span_num, span)) = spans.next() {
let (line, _) = self
.nlc
.nlc()
.byte_to_line_num_and_col_num(self.src, span.start())
.unwrap_or((0, 0));
let next_line = spans
.peek()
.map(|(_, span)| span)
.map(|s| self.nlc.byte_to_line_num(s.start()).unwrap_or(line))
.map(|s| self.nlc().byte_to_line_num(s.start()).unwrap_or(line))
.unwrap_or(line);
// Is this line contiguous with the next, if not prefix it with dots.
let dots = if next_line - line > 1 { "..." } else { "" };
Expand Down Expand Up @@ -180,7 +188,7 @@ impl<'a> SpannedDiagnosticFormatter<'a> {
) -> String {
let mut lines = spans
.clone()
.map(|span| self.nlc.span_line_bytes(span))
.map(|span| self.nlc().span_line_bytes(span))
.collect::<Vec<_>>();
lines.dedup();
assert!(lines.len() == 1);
Expand All @@ -192,10 +200,10 @@ impl<'a> SpannedDiagnosticFormatter<'a> {
// ____ ___
// indent indent
let (line_at_start, _) = self
.nlc
.nlc()
.byte_to_line_num_and_col_num(self.src, first_span.start())
.expect("Span should correlate to a line in source");
let (line_start_byte, end_byte) = self.nlc.span_line_bytes(*first_span);
let (line_start_byte, end_byte) = self.nlc().span_line_bytes(*first_span);
// Print the line_num/source text for the line.
out.push_str(&format!(
"{}| {}\n",
Expand Down Expand Up @@ -304,15 +312,15 @@ impl<'a> SpannedDiagnosticFormatter<'a> {

let mut prod_lines = r_prod_spans
.iter()
.map(|span| self.nlc.span_line_bytes(*span))
.map(|span| self.nlc().span_line_bytes(*span))
.collect::<Vec<_>>();
prod_lines.sort();
prod_lines.dedup();

for lines in &prod_lines {
let mut spans_on_line = Vec::new();
for span in &r_prod_spans {
if lines == &self.nlc.span_line_bytes(*span) {
if lines == &self.nlc().span_line_bytes(*span) {
spans_on_line.push(*span)
}
}
Expand Down Expand Up @@ -375,7 +383,7 @@ mod test {
fn underline_multiline_span_test() {
let s = "\naaaaaabbb\nbbb\nbbbb\n";
let test_path = PathBuf::from("test");
let formatter = SpannedDiagnosticFormatter::new(s, &test_path).unwrap();
let formatter = SpannedDiagnosticFormatter::new(s, &test_path);

let span = Span::new(7, 7 + 12);
let out = format!(
Expand Down Expand Up @@ -413,7 +421,7 @@ mod test {
fn underline_single_line_span_test() {
let s = "\naaaaaabbb bbb bbbb\n";
let test_path = PathBuf::from("test");
let formatter = SpannedDiagnosticFormatter::new(s, &test_path).unwrap();
let formatter = SpannedDiagnosticFormatter::new(s, &test_path);

let span = Span::new(7, 7 + 12);
let out = format!(
Expand Down Expand Up @@ -442,7 +450,7 @@ mod test {
fn span_prefix() {
let s = "\naaaaaabbb\nbbb\nbbbb\n";
let test_path = PathBuf::from("test");
let formatter = SpannedDiagnosticFormatter::new(s, &test_path).unwrap();
let formatter = SpannedDiagnosticFormatter::new(s, &test_path);
// For raw string alignment.
let mut out = String::from("\n");
// On occasion we want dots to signal that the lines are not contiguous.
Expand Down Expand Up @@ -473,7 +481,7 @@ mod test {
fn span_prefix_2() {
let s = "\n\n\n\n\n\n\n\n\n\n\naaaaaabbb\nbbb\nbbbb\n";
let test_path = PathBuf::from("test");
let formatter = SpannedDiagnosticFormatter::new(s, &test_path).unwrap();
let formatter = SpannedDiagnosticFormatter::new(s, &test_path);
let mut out = String::from("\n");
// On occasion we want dots to signal that the lines are not contiguous.
out.push_str(&formatter.prefixed_underline_span_with_text(
Expand Down Expand Up @@ -504,7 +512,7 @@ mod test {
let crabs = " 🦀🦀🦀 ";
let crustaceans = format!("\"{crabs}\n{crabs}\"");
let test_path = PathBuf::from("test");
let formatter = SpannedDiagnosticFormatter::new(&crustaceans, &test_path).unwrap();
let formatter = SpannedDiagnosticFormatter::new(&crustaceans, &test_path);
// For raw string alignment.
let mut out = String::from("\n");
out.push_str(&formatter.underline_span_with_text(
Expand All @@ -528,7 +536,7 @@ mod test {
let lobster = "🦞";
let crustaceans = format!("{crab}{lobster}{crab}{crab}{lobster}");
let test_path = PathBuf::from("test");
let formatter = SpannedDiagnosticFormatter::new(&crustaceans, &test_path).unwrap();
let formatter = SpannedDiagnosticFormatter::new(&crustaceans, &test_path);
// For raw string alignment.
let mut out = String::from("\n");
out.push_str(&formatter.prefixed_underline_span_with_text(
Expand Down Expand Up @@ -560,7 +568,7 @@ mod test {
fn underline_single_line_spans_test() {
let s = "\naaaaaabbb bbb bbbb\n";
let test_path = PathBuf::from("test");
let formatter = SpannedDiagnosticFormatter::new(s, &test_path).unwrap();
let formatter = SpannedDiagnosticFormatter::new(s, &test_path);
let spans = [(7, 10), (11, 14), (15, 19)]
.iter()
.map(|(i, j): &(usize, usize)| Span::new(*i, *j));
Expand Down Expand Up @@ -617,7 +625,7 @@ mod test {
let lobster = "🦞";
let crustaceans = format!("{crab}{lobster}{crab}{lobster}");
let test_path = PathBuf::from("test");
let formatter = SpannedDiagnosticFormatter::new(&crustaceans, &test_path).unwrap();
let formatter = SpannedDiagnosticFormatter::new(&crustaceans, &test_path);
// For raw string alignment.
let mut out = String::from("\n");
let spans = [
Expand Down
Loading