expanderparser (version 1.5.11)
index
/net/elbe.acc.bessy.de/srv/projects/ctl/pfeiffer/project-elbe/pyexpander/expanderparser.py

 
Modules
       
bisect
re

 
Classes
       
__builtin__.object
IndexedString
LineNumbers
ParsedItem
ParsedCommand
ParsedComment
ParsedEval
ParsedLiteral
ParsedPureCommand
ParsedVar
exceptions.Exception(exceptions.BaseException)
ParseException

 
class IndexedString(__builtin__.object)
    a string together with row column information.
 
  Methods defined here:
__init__(self, st)
__repr__(self)
__str__(self)
rowcol(self, pos)
calculate (row,column) from a string position.
st(self)
return the raw string.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class LineNumbers(__builtin__.object)
    convert a string position to row and column.
 
Here is an example:
>>> txt='''01234
... 67
... 9abcd'''
>>> l=LineNumbers(txt)
>>> l.rowcol(0)
(1, 1)
>>> l.rowcol(1)
(1, 2)
>>> l.rowcol(4)
(1, 5)
>>> l.rowcol(5)
(1, 6)
>>> l.rowcol(6)
(2, 1)
>>> l.rowcol(7)
(2, 2)
>>> l.rowcol(8)
(2, 3)
>>> l.rowcol(9)
(3, 1)
>>> l.rowcol(13)
(3, 5)
>>> l.rowcol(14)
(3, 6)
>>> l.rowcol(16)
(3, 8)
 
  Methods defined here:
__init__(self, st)
rowcol(self, pos)
calculate (row,column) from a string position.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ParseException(exceptions.Exception)
    used for Exceptions in this module.
 
 
Method resolution order:
ParseException
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods defined here:
__init__(self, value, pos=None, rowcol=None)
# pylint: disable=W0231
__str__(self)
# pylint: enable=W0231

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
class ParsedCommand(ParsedItem)
    class of a pyexpander command with arguments.
 
A command has the form "$name(argument1, argument2, ...)".
 
 
Method resolution order:
ParsedCommand
ParsedItem
__builtin__.object

Methods defined here:
__init__(self, idxst, start, end, ident)
__repr__(self)
__str__(self)
args(self)
return the arguments of the command.

Methods inherited from ParsedItem:
end(self)
return the end of the ParsedItem in the source string.
positions(self)
return start and end of ParsedItem in the source string.
rowcol(self, pos=None)
calculate (row,column) from a string position.
start(self)
return the start of the ParsedItem in the source string.
string(self)
return the string that represents the ParsedItem.

Data descriptors inherited from ParsedItem:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ParsedComment(ParsedItem)
    class of a parsed comment.
 
A comment in pyexpander starts with '$#'.
 
 
Method resolution order:
ParsedComment
ParsedItem
__builtin__.object

Methods defined here:
__init__(self, idxst, start, end)

Methods inherited from ParsedItem:
__repr__(self)
__str__(self)
end(self)
return the end of the ParsedItem in the source string.
positions(self)
return start and end of ParsedItem in the source string.
rowcol(self, pos=None)
calculate (row,column) from a string position.
start(self)
return the start of the ParsedItem in the source string.
string(self)
return the string that represents the ParsedItem.

Data descriptors inherited from ParsedItem:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ParsedEval(ParsedItem)
    class of an pyexpander expression.
 
A pyexpander expression has the form "$(expression)" e.g. "$(a+1)". This is
different from ParsedVar where the string within the brackets is a simple
identifier.
 
 
Method resolution order:
ParsedEval
ParsedItem
__builtin__.object

Methods defined here:
__init__(self, idxst, start, end)

Methods inherited from ParsedItem:
__repr__(self)
__str__(self)
end(self)
return the end of the ParsedItem in the source string.
positions(self)
return start and end of ParsedItem in the source string.
rowcol(self, pos=None)
calculate (row,column) from a string position.
start(self)
return the start of the ParsedItem in the source string.
string(self)
return the string that represents the ParsedItem.

Data descriptors inherited from ParsedItem:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ParsedItem(__builtin__.object)
    base class of parsed items.
 
  Methods defined here:
__init__(self, idxst, start, end)
__repr__(self)
__str__(self)
end(self)
return the end of the ParsedItem in the source string.
positions(self)
return start and end of ParsedItem in the source string.
rowcol(self, pos=None)
calculate (row,column) from a string position.
start(self)
return the start of the ParsedItem in the source string.
string(self)
return the string that represents the ParsedItem.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ParsedLiteral(ParsedItem)
    class of a parsed literal.
 
A literal is a substring in the input that shouldn't be modified by
pyexpander.
 
 
Method resolution order:
ParsedLiteral
ParsedItem
__builtin__.object

Methods defined here:
__init__(self, idxst, start, end)

Methods inherited from ParsedItem:
__repr__(self)
__str__(self)
end(self)
return the end of the ParsedItem in the source string.
positions(self)
return start and end of ParsedItem in the source string.
rowcol(self, pos=None)
calculate (row,column) from a string position.
start(self)
return the start of the ParsedItem in the source string.
string(self)
return the string that represents the ParsedItem.

Data descriptors inherited from ParsedItem:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ParsedPureCommand(ParsedItem)
    class of a pyexpander command without arguments.
 
A pure command has the form "$name". Such a command has no arguments which
would be enclosed in round brackets immediately following the name.
 
 
Method resolution order:
ParsedPureCommand
ParsedItem
__builtin__.object

Methods defined here:
__init__(self, idxst, start, end)

Methods inherited from ParsedItem:
__repr__(self)
__str__(self)
end(self)
return the end of the ParsedItem in the source string.
positions(self)
return start and end of ParsedItem in the source string.
rowcol(self, pos=None)
calculate (row,column) from a string position.
start(self)
return the start of the ParsedItem in the source string.
string(self)
return the string that represents the ParsedItem.

Data descriptors inherited from ParsedItem:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ParsedVar(ParsedItem)
    class of a parsed variable.
 
A variable in pyexpander has the form "$(identifier)".
 
 
Method resolution order:
ParsedVar
ParsedItem
__builtin__.object

Methods defined here:
__init__(self, idxst, start, end)

Methods inherited from ParsedItem:
__repr__(self)
__str__(self)
end(self)
return the end of the ParsedItem in the source string.
positions(self)
return start and end of ParsedItem in the source string.
rowcol(self, pos=None)
calculate (row,column) from a string position.
start(self)
return the start of the ParsedItem in the source string.
string(self)
return the string that represents the ParsedItem.

Data descriptors inherited from ParsedItem:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
parseAll(idxst, pos)
parse everything.
>>> def test(st,pos):
...     idxst= IndexedString(st)
...     pprint(parseAll(idxst,pos))
...
 
>>> test("abc",0)
('ParsedLiteral', (0, 2), 'abc')
>>> test("abc$xyz",0)
('ParsedLiteral', (0, 2), 'abc')
('ParsedPureCommand', (4, 6), 'xyz')
>>> test("abc${xyz}efg",0)
('ParsedLiteral', (0, 2), 'abc')
('ParsedPureCommand', (5, 7), 'xyz')
('ParsedLiteral', (9, 11), 'efg')
>>> test("abc$xyz(2*4)",0)
('ParsedLiteral', (0, 2), 'abc')
('ParsedCommand', (8, 10), '2*4', 'xyz')
>>> test("abc$(2*4)ab",0)
('ParsedLiteral', (0, 2), 'abc')
('ParsedEval', (5, 7), '2*4')
('ParsedLiteral', (9, 10), 'ab')
>>> test("abc\\$(2*4)ab",0)
('ParsedLiteral', (0, 2), 'abc')
('ParsedLiteral', (4, 4), '$')
('ParsedLiteral', (5, 11), '(2*4)ab')
>>> test("ab$func(1+2)\\\nnew line",0)
('ParsedLiteral', (0, 1), 'ab')
('ParsedCommand', (8, 10), '1+2', 'func')
('ParsedLiteral', (14, 21), 'new line')
>>> test("ab$func(1+2)\nnew line",0)
('ParsedLiteral', (0, 1), 'ab')
('ParsedCommand', (8, 10), '1+2', 'func')
('ParsedLiteral', (12, 20), '\nnew line')
>>> test("ab$(xyz)(56)",0)
('ParsedLiteral', (0, 1), 'ab')
('ParsedVar', (4, 6), 'xyz')
('ParsedLiteral', (8, 11), '(56)')
 
>>> test(r'''
... Some text with a macro: $(xy)
... an escaped dollar: \$(xy)
... a macro within letters: abc${xy}def
... a pyexpander command structure:
... $if(a=1)
... here
... $else
... there
... $endif
... now a continued\
... line
... from here:$# the rest is a comment
... now an escaped continued\\
... line
... ''',0)
('ParsedLiteral', (0, 24), '\nSome text with a macro: ')
('ParsedVar', (27, 28), 'xy')
('ParsedLiteral', (30, 49), '\nan escaped dollar: ')
('ParsedLiteral', (51, 51), '$')
('ParsedLiteral', (52, 83), '(xy)\na macro within letters: abc')
('ParsedPureCommand', (86, 87), 'xy')
('ParsedLiteral', (89, 124), 'def\na pyexpander command structure:\n')
('ParsedCommand', (129, 131), 'a=1', 'if')
('ParsedLiteral', (133, 138), '\nhere\n')
('ParsedPureCommand', (140, 143), 'else')
('ParsedLiteral', (144, 150), '\nthere\n')
('ParsedPureCommand', (152, 156), 'endif')
('ParsedLiteral', (157, 172), '\nnow a continued')
('ParsedLiteral', (175, 189), 'line\nfrom here:')
('ParsedComment', (192, 214), ' the rest is a comment\n')
('ParsedLiteral', (215, 238), 'now an escaped continued')
('ParsedLiteral', (239, 239), '\\')
('ParsedLiteral', (241, 246), '\nline\n')
parseBackslash(idxst, pos)
parses a backslash.
>>> def test(st,pos):
...   idxst= IndexedString(st)
...   (p,elm)= parseBackslash(idxst,pos)
...   print "Parsed: %s" % elm
...   print "rest of string:", st[p:]
...
>>> test(r"\abc",0)
Parsed: ('ParsedLiteral', (0, 0), '\\')
rest of string: abc
>>> test("\\",0)
Parsed: ('ParsedLiteral', (0, 2), '\\')
rest of string: 
>>> test("\\\rab",0)
Parsed: None
rest of string: ab
>>> test("\\\nab",0)
Parsed: None
rest of string: ab
>>> test("\\\r\nab",0)
Parsed: None
rest of string: ab
>>> test("\\\n\nab",0)
Parsed: None
rest of string: 
ab
parseBracketed(idxst, pos)
parse an identifier in curly brackets.
 
Here are some examples:
>>> def test(st,pos):
...     idxst= IndexedString(st)
...     (a,b)= parseBracketed(idxst,pos)
...     print st[a:b]
...
>>> test(r'{abc}',0)
{abc}
>>> test(r'{ab8c}',0)
{ab8c}
>>> test(r'{c}',0)
{c}
>>> test(r'{}',0)
Traceback (most recent call last):
    ...
ParseException: command enclosed in curly brackets at line 1, col 1
>>> test(r'{abc',0)
Traceback (most recent call last):
    ...
ParseException: command enclosed in curly brackets at line 1, col 1
>>> test(r'x{ab8c}',1)
{ab8c}
parseCode(idxst, pos)
parse python code, it MUST start with a '('.
 
Here are some examples:
>>> def test(st,pos):
...     idxst= IndexedString(st)
...     (a,b)= parseCode(idxst,pos)
...     print st[a:b]
...
>>> test(r'(a+b)',0)
(a+b)
>>> test(r'(a+(b*c))',0)
(a+(b*c))
>>> test(r'(a+(b*c)+")")',0)
(a+(b*c)+")")
>>> test(r"(a+(b*c)+''')''')",0)
(a+(b*c)+''')''')
>>> test(r"(a+(b*c)+''')'''+# comment )\n)",0)
Traceback (most recent call last):
    ...
ParseException: end of bracket expression not found at line 1, col 1
>>>
>>> test("(a+(b*c)+''')'''+# comment )\n)",0)
(a+(b*c)+''')'''+# comment )
)
parseComment(idxst, pos)
parse a python comment.
 
Here are some examples:
>>> def test(st,pos):
...     idxst= IndexedString(st)
...     (a,b)= parseComment(idxst,pos)
...     print repr(st[a:b])
>>> test("#abc",0)
'#abc'
>>> test("#abc\nef",0)
'#abc\n'
>>> test("#abc\r\nef",0)
'#abc\r\n'
>>> test("xy#abc",2)
'#abc'
>>> test("xy#abc\nef",2)
'#abc\n'
>>> test("xy#abc\nef",3)
Traceback (most recent call last):
    ...
ParseException: start of comment not found at line 1, col 4
parseDollar(idxst, pos)
parse things that follow a dollar.
 
Here are some examples:
>>> def test(st,pos):
...   idxst= IndexedString(st)
...   (p,elm)= parseDollar(idxst,pos)
...   print "Parsed: %s" % elm
...   print "rest of string:", st[p:]
...
>>> test("$abc",0)
Parsed: ('ParsedPureCommand', (1, 3), 'abc')
rest of string: 
>>> test("$abc%&/",0)
Parsed: ('ParsedPureCommand', (1, 3), 'abc')
rest of string: %&/
>>> test("$abc(2*3)",0)
Parsed: ('ParsedCommand', (5, 7), '2*3', 'abc')
rest of string: 
>>> test(" $abc(2*sin(x))",1)
Parsed: ('ParsedCommand', (6, 13), '2*sin(x)', 'abc')
rest of string: 
>>> test(" $abc(2*sin(x))bn",1)
Parsed: ('ParsedCommand', (6, 13), '2*sin(x)', 'abc')
rest of string: bn
>>> test(" $# a comment\nnew line",1)
Parsed: ('ParsedComment', (3, 13), ' a comment\n')
rest of string: new line
>>> test("$(abc)",0)
Parsed: ('ParsedVar', (2, 4), 'abc')
rest of string: 
>>> test("$(abc*2)",0)
Parsed: ('ParsedEval', (2, 6), 'abc*2')
rest of string: 
>>> test(" $(2*x(y))abc",1)
Parsed: ('ParsedEval', (3, 8), '2*x(y)')
rest of string: abc
parseStringLiteral(idxst, pos)
parse a python string literal.
 
returns 2 numbers, the index where the string starts and
the index of the first character *after* the string
 
Here are some examples:
>>> def test(st,pos):
...     idxst= IndexedString(st)
...     (a,b)= parseStringLiteral(idxst,pos)
...     print st[a:b]
...
 
>>> test(r'''"abc"''',0)
"abc"
 
>>> test("'''ab'c'd'''",0)
'''ab'c'd'''
>>> test("'''ab'cd''''",0)
'''ab'cd'''
 
>>> test(r'''U"abc"''',0)
U"abc"
>>> test(r'''xU"abc"''',1)
U"abc"
>>> test(r'''xUr"abc"''',1)
Ur"abc"
 
>>> test(r'''xUr"ab\\"c"''',1)
Ur"ab\\"
 
>>> test(r'''xUr"ab\"c"''',1)
Ur"ab\"c"
>>> test(r'''xUr"ab\"c"''',0)
Traceback (most recent call last):
    ...
ParseException: start of string expected at line 1, col 1
>>> test(r'''"ab''',0)
Traceback (most recent call last):
    ...
ParseException: end of string not found at line 1, col 1
>>> test(r"'''ab'",0)
Traceback (most recent call last):
    ...
ParseException: end of string not found at line 1, col 1
>>> test(r'''"ab\"''',0)
Traceback (most recent call last):
    ...
ParseException: end of string not found at line 1, col 1
pprint(parselist)
pretty print a parselist.
scanPyIdentList(st)
scan a list of python identifiers.
 
Here are some examples:
>>> scanPyIdentList("a,b")
['a', 'b']
>>> scanPyIdentList("a,b.d, c")
['a', 'b.d', 'c']
>>> scanPyIdentList("a,b.d, c&")
Traceback (most recent call last):
    ...
ParseException: list of python identifiers expected
scanPyIn(st)
scan a python "in" statement.
 
Here are some examples:
>>> scanPyIn(" (a,b) in k.items() ")
('(a,b)', 'in', 'k.items()')

 
Data
        __version__ = '1.5.11'
rx_CodePart = <_sre.SRE_Pattern object>
rx_DollarFollows = <_sre.SRE_Pattern object>
rx_StringLiteralStart = <_sre.SRE_Pattern object>
rx_bracketed = <_sre.SRE_Pattern object>
rx_csv = <_sre.SRE_Pattern object>
rx_lineEnd = <_sre.SRE_Pattern object>
rx_pyIdent = <_sre.SRE_Pattern object>
rx_py_in = <_sre.SRE_Pattern object>
rx_top = <_sre.SRE_Pattern object>