bashlex
bashlex copied to clipboard
`[[ <exp> ]]` seems to be broken
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?