我有一些文字,其中大多数关键字很容易找到,有些很难找到。如果存在,所有关键字总是在一行上左对齐,并以分号结束。然而,偶尔有人会指出:OH错过了一个关键字,它将是一个不以分号结尾的短语(左对齐)。在这些情况下,我必须建立一个不同类型的关键字。并且如果字符串显示在一行中的任何其他位置(包裹或者我应该将其视为普通文本。
作为旁注:我也注意到在输出中我得到了很多'Exception Raised'非致命错误,据我所知,它看起来像是期待语法中的某些内容并且只是自动报告说找不到它。我觉得在我的情况下,因为我有| pyparsing将其视为'不在乎,即使它在错误的点上错过了我的otherAction(见下文)。
我的语法是我之前的问题的后续处理并处理标准关键字就好了,但是当我尝试添加关键字的不同模式时,它会错过它,而是将其作为不同操作的主体包含在内。我假设有人会在我错过其中一个特殊情况时告诉我,所以正文的结尾是一个标准的关键字,以一行开头的分号结尾。
代码:
#!/usr/bin/python
from pyparsing import *
def testString(text, grammar, examineFurther=False):
print 'Input Text is \n%s' % text
tokens = (grammar + stringEnd).parseString(text)
examine = False
print 'token groups: %s' % len(tokens)
try:
for res in tokens:
print 'each result in tokens: %s' % res.dump()
except Exception, issue:
print 'reason 1 %s' % issue
pass
if examineFurther:
try:
results, start, end = next(grammar.scanString(text))
print 'Text is length %s and end scan at %s' % (len(text), end)
lmx = end+10
if len(text) < lmx:
lmx = len(text) - 1
print 'debug in text: end %s to end+10 is >%s<' % (end, text[end:lmx])
except Exception, issue:
print 'reason 2 %s' % issue
pass
return tokens
if __name__ == '__main__':
text = """look; We come to a fork in the creek and must
decide which way to travel
LEFT OFF Another path was seen prior to the fork branching off to the
northwest
south; The stream rapidly descends over rocks and the roar of water is heard
in the distance. Curiosity may drive one to pursue
further in this direction
wEst; a rocky gorge ascends up from a dry branch of the creek
quote; An old rusty pan is nearby
"""
# This grammar 'misses the 'LEFT OFF' keyword
data = Word(printables.replace(';', ''))
kw = Combine(data + Literal(';'))('ACTION')
kw.setDebug(False)
body = OneOrMore(~kw + data).setParseAction(lambda tokens: " ".join(
tokens))('BODY')
body.setDebug(False)
grammar = OneOrMore(Group(kw + body))
tokens = testString(text, grammar)
print 'Test1: Tokens found %s' % tokens
# for simplicity just redefine the grammar from scratch
data = None
kw = None
body = None
grammar = None
# Now try to capture the new keyword: 'LEFT OFF' we just got notified that
# we missed
# the only given we have is it will begin a line
# a keyword is basically an action
print '\nTEST2 start\nNow attempt to capture the missed action/keyword'
data = Word(printables.replace(';', ''))
standardAction = Combine(data + Literal(';'))('ACTION')
standardAction.setDebug(True)
exceptions = ['LEFT OFF', ] # 'FUTURE XYZZY',]
otherAction = Combine(lineStart + oneOf(exceptions))('ACTION')
otherAction.setDebug(True)
action = otherAction | standardAction
body = OneOrMore(~action + data).setParseAction(lambda tokens: " ".join(
tokens))('BODY')
body.setDebug(False)
grammar = OneOrMore(Group(action + body))
tokens = testString(text, grammar,examineFurther=False)
print 'Test2: Tokens found %s' % tokens
输出:
look; We come to a fork in the creek and must
decide which way to travel
LEFT OFF Another path was seen prior to the fork branching off to the
northwest
south; The stream rapidly descends over rocks and the roar of water is heard
in the distance. Curiosity may drive one to pursue
further in this direction
wEst; a rocky gorge ascends up from a dry branch of the creek
quote; An old rusty pan is nearby
token groups: 4
each result in tokens: ['look;', 'We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest']
- ACTION: look;
- BODY: We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest
each result in tokens: ['south;', 'The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction']
- ACTION: south;
- BODY: The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction
each result in tokens: ['wEst;', 'a rocky gorge ascends up from a dry branch of the creek']
- ACTION: wEst;
- BODY: a rocky gorge ascends up from a dry branch of the creek
each result in tokens: ['quote;', 'An old rusty pan is nearby']
- ACTION: quote;
- BODY: An old rusty pan is nearby
Test1: Tokens found [['look;', 'We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest'], ['south;', 'The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction'], ['wEst;', 'a rocky gorge ascends up from a dry branch of the creek'], ['quote;', 'An old rusty pan is nearby']]
TEST2 start
Now attempt to capture the missed action/keyword
Input Text is
look; We come to a fork in the creek and must
decide which way to travel
LEFT OFF Another path was seen prior to the fork branching off to the
northwest
south; The stream rapidly descends over rocks and the roar of water is heard
in the distance. Curiosity may drive one to pursue
further in this direction
wEst; a rocky gorge ascends up from a dry branch of the creek
quote; An old rusty pan is nearby
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 0(1,1)
Exception raised:Expected Re:('LEFT\\ OFF') (at char 0), (line:1, col:1)
Match Combine:({W:(0123...) ";"}) at loc 0(1,1)
Matched Combine:({W:(0123...) ";"}) -> ['look;']
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 5(1,6)
Exception raised:Expected lineStart (at char 6), (line:1, col:7)
Match Combine:({W:(0123...) ";"}) at loc 5(1,6)
Exception raised:Expected ";" (at char 8), (line:1, col:9)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 8(1,9)
Exception raised:Expected lineStart (at char 9), (line:1, col:10)
Match Combine:({W:(0123...) ";"}) at loc 8(1,9)
Exception raised:Expected ";" (at char 13), (line:1, col:14)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 13(1,14)
Exception raised:Expected lineStart (at char 14), (line:1, col:15)
Match Combine:({W:(0123...) ";"}) at loc 13(1,14)
Exception raised:Expected ";" (at char 16), (line:1, col:17)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 16(1,17)
Exception raised:Expected lineStart (at char 17), (line:1, col:18)
Match Combine:({W:(0123...) ";"}) at loc 16(1,17)
Exception raised:Expected ";" (at char 18), (line:1, col:19)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 18(1,19)
Exception raised:Expected lineStart (at char 19), (line:1, col:20)
Match Combine:({W:(0123...) ";"}) at loc 18(1,19)
Exception raised:Expected ";" (at char 23), (line:1, col:24)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 23(1,24)
Exception raised:Expected lineStart (at char 24), (line:1, col:25)
Match Combine:({W:(0123...) ";"}) at loc 23(1,24)
Exception raised:Expected ";" (at char 26), (line:1, col:27)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 26(1,27)
Exception raised:Expected lineStart (at char 27), (line:1, col:28)
Match Combine:({W:(0123...) ";"}) at loc 26(1,27)
Exception raised:Expected ";" (at char 30), (line:1, col:31)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 30(1,31)
Exception raised:Expected lineStart (at char 31), (line:1, col:32)
Match Combine:({W:(0123...) ";"}) at loc 30(1,31)
Exception raised:Expected ";" (at char 36), (line:1, col:37)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 36(1,37)
Exception raised:Expected lineStart (at char 37), (line:1, col:38)
Match Combine:({W:(0123...) ";"}) at loc 36(1,37)
Exception raised:Expected ";" (at char 40), (line:1, col:41)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 40(1,41)
Exception raised:Expected lineStart (at char 41), (line:1, col:42)
Match Combine:({W:(0123...) ";"}) at loc 40(1,41)
Exception raised:Expected ";" (at char 45), (line:1, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 45(1,1)
Exception raised:Expected lineStart (at char 45), (line:1, col:1)
Match Combine:({W:(0123...) ";"}) at loc 45(1,1)
Exception raised:Expected ";" (at char 52), (line:2, col:7)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 52(2,7)
Exception raised:Expected lineStart (at char 53), (line:2, col:8)
Match Combine:({W:(0123...) ";"}) at loc 52(2,7)
Exception raised:Expected ";" (at char 58), (line:2, col:13)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 58(2,13)
Exception raised:Expected lineStart (at char 59), (line:2, col:14)
Match Combine:({W:(0123...) ";"}) at loc 58(2,13)
Exception raised:Expected ";" (at char 62), (line:2, col:17)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 62(2,17)
Exception raised:Expected lineStart (at char 63), (line:2, col:18)
Match Combine:({W:(0123...) ";"}) at loc 62(2,17)
Exception raised:Expected ";" (at char 65), (line:2, col:20)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 65(2,20)
Exception raised:Expected lineStart (at char 66), (line:2, col:21)
Match Combine:({W:(0123...) ";"}) at loc 65(2,20)
Exception raised:Expected ";" (at char 72), (line:2, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 72(2,1)
Exception raised:Expected lineStart (at char 72), (line:2, col:1)
Match Combine:({W:(0123...) ";"}) at loc 72(2,1)
Exception raised:Expected ";" (at char 77), (line:3, col:5)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 77(3,5)
Exception raised:Expected lineStart (at char 78), (line:3, col:6)
Match Combine:({W:(0123...) ";"}) at loc 77(3,5)
Exception raised:Expected ";" (at char 81), (line:3, col:9)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 81(3,9)
Exception raised:Expected lineStart (at char 82), (line:3, col:10)
Match Combine:({W:(0123...) ";"}) at loc 81(3,9)
Exception raised:Expected ";" (at char 89), (line:3, col:17)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 89(3,17)
Exception raised:Expected lineStart (at char 90), (line:3, col:18)
Match Combine:({W:(0123...) ";"}) at loc 89(3,17)
Exception raised:Expected ";" (at char 94), (line:3, col:22)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 94(3,22)
Exception raised:Expected lineStart (at char 95), (line:3, col:23)
Match Combine:({W:(0123...) ";"}) at loc 94(3,22)
Exception raised:Expected ";" (at char 98), (line:3, col:26)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 98(3,26)
Exception raised:Expected lineStart (at char 99), (line:3, col:27)
Match Combine:({W:(0123...) ";"}) at loc 98(3,26)
Exception raised:Expected ";" (at char 103), (line:3, col:31)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 103(3,31)
Exception raised:Expected lineStart (at char 104), (line:3, col:32)
Match Combine:({W:(0123...) ";"}) at loc 103(3,31)
Exception raised:Expected ";" (at char 109), (line:3, col:37)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 109(3,37)
Exception raised:Expected lineStart (at char 110), (line:3, col:38)
Match Combine:({W:(0123...) ";"}) at loc 109(3,37)
Exception raised:Expected ";" (at char 112), (line:3, col:40)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 112(3,40)
Exception raised:Expected lineStart (at char 113), (line:3, col:41)
Match Combine:({W:(0123...) ";"}) at loc 112(3,40)
Exception raised:Expected ";" (at char 116), (line:3, col:44)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 116(3,44)
Exception raised:Expected lineStart (at char 117), (line:3, col:45)
Match Combine:({W:(0123...) ";"}) at loc 116(3,44)
Exception raised:Expected ";" (at char 121), (line:3, col:49)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 121(3,49)
Exception raised:Expected lineStart (at char 122), (line:3, col:50)
Match Combine:({W:(0123...) ";"}) at loc 121(3,49)
Exception raised:Expected ";" (at char 131), (line:3, col:59)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 131(3,59)
Exception raised:Expected lineStart (at char 132), (line:3, col:60)
Match Combine:({W:(0123...) ";"}) at loc 131(3,59)
Exception raised:Expected ";" (at char 135), (line:3, col:63)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 135(3,63)
Exception raised:Expected lineStart (at char 136), (line:3, col:64)
Match Combine:({W:(0123...) ";"}) at loc 135(3,63)
Exception raised:Expected ";" (at char 138), (line:3, col:66)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 138(3,66)
Exception raised:Expected lineStart (at char 139), (line:3, col:67)
Match Combine:({W:(0123...) ";"}) at loc 138(3,66)
Exception raised:Expected ";" (at char 142), (line:3, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 142(3,1)
Exception raised:Expected lineStart (at char 142), (line:3, col:1)
Match Combine:({W:(0123...) ";"}) at loc 142(3,1)
Exception raised:Expected ";" (at char 152), (line:4, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 152(4,1)
Exception raised:Expected lineStart (at char 152), (line:4, col:1)
Match Combine:({W:(0123...) ";"}) at loc 152(4,1)
Matched Combine:({W:(0123...) ";"}) -> ['south;']
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 153(5,1)
Exception raised:Expected Re:('LEFT\\ OFF') (at char 153), (line:5, col:1)
Match Combine:({W:(0123...) ";"}) at loc 153(5,1)
Matched Combine:({W:(0123...) ";"}) -> ['south;']
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 159(5,7)
Exception raised:Expected lineStart (at char 160), (line:5, col:8)
Match Combine:({W:(0123...) ";"}) at loc 159(5,7)
Exception raised:Expected ";" (at char 163), (line:5, col:11)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 163(5,11)
Exception raised:Expected lineStart (at char 164), (line:5, col:12)
Match Combine:({W:(0123...) ";"}) at loc 163(5,11)
Exception raised:Expected ";" (at char 170), (line:5, col:18)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 170(5,18)
Exception raised:Expected lineStart (at char 171), (line:5, col:19)
Match Combine:({W:(0123...) ";"}) at loc 170(5,18)
Exception raised:Expected ";" (at char 178), (line:5, col:26)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 178(5,26)
Exception raised:Expected lineStart (at char 179), (line:5, col:27)
Match Combine:({W:(0123...) ";"}) at loc 178(5,26)
Exception raised:Expected ";" (at char 187), (line:5, col:35)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 187(5,35)
Exception raised:Expected lineStart (at char 188), (line:5, col:36)
Match Combine:({W:(0123...) ";"}) at loc 187(5,35)
Exception raised:Expected ";" (at char 192), (line:5, col:40)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 192(5,40)
Exception raised:Expected lineStart (at char 193), (line:5, col:41)
Match Combine:({W:(0123...) ";"}) at loc 192(5,40)
Exception raised:Expected ";" (at char 198), (line:5, col:46)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 198(5,46)
Exception raised:Expected lineStart (at char 199), (line:5, col:47)
Match Combine:({W:(0123...) ";"}) at loc 198(5,46)
Exception raised:Expected ";" (at char 202), (line:5, col:50)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 202(5,50)
Exception raised:Expected lineStart (at char 203), (line:5, col:51)
Match Combine:({W:(0123...) ";"}) at loc 202(5,50)
Exception raised:Expected ";" (at char 206), (line:5, col:54)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 206(5,54)
Exception raised:Expected lineStart (at char 207), (line:5, col:55)
Match Combine:({W:(0123...) ";"}) at loc 206(5,54)
Exception raised:Expected ";" (at char 211), (line:5, col:59)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 211(5,59)
Exception raised:Expected lineStart (at char 212), (line:5, col:60)
Match Combine:({W:(0123...) ";"}) at loc 211(5,59)
Exception raised:Expected ";" (at char 214), (line:5, col:62)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 214(5,62)
Exception raised:Expected lineStart (at char 215), (line:5, col:63)
Match Combine:({W:(0123...) ";"}) at loc 214(5,62)
Exception raised:Expected ";" (at char 220), (line:5, col:68)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 220(5,68)
Exception raised:Expected lineStart (at char 221), (line:5, col:69)
Match Combine:({W:(0123...) ";"}) at loc 220(5,68)
Exception raised:Expected ";" (at char 223), (line:5, col:71)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 223(5,71)
Exception raised:Expected lineStart (at char 224), (line:5, col:72)
Match Combine:({W:(0123...) ";"}) at loc 223(5,71)
Exception raised:Expected ";" (at char 229), (line:5, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 229(5,1)
Exception raised:Expected lineStart (at char 229), (line:5, col:1)
Match Combine:({W:(0123...) ";"}) at loc 229(5,1)
Exception raised:Expected ";" (at char 232), (line:6, col:3)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 232(6,3)
Exception raised:Expected lineStart (at char 233), (line:6, col:4)
Match Combine:({W:(0123...) ";"}) at loc 232(6,3)
Exception raised:Expected ";" (at char 236), (line:6, col:7)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 236(6,7)
Exception raised:Expected lineStart (at char 237), (line:6, col:8)
Match Combine:({W:(0123...) ";"}) at loc 236(6,7)
Exception raised:Expected ";" (at char 246), (line:6, col:17)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 246(6,17)
Exception raised:Expected lineStart (at char 247), (line:6, col:18)
Match Combine:({W:(0123...) ";"}) at loc 246(6,17)
Exception raised:Expected ";" (at char 256), (line:6, col:27)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 256(6,27)
Exception raised:Expected lineStart (at char 257), (line:6, col:28)
Match Combine:({W:(0123...) ";"}) at loc 256(6,27)
Exception raised:Expected ";" (at char 260), (line:6, col:31)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 260(6,31)
Exception raised:Expected lineStart (at char 261), (line:6, col:32)
Match Combine:({W:(0123...) ";"}) at loc 260(6,31)
Exception raised:Expected ";" (at char 266), (line:6, col:37)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 266(6,37)
Exception raised:Expected lineStart (at char 267), (line:6, col:38)
Match Combine:({W:(0123...) ";"}) at loc 266(6,37)
Exception raised:Expected ";" (at char 270), (line:6, col:41)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 270(6,41)
Exception raised:Expected lineStart (at char 271), (line:6, col:42)
Match Combine:({W:(0123...) ";"}) at loc 270(6,41)
Exception raised:Expected ";" (at char 273), (line:6, col:44)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 273(6,44)
Exception raised:Expected lineStart (at char 274), (line:6, col:45)
Match Combine:({W:(0123...) ";"}) at loc 273(6,44)
Exception raised:Expected ";" (at char 280), (line:6, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 280(6,1)
Exception raised:Expected lineStart (at char 280), (line:6, col:1)
Match Combine:({W:(0123...) ";"}) at loc 280(6,1)
Exception raised:Expected ";" (at char 288), (line:7, col:8)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 288(7,8)
Exception raised:Expected lineStart (at char 289), (line:7, col:9)
Match Combine:({W:(0123...) ";"}) at loc 288(7,8)
Exception raised:Expected ";" (at char 291), (line:7, col:11)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 291(7,11)
Exception raised:Expected lineStart (at char 292), (line:7, col:12)
Match Combine:({W:(0123...) ";"}) at loc 291(7,11)
Exception raised:Expected ";" (at char 296), (line:7, col:16)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 296(7,16)
Exception raised:Expected lineStart (at char 297), (line:7, col:17)
Match Combine:({W:(0123...) ";"}) at loc 296(7,16)
Exception raised:Expected ";" (at char 306), (line:7, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 306(7,1)
Exception raised:Expected lineStart (at char 306), (line:7, col:1)
Match Combine:({W:(0123...) ";"}) at loc 306(7,1)
Matched Combine:({W:(0123...) ";"}) -> ['wEst;']
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 307(8,1)
Exception raised:Expected Re:('LEFT\\ OFF') (at char 307), (line:8, col:1)
Match Combine:({W:(0123...) ";"}) at loc 307(8,1)
Matched Combine:({W:(0123...) ";"}) -> ['wEst;']
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 312(8,6)
Exception raised:Expected lineStart (at char 313), (line:8, col:7)
Match Combine:({W:(0123...) ";"}) at loc 312(8,6)
... deleted so i could post the question
Exception raised:Expected lineStart (at char 384), (line:9, col:15)
Match Combine:({W:(0123...) ";"}) at loc 383(9,14)
Exception raised:Expected ";" (at char 389), (line:9, col:20)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 389(9,20)
Exception raised:Expected lineStart (at char 390), (line:9, col:21)
Match Combine:({W:(0123...) ";"}) at loc 389(9,20)
Exception raised:Expected ";" (at char 393), (line:9, col:24)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 393(9,24)
Exception raised:Expected lineStart (at char 394), (line:9, col:25)
Match Combine:({W:(0123...) ";"}) at loc 393(9,24)
Exception raised:Expected ";" (at char 396), (line:9, col:27)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 396(9,27)
Exception raised:Expected lineStart (at char 397), (line:9, col:28)
Match Combine:({W:(0123...) ";"}) at loc 396(9,27)
Exception raised:Expected ";" (at char 403), (line:9, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 403(9,1)
Exception raised:Expected lineStart (at char 403), (line:9, col:1)
Match Combine:({W:(0123...) ";"}) at loc 403(9,1)
Exception raised:Expected W:(0123...) (at char 404), (line:10, col:1)
Match Combine:({lineStart Re:('LEFT\\ OFF')}) at loc 404(10,1)
Exception raised:Expected Re:('LEFT\\ OFF') (at char 404), (line:10, col:1)
Match Combine:({W:(0123...) ";"}) at loc 404(10,1)
Exception raised:Expected W:(0123...) (at char 404), (line:10, col:1)
token groups: 4
each result in tokens: ['look;', 'We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest']
- ACTION: look;
- BODY: We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest
each result in tokens: ['south;', 'The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction']
- ACTION: south;
- BODY: The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction
each result in tokens: ['wEst;', 'a rocky gorge ascends up from a dry branch of the creek']
- ACTION: wEst;
- BODY: a rocky gorge ascends up from a dry branch of the creek
each result in tokens: ['quote;', 'An old rusty pan is nearby']
- ACTION: quote;
- BODY: An old rusty pan is nearby
Test2: Tokens found [['look;', 'We come to a fork in the creek and must decide which way to travel LEFT OFF Another path was seen prior to the fork branching off to the northwest'], ['south;', 'The stream rapidly descends over rocks and the roar of water is heard in the distance. Curiosity may drive one to pursue further in this direction'], ['wEst;', 'a rocky gorge ascends up from a dry branch of the creek'], ['quote;', 'An old rusty pan is nearby']]
Process finished with exit code 0
正如可以看到的那样,'LEFT OFF'没有按预期解析出来。
答案 0 :(得分:1)
这个问题非常有效。
源代码将这两种行为显示为两个单独的测试:
第一次测试没有新的“LEFT OFF”关键字,即:只使用后跟“;”的任何单词,这可以按预期工作
第二个测试,称为“TEST2”,是在添加一个必须遵循这种语法的新唯一关键字之后:一行中的前两个单词必须完全“LEFT OFF”
“为什么?”是不重要的。事实是 TEST2失败,这是一个引人入胜的问题。
问题似乎来自一线:
otherAction = Combine(lineStart + oneOf(exceptions))('ACTION')
由于此快速解决方法替换使应用有效:
otherAction = oneOf(exceptions)('ACTION')
虽然这在任何地方都匹配“LEFT OFF”,而不是如定义那样只在一行的开头。
Mr.McGuire ,您认为描述此“otherAction”符合要求的方式是什么?