adventurelib icon indicating copy to clipboard operation
adventurelib copied to clipboard

Backtracking parser

Open lordmauve opened this issue 6 years ago • 2 comments

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.

lordmauve avatar Jul 28 '18 12:07 lordmauve

Maybe parse module could help: https://pypi.org/project/parse/

adrianogil avatar Apr 27 '20 13:04 adrianogil

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.

lordmauve avatar Apr 27 '20 16:04 lordmauve