mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 03:45:04 +00:00
Add an example
This commit is contained in:
parent
6ff019c25f
commit
9726ecdc99
2 changed files with 78 additions and 3 deletions
17
minirust.rs
17
minirust.rs
|
@ -66,3 +66,20 @@ impl<'f> Iterator for Children<'f> {
|
||||||
next
|
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);
|
||||||
|
|
64
rfc.md
64
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
|
during refactorings. Ideally, IDEs should be able to incrementally
|
||||||
relex and reparse the file as the user types, because syntax tree is
|
relex and reparse the file as the user types, because syntax tree is
|
||||||
necessary to correctly handle certain code-editing actions like
|
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
|
Currently rustc uses the AST approach, which preserves the source code
|
||||||
information to some extent by storing spans in the AST.
|
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
|
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.
|
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
|
# Drawbacks
|
||||||
|
|
Loading…
Reference in a new issue