@@ -29,37 +29,48 @@ mod ffi {
2929 #[ namespace = "slang::syntax" ]
3030 type SyntaxTree ;
3131
32- /// Create a new persistent context (owns the Driver)
32+ /// Create a new persistent context
3333 fn new_slang_context ( ) -> UniquePtr < SlangContext > ;
3434
35- // Methods on SlangContext
36- fn add_source ( self : Pin < & mut SlangContext > , path : & str ) ;
37- fn add_include ( self : Pin < & mut SlangContext > , path : & str ) ;
38- fn add_define ( self : Pin < & mut SlangContext > , def : & str ) ;
35+ /// Set the include directories
36+ fn set_includes ( self : Pin < & mut SlangContext > , includes : & Vec < String > ) ;
37+ /// Set the preprocessor defines
38+ fn set_defines ( self : Pin < & mut SlangContext > , defines : & Vec < String > ) ;
3939
4040 /// Parse all added sources. Returns true on success.
41- fn parse ( self : Pin < & mut SlangContext > ) -> Result < bool > ;
41+ fn parse_file ( self : Pin < & mut SlangContext > , path : & str ) -> Result < SharedPtr < SyntaxTree > > ;
4242
43- /// Retrieves the number of parsed syntax trees
44- fn get_tree_count ( self : & SlangContext ) -> usize ;
43+ /// Rename names in the syntax tree with a given prefix and suffix
44+ fn rename ( tree : SharedPtr < SyntaxTree > , prefix : & str , suffix : & str )
45+ -> SharedPtr < SyntaxTree > ;
46+
47+ /// Print a specific tree
48+ fn print_tree ( tree : SharedPtr < SyntaxTree > , options : SlangPrintOpts ) -> String ;
49+ }
50+ }
4551
46- /// Retrieves a shared pointer to a specific syntax tree by index
47- fn get_tree ( self : & SlangContext , index : usize ) -> SharedPtr < SyntaxTree > ;
52+ /// A wrapper around a Slang syntax tree
53+ pub struct SyntaxTree {
54+ ptr : SharedPtr < ffi:: SyntaxTree > ,
55+ }
4856
49- /// Rename names in the syntax tree with a given prefix and suffix
50- fn rename_tree (
51- self : & SlangContext ,
52- tree : SharedPtr < SyntaxTree > ,
53- prefix : & str ,
54- suffix : & str ,
55- ) -> SharedPtr < SyntaxTree > ;
56-
57- /// Print a specific tree using the context's SourceManager
58- fn print_tree (
59- self : & SlangContext ,
60- tree : SharedPtr < SyntaxTree > ,
61- options : SlangPrintOpts ,
62- ) -> String ;
57+ impl SyntaxTree {
58+ /// Renames all names in the syntax tree with the given prefix and suffix
59+ pub fn rename ( & self , prefix : Option < & str > , suffix : Option < & str > ) -> Self {
60+ let p = prefix. unwrap_or ( "" ) ;
61+ let s = suffix. unwrap_or ( "" ) ;
62+
63+ let ptr = if p. is_empty ( ) && s. is_empty ( ) {
64+ self . ptr . clone ( )
65+ } else {
66+ ffi:: rename ( self . ptr . clone ( ) , p, s)
67+ } ;
68+
69+ SyntaxTree { ptr : ptr }
70+ }
71+
72+ pub fn display ( & self , options : SlangPrintOpts ) -> String {
73+ ffi:: print_tree ( self . ptr . clone ( ) , options)
6374 }
6475}
6576
@@ -76,62 +87,21 @@ impl SlangSession {
7687 }
7788 }
7889
79- /// Adds a source file to be parsed
80- pub fn add_source ( & mut self , path : & str ) {
81- self . ctx . pin_mut ( ) . add_source ( path) ;
82- }
83-
84- /// Adds an include directory
85- pub fn add_include ( & mut self , path : & str ) {
86- self . ctx . pin_mut ( ) . add_include ( path) ;
87- }
88-
89- /// Adds a preprocessor define
90- pub fn add_define ( & mut self , define : & str ) {
91- self . ctx . pin_mut ( ) . add_define ( define) ;
92- }
93-
94- /// Parses all added source files into syntax trees
95- pub fn parse ( & mut self ) -> Result < bool , Box < dyn std:: error:: Error > > {
96- Ok ( self . ctx . pin_mut ( ) . parse ( ) ?)
97- }
98-
99- /// Returns the parsed syntax trees as a Rust vector
100- pub fn get_trees ( & self ) -> Vec < SharedPtr < ffi:: SyntaxTree > > {
101- let count = self . ctx . get_tree_count ( ) ;
102- let mut trees = Vec :: with_capacity ( count) ;
103- for i in 0 ..count {
104- trees. push ( self . ctx . get_tree ( i) ) ;
105- }
106- trees
107- }
108-
109- /// Returns an iterator over the parsed syntax trees
110- pub fn trees_iter ( & self ) -> impl Iterator < Item = SharedPtr < ffi:: SyntaxTree > > + ' _ {
111- ( 0 ..self . ctx . get_tree_count ( ) ) . map ( |i| self . ctx . get_tree ( i) )
90+ /// Sets the include directories
91+ pub fn set_includes ( mut self , includes : & [ String ] ) -> Self {
92+ self . ctx . pin_mut ( ) . set_includes ( & includes. to_vec ( ) ) ;
93+ self
11294 }
11395
114- /// Renames names in the syntax tree with a given prefix and suffix
115- pub fn rename_tree (
116- & self ,
117- tree : SharedPtr < ffi:: SyntaxTree > ,
118- prefix : Option < & str > ,
119- suffix : Option < & str > ,
120- ) -> SharedPtr < ffi:: SyntaxTree > {
121- if prefix. is_none ( ) && suffix. is_none ( ) {
122- return tree;
123- }
124- let prefix = prefix. unwrap_or ( "" ) ;
125- let suffix = suffix. unwrap_or ( "" ) ;
126- self . ctx . rename_tree ( tree, prefix, suffix)
96+ /// Sets the preprocessor defines
97+ pub fn set_defines ( mut self , defines : & [ String ] ) -> Self {
98+ self . ctx . pin_mut ( ) . set_defines ( & defines. to_vec ( ) ) ;
99+ self
127100 }
128101
129- /// Prints a syntax tree with given printing options
130- pub fn print_tree (
131- & self ,
132- tree : SharedPtr < ffi:: SyntaxTree > ,
133- opts : ffi:: SlangPrintOpts ,
134- ) -> String {
135- self . ctx . print_tree ( tree, opts)
102+ /// Parses a source file and returns the syntax tree
103+ pub fn parse ( & mut self , path : & str ) -> Result < SyntaxTree , Box < dyn std:: error:: Error > > {
104+ let ptr = self . ctx . pin_mut ( ) . parse_file ( path) ?;
105+ Ok ( SyntaxTree { ptr } )
136106 }
137107}
0 commit comments