Вспомогательная функция pyparsing 'counttedArray (expr)' - почти то, что нужно.Определение синтаксического анализатора и измененная вспомогательная функция:
def numLinesList(expr, name=None):
"""Helper to snarf an end-of-line integer and match 'expr' N times after.
Almost exactly like pyparsing.countedArray.
Matches patterns of the form::
... num_lines
line one
line two
num_lines'th line
"""
arrayExpr = Forward()
def numLinesAction(s, l, t):
n = int(t[0])
arrayExpr << (n and Group(And([expr]*(n+1))) or Group(empty))
return []
matcher = Word(nums).setParseAction(numLinesAction, callDuringTry=True) \
+ arrayExpr
# remove first empty string
matcher.addParseAction(lambda t: [t[0][1:]])
if name:
matcher = matcher.setResultsName(name)
return matcher
text_meta = Type("T") + coord + color + size + visibility + show_name_value \
+ angle + alignment
text_data_line = SkipTo(LineEnd()) + EOL
text_data = numLinesList(text_data_line, 'text')
text = text_meta + text_data
Для входного фрагмента:
...
T 41600 47800 9 10 1 0 0 0 2
This is line 1
line 2 is here...
T 41600 47000 9 10 1 0 0 0 2
Another first line
second line foo
Это выводит:
['T', 41600, 47800, '9', 10, True, '0', 0, 0, ['This is line 1', 'line 2 is here...']]
['T', 41600, 47000, '9', 10, True, '0', 0, 0, ['Another first line', 'second line foo']]