From 9726ecdc99a35467af4aa9f375821805d55e4869 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Dec 2017 16:56:22 +0300 Subject: [PATCH] Add an example --- minirust.rs | 17 ++++++++++++++ rfc.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/minirust.rs b/minirust.rs index d92c03bea8..009892ca91 100644 --- a/minirust.rs +++ b/minirust.rs @@ -66,3 +66,20 @@ impl<'f> Iterator for Children<'f> { next } } + + + +pub const ERROR: NodeKind = NodeKind(0); +pub const WHITESPACE: NodeKind = NodeKind(1); +pub const STRUCT_KW: NodeKind = NodeKind(2); +pub const IDENT: NodeKind = NodeKind(3); +pub const L_CURLY: NodeKind = NodeKind(4); +pub const R_CURLY: NodeKind = NodeKind(5); +pub const COLON: NodeKind = NodeKind(6); +pub const COMMA: NodeKind = NodeKind(7); +pub const AMP: NodeKind = NodeKind(8); +pub const LINE_COMMENT: NodeKind = NodeKind(9); +pub const FILE: NodeKind = NodeKind(10); +pub const STRUCT_DEF: NodeKind = NodeKind(11); +pub const FIELD_DEF: NodeKind = NodeKind(12); +pub const TYPE: NodeKind = NodeKind(13); diff --git a/rfc.md b/rfc.md index 1476cbaf21..dfba7cb2c1 100644 --- a/rfc.md +++ b/rfc.md @@ -83,7 +83,8 @@ source code because, for example, it's important to preserve comments during refactorings. Ideally, IDEs should be able to incrementally relex and reparse the file as the user types, because syntax tree is necessary to correctly handle certain code-editing actions like -autoindentation or joining lines. +autoindentation or joining lines. IDE also must be able to produce +partial parse trees when some input is missing or invalid. Currently rustc uses the AST approach, which preserves the source code information to some extent by storing spans in the AST. @@ -117,12 +118,69 @@ the actual data about identifier names, constant values etc. All nodes in the tree are of the same type and store a constant for the syntactic category of the element and a range in the source code. -Here is a minimal implementation of this data structure: +Here is a minimal implementation of this data structure with some Rust +syntactic categories -```Rust +```rust ``` +Here is a rust snippet and the corresponding parse tree: + +```rust +struct Foo { + field1: u32, + & + // non-doc comment + field2: +} +``` + + +``` +FILE + STRUCT_DEF + STRUCT_KW + WHITESPACE + IDENT + WHITESPACE + L_CURLY + WHITESPACE + FIELD_DEF + IDENT + COLON + WHITESPACE + TYPE + IDENT + COMMA + WHITESPACE + ERROR + AMP + WHITESPACE + FIELD_DEF + LINE_COMMENT + WHITESPACE + IDENT + COLON + ERROR + WHITESPACE + R_CURLY +``` + +Note several features of the tree: + +* All whitespace and comments are explicitly accounted for. + +* The node for `STRUCT_DEF` contains the error element for `&`, but + still represents the following field correctly. + +* The second field of the struct is incomplete: `FIELD_DEF` node for + it contains an `ERROR` element, but nevertheless has the correct + `NodeKind`. + +* The non-documenting comment is correctly attached to the following + field. + # Drawbacks