1

ほとんどのキーワードが見つけやすく、いくつかのキーワードが見つけにくいテキストがあります。すべてのキーワードは、存在する場合は常に行内で左揃えになり、セミコロンで終了します。ただし、時折誰かが指摘することがあります: ああ、あなたはキーワードを逃しました。そのような場合、別のタイプのキーワードを構築する必要があります。そして、文字列が行の他の場所に表示された場合 (ラップされているか、通常のテキストとして扱うことになっているものは何でも.

補足として、出力に「例外が発生しました」という致命的ではないエラーがたくさんあることに気づきました。私が知る限り、文法に何かがあることを期待しているように見え、それができなかったと自動的に報告します見つからない。私の場合、私は|を持っていると思います。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」は期待どおりに解析されませんでした。

4

1 に答える 1

1

質問は非常に有効です。

ソース コードは、2 つの動作を 2 つの別個のテストとして示しています。

  1. 最初のテストでは、新しい「LEFT OFF」キーワードを使用しません。つまり、任意の単語の後に「;」を使用するだけで、期待どおりに機能します。

  2. 「TEST2」と呼ばれる 2 番目のテストは、次の構文に従う必要がある新しい一意のキーワードを追加した後です。行の最初の 2 つの単語は、正確に「LEFT OFF」でなければなりません。

    "どうして?" 重要ではありません。実際、TEST2 は失敗します。これは興味深い質問です。

問題は次の行にあるようです:

otherAction = Combine(lineStart + oneOf(exceptions))('ACTION')

この簡単な回避策の置き換えにより、アプリが機能するようになります。

otherAction = oneOf(exceptions)('ACTION')

ただし、これはどこでも「LEFT OFF」に一致し、定義されているように、行頭だけでは一致しません。

McGuire さん、要件を満たすためにこの「otherAction」をどのように記述すればよいと思いますか?

于 2013-08-22T11:26:44.470 に答える