A Magic: The Gathering (MTG) formal grammar and oracle text parser. The goal of this project is to parse human readable MTG abilities into a strongly typed abstract syntax tree (AST).
There are many motivations behind such a project, but first, take a look at the live demo ! You can enter a card's oracle text (or go for a random card), and watch the parser create the ability tree for that card. If the card's name is referenced anywhere in the oracle text, you will also need to input it below for the lexer to recognize it.
Below the inputs, you will see the preprocessed oracle text, where all instances of the card's name have been replaced with the special character "~". Below, in blue are the tokens that are built from the oracle text. Then, in orange, are the nodes of the ability tree composing from tokens and each other to create the full ability tree. Each tree node spans horizontally the input that was required to build it. The goal is to build an ability tree node, representing full and valid oracle text's abilities.
While pure text rulings are nice for humans, it is quite hard for a computer to make sense of the abilities of MTG cards while only reading text. Converting this text into an AST makes this task way easier, since trees are data structures that are easy to work with from a machine standpoint. Generating AST for cards allows for all kind of automation on such cards, like generating code to handle a card's effects, make statistics, or look for subtrees to find cards that synergise well with each other (hello, Hexxed).
The project is built in Rust, for it's strong type system and expressiveness that matches well the complexity of MTG abilities. The documentation for the project can be found here.
As of me writing this, the entire project is still very much in the prototyping phase, and can currently only handle 2k of the ~37k cards in MTG. The goal is to be able to parse every single card. The coverage is kept up to date on the README of the project's repo.