| |
- 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()')
|