prism 0.18.0 → 0.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -1
- data/README.md +2 -1
- data/config.yml +188 -55
- data/docs/building.md +9 -2
- data/docs/configuration.md +10 -9
- data/docs/encoding.md +24 -56
- data/docs/local_variable_depth.md +229 -0
- data/docs/ruby_api.md +2 -0
- data/docs/serialization.md +18 -13
- data/ext/prism/api_node.c +337 -195
- data/ext/prism/extconf.rb +13 -7
- data/ext/prism/extension.c +96 -32
- data/ext/prism/extension.h +1 -1
- data/include/prism/ast.h +340 -137
- data/include/prism/defines.h +17 -0
- data/include/prism/diagnostic.h +11 -5
- data/include/prism/encoding.h +248 -0
- data/include/prism/options.h +2 -2
- data/include/prism/parser.h +62 -42
- data/include/prism/regexp.h +2 -2
- data/include/prism/util/pm_buffer.h +9 -1
- data/include/prism/util/pm_memchr.h +2 -2
- data/include/prism/util/pm_strpbrk.h +3 -3
- data/include/prism/version.h +2 -2
- data/include/prism.h +13 -15
- data/lib/prism/compiler.rb +12 -0
- data/lib/prism/debug.rb +9 -4
- data/lib/prism/desugar_compiler.rb +3 -3
- data/lib/prism/dispatcher.rb +56 -0
- data/lib/prism/dot_visitor.rb +476 -198
- data/lib/prism/dsl.rb +66 -46
- data/lib/prism/ffi.rb +16 -3
- data/lib/prism/lex_compat.rb +19 -9
- data/lib/prism/mutation_compiler.rb +20 -0
- data/lib/prism/node.rb +1173 -450
- data/lib/prism/node_ext.rb +41 -16
- data/lib/prism/parse_result.rb +12 -15
- data/lib/prism/ripper_compat.rb +49 -34
- data/lib/prism/serialize.rb +242 -212
- data/lib/prism/visitor.rb +12 -0
- data/lib/prism.rb +20 -4
- data/prism.gemspec +4 -10
- data/rbi/prism.rbi +605 -230
- data/rbi/prism_static.rbi +3 -0
- data/sig/prism.rbs +379 -124
- data/sig/prism_static.rbs +1 -0
- data/src/diagnostic.c +228 -222
- data/src/encoding.c +5137 -0
- data/src/node.c +66 -0
- data/src/options.c +21 -2
- data/src/prettyprint.c +806 -406
- data/src/prism.c +1092 -700
- data/src/regexp.c +3 -3
- data/src/serialize.c +227 -157
- data/src/util/pm_buffer.c +10 -1
- data/src/util/pm_memchr.c +1 -1
- data/src/util/pm_strpbrk.c +4 -4
- metadata +5 -11
- data/include/prism/enc/pm_encoding.h +0 -227
- data/src/enc/pm_big5.c +0 -116
- data/src/enc/pm_cp51932.c +0 -57
- data/src/enc/pm_euc_jp.c +0 -69
- data/src/enc/pm_gbk.c +0 -65
- data/src/enc/pm_shift_jis.c +0 -57
- data/src/enc/pm_tables.c +0 -2073
- data/src/enc/pm_unicode.c +0 -2369
- data/src/enc/pm_windows_31j.c +0 -57
data/docs/encoding.md
CHANGED
@@ -16,12 +16,24 @@ The key of the comment can be either "encoding" or "coding". The value of the co
|
|
16
16
|
* `Big5`
|
17
17
|
* `Big5-HKSCS`
|
18
18
|
* `Big5-UAO`
|
19
|
+
* `CESU-8`
|
19
20
|
* `CP51932`
|
20
21
|
* `CP850`
|
21
22
|
* `CP852`
|
22
23
|
* `CP855`
|
24
|
+
* `CP949`
|
25
|
+
* `CP950`
|
26
|
+
* `CP951`
|
27
|
+
* `Emacs-Mule`
|
23
28
|
* `EUC-JP`
|
29
|
+
* `eucJP-ms`
|
30
|
+
* `EUC-JIS-2004`
|
31
|
+
* `EUC-KR`
|
32
|
+
* `EUC-TW`
|
33
|
+
* `GB12345`
|
34
|
+
* `GB18030`
|
24
35
|
* `GB1988`
|
36
|
+
* `GB2312`
|
25
37
|
* `GBK`
|
26
38
|
* `IBM437`
|
27
39
|
* `IBM720`
|
@@ -33,6 +45,7 @@ The key of the comment can be either "encoding" or "coding". The value of the co
|
|
33
45
|
* `IBM860`
|
34
46
|
* `IBM861`
|
35
47
|
* `IBM862`
|
48
|
+
* `IBM863`
|
36
49
|
* `IBM864`
|
37
50
|
* `IBM865`
|
38
51
|
* `IBM866`
|
@@ -53,21 +66,31 @@ The key of the comment can be either "encoding" or "coding". The value of the co
|
|
53
66
|
* `ISO-8859-15`
|
54
67
|
* `ISO-8859-16`
|
55
68
|
* `KOI8-R`
|
69
|
+
* `KOI8-U`
|
56
70
|
* `macCentEuro`
|
57
71
|
* `macCroatian`
|
58
72
|
* `macCyrillic`
|
59
73
|
* `macGreek`
|
60
74
|
* `macIceland`
|
75
|
+
* `MacJapanese`
|
61
76
|
* `macRoman`
|
62
77
|
* `macRomania`
|
63
78
|
* `macThai`
|
64
79
|
* `macTurkish`
|
65
80
|
* `macUkraine`
|
66
81
|
* `Shift_JIS`
|
82
|
+
* `SJIS-DoCoMo`
|
83
|
+
* `SJIS-KDDI`
|
84
|
+
* `SJIS-SoftBank`
|
85
|
+
* `stateless-ISO-2022-JP`
|
86
|
+
* `stateless-ISO-2022-JP-KDDI`
|
67
87
|
* `TIS-620`
|
68
88
|
* `US-ASCII`
|
69
89
|
* `UTF-8`
|
70
90
|
* `UTF8-MAC`
|
91
|
+
* `UTF8-DoCoMo`
|
92
|
+
* `UTF8-KDDI`
|
93
|
+
* `UTF8-SoftBank`
|
71
94
|
* `Windows-1250`
|
72
95
|
* `Windows-1251`
|
73
96
|
* `Windows-1252`
|
@@ -80,62 +103,7 @@ The key of the comment can be either "encoding" or "coding". The value of the co
|
|
80
103
|
* `Windows-31J`
|
81
104
|
* `Windows-874`
|
82
105
|
|
83
|
-
For each of these encodings, prism provides
|
84
|
-
|
85
|
-
## Support for other encodings
|
86
|
-
|
87
|
-
If an encoding is encountered that is not supported by prism, prism will call a user-provided callback function with the name of the encoding if one is provided. That function can be registered with `pm_parser_register_encoding_decode_callback`. The user-provided callback function can then provide a pointer to an encoding struct that contains the requisite functions that prism will use to parse identifiers going forward.
|
88
|
-
|
89
|
-
If the user-provided callback function returns `NULL` (the value also provided by the default implementation in case a callback was not registered), an error will be added to the parser's error list and parsing will continue on using the default UTF-8 encoding.
|
90
|
-
|
91
|
-
```c
|
92
|
-
// This struct defines the functions necessary to implement the encoding
|
93
|
-
// interface so we can determine how many bytes the subsequent character takes.
|
94
|
-
// Each callback should return the number of bytes, or 0 if the next bytes are
|
95
|
-
// invalid for the encoding and type.
|
96
|
-
typedef struct {
|
97
|
-
// Return the number of bytes that the next character takes if it is valid
|
98
|
-
// in the encoding. Does not read more than n bytes. It is assumed that n is
|
99
|
-
// at least 1.
|
100
|
-
size_t (*char_width)(const uint8_t *b, ptrdiff_t n);
|
101
|
-
|
102
|
-
// Return the number of bytes that the next character takes if it is valid
|
103
|
-
// in the encoding and is alphabetical. Does not read more than n bytes. It
|
104
|
-
// is assumed that n is at least 1.
|
105
|
-
size_t (*alpha_char)(const uint8_t *b, ptrdiff_t n);
|
106
|
-
|
107
|
-
// Return the number of bytes that the next character takes if it is valid
|
108
|
-
// in the encoding and is alphanumeric. Does not read more than n bytes. It
|
109
|
-
// is assumed that n is at least 1.
|
110
|
-
size_t (*alnum_char)(const uint8_t *b, ptrdiff_t n);
|
111
|
-
|
112
|
-
// Return true if the next character is valid in the encoding and is an
|
113
|
-
// uppercase character. Does not read more than n bytes. It is assumed that
|
114
|
-
// n is at least 1.
|
115
|
-
bool (*isupper_char)(const uint8_t *b, ptrdiff_t n);
|
116
|
-
|
117
|
-
// The name of the encoding. This should correspond to a value that can be
|
118
|
-
// passed to Encoding.find in Ruby.
|
119
|
-
const char *name;
|
120
|
-
|
121
|
-
// Return true if the encoding is a multibyte encoding.
|
122
|
-
bool multibyte;
|
123
|
-
} pm_encoding_t;
|
124
|
-
|
125
|
-
// When an encoding is encountered that isn't understood by prism, we provide
|
126
|
-
// the ability here to call out to a user-defined function to get an encoding
|
127
|
-
// struct. If the function returns something that isn't NULL, we set that to
|
128
|
-
// our encoding and use it to parse identifiers.
|
129
|
-
typedef pm_encoding_t *(*pm_encoding_decode_callback_t)(pm_parser_t *parser, const uint8_t *name, size_t width);
|
130
|
-
|
131
|
-
// Register a callback that will be called when prism encounters a magic comment
|
132
|
-
// with an encoding referenced that it doesn't understand. The callback should
|
133
|
-
// return NULL if it also doesn't understand the encoding or it should return a
|
134
|
-
// pointer to a pm_encoding_t struct that contains the functions necessary to
|
135
|
-
// parse identifiers.
|
136
|
-
PRISM_EXPORTED_FUNCTION void
|
137
|
-
pm_parser_register_encoding_decode_callback(pm_parser_t *parser, pm_encoding_decode_callback_t callback);
|
138
|
-
```
|
106
|
+
For each of these encodings, prism provides functions for checking if the subsequent bytes can be interpreted as a character, and then if that character is alphabetic, alphanumeric, or uppercase.
|
139
107
|
|
140
108
|
## Getting notified when the encoding changes
|
141
109
|
|
@@ -0,0 +1,229 @@
|
|
1
|
+
# Local variable depth
|
2
|
+
|
3
|
+
One feature of Prism is that it resolves local variables as it parses. It's necessary to do this because of ambiguities in the grammar. For example, consider the following code:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
foo / bar#/
|
7
|
+
```
|
8
|
+
|
9
|
+
If `foo` is a local variable, this is a call to `/` with `bar` as an argument, followed by a comment. If it's not a local variable, this is a method call to `foo` with a regular expression argument.
|
10
|
+
|
11
|
+
"Depth" refers to the number of visible scopes that Prism has to go up to find the declaration of a local variable.
|
12
|
+
Note that this follows the same scoping rules as Ruby, so a local variable is only visible in the scope it is declared in and in blocks nested in that scope.
|
13
|
+
The rules for calculating the depth are very important to understand because they may differ from individual Ruby implementations since they are not specified by the language.
|
14
|
+
|
15
|
+
Prism uses the minimum number of scopes, i.e., it only creates scopes when necessary semantically, in other words when there must be distinct scopes (which can be observed through `binding.local_variables`).
|
16
|
+
That are no "transparent/invisible" scopes in Prism.
|
17
|
+
Some Ruby implementations use those for some language constructs and need to adjust by maintaining a depth offset.
|
18
|
+
|
19
|
+
Below are the places where a local variable can be written/targeted, along with how the depth is calculated at that point.
|
20
|
+
|
21
|
+
## General
|
22
|
+
|
23
|
+
In the course of general Ruby code when reading a local variable, the depth is equal to the number of scopes to go up to find the declaration of that variable. For example:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
foo = 1
|
27
|
+
bar = 2
|
28
|
+
baz = 3
|
29
|
+
|
30
|
+
foo # depth 0
|
31
|
+
tap { bar } # depth 1
|
32
|
+
tap { tap { baz } } # depth 2
|
33
|
+
```
|
34
|
+
|
35
|
+
This also includes writing to a local variable, which could be writing to a local variable that is already declared. For example:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
foo = 1
|
39
|
+
bar = 2
|
40
|
+
|
41
|
+
foo = 3 # depth 0
|
42
|
+
tap { bar = 4 } # depth 1
|
43
|
+
```
|
44
|
+
|
45
|
+
This includes multiple assignment, where the same principle applies. For example:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
foo = 1
|
49
|
+
bar = 2
|
50
|
+
|
51
|
+
foo, bar = 3, 4 # depth 0
|
52
|
+
tap { foo, bar = 5, 6 } # depth 1
|
53
|
+
```
|
54
|
+
|
55
|
+
## `for` loops
|
56
|
+
|
57
|
+
`for` loops in Ruby break down to calls to `.each` with a block.
|
58
|
+
However in that case local variable reads and writes within the block will be in the same scope as the scope surrounding the `for` and not in a deeper/separate scope (surprising, but this is Ruby semantics).
|
59
|
+
For example:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
foo = 1
|
63
|
+
|
64
|
+
for e in baz
|
65
|
+
foo # depth 0
|
66
|
+
bar = 2 # depth 0
|
67
|
+
end
|
68
|
+
|
69
|
+
p bar # depth 0, prints 2
|
70
|
+
```
|
71
|
+
|
72
|
+
The local variable(s) used for the index of the `for` are also at the same depth (as variables inside and outside the `for`):
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
for e in [1, 2] # depth 0
|
76
|
+
e # depth 0
|
77
|
+
end
|
78
|
+
|
79
|
+
p e # depth 0, prints 2
|
80
|
+
```
|
81
|
+
|
82
|
+
## Pattern matching captures
|
83
|
+
|
84
|
+
You can target a local variable in a pattern matching expression using capture syntax. Using this syntax, you can target local variables in the current scope or in visible parent scopes. For example:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
42 => bar # depth 0
|
88
|
+
```
|
89
|
+
|
90
|
+
The example above writes to a local variable in the current scope. If the variable is already declared in a higher visible scope, it will be written to that scope instead. For example:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
foo = 1
|
94
|
+
tap { 42 => foo } # depth 1
|
95
|
+
```
|
96
|
+
|
97
|
+
## Named capture groups
|
98
|
+
|
99
|
+
You can target local variables through named capture groups in regular expressions if they are used on the left-hand side of a `=~` operator. For example:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
/(?<foo>\d+)/ =~ "42" # depth 0
|
103
|
+
```
|
104
|
+
|
105
|
+
This will write to a `foo` local variable. If the variable is already declared in a higher visible scope, it will be written to that scope instead. For example:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
foo = 1
|
109
|
+
tap { /(?<foo>\d+)/ =~ "42" } # depth 1
|
110
|
+
```
|
111
|
+
|
112
|
+
## "interpolated once" regular expressions
|
113
|
+
|
114
|
+
Regular expressions that interpolate local variables (unrelated to capture group local variables) and have the `o` flag will only interpolate the local variables once for the runtime of the program.
|
115
|
+
In CRuby, this is implemented by compiling the regular expression within a nested instruction sequence, which means CRuby thinks the depth is one more than prism does. For example:
|
116
|
+
|
117
|
+
```
|
118
|
+
$ ruby --dump=insns -e 'foo = 1; /#{foo}/o'
|
119
|
+
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,18)> (catch: false)
|
120
|
+
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
|
121
|
+
[ 1] foo@0
|
122
|
+
0000 putobject_INT2FIX_1_ ( 1)[Li]
|
123
|
+
0001 setlocal_WC_0 foo@0
|
124
|
+
0003 once block in <main>, <is:0>
|
125
|
+
0006 leave
|
126
|
+
|
127
|
+
== disasm: #<ISeq:block in <main>@-e:1 (1,9)-(1,18)> (catch: false)
|
128
|
+
0000 putobject "" ( 1)
|
129
|
+
0002 getlocal_WC_1 foo@0
|
130
|
+
0004 dup
|
131
|
+
0005 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
|
132
|
+
0007 anytostring
|
133
|
+
0008 toregexp 0, 2
|
134
|
+
0011 leave
|
135
|
+
```
|
136
|
+
|
137
|
+
In this case CRuby fetches the local variable with `getlocal_WC_1` as the second instruction to the "once" instruction sequence. When compiling CRuby, prism therefore will adjust the depth to account for this difference.
|
138
|
+
|
139
|
+
## `rescue` clauses
|
140
|
+
|
141
|
+
In CRuby, `rescue` clauses are implemented as their own instruction sequence, and therefore CRuby thinks the depth is one more than prism does. For example:
|
142
|
+
|
143
|
+
```
|
144
|
+
$ ruby --dump=insns -e 'begin; foo = 1; rescue; foo; end'
|
145
|
+
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,32)> (catch: true)
|
146
|
+
== catch table
|
147
|
+
| catch type: rescue st: 0000 ed: 0004 sp: 0000 cont: 0005
|
148
|
+
| == disasm: #<ISeq:rescue in <main>@-e:1 (1,16)-(1,28)> (catch: true)
|
149
|
+
| local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
|
150
|
+
| [ 1] $!@0
|
151
|
+
| 0000 getlocal_WC_0 $!@0 ( 1)
|
152
|
+
| 0002 putobject StandardError
|
153
|
+
| 0004 checkmatch 3
|
154
|
+
| 0006 branchunless 11
|
155
|
+
| 0008 getlocal_WC_1 foo@0[Li]
|
156
|
+
| 0010 leave
|
157
|
+
| 0011 getlocal_WC_0 $!@0
|
158
|
+
| 0013 throw 0
|
159
|
+
| catch type: retry st: 0004 ed: 0005 sp: 0000 cont: 0000
|
160
|
+
|------------------------------------------------------------------------
|
161
|
+
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
|
162
|
+
[ 1] foo@0
|
163
|
+
0000 putobject_INT2FIX_1_ ( 1)[Li]
|
164
|
+
0001 dup
|
165
|
+
0002 setlocal_WC_0 foo@0
|
166
|
+
0004 nop
|
167
|
+
0005 leave
|
168
|
+
```
|
169
|
+
|
170
|
+
In the catch table, CRuby is reading the `foo` local variable using `getlocal_WC_1` as the fifth instruction to the "rescue" instruction sequence. When compiling CRuby, prism therefore will adjust the depth to account for this difference.
|
171
|
+
|
172
|
+
Note that this includes the error reference, which can target local variables, as in:
|
173
|
+
|
174
|
+
```
|
175
|
+
$ ruby --dump=insns -e 'foo = 1; begin; rescue => foo; end'
|
176
|
+
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,34)> (catch: true)
|
177
|
+
== catch table
|
178
|
+
| catch type: rescue st: 0003 ed: 0004 sp: 0000 cont: 0005
|
179
|
+
| == disasm: #<ISeq:rescue in <main>@-e:1 (1,16)-(1,30)> (catch: true)
|
180
|
+
| local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
|
181
|
+
| [ 1] $!@0
|
182
|
+
| 0000 getlocal_WC_0 $!@0 ( 1)
|
183
|
+
| 0002 putobject StandardError
|
184
|
+
| 0004 checkmatch 3
|
185
|
+
| 0006 branchunless 14
|
186
|
+
| 0008 getlocal_WC_0 $!@0
|
187
|
+
| 0010 setlocal_WC_1 foo@0
|
188
|
+
| 0012 putnil
|
189
|
+
| 0013 leave
|
190
|
+
| 0014 getlocal_WC_0 $!@0
|
191
|
+
| 0016 throw 0
|
192
|
+
| catch type: retry st: 0004 ed: 0005 sp: 0000 cont: 0003
|
193
|
+
|------------------------------------------------------------------------
|
194
|
+
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
|
195
|
+
[ 1] foo@0
|
196
|
+
0000 putobject_INT2FIX_1_ ( 1)[Li]
|
197
|
+
0001 setlocal_WC_0 foo@0
|
198
|
+
0003 putnil
|
199
|
+
0004 nop
|
200
|
+
0005 leave
|
201
|
+
```
|
202
|
+
|
203
|
+
Note that CRuby is writing to the `foo` local variable using the `setlocal_WC_1` instruction as the sixth instruction to the "rescue" instruction sequence. When compiling CRuby, prism therefore will adjust the depth to account for this difference.
|
204
|
+
|
205
|
+
## Post execution blocks
|
206
|
+
|
207
|
+
The `END {}` syntax allows executing code when the program exits. In CRuby, this is implemented as two nested instruction sequences. CRuby therefore thinks the depth is two more than prism does. For example:
|
208
|
+
|
209
|
+
```
|
210
|
+
$ ruby --dump=insns -e 'foo = 1; END { foo }'
|
211
|
+
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,20)> (catch: false)
|
212
|
+
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
|
213
|
+
[ 1] foo@0
|
214
|
+
0000 putobject_INT2FIX_1_ ( 1)[Li]
|
215
|
+
0001 setlocal_WC_0 foo@0
|
216
|
+
0003 once block in <main>, <is:0>
|
217
|
+
0006 leave
|
218
|
+
|
219
|
+
== disasm: #<ISeq:block in <main>@-e:0 (0,0)-(-1,-1)> (catch: false)
|
220
|
+
0000 putspecialobject 1 ( 1)
|
221
|
+
0002 send <calldata!mid:core#set_postexe, argc:0, FCALL>, block in <main>
|
222
|
+
0005 leave
|
223
|
+
|
224
|
+
== disasm: #<ISeq:block in <main>@-e:1 (1,9)-(1,20)> (catch: false)
|
225
|
+
0000 getlocal foo@0, 2 ( 1)[LiBc]
|
226
|
+
0003 leave [Br]
|
227
|
+
```
|
228
|
+
|
229
|
+
In the instruction sequence corresponding to the code that gets executed inside the `END` block, CRuby is reading the `foo` local variable using `getlocal` as the second instruction to the `"block in <main>"` instruction sequence. When compiling CRuby, prism therefore will adjust the depth to account for this difference.
|
data/docs/ruby_api.md
CHANGED
@@ -25,6 +25,8 @@ The full API is documented below.
|
|
25
25
|
* `Prism.load(source, serialized)` - load the serialized syntax tree using the source as a reference into a syntax tree
|
26
26
|
* `Prism.parse_comments(source)` - parse the comments corresponding to the given source string and return them
|
27
27
|
* `Prism.parse_file_comments(source)` - parse the comments corresponding to the given source file and return them
|
28
|
+
* `Prism.parse_success?(source)` - parse the syntax tree corresponding to the given source string and return true if it was parsed without errors
|
29
|
+
* `Prism.parse_file_success?(filepath)` - parse the syntax tree corresponding to the given source file and return true if it was parsed without errors
|
28
30
|
|
29
31
|
## Nodes
|
30
32
|
|
data/docs/serialization.md
CHANGED
@@ -9,24 +9,28 @@ The syntax tree still requires a copy of the original source, as for the most pa
|
|
9
9
|
|
10
10
|
Let us define some simple types for readability.
|
11
11
|
|
12
|
-
###
|
12
|
+
### varuint
|
13
13
|
|
14
|
-
A variable-length integer with the value fitting in `uint32_t` using between 1 and 5 bytes, using the [LEB128](https://en.wikipedia.org/wiki/LEB128) encoding.
|
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
15
|
This drastically cuts down on the size of the serialized string, especially when the source file is large.
|
16
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
|
+
|
17
21
|
### string
|
18
22
|
|
19
23
|
| # bytes | field |
|
20
24
|
| --- | --- |
|
21
|
-
|
|
25
|
+
| varuint | the length of the string in bytes |
|
22
26
|
| ... | the string bytes |
|
23
27
|
|
24
28
|
### location
|
25
29
|
|
26
30
|
| # bytes | field |
|
27
31
|
| --- | --- |
|
28
|
-
|
|
29
|
-
|
|
32
|
+
| varuint | byte offset into the source string where this location begins |
|
33
|
+
| varuint | length of the location in bytes in the source string |
|
30
34
|
|
31
35
|
### comment
|
32
36
|
|
@@ -34,7 +38,6 @@ The comment type is one of:
|
|
34
38
|
|
35
39
|
* 0=`INLINE` (`# comment`)
|
36
40
|
* 1=`EMBEDDED_DOCUMENT` (`=begin`/`=end`)
|
37
|
-
* 2=`__END__` (after `__END__`)
|
38
41
|
|
39
42
|
| # bytes | field |
|
40
43
|
| --- | --- |
|
@@ -72,17 +75,18 @@ The header is structured like the following table:
|
|
72
75
|
| `1` | patch version number |
|
73
76
|
| `1` | 1 indicates only semantics fields were serialized, 0 indicates all fields were serialized (including location fields) |
|
74
77
|
| string | the encoding name |
|
75
|
-
|
|
76
|
-
|
|
78
|
+
| varsint | the start line |
|
79
|
+
| varuint | number of comments |
|
77
80
|
| comment* | comments |
|
78
|
-
|
|
81
|
+
| varuint | number of magic comments |
|
79
82
|
| magic comment* | magic comments |
|
80
|
-
|
|
83
|
+
| location? | the optional location of the `__END__` keyword and its contents |
|
84
|
+
| varuint | number of errors |
|
81
85
|
| diagnostic* | errors |
|
82
|
-
|
|
86
|
+
| varuint | number of warnings |
|
83
87
|
| diagnostic* | warnings |
|
84
88
|
| `4` | content pool offset |
|
85
|
-
|
|
89
|
+
| varuint | content pool size |
|
86
90
|
|
87
91
|
After the header comes the body of the serialized string.
|
88
92
|
The body consists of a sequence of nodes that is built using a prefix traversal order of the syntax tree.
|
@@ -103,6 +107,7 @@ Every field on the node is then appended to the serialized string. The fields ca
|
|
103
107
|
* `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.
|
104
108
|
* `location` - A field that is a location. This is structured as a variable-length integer start followed by a variable-length integer length.
|
105
109
|
* `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.
|
110
|
+
* `uint8` - A field that is an 8-bit unsigned integer. This is structured as a single byte.
|
106
111
|
* `uint32` - A field that is a 32-bit unsigned integer. This is structured as a variable-length integer.
|
107
112
|
|
108
113
|
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.
|
@@ -159,7 +164,7 @@ serialize(const uint8_t *source, size_t length) {
|
|
159
164
|
}
|
160
165
|
```
|
161
166
|
|
162
|
-
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 `
|
167
|
+
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:
|
163
168
|
|
164
169
|
| # bytes | field |
|
165
170
|
| ------- | -------------------------- |
|