tree-sitter-bash icon indicating copy to clipboard operation
tree-sitter-bash copied to clipboard

Parse error when redirection occurs before text

Open kingcritter opened this issue 5 years ago • 1 comments

This is a parse error:

echo >&2 "foobar"

This is not a parse error:

echo "foobar" >&2 

Both are equally valid bash syntax.

kingcritter avatar Jan 21 '20 20:01 kingcritter

I am experiencing a similar issue involving a parse error and redirects.

From the bash manpage:

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator.

The following line is a simple command with no variable assignments and no words, a single redirection, and terminated by a newline. This command creates or truncates the file t. (or, more specifically, opens file t for writing on file descriptor 1.)

>t

This is my result:

(program (ERROR (file_redirect (word))))

I believe the source of our issue lies between lines 270 and 283 of grammar.js:

    command: $ => prec.left(seq(
      repeat(choice(
        $.variable_assignment,
        $.file_redirect
      )),
      $.command_name,
      repeat(choice(
        $._literal,
        seq(
          choice('=~', '=='),
          choice($._literal, $.regex)
        )
      ))
    )),

The above appears to specify that all simple commands must include at least one word, and does not specify anything about redirections that follow or are within a simple command.

Addressing this will likely break backwards-compatibility, but in my opinion, it needs to be done.

Navigatron avatar Feb 24 '20 15:02 Navigatron

Same problem here:

#!/usr/bin/env bash

if [ "${2}" == '' ]; then
	echo >&2 "${1}"
else
	echo >&2 "${1}" <<<"${2}"
fi

echo "${@}"

ZiViZiViZ avatar Aug 15 '23 12:08 ZiViZiViZ

Hi @amaanq

Can you please take a look at this? This issue has been reported at least 3 times:

  1. #72
  2. #125
  3. #140 (closed but not fixed)

ZiViZiViZ avatar Aug 15 '23 12:08 ZiViZiViZ

@amaanq I ran :TSUpdate and still getting the wrong syntax highlights

ZiViZiViZ avatar Aug 18 '23 20:08 ZiViZiViZ

Hi @ZiViZiViZ

I rebuilt .../nvim-treesitter/parser/bash.so using the latest commit from master (c0f5797a728fc4ebd78a8b0e436b1494a8ab5f51) and the TS ERRORs were gone.

Try doing the same and if you still have issues, you might have a different problem.

Thanks @amaanq 😄

IzhakJakov avatar Aug 18 '23 20:08 IzhakJakov

@amaanq I ran :TSUpdate and still getting the wrong syntax highlights

or you can just wait until nvim-ts updates the bash revision (happens nightly)

amaanq avatar Aug 18 '23 21:08 amaanq