Debian Codegraph @

docs/serialization.md

raw symbols SCIP
1 # Serialization
2
3 Prism ships with the ability to serialize a syntax tree to a single string.
4 The string can then be deserialized back into a syntax tree using a language other than C.
5 This is useful for using the parsing logic in other tools without having to write a parser in that language.
6 The syntax tree still requires a copy of the original source, as for the most part it just contains byte offsets into the source string.
7
8 ## Types
9
10 Let us define some simple types for readability.
11
12 ### varuint
13
14 A variable-length unsigned integer with the value fitting in `uint32_t` using between 1 and 5 bytes, using the [LEB128](https://en.wikipedia.org/wiki/LEB128) encoding.
15 This drastically cuts down on the size of the serialized string, especially when the source file is large.
16
17 ### varsint
18
19 A variable-length signed integer with the value fitting in `int32_t` using between 1 and 5 bytes, using [ZigZag encoding](https://protobuf.dev/programming-guides/encoding/#signed-ints) into [LEB128].
20
21 ### string
22
23 | # bytes | field |
24 | --- | --- |
25 | varuint | the length of the string in bytes |
26 | ... | the string bytes |
27
28 ### location
29
30 | # bytes | field |
31 | --- | --- |
32 | varuint | byte offset into the source string where this location begins |
33 | varuint | length of the location in bytes in the source string |
34
35 ### comment
36
37 The comment type is one of:
38
39 * 0=`INLINE` (`# comment`)
40 * 1=`EMBEDDED_DOCUMENT` (`=begin`/`=end`)
41
42 | # bytes | field |
43 | --- | --- |
44 | `1` | comment type |
45 | location | the location in the source of this comment |
46
47 ### magic comment
48
49 | # bytes | field |
50 | --- | --- |
51 | location | the location of the key of the magic comment |
52 | location | the location of the value of the magic comment |
53
54 ### error
55
56 | # bytes | field |
57 | --- | --- |
58 | varuint | type |
59 | string | error message (ASCII-only characters) |
60 | location | the location in the source this error applies to |
61 | `1` | the level of the error: `0` for `fatal`, `1` for `argument`, `2` for `load` |
62
63 ### warning
64
65 | # bytes | field |
66 | --- | --- |
67 | varuint | type |
68 | string | warning message (ASCII-only characters) |
69 | location | the location in the source this warning applies to |
70 | `1` | the level of the warning: `0` for `default` and `1` for `verbose` |
71
72 ### integer
73
74 | # bytes | field |
75 | --- | --- |
76 | `1` | `1` if the integer is negative, `0` if the integer is positive |
77 | varuint | the number of words in this integer |
78 | varuint+ | the words of the integer, least-significant to most-significant |
79
80 ## Structure
81
82 The serialized string representing the syntax tree is composed of three parts: the header, the body, and the constant pool.
83 The header contains information like the version of prism that serialized the tree.
84 The body contains the actual nodes in the tree.
85 The constant pool contains constants that were interned while parsing.
86
87 The header is structured like the following table:
88
89 | # bytes | field |
90 | --- | --- |
91 | `5` | "PRISM" |
92 | `1` | major version number |
93 | `1` | minor version number |
94 | `1` | patch version number |
95 | `1` | 1 indicates only semantics fields were serialized, 0 indicates all fields were serialized (including location fields) |
96 | string | the encoding name |
97 | varsint | the start line |
98 | varuint | number of newline offsets |
99 | varuint* | newline offsets |
100 | varuint | number of comments |
101 | comment* | comments |
102 | varuint | number of magic comments |
103 | magic comment* | magic comments |
104 | location? | the optional location of the `__END__` keyword and its contents |
105 | varuint | number of errors |
106 | error* | errors |
107 | varuint | number of warnings |
108 | warning* | warnings |
109 | `4` | content pool offset |
110 | varuint | content pool size |
111
112 After the header comes the body of the serialized string.
113 The body consists of a sequence of nodes that is built using a prefix traversal order of the syntax tree.
114 Each node is structured like the following table:
115
116 | # bytes | field |
117 | --- | --- |
118 | `1` | node type |
119 | varuint | node identifier |
120 | location | node location |
121 | varuint | node flags |
122
123 Every field on the node is then appended to the serialized string. The fields can be determined by referencing `config.yml`. Depending on the type of field, it could take a couple of different forms, described below:
124
125 * `double` - A field that is a `double`. This is structured as a sequence of 8 bytes in native endian order.
126 * `node` - A field that is a node. This is structured just as like parent node.
127 * `node?` - A field that is a node that is optionally present. If the node is not present, then a single `0` byte will be written in its place. If it is present, then it will be structured just as like parent node.
128 * `node[]` - A field that is an array of nodes. This is structured as a variable-length integer length, followed by the child nodes themselves.
129 * `string` - A field that is a string. For example, this is used as the name of the method in a call node, since it cannot directly reference the source string (as in `@-` or `foo=`). This is structured as a variable-length integer byte length, followed by the string itself (_without_ a trailing null byte).
130 * `constant` - A variable-length integer that represents an index in the constant pool.
131 * `constant?` - An optional variable-length integer that represents an index in the constant pool. If it's not present, then a single `0` byte will be written in its place.
132 * `integer` - A field that represents an arbitrary-sized integer. The structure is listed above.
133 * `location` - A field that is a location. This is structured as a variable-length integer start followed by a variable-length integer length.
134 * `location?` - A field that is a location that is optionally present. If the location is not present, then a single `0` byte will be written in its place. If it is present, then it will be structured just like the `location` child node.
135 * `uint8` - A field that is an 8-bit unsigned integer. This is structured as a single byte.
136 * `uint32` - A field that is a 32-bit unsigned integer. This is structured as a variable-length integer.
137
138 After the syntax tree, the content pool is serialized. This is a list of constants that were referenced from within the tree. The content pool begins at the offset specified in the header. Constants can be either "owned" (in which case their contents are embedded in the serialization) or "shared" (in which case their contents represent a slice of the source string). The most significant bit of the constant indicates whether it is owned or shared.
139
140 In the case that it is owned, the constant is structured as follows:
141
142 | # bytes | field |
143 | --- | --- |
144 | `4` | the byte offset in the serialization for the contents of the constant |
145 | `4` | the byte length in the serialization |
146
147 Note that you will need to mask off the most significant bit for the byte offset in the serialization. In the case that it is shared, the constant is structured as follows:
148
149 | # bytes | field |
150 | --- | --- |
151 | `4` | the byte offset in the source string for the contents of the constant |
152 | `4` | the byte length in the source string |
153
154 After the constant pool, the contents of the owned constants are serialized. This is just a sequence of bytes that represent the contents of the constants. At the end of the serialization, the buffer is null terminated.
155
156 ## APIs
157
158 The relevant APIs and struct definitions are listed below:
159
160 ```c
161 // A pm_buffer_t is a simple memory buffer that stores data in a contiguous
162 // block of memory. It is used to store the serialized representation of a
163 // prism tree.
164 typedef struct {
165 char *value;
166 size_t length;
167 size_t capacity;
168 } pm_buffer_t;
169
170 // Free the memory associated with the buffer.
171 void pm_buffer_free(pm_buffer_t *);
172
173 // Parse and serialize the AST represented by the given source to the given
174 // buffer.
175 void pm_serialize_parse(pm_buffer_t *buffer, const uint8_t *source, size_t length, const char *data);
176 ```
177
178 Typically you would use a stack-allocated `pm_buffer_t` and call `pm_serialize_parse`, as in:
179
180 ```c
181 void
182 serialize(const uint8_t *source, size_t length) {
183 pm_buffer_t buffer = { 0 };
184 pm_serialize_parse(&buffer, source, length, NULL);
185
186 // Do something with the serialized string.
187
188 pm_buffer_free(&buffer);
189 }
190 ```
191
192 The final argument to `pm_serialize_parse` is an optional string that controls the options to the parse function. This includes all of the normal options that could be passed to `pm_parser_init` through a `pm_options_t` struct, but serialized as a string to make it easier for callers through FFI. Note that no `varuint` are used here to make it easier to produce the data for the caller, and also serialized size is less important here. The format of the data is structured as follows:
193
194 | # bytes | field |
195 | ------- | -------------------------- |
196 | `4` | the length of the filepath |
197 | ... | the filepath bytes |
198 | `4` | the line number |
199 | `4` | the length the encoding |
200 | ... | the encoding bytes |
201 | `1` | frozen string literal |
202 | `1` | command line flags |
203 | `1` | syntax version, see [pm_options_version_t](https://github.com/ruby/prism/blob/main/include/prism/options.h) for valid values |
204 | `1` | whether or not the encoding is locked (should almost always be false) |
205 | `4` | the number of scopes |
206 | ... | the scopes |
207
208 Command line flags are a bitset. By default every flag is `0`. It includes the following values:
209
210 * `0x1` - the `-a` option
211 * `0x2` - the `-e` option
212 * `0x4` - the `-l` option
213 * `0x8` - the `-n` option
214 * `0x10` - the `-p` option
215 * `0x20` - the `-x` option
216
217 Scopes are ordered from the outermost scope to the innermost one.
218
219 Each scope is laid out as follows:
220
221 | # bytes | field |
222 | ------- | -------------------------- |
223 | `4` | the number of locals |
224 | ... | the locals |
225
226 Each local is laid out as follows:
227
228 | # bytes | field |
229 | ------- | -------------------------- |
230 | `4` | the length of the local |
231 | ... | the local bytes |
232
233 The data can be `NULL` (as seen in the example above).