Tim Chase <[EMAIL PROTECTED]> wrote:

> The parser also has to accomodate "raw" and "unicode" string 
> prefixes, as they're valid too:
> 
>    def f(x):
>      r"raw!"
>      pass
> 
>    def f(x):
>      u"Unicode"
>      pass
> 
> 
> in addition. Okay...in most of these cases, the pathological 
> coder should be taken out back and beaten, but it's a non-trivial 
> problem :)

Fortunately someone already wrote a parser for Python code, so it is pretty 
trivial:

>>> import compiler
>>> class Visitor(compiler.visitor.ASTVisitor):
        def visitFunction(self, node):
                print node.doc

>>> def showdoc(source):
        compiler.walk(compiler.parse(source), Visitor())

        
>>> showdoc(r'''def f(x):
     """this is a \"""docstring\"""
     with \\\"""and\""" mutliple lines"""
     pass
''')
this is a """docstring"""
     with \"""and""" mutliple lines
>>> showdoc(r'''def f(x):
     u"Unicode" " with concatenation"
     pass
''')
Unicode with concatenation
>>> 
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to