KenKundert / psf_utils

Read Spectre PSF files
49 stars 14 forks source link

Parser does not handle escaped quotes in strings #11

Closed KristofferC closed 1 year ago

KristofferC commented 2 years ago

For example, a file with the following header:

HEADER
"PSFversion" "1.00"
"design" "(\"test\" \"sweep\" \"schematic\" \"\")"
"simulator" "spectre"
"temperature" "27"
"host" ""
"date" "May 27 16:45:28 2022"
"title" ""
"analysisList" "()"
END

gives the error:

>>> psf = PSF('simRunData', use_cache=False)
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/psf_utils/psf.py", line 86, in __init__
    sections = parser.parse(filename, content)
  File "/usr/local/lib/python3.9/site-packages/psf_utils/parse.py", line 488, in parse
    result = self.parser.parse(content, tracking=False, lexer=self.lexer)
  File "/usr/local/lib/python3.9/site-packages/ply/yacc.py", line 265, in parse
    return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
  File "/usr/local/lib/python3.9/site-packages/ply/yacc.py", line 921, in parseopt_notrack
    lookahead = get_token()     # Get the next token
  File "/usr/local/lib/python3.9/site-packages/ply/lex.py", line 384, in token
    newtok = self.lexerrorf(tok)
  File "/usr/local/lib/python3.9/site-packages/psf_utils/parse.py", line 168, in t_error
    raise ParseError("illegal character '%s'." % c, loc)
psf_utils.parse.ParseError: simRunData(3): illegal character 't'.
    "design" "(\"test\" \"sweep\" \"schematic\" \"\")"
                 ^

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/psf_utils/psf.py", line 88, in __init__
    raise Error(str(e))
inform.inform.Error: simRunData(3): illegal character 't'.
    "design" "(\"test\" \"sweep\" \"schematic\" \"\")"

What is expected is to get a value of the unescaped string ("test" "sweep" "schematic" "")

I think this way of matching against strings:

https://github.com/KenKundert/psf_utils/blob/f0c354243f78b7ab3e76d150de6ca1b72521946a/psf_utils/parse.py#L162-L163

is a bit too simplistic in the presence of escaping.

KenKundert commented 2 years ago

How you are getting escaped strings in a PSF file?

KenKundert commented 2 years ago

Never mind, I see it now. The design field is simply the first line in a Spectre netlist, which may contain quote characters.

KenKundert commented 2 years ago

I have worked out the solution for this but won't release it until I look at and try to resolve your other issues. In the mean time you can replace line 163 with the following:

t_STRING = r'"([^\\\n"]|(\\.))*"'                                                              

That should resolve this problem.

KenKundert commented 2 years ago

I have pushed this fix on to GitHub. Please check it out and confirm that it works as expected. Once I hear back from you and things settle down, I will release it to PyPI.