aboutsummaryrefslogtreecommitdiff
path: root/rfc.md
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2017-12-22 13:56:22 +0000
committerAleksey Kladov <[email protected]>2017-12-22 13:56:22 +0000
commit9726ecdc99a35467af4aa9f375821805d55e4869 (patch)
treed2cbd6ce7b1640042acf79912be61da00fbf82cb /rfc.md
parent6ff019c25f027be1bf2896ce82659dc8d99515f8 (diff)
Add an example
Diffstat (limited to 'rfc.md')
-rw-r--r--rfc.md64
1 files changed, 61 insertions, 3 deletions
diff --git a/rfc.md b/rfc.md
index 1476cbaf2..dfba7cb2c 100644
--- a/rfc.md
+++ b/rfc.md
@@ -83,7 +83,8 @@ source code because, for example, it's important to preserve comments
83during refactorings. Ideally, IDEs should be able to incrementally 83during refactorings. Ideally, IDEs should be able to incrementally
84relex and reparse the file as the user types, because syntax tree is 84relex and reparse the file as the user types, because syntax tree is
85necessary to correctly handle certain code-editing actions like 85necessary to correctly handle certain code-editing actions like
86autoindentation or joining lines. 86autoindentation or joining lines. IDE also must be able to produce
87partial parse trees when some input is missing or invalid.
87 88
88Currently rustc uses the AST approach, which preserves the source code 89Currently rustc uses the AST approach, which preserves the source code
89information to some extent by storing spans in the AST. 90information to some extent by storing spans in the AST.
@@ -117,12 +118,69 @@ the actual data about identifier names, constant values etc.
117All nodes in the tree are of the same type and store a constant for 118All nodes in the tree are of the same type and store a constant for
118the syntactic category of the element and a range in the source code. 119the syntactic category of the element and a range in the source code.
119 120
120Here is a minimal implementation of this data structure: 121Here is a minimal implementation of this data structure with some Rust
122syntactic categories
121 123
122 124
123```Rust 125```rust
124``` 126```
125 127
128Here is a rust snippet and the corresponding parse tree:
129
130```rust
131struct Foo {
132 field1: u32,
133 &
134 // non-doc comment
135 field2:
136}
137```
138
139
140```
141FILE
142 STRUCT_DEF
143 STRUCT_KW
144 WHITESPACE
145 IDENT
146 WHITESPACE
147 L_CURLY
148 WHITESPACE
149 FIELD_DEF
150 IDENT
151 COLON
152 WHITESPACE
153 TYPE
154 IDENT
155 COMMA
156 WHITESPACE
157 ERROR
158 AMP
159 WHITESPACE
160 FIELD_DEF
161 LINE_COMMENT
162 WHITESPACE
163 IDENT
164 COLON
165 ERROR
166 WHITESPACE
167 R_CURLY
168```
169
170Note several features of the tree:
171
172* All whitespace and comments are explicitly accounted for.
173
174* The node for `STRUCT_DEF` contains the error element for `&`, but
175 still represents the following field correctly.
176
177* The second field of the struct is incomplete: `FIELD_DEF` node for
178 it contains an `ERROR` element, but nevertheless has the correct
179 `NodeKind`.
180
181* The non-documenting comment is correctly attached to the following
182 field.
183
126 184
127 185
128# Drawbacks 186# Drawbacks