lukehutch / pikaparser

The Pika Parser reference implementation
MIT License
141 stars 12 forks source link

CharSeq does not match if the character sequence ends at eof #3

Closed wrandelshofer closed 4 years ago

wrandelshofer commented 4 years ago

I have made the following grammar description:

Program <- "R"/"U"/"F"/"L"/"D"/"B";

I expected, that this grammar would be able to parse the following input String:

input = "B";

However the returned MemoTable did not match any clause.

The MemoTable did match a clause, when I added an additional character to the String, for example a space character.

input = "B ";

Similarly, the following input only found one matching clause, but I expected two:

input = "RB";

Again, adding another character at the end of the input String helped.

input = "RB ";

I suspect, that there is a problem in method match of class CharSeq.

    @Override
    public Match match(MatchDirection matchDirection, MemoTable memoTable, MemoKey memoKey, String input) {
        if (memoKey.startPos < input.length() - str.length()
                && input.regionMatches(ignoreCase, memoKey.startPos, str, 0, str.length())) {
            // Terminals are not memoized (i.e. don't look in the memo table)
            return new Match(memoKey, /* firstMatchingSubClauseIdx = */ 0, /* len = */ str.length(),
                    Match.NO_SUBCLAUSE_MATCHES);
        }
        return null;
    }

By changing the if-condition to <= instead of <, I can achieve the desired results.

        if (memoKey.startPos <= input.length() - str.length()
lukehutch commented 4 years ago

Good find! Thank you, fixed.