|
| 1 | +# qapi-schema |
| 2 | + |
| 3 | +Package qapi-schema is a fully compliant[^1] QAPI[^2] schema language parser. |
| 4 | +The QAPI schema language looks approximately like JSON, but it differs |
| 5 | +slightly in many ways, which can confuse a regular JSON parser. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +If you want to parse QAPI schema from your Go code, all you have to do |
| 10 | +is called `qapischema.Parse`: |
| 11 | + |
| 12 | +```go |
| 13 | +input := `{ 'struct': 'DiskThing', |
| 14 | + 'data': { |
| 15 | + 'diskname': { |
| 16 | + 'type':'str', |
| 17 | + 'if':'DISKS_HAVE_NAMES' } } }` |
| 18 | +schema, _ := qapischema.Parse(input) |
| 19 | +``` |
| 20 | + |
| 21 | +The above code snippet would produce a `*qapischema.Tree` that looks like this: |
| 22 | + |
| 23 | +```txt |
| 24 | +&qapischema.Tree{ |
| 25 | + Node: qapischema.Root{ }, |
| 26 | + Children: []*qapischema.Tree{ |
| 27 | + { |
| 28 | + Node: &qapischema.Struct{ |
| 29 | + Name: "DiskThing", |
| 30 | + Members: []qapischema.Member{ |
| 31 | + { |
| 32 | + Name: "diskname", |
| 33 | + Type: qapischema.TypeRef{ |
| 34 | + Type: "str", |
| 35 | + }, |
| 36 | + If: &qapischema.Cond{ |
| 37 | + If: &qapischema.CondIf("DISKS_HAVE_NAMES"), |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Once the QAPI has been parsed, you can walk the `*qapischema.Tree` and do |
| 48 | +whatever it is that you need to do. |
| 49 | + |
| 50 | +The `Node` field in `*qapischema.Tree` is an interface type, and so a type |
| 51 | +assertion is required to identify and access the QAPI-type-specific data |
| 52 | +in the node you're visiting within the tree. |
| 53 | + |
| 54 | +```go |
| 55 | +func visit(tree *qapischema.Tree) { |
| 56 | + switch data := tree.Node.(type) { |
| 57 | + // Root node, no data, traverse the subtrees in the .Children field. |
| 58 | + case qapischema.Root: |
| 59 | + case qapischema.Include: |
| 60 | + case qapischema.Pragma: |
| 61 | + case *qapischema.Struct: |
| 62 | + case *qapischema.Union: |
| 63 | + case *qapischema.Event: |
| 64 | + case *qapischema.Command: |
| 65 | + case *qapischema.Alternate: |
| 66 | + } |
| 67 | + |
| 68 | + // Process the rest of the document |
| 69 | + for _, t := range tree.Children { |
| 70 | + visit(t) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func main() { |
| 75 | + tree, _ := qapischema.Parse(input) |
| 76 | + |
| 77 | + visit(tree) |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Reporting defects |
| 82 | + |
| 83 | +There is a lot of room for improvement with how this parser emits diagnostic |
| 84 | +information. That is, it doesn't emit any at all. The parser will simply stop |
| 85 | +parsing when it's not able to parse something. It won't complain, it will just |
| 86 | +stop. |
| 87 | + |
| 88 | +So, when it comes to identifying which part of the document the parser did not |
| 89 | +understand, just compare the input schema to the output until you find the |
| 90 | +first element in the input schema that is missing from the parse tree. |
| 91 | + |
| 92 | +There are two utilities included in this module: `qapilex` and `qapiparse`. |
| 93 | + |
| 94 | +`qapiparse` parses QAPI from its stdin and prints a pretty string representation |
| 95 | +of the parse tree to stdout. This can be very helpful for figuring out where the |
| 96 | +parser stopped. |
| 97 | + |
| 98 | +In your bug report, please include the QAPI input that surfaced the failure to |
| 99 | +parse. If possible, try to reduce the QAPI input down to a minimal viable |
| 100 | +reproducer. |
| 101 | + |
| 102 | +## Acknowledgements |
| 103 | + |
| 104 | +Many thanks to: |
| 105 | + |
| 106 | +* Thorsten Ball, the author of _Writing an Interpreter in Go_[^3]. The lessons |
| 107 | + in that book's chapter on lexing were directly applied to create package |
| 108 | + `internal/lex`. |
| 109 | +* Jeremy Brown, whose GopherCon 2022 talk[^4] demonstrated simple and elegant |
| 110 | + ways to write parser combinators in Go, which directly inspired much of |
| 111 | + package `internal/parse`. |
| 112 | + |
| 113 | +[^1]: At least, it's intended to be fully compliant. If it is not, please |
| 114 | +file a bug. |
| 115 | + |
| 116 | +[^2]: https://qemu.readthedocs.io/en/latest/devel/qapi-code-gen.html#introduction |
| 117 | + |
| 118 | +[^3]: https://interpreterbook.com/ |
| 119 | + |
| 120 | +[^4]: [GopherCon 2022: Jeremy Brown - Parsing w/o Code Generators: Parser Combinators in Go with Generics]( |
| 121 | +https://www.youtube.com/watch?v=x5p_SJNRB4U) |
0 commit comments