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

[[ <exp> ]] seems to be broken #96

Open
Vosjedev opened this issue Jul 31, 2024 · 0 comments
Open

[[ <exp> ]] seems to be broken #96

Vosjedev opened this issue Jul 31, 2024 · 0 comments

Comments

@Vosjedev
Copy link

Vosjedev commented Jul 31, 2024

I tried the following script:

if [[ -z "$var" ]]; then
    echo "-z var == true"
else
    echo "-z var == false"
fi

And it gave me an error:

In [26]: bashlex.parse("""
    ...: if [[ -z "$var" ]]; then
    ...:     echo "-z var == true"
    ...: else
    ...:     echo "-z var == false"
    ...: fi
    ...: """)
---------------------------------------------------------------------------
ParsingError                              Traceback (most recent call last)
Cell In[26], line 1
----> 1 bashlex.parse("""
      2 if [[ -z "$var" ]]; then
      3     echo "-z var == true"
      4 else
      5     echo "-z var == false"
      6 fi
      7 """)

File ~/.cache/pypoetry/virtualenvs/virtualposix-qB8zXGtl-py3.12/lib/python3.12/site-packages/bashlex/parser.py:610, in parse(s, strictmode, expansionlimit, convertpos)
    591 '''parse the input string, returning a list of nodes
    592 
    593 top level node kinds are:
   (...)
    607 command substitutions found during word expansion.
    608 '''
    609 p = _parser(s, strictmode=strictmode, expansionlimit=expansionlimit)
--> 610 parts = [p.parse()]
    612 class endfinder(ast.nodevisitor):
    613     def __init__(self):

File ~/.cache/pypoetry/virtualenvs/virtualposix-qB8zXGtl-py3.12/lib/python3.12/site-packages/bashlex/parser.py:691, in _parser.parse(self)
    686 def parse(self):
    687     # yacc.yacc returns a parser object that is not reentrant, it has
    688     # some mutable state. we make a shallow copy of it so no
    689     # state spills over to the next call to parse on it
    690     theparser = copy.copy(yaccparser)
--> 691     tree = theparser.parse(lexer=self.tok, context=self)
    693     return tree

File ~/.cache/pypoetry/virtualenvs/virtualposix-qB8zXGtl-py3.12/lib/python3.12/site-packages/bashlex/yacc.py:537, in LRParser.parse(self, input, lexer, debug, tracking, context)
    535     errtoken.lexer = lexer
    536 self.state = state
--> 537 tok = self.errorfunc(errtoken)
    538 if self.errorok:
    539     # User must have done some kind of panic
    540     # mode recovery on their own.  The
    541     # returned token is the next lookahead
    542     lookahead = tok

File ~/.cache/pypoetry/virtualenvs/virtualposix-qB8zXGtl-py3.12/lib/python3.12/site-packages/bashlex/parser.py:548, in p_error(p)
    544     raise errors.ParsingError('unexpected EOF',
    545                               p.lexer.source,
    546                               len(p.lexer.source))
    547 else:
--> 548     raise errors.ParsingError('unexpected token %r' % p.value,
    549                               p.lexer.source, p.lexpos)

ParsingError: unexpected token '-z' (position 7)

The same error appears with

[[ -z "$var" ]]

and

[[ "$var" == '' ]]

except in that last one the unexpected token is '"$var"'.

My conclusion from this is that the [[ command does not work correctly. Is this fixable?

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

No branches or pull requests

1 participant