git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] chainlint: latch line numbers at which each token starts and ends

From
Eric Sunshine via GitGitGadget <gitgitgadget@gmail.com>
Date
Nov 9, 2022, 16:58 UTC
Message-ID
<c8a316426be4cefb6382f524a89226c76d6d1d97.1668013114.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1413.git.1668013114.gitgitgadget@gmail.com>
From: Eric Sunshine <sunshine@sunshineco.com>

When chainlint detects problems in a test, it prints out the name of the test script, the name of the problematic test, and a copy of the test definition with "?!FOO?!" annotations inserted at the locations where problems were detected. Taken together this information is sufficient for the test author to identify the problematic code in the original test definition. However, in a lengthy script or a lengthy test definition, the author may still end up using the editor's search feature to home in on the exact problem location.

To further assist the test author, an upcoming change will display line numbers along with the annotated test definition, thus allowing the author to jump directly to each problematic line. As preparation, upgrade Lexer to latch the line numbers at which each token starts and ends, and return that information with the token itself.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 t/chainlint.pl | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/t/chainlint.pl b/t/chainlint.pl
index fcf4d459249..01f261165b1 100755
--- a/t/chainlint.pl
+++ b/t/chainlint.pl
@@ -67,6 +67,7 @@ sub new {
 	bless {
 		parser => $parser,
 		buff => $s,
+		lineno => 1,
 		heretags => []
 	} => $class;
 }
@@ -97,7 +98,9 @@ sub scan_op {
 sub scan_sqstring {
 	my $self = shift @_;
 	${$self->{buff}} =~ /\G([^']*'|.*\z)/sgc;
-	return "'" . $1;
+	my $s = $1;
+	$self->{lineno} += () = $s =~ /\n/sg;
+	return "'" . $s;
 }
 
 sub scan_dqstring {
@@ -115,7 +118,7 @@ sub scan_dqstring {
 		if ($c eq '\\') {
 			$s .= '\\', last unless $$b =~ /\G(.)/sgc;
 			$c = $1;
-			next if $c eq "\n"; # line splice
+			$self->{lineno}++, next if $c eq "\n"; # line splice
 			# backslash escapes only $, `, ", \ in dq-string
 			$s .= '\\' unless $c =~ /^[\$`"\\]$/;
 			$s .= $c;
@@ -123,6 +126,7 @@ sub scan_dqstring {
 		}
 		die("internal error scanning dq-string '$c'\n");
 	}
+	$self->{lineno} += () = $s =~ /\n/sg;
 	return $s;
 }
 
@@ -137,6 +141,7 @@ sub scan_balanced {
 		$depth--;
 		last if $depth == 0;
 	}
+	$self->{lineno} += () = $s =~ /\n/sg;
 	return $s;
 }
 
@@ -165,6 +170,8 @@ sub swallow_heredocs {
 	while (my $tag = shift @$tags) {
 		my $indent = $tag =~ s/^\t// ? '\\s*' : '';
 		$$b =~ /(?:\G|\n)$indent\Q$tag\E(?:\n|\z)/gc;
+		my $body = $&;
+		$self->{lineno} += () = $body =~ /\n/sg;
 	}
 }
 
@@ -172,11 +179,12 @@ sub scan_token {
 	my $self = shift @_;
 	my $b = $self->{buff};
 	my $token = '';
-	my $start;
+	my ($start, $startln);
 RESTART:
+	$startln = $self->{lineno};
 	$$b =~ /\G[ \t]+/gc; # skip whitespace (but not newline)
 	$start = pos($$b) || 0;
-	return ["\n", $start, pos($$b)] if $$b =~ /\G#[^\n]*(?:\n|\z)/gc; # comment
+	$self->{lineno}++, return ["\n", $start, pos($$b), $startln, $startln] if $$b =~ /\G#[^\n]*(?:\n|\z)/gc; # comment
 	while (1) {
 		# slurp up non-special characters
 		$token .= $1 if $$b =~ /\G([^\\;&|<>(){}'"\$\s]+)/gc;
@@ -188,20 +196,20 @@ RESTART:
 		$token .= $self->scan_sqstring(), next if $c eq "'";
 		$token .= $self->scan_dqstring(), next if $c eq '"';
 		$token .= $c . $self->scan_dollar(), next if $c eq '$';
-		$self->swallow_heredocs(), $token = $c, last if $c eq "\n";
+		$self->{lineno}++, $self->swallow_heredocs(), $token = $c, last if $c eq "\n";
 		$token = $self->scan_op($c), last if $c =~ /^[;&|<>]$/;
 		$token = $c, last if $c =~ /^[(){}]$/;
 		if ($c eq '\\') {
 			$token .= '\\', last unless $$b =~ /\G(.)/sgc;
 			$c = $1;
-			next if $c eq "\n" && length($token); # line splice
-			goto RESTART if $c eq "\n"; # line splice
+			$self->{lineno}++, next if $c eq "\n" && length($token); # line splice
+			$self->{lineno}++, goto RESTART if $c eq "\n"; # line splice
 			$token .= '\\' . $c;
 			next;
 		}
 		die("internal error scanning character '$c'\n");
 	}
-	return length($token) ? [$token, $start, pos($$b)] : undef;
+	return length($token) ? [$token, $start, pos($$b), $startln, $self->{lineno}] : undef;
 }
 
 # ShellParser parses POSIX shell scripts (with minor extensions for Bash). It
-- 
gitgitgadget
Previous: brian m. carlsonNext: Eric Sunshine via GitGitGadget
Message 9 of 20 in “chainlint: emit line numbers alongside test definitions”
  1. 0/3 chainlint: emit line numbers alongside test definitionsEric Sunshine via GitGitGadget, Nov 9, 2022
  2. 1/3 chainlint: sidestep impoverished macOS "terminfo"Eric Sunshine via GitGitGadget, Nov 9, 2022
  3. Taylor BlauNov 9, 2022
  4. brian m. carlsonNov 10, 2022
  5. Eric SunshineNov 10, 2022
  6. brian m. carlsonNov 10, 2022
  7. Eric SunshineNov 10, 2022
  8. brian m. carlsonNov 10, 2022
  9. 2/3 chainlint: latch line numbers at which each token starts and endsEric Sunshine via GitGitGadget, Nov 9, 2022
  10. 3/3 chainlint: prefix annotated test definition with line numbersEric Sunshine via GitGitGadget, Nov 9, 2022
  11. Taylor BlauNov 9, 2022
  12. 0/3 chainlint: emit line numbers alongside test definitionsEric Sunshine via GitGitGadget, Nov 11, 2022
  13. 1/3 chainlint: sidestep impoverished macOS "terminfo"Eric Sunshine via GitGitGadget, Nov 11, 2022
  14. Ævar Arnfjörð BjarmasonNov 11, 2022
  15. Eric SunshineNov 11, 2022
  16. Eric SunshineNov 11, 2022
  17. Taylor BlauNov 11, 2022
  18. 2/3 chainlint: latch line numbers at which each token starts and endsEric Sunshine via GitGitGadget, Nov 11, 2022
  19. 3/3 chainlint: prefix annotated test definition with line numbersEric Sunshine via GitGitGadget, Nov 11, 2022
  20. Ævar Arnfjörð BjarmasonNov 11, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.