@@ -7,6 +7,7 @@ use std::cmp;
77mod file_builder;
88pub use self :: file_builder:: { FileBuilder , Sink } ;
99
10+ /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.
1011#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
1112pub struct SyntaxKind ( pub ( crate ) u32 ) ;
1213
@@ -37,19 +38,25 @@ pub(crate) struct SyntaxInfo {
3738 pub name : & ' static str ,
3839}
3940
41+ /// A token of Rust source.
4042#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
4143pub struct Token {
44+ /// The kind of token.
4245 pub kind : SyntaxKind ,
46+ /// The length of the token.
4347 pub len : TextUnit ,
4448}
4549
50+ /// The contents of a Rust source file.
51+ #[ derive( Debug ) ]
4652pub struct File {
4753 text : String ,
4854 nodes : Vec < NodeData > ,
4955 errors : Vec < SyntaxErrorData > ,
5056}
5157
5258impl File {
59+ /// The root node of this source file.
5360 pub fn root < ' f > ( & ' f self ) -> Node < ' f > {
5461 assert ! ( !self . nodes. is_empty( ) ) ;
5562 Node {
@@ -59,35 +66,42 @@ impl File {
5966 }
6067}
6168
69+ /// A reference to a token in a Rust source file.
6270#[ derive( Clone , Copy ) ]
6371pub struct Node < ' f > {
6472 file : & ' f File ,
6573 idx : NodeIdx ,
6674}
6775
6876impl < ' f > Node < ' f > {
77+ /// The kind of the token at this node.
6978 pub fn kind ( & self ) -> SyntaxKind {
7079 self . data ( ) . kind
7180 }
7281
82+ /// The text range covered by the token at this node.
7383 pub fn range ( & self ) -> TextRange {
7484 self . data ( ) . range
7585 }
7686
87+ /// The text at this node.
7788 pub fn text ( & self ) -> & ' f str {
7889 & self . file . text . as_str ( ) [ self . range ( ) ]
7990 }
8091
92+ /// The parent node to this node.
8193 pub fn parent ( & self ) -> Option < Node < ' f > > {
8294 self . as_node ( self . data ( ) . parent )
8395 }
8496
97+ /// The children nodes of this node.
8598 pub fn children ( & self ) -> Children < ' f > {
8699 Children {
87100 next : self . as_node ( self . data ( ) . first_child ) ,
88101 }
89102 }
90103
104+ /// Any errors contained in this node.
91105 pub fn errors ( & self ) -> SyntaxErrors < ' f > {
92106 let pos = self . file . errors . iter ( ) . position ( |e| e. node == self . idx ) ;
93107 let next = pos. map ( |i| ErrorIdx ( i as u32 ) ) . map ( |idx| SyntaxError {
@@ -123,7 +137,7 @@ impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> {
123137
124138impl < ' f > cmp:: Eq for Node < ' f > { }
125139
126- #[ derive( Clone , Copy ) ]
140+ #[ derive( Clone , Copy , Debug ) ]
127141pub struct SyntaxError < ' f > {
128142 file : & ' f File ,
129143 idx : ErrorIdx ,
@@ -162,6 +176,7 @@ impl<'f> SyntaxError<'f> {
162176 }
163177}
164178
179+ #[ derive( Debug ) ]
165180pub struct Children < ' f > {
166181 next : Option < Node < ' f > > ,
167182}
@@ -176,6 +191,7 @@ impl<'f> Iterator for Children<'f> {
176191 }
177192}
178193
194+ #[ derive( Debug ) ]
179195pub struct SyntaxErrors < ' f > {
180196 next : Option < SyntaxError < ' f > > ,
181197}
@@ -190,9 +206,10 @@ impl<'f> Iterator for SyntaxErrors<'f> {
190206 }
191207}
192208
193- #[ derive( Clone , Copy , PartialEq , Eq ) ]
209+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
194210struct NodeIdx ( u32 ) ;
195211
212+ #[ derive( Debug ) ]
196213struct NodeData {
197214 kind : SyntaxKind ,
198215 range : TextRange ,
@@ -215,9 +232,10 @@ impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> {
215232 }
216233}
217234
218- #[ derive( Clone , Copy ) ]
235+ #[ derive( Clone , Copy , Debug ) ]
219236struct ErrorIdx ( u32 ) ;
220237
238+ #[ derive( Debug ) ]
221239struct SyntaxErrorData {
222240 node : NodeIdx ,
223241 message : String ,
0 commit comments