Skip to content
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

syntax: got "}" can only be used to close a block on a seemingly normal block #739

Closed
andras-kth opened this issue Oct 6, 2021 · 8 comments · Fixed by #771
Closed

syntax: got "}" can only be used to close a block on a seemingly normal block #739

andras-kth opened this issue Oct 6, 2021 · 8 comments · Fixed by #771

Comments

@andras-kth
Copy link

I'm struggling to understand what's wrong with this sample:

$ shfmt
@() {
    echo "$@"
}
@ hello world\!
<standard input>:3:1: "}" can only be used to close a block
@andras-kth
Copy link
Author

Apparently, the "bug(?)" has to do with the "at" sign (@), since using an equal sign (=) instead works fine...

$ shfmt
=() {
    echo "$@"
}
= Hello world\!
=() {
        echo "$@"
}
= Hello world\!
$

@andras-kth
Copy link
Author

Found a workaround (I just love the internet...):

$ shfmt 
@()
{
    echo "$@"
}
@ Hello world\!
@()
{
        echo "$@"
}
@ Hello world\!

I'm still not sure why shfmt treats @ special,
or why the added line-break makes it tick,
but this solves my issue for the time being...

@andras-kth
Copy link
Author

Interestingly, it's not only the "at" sign (@) that appears to be special but the plus sign (+) as well:

$ for i in @ + - = , ^; do echo; echo __ $i __; echo "$i() { echo yay; }" | shfmt; echo; done

__ @ __
<standard input>:1:17: "}" can only be used to close a block


__ + __
<standard input>:1:17: "}" can only be used to close a block


__ - __
-() { echo yay; }


__ = __
=() { echo yay; }


__ , __
,() { echo yay; }


__ ^ __
^() { echo yay; }

@mvdan
Copy link
Owner

mvdan commented Oct 8, 2021

Thanks for the report! This might be a bug in the parser. I'd be happy to review a patch if you're up for looking into it :)

@mvdan mvdan changed the title Got "}" can only be used to close a block on a seemingly normal block syntax: got "}" can only be used to close a block on a seemingly normal block Oct 8, 2021
@andras-kth
Copy link
Author

andras-kth commented Oct 9, 2021

Thanks for the report! This might be a bug in the parser. I'd be happy to review a patch if you're up for looking into it :)

Actually, I'm not. The only reason I use this tool is because I'm forced to.
I'm fully capable of writing shell code in a format that I like, and I truly hate
some of the formatting choices made for me...

One clear and present danger with tools like this is that they are often misused
by local micro-dictators to force a straight-jacket of uniformity on their work force,
in the name of maintainability.

@mvdan
Copy link
Owner

mvdan commented Oct 9, 2021

I'm not sure how to answer to that, so I won't :)

@riacataquian
Copy link
Collaborator

I'll take a look at this @mvdan :)

@riacataquian riacataquian self-assigned this Nov 15, 2021
@mvdan
Copy link
Owner

mvdan commented Nov 15, 2021

Sounds good!

riacataquian added a commit to riacataquian/sh that referenced this issue Dec 8, 2021
the lexer assumes an extglob token if any of the wildcards expressions
(such as `@`, and `+`) are succeeded by a left parenthesis but that
proves to be an issue if the wildcard is used as a function name.

example input:
```
$ cat in.sh
@() {
  echo "$@";
}
```

`bash` and `gosh` comparison:
```
$ bash ./in.sh
hello
$ ./gosh in.sh
in.sh:5:1: "}" can only be used to close a block
```

given `in.sh`, gosh reports about a syntax error - this is because
a closing bracket is found while the lexer doesn't assume a function
block

fix the issue by assuming a function if:
* if the expression is found at the beginning of the statement or if its
  preceded by a "function"
* if `(` is immediately succeeded by a `)` - although this is a valid
  bash syntax, we'll operate on the likelihood that it is a function

fixes mvdan#739
riacataquian added a commit to riacataquian/sh that referenced this issue Dec 8, 2021
the lexer assumes an extglob token if any of the wildcards expressions
(such as `@`, and `+`) are succeeded by a left parenthesis but that
proves to be an issue if the wildcard is used as a function name.

example input:
```
$ cat in.sh
@() {
  echo "$@";
}
```

`bash` and `gosh` comparison:
```
$ bash ./in.sh
hello
$ ./gosh in.sh
in.sh:5:1: "}" can only be used to close a block
```

given `in.sh`, gosh reports about a syntax error - this is because
a closing bracket is found while the lexer isn't assuming a function
block

fix the issue by assuming a function if one of the conditions below is
true:
* if the expression is found at the beginning of the statement or if its
  preceded by a "function"
* if `(` is immediately succeeded by a `)` - although this is a valid
  bash syntax, we'll operate on the likelihood that it is a function

fixes mvdan#739
riacataquian added a commit to riacataquian/sh that referenced this issue Dec 11, 2021
the lexer assumes an extglob token if any of the wildcards expressions
(such as `@`, and `+`) are succeeded by a left parenthesis but that
proves to be an issue if the wildcard is used as a function name.

example input:
```
$ cat in.sh
@() {
  echo "$@";
}
```

`bash` and `gosh` comparison:
```
$ bash ./in.sh
hello
$ ./gosh in.sh
in.sh:5:1: "}" can only be used to close a block
```

given `in.sh`, gosh reports about a syntax error - this is because
a closing bracket is found while the lexer isn't assuming a function
block

fix the issue by assuming a function if one of the conditions below is
true:
* if the expression is found at the beginning of the statement or if its
  preceded by a "function"
* if `(` is immediately succeeded by a `)` - although this is a valid
  bash syntax, we'll operate on the likelihood that it is a function

fixes mvdan#739
riacataquian added a commit to riacataquian/sh that referenced this issue Dec 11, 2021
the lexer assumes an extglob token if any of the wildcards expressions
(such as `@`, and `+`) are succeeded by a left parenthesis but that
proves to be an issue if the wildcard is used as a function name.

example input:
```
$ cat in.sh
@() {
  echo "$@";
}
```

`bash` and `gosh` comparison:
```
$ bash ./in.sh
hello
$ ./gosh in.sh
in.sh:5:1: "}" can only be used to close a block
```

given `in.sh`, gosh reports about a syntax error - this is because
a closing bracket is found while the lexer isn't assuming a function
block

fix the issue by assuming a function if one of the conditions below is
true:
* if the expression is found at the beginning of the statement or if its
  preceded by a "function"
* if `(` is immediately succeeded by a `)` - although this is a valid
  bash syntax, we'll operate on the likelihood that it is a function

fixes mvdan#739
riacataquian added a commit that referenced this issue Dec 11, 2021
the lexer assumes an extglob token if any of the wildcards expressions
(such as `@`, and `+`) are succeeded by a left parenthesis but that
proves to be an issue if the wildcard is used as a function name.

example input:
```
$ cat in.sh
@() {
  echo "$@";
}
```

`bash` and `gosh` comparison:
```
$ bash ./in.sh
hello
$ ./gosh in.sh
in.sh:5:1: "}" can only be used to close a block
```

given `in.sh`, gosh reports about a syntax error - this is because
a closing bracket is found while the lexer isn't assuming a function
block

fix the issue by assuming a function if one of the conditions below is
true:
* if the expression is found at the beginning of the statement or if its
  preceded by a "function"
* if `(` is immediately succeeded by a `)` - although this is a valid
  bash syntax, we'll operate on the likelihood that it is a function

fixes #739
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants