diff options
author | Aleksey Kladov <[email protected]> | 2017-12-22 13:56:22 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2017-12-22 13:56:22 +0000 |
commit | 9726ecdc99a35467af4aa9f375821805d55e4869 (patch) | |
tree | d2cbd6ce7b1640042acf79912be61da00fbf82cb | |
parent | 6ff019c25f027be1bf2896ce82659dc8d99515f8 (diff) |
Add an example
-rw-r--r-- | minirust.rs | 17 | ||||
-rw-r--r-- | rfc.md | 64 |
2 files changed, 78 insertions, 3 deletions
diff --git a/minirust.rs b/minirust.rs index d92c03bea..009892ca9 100644 --- a/minirust.rs +++ b/minirust.rs | |||
@@ -66,3 +66,20 @@ impl<'f> Iterator for Children<'f> { | |||
66 | next | 66 | next |
67 | } | 67 | } |
68 | } | 68 | } |
69 | |||
70 | |||
71 | |||
72 | pub const ERROR: NodeKind = NodeKind(0); | ||
73 | pub const WHITESPACE: NodeKind = NodeKind(1); | ||
74 | pub const STRUCT_KW: NodeKind = NodeKind(2); | ||
75 | pub const IDENT: NodeKind = NodeKind(3); | ||
76 | pub const L_CURLY: NodeKind = NodeKind(4); | ||
77 | pub const R_CURLY: NodeKind = NodeKind(5); | ||
78 | pub const COLON: NodeKind = NodeKind(6); | ||
79 | pub const COMMA: NodeKind = NodeKind(7); | ||
80 | pub const AMP: NodeKind = NodeKind(8); | ||
81 | pub const LINE_COMMENT: NodeKind = NodeKind(9); | ||
82 | pub const FILE: NodeKind = NodeKind(10); | ||
83 | pub const STRUCT_DEF: NodeKind = NodeKind(11); | ||
84 | pub const FIELD_DEF: NodeKind = NodeKind(12); | ||
85 | pub const TYPE: NodeKind = NodeKind(13); | ||
@@ -83,7 +83,8 @@ source code because, for example, it's important to preserve comments | |||
83 | during refactorings. Ideally, IDEs should be able to incrementally | 83 | during refactorings. Ideally, IDEs should be able to incrementally |
84 | relex and reparse the file as the user types, because syntax tree is | 84 | relex and reparse the file as the user types, because syntax tree is |
85 | necessary to correctly handle certain code-editing actions like | 85 | necessary to correctly handle certain code-editing actions like |
86 | autoindentation or joining lines. | 86 | autoindentation or joining lines. IDE also must be able to produce |
87 | partial parse trees when some input is missing or invalid. | ||
87 | 88 | ||
88 | Currently rustc uses the AST approach, which preserves the source code | 89 | Currently rustc uses the AST approach, which preserves the source code |
89 | information to some extent by storing spans in the AST. | 90 | information to some extent by storing spans in the AST. |
@@ -117,12 +118,69 @@ the actual data about identifier names, constant values etc. | |||
117 | All nodes in the tree are of the same type and store a constant for | 118 | All nodes in the tree are of the same type and store a constant for |
118 | the syntactic category of the element and a range in the source code. | 119 | the syntactic category of the element and a range in the source code. |
119 | 120 | ||
120 | Here is a minimal implementation of this data structure: | 121 | Here is a minimal implementation of this data structure with some Rust |
122 | syntactic categories | ||
121 | 123 | ||
122 | 124 | ||
123 | ```Rust | 125 | ```rust |
124 | ``` | 126 | ``` |
125 | 127 | ||
128 | Here is a rust snippet and the corresponding parse tree: | ||
129 | |||
130 | ```rust | ||
131 | struct Foo { | ||
132 | field1: u32, | ||
133 | & | ||
134 | // non-doc comment | ||
135 | field2: | ||
136 | } | ||
137 | ``` | ||
138 | |||
139 | |||
140 | ``` | ||
141 | FILE | ||
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 | |||
170 | Note 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 |