adventurelib
adventurelib copied to clipboard
Backtracking parser
Adventurelib's command parser is inefficient when there are 2+ wildcards in a command. It tries all possible variations of assignment to each wildcard until one matches.
A more efficient parser would greedily match words until a constraint is violated and backtrack to see if any word in the input could fix it. This means it can fail fast if no such word exists.
Maybe parse module could help: https://pypi.org/project/parse/
I don't think so. Both regular expressions and the parse
package are intended to match character-by-character. We don't need to do that because commands are defined as being word-split. So it's just a case of applying the same backtracking that regex/parse would do, but on a word-by-word basis.
Additionally, adventurelib
is a one-file module that has no dependencies outside the standard library, which is a pretty nice property for beginners/educational contexts. I'd like to preserve that property if possible.