1+ //! Easy to work with AST parsing and util.
2+ //!
3+ //! The AST is stored as an ECS like structure
4+ //! This allows fast queries over all features groups etc.
5+ //! Features, Attributes, Imports and Directories are stored in a typed radix tree.
6+ //! The radix tree is represented via a map (sym0,name,ty) -> sym1
7+ //! where ty is the type of sym1. Using this representation lowers total
8+ //! memory consumption by a nice 20%
9+
110use crate :: core:: * ;
211use check:: ErrorInfo ;
312use hashbrown:: HashMap ;
@@ -18,13 +27,7 @@ mod transform;
1827mod visitor;
1928pub use def:: * ;
2029pub use visitor:: * ;
21- //Easy to work with AST parsing and util.
22- //The AST is stored as an ECS like structure
23- //This allows fast queries over all features groups etc.
24- //Features, Attributes, Imports and Directories are stored in a typed radix tree.
25- //The radix tree is represented via a map (sym0,name,ty) -> sym1
26- //where ty is the type of sym1. Using this representation lowers total
27- //memory consumption by a nice 20%
30+
2831pub fn uri_to_path ( uri : & Url ) -> Option < Vec < Ustr > > {
2932 let mut p = uri. to_file_path ( ) . ok ( ) ?;
3033 p. set_extension ( "" ) ;
4750 }
4851}
4952
50- //1->N parent child relation
53+ /// 1->N parent child relation
5154#[ derive( Default , Debug , Clone ) ]
5255struct TreeMap {
5356 children : HashMap < Symbol , Vec < Symbol > > ,
@@ -59,7 +62,7 @@ impl TreeMap {
5962 self . parent . insert ( child, parent) ;
6063 }
6164}
62- //Ast container each symbol kind lives in its own vector
65+ /// Ast container each symbol kind lives in its own vector
6366#[ derive( Clone , Debug , Default ) ]
6467struct Ast {
6568 keywords : Vec < Keyword > ,
@@ -90,7 +93,7 @@ impl Ast {
9093 _ => unimplemented ! ( ) ,
9194 }
9295 }
93- //call f for each child under sym and prefix
96+ /// call f for each child under sym and prefix
9497 fn lookup < F : FnMut ( Symbol ) > ( & self , sym : Symbol , prefix : Ustr , mut f : F ) {
9598 match sym {
9699 Symbol :: Root => {
@@ -143,7 +146,7 @@ impl Ast {
143146 fn lsp_range ( & self , sym : Symbol , source : & Rope ) -> Option < tower_lsp:: lsp_types:: Range > {
144147 self . span ( sym) . and_then ( |s| lsp_range ( s, source) )
145148 }
146- //source range for a symbol if there is any
149+ /// source range for a symbol if there is any
147150 fn span ( & self , sym : Symbol ) -> Option < Span > {
148151 match sym {
149152 Symbol :: Feature ( i) => Some ( self . features [ i] . name . span . clone ( ) ) ,
@@ -171,7 +174,7 @@ impl Ast {
171174 . into_iter ( )
172175 . flat_map ( |v| v. iter ( ) . cloned ( ) )
173176 }
174- //utility iterators over different elements of interest
177+ /// utility iterators over different elements of interest
175178 fn all_imports ( & self ) -> impl Iterator < Item = Symbol > + DoubleEndedIterator {
176179 ( 0 ..self . import . len ( ) ) . map ( Symbol :: Import )
177180 }
@@ -217,7 +220,7 @@ impl Ast {
217220 fn all_lang_lvls ( & self ) -> impl Iterator < Item = Symbol > {
218221 ( 0 ..self . includes . len ( ) ) . map ( Symbol :: LangLvl )
219222 }
220- //Search a symbol by byte offset in O(N)
223+ /// Search a symbol by byte offset in O(N)
221224 fn find ( & self , offset : usize ) -> Option < Symbol > {
222225 self . all_imports ( )
223226 . chain ( self . all_features ( ) )
@@ -250,7 +253,7 @@ impl Ast {
250253 return relatives;
251254 }
252255}
253- //Combines the AST with metadata, this is also a public interface to the AST.
256+ /// Combines the AST with metadata, this is also a public interface to the AST.
254257#[ derive( Clone , Debug ) ]
255258pub struct AstDocument {
256259 ast : Ast ,
@@ -270,7 +273,7 @@ impl AstDocument {
270273 self . ast . structure . parent . get ( & sym) . cloned ( )
271274 }
272275 }
273- //parent feature of an attribute
276+ /// parent feature of an attribute
274277 pub fn scope ( & self , mut sym : Symbol ) -> Symbol {
275278 while let Some ( p) = self . parent ( sym, true ) {
276279 match sym {
@@ -285,7 +288,7 @@ impl AstDocument {
285288 pub fn name ( & self , sym : Symbol ) -> Option < Ustr > {
286289 self . ast . name ( sym)
287290 }
288- //iterators over diffrent symbole types in the tree
291+ /// iterators over diffrent symbole types in the tree
289292 pub fn all_lang_lvls ( & self ) -> impl Iterator < Item = Symbol > {
290293 self . ast . all_lang_lvls ( )
291294 }
@@ -396,8 +399,8 @@ impl AstDocument {
396399 _ => 0 ,
397400 }
398401 }
399- //Find all symboles under root with prefix path.
400- //Search branches can be aborted with a filter
402+ /// Find all symboles under root with prefix path.
403+ /// Search branches can be aborted with a filter
401404 pub fn lookup < ' a , F : Fn ( Symbol ) -> bool + ' a > (
402405 & ' a self ,
403406 root : Symbol ,
@@ -417,7 +420,7 @@ impl AstDocument {
417420 } )
418421 } )
419422 }
420- //Find imports for any prefix of path
423+ /// Find imports for any prefix of path
421424 pub fn lookup_import < ' a > (
422425 & ' a self ,
423426 path : & ' a [ Ustr ] ,
@@ -441,7 +444,7 @@ impl AstDocument {
441444 }
442445 } )
443446 }
444- //Also track the binding for path, each segment of path has some symbole bound to it
447+ /// Also track the binding for path, each segment of path has some symbole bound to it
445448 pub fn lookup_with_binding < ' a , F : Fn ( Symbol ) -> bool + ' a > (
446449 & ' a self ,
447450 root : Symbol ,
@@ -461,7 +464,7 @@ impl AstDocument {
461464 } )
462465 } )
463466 }
464- // returns all Symbols with same name, used for cardinality resolving
467+ /// returns all Symbols with same name, used for cardinality resolving
465468 pub fn get_all_entities ( & self , path : & [ Ustr ] ) -> Vec < Symbol > {
466469 let ustr_path = Ustr :: from ( path. iter ( ) . map ( |s| s. to_string ( ) ) . join ( "." ) . as_str ( ) ) ;
467470 let mut res = vec ! [ ] ;
@@ -510,7 +513,7 @@ impl AstDocument {
510513 res
511514 }
512515
513- //prefix of sym from root
516+ /// prefix of sym from root
514517 pub fn prefix ( & self , mut sym : Symbol ) -> Vec < Ustr > {
515518 if matches ! ( sym, Symbol :: Import ( ..) ) {
516519 return self . ast . import_prefix ( sym) . into ( ) ;
@@ -551,7 +554,7 @@ impl AstDocument {
551554 pub fn find ( & self , offset : usize ) -> Option < Symbol > {
552555 self . ast . find ( offset)
553556 }
554- //All children under root, when merge_root_features sub features are ignored
557+ /// All children under root, when merge_root_features sub features are ignored
555558 pub fn visit_named_children < F : FnMut ( Symbol , & [ Ustr ] ) -> bool > (
556559 & self ,
557560 root : Symbol ,
@@ -560,7 +563,7 @@ impl AstDocument {
560563 ) {
561564 self . visit_named_children_depth ( root, merge_root_features, |sym, prefix, _| f ( sym, prefix) )
562565 }
563- //Iterate all named symbole under root and track their prefix from root
566+ /// Iterate all named symbole under root and track their prefix from root
564567 pub fn visit_named_children_depth < F : FnMut ( Symbol , & [ Ustr ] , usize ) -> bool > (
565568 & self ,
566569 root : Symbol ,
@@ -612,7 +615,7 @@ impl AstDocument {
612615 pub fn find_all_of ( & self , name : Ustr ) -> Vec < Symbol > {
613616 self . ast . find_all_of ( Symbol :: Root , name)
614617 }
615- //Iterate all named symbole under root
618+ /// Iterate all named symbole under root
616619 pub fn visit_children_depth < F : FnMut ( Symbol , u32 ) -> bool > (
617620 & self ,
618621 root : Symbol ,
@@ -645,7 +648,7 @@ impl AstDocument {
645648 }
646649 }
647650 }
648- //Iterate all attributes under root, and track theire associated feature
651+ /// Iterate all attributes under root, and track theire associated feature
649652 pub fn visit_attributes < ' a , F : FnMut ( Symbol , Symbol , & [ Ustr ] ) > ( & self , root : Symbol , mut f : F ) {
650653 assert ! ( matches!( root, Symbol :: Feature ( ..) | Symbol :: Root ) ) ;
651654 let mut owner = root;
0 commit comments