dabeaz/ply — Issue #8: Literals empty list causes IndexError
Author: wnissen
Created: 2011-12-21 19:33:09+00:00
State: open

If i set:

literals = []

I get:

Traceback (most recent call last):
File "Translator.py", line 379, in
lexer = lex(outputText)
File "Translator.py", line 105, in lex
lexer = lex.lex(reflags=re.VERBOSE, module=LexerPassThrough)
File "ply/lex.py", line 920, in lex
IndexError: list index out of range

Below is a suggested possible patch.

```
*** lex.py.old	Wed Dec 21 10:44:55 2011
--- lex.py	Wed Dec 21 10:45:28 2011
***************
*** 917,923 ****

# Get literals specification
if isinstance(linfo.literals,(list,tuple)):
! lexobj.lexliterals = type(linfo.literals[0])().join(linfo.literals)
else:
lexobj.lexliterals = linfo.literals

--- 917,926 ----

# Get literals specification
if isinstance(linfo.literals,(list,tuple)):
! if linfo.literals:
! lexobj.lexliterals = type(linfo.literals[0])().join(linfo.literals)
! else:
! lexobj.lexliterals = ""
else:
lexobj.lexliterals = linfo.literals
```

I could also see calling `iter` on linfo.literals, which would be more Pythonic than an explicit check for list/tuple, but that's maybe going too far.