#4 Parsing a filename ends with exception ...

closed
nobody
None
9
2004-08-28
2004-08-28
No

Hi Paul,
i have a simpe problem,
i want to parse all components of a filename, and i get
an exception :-( Could you help me?

Here the grammar :

#
# The Parsing Header of the Trace file
#
# [ /ORACLE/COSI/trace/cosi2_ora_11629.trc
# Oracle9i Enterprise Edition Release
9.0.1.4.0 - 64bit Production
# With the Partitioning and Real Application
Clusters options
# JServer Release 9.0.1.4.0 - Production ]

_PATH_DELIM_ = Literal('/')
_PATH_COMPONENT_ = OneOrMore ( alphanums ^
Literal('_') ).setDebug()
_PATH_TRACE_FILE_ = OneOrMore (
_PATH_COMPONENT_ + _PATH_DELIM_ )

_TRACE_FILE_NAME_ = _PATH_COMPONENT_ +
Literal('.trc')

_HEADER_TRACE_FILE_ = ( Literal('/') +
_PATH_TRACE_FILE_ + _TRACE_FILE_NAME_ )

_PATH_COMPONENT_.setDebug()
_PATH_DELIM_.setDebug()
_PATH_TRACE_FILE_.setDebug()
_TRACE_FILE_NAME_.setDebug()

traceHeader = (
_HEADER_TRACE_FILE_.setResultsName
('HeaderTraceFile') )

traceHeader.setDebug()

and here the simple example ...

traceHeaderStr = """
/ORACLE/COSI/trace/cosi2_ora_11629.trc
"""

ends with :

/ORACLE/COSI/trace/cosi2_ora_11629.trc

Traceback (most recent call last):
File
"F:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 307, in RunScript
debugger.run(codeObject, __main__.__dict__,
start_stepping=0)
File
"F:\Python23\lib\site-packages\Pythonwin\pywin\debugger\__init__.py",
line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals,
start_stepping)
File
"F:\Python23\lib\site-packages\Pythonwin\pywin\debugger\debugger.py",
line 592, in run
exec cmd in globals, locals
File "G:\etprof\Test\test_etscan.py", line 350, in ?
fields =
getextendedTraceLine().parseString(traceHeaderStr)
File "F:\Python23\Lib\site-packages\pyparsing.py",
line 480, in parseString
loc, tokens = self.parse( instring.expandtabs(), 0 )
File "F:\Python23\Lib\site-packages\pyparsing.py",
line 430, in parse
loc,tokens = self.parseImpl( instring, loc, doActions )
File "F:\Python23\Lib\site-packages\pyparsing.py",
line 1166, in parseImpl
raise exceptions[0]
ParseException: Expected
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
(2), (2,2)

Could i do anything to get a better error-output?

Discussion

  • Carl Reitschuster

    • priority: 5 --> 9
     
  • Carl Reitschuster

    • status: open --> closed
     
  • Carl Reitschuster

    Logged In: YES
    user_id=385682

    I got it :-)

    changed the line

    _PATH_COMPONENT_ = OneOrMore ( alphanums ^
    Literal('_') ).setDebug()

    to

    _PATH_COMPONENT_ = Word( alphanums + '_' )

    i forgot that alphanums is no parsing object but a simple
    string.

    Carl

     
  • Paul McGuire

    Paul McGuire - 2004-08-29

    Logged In: YES
    user_id=893320

    Carl -

    Great! You beat me to the answer about your specific
    problem. Here are some other tips:
    - The results for the filename will be broken into two pieces,
    since it is made up of two expressions. If you want the
    filename to look more like a filename, change
    _TRACE_FILE_NAME_ = _PATH_COMPONENT_ + Literal('.trc')
    to:
    _TRACE_FILE_NAME_ = Combine( _PATH_COMPONENT_ +
    Literal('.trc') )

    Combine will join the embedded tokens into a single token
    string.

    - The output for your grammar lists all of the tokens as one
    list, regardless of which part is which. I would suggest using
    setResultsName() to make it easier to break up the list into
    its path and filename parts.

    _PATH_TRACE_FILE_ = ( OneOrMore ( _PATH_COMPONENT_
    + _PATH_DELIM_ ) ).setResultsName("path")

    _TRACE_FILE_NAME_ = Combine( _PATH_COMPONENT_ +
    Literal('.trc') ).setResultsName("fnam")

    Lets you write:

    trcFileToks = traceHeader.parseString( traceHeaderStr )
    print trcFileToks.path
    print trcFileToks.fnam

    -- Paul

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks