Adding a different type of keyword to a grammar and then getting it to recognize it

StackOverflow https://stackoverflow.com/questions/18085428

  •  23-06-2022
  •  | 
  •  

Вопрос

I have a some text where most of the keywords are easy to find and some are difficult to find. All keywords are always left justified on a line if present and are terminated with a semicolon. However occasionally someone will point out: OH you missed a keyword and it will be some phrase (left justified on a line) that isn't terminated with a semicolon. In those cases I have to build up a different type of keyword. And if the string shows up anywhere else in a line (wrapped or whatever I'm supposed to just treat it as normal text.

As a side note: I've also noticed in the output I get a lot of 'Exception Raised' non fatal errors where as far as I can tell it looks like its expecting something to be there in the grammar and just automatically reports it couldn't find it. and I think in my case because I have | pyparsing treats it as a 'don't care, even though it misses my otherAction (see below) at the wrong point.

My grammar is a follow on of my previous question and handles the standard keyword just fine but when I try adding in a different pattern of the keyword it misses it and instead includes it as a body of a different action. I assume someone will tell me when I miss one of these special cases so the end of the body is a standard keyword ending with a semicolon at the start of a line.

the code:

#!/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

the output:

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

As one can see 'LEFT OFF' wasn't parsed out as expected.

Это было полезно?

Решение

The question is very valid.

Source code shows the two behaviors as two separate tests:

  1. first test is without the new "LEFT OFF" keyword, that is: just using any word followed by ";", which does work as expected

  2. the second test, referred to as "TEST2" is after adding a new unique keyword that must follow this syntax: first two words in a line must be exactly "LEFT OFF"

    "Why?" is unimportant. The fact is TEST2 fails, that's the intrigging question.

The problem seems to come from line:

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

since this quick workaround replacement makes the app work:

otherAction = oneOf(exceptions)('ACTION')

though this matches "LEFT OFF" anywhere, and not, as defined, only at the beginning of a line.

Mr.McGuire, what would you think is the way to describe this "otherAction" to meet the requirement?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top