@@ -22,116 +22,87 @@ mod ffi {
2222 // Include Slang header to define SyntaxTree type for CXX
2323 include ! ( "slang/syntax/SyntaxTree.h" ) ;
2424
25- /// Opaque type for the Slang Driver wrapper
25+ /// Opaque type for the Slang Context
2626 type SlangContext ;
2727
2828 /// Opaque type for the Slang SyntaxTree
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
40- /// Parse all added sources. Returns true on success.
41- fn parse ( self : Pin < & mut SlangContext > ) -> Result < bool > ;
42-
43- /// Retrieves the number of parsed syntax trees
44- fn get_tree_count ( self : & SlangContext ) -> usize ;
45-
46- /// Retrieves a shared pointer to a specific syntax tree by index
47- fn get_tree ( self : & SlangContext , index : usize ) -> SharedPtr < SyntaxTree > ;
40+ /// Parse all added sources. Returns a syntax tree on success, or an error message on failure.
41+ fn parse_file ( self : Pin < & mut SlangContext > , path : & str ) -> Result < SharedPtr < SyntaxTree > > ;
4842
4943 /// 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 ;
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 ;
6349 }
6450}
6551
66- /// A persistent Slang session
67- pub struct SlangSession {
68- ctx : UniquePtr < ffi:: SlangContext > ,
52+ /// Extension trait for SyntaxTree
53+ pub trait SyntaxTreeExt {
54+ fn rename ( & self , prefix : Option < & str > , suffix : Option < & str > ) -> Self ;
55+ fn display ( & self , options : SlangPrintOpts ) -> String ;
6956}
7057
71- impl SlangSession {
72- /// Creates a new Slang session
73- pub fn new ( ) -> Self {
74- Self {
75- ctx : ffi :: new_slang_context ( ) ,
58+ impl SyntaxTreeExt for SharedPtr < ffi :: SyntaxTree > {
59+ /// Renames all names in the syntax tree with the given prefix and suffix
60+ fn rename ( & self , prefix : Option < & str > , suffix : Option < & str > ) -> Self {
61+ if prefix . is_none ( ) && suffix . is_none ( ) {
62+ return self . clone ( ) ;
7663 }
64+ ffi:: rename ( self . clone ( ) , prefix. unwrap_or ( "" ) , suffix. unwrap_or ( "" ) )
7765 }
7866
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) ;
67+ /// Displays the syntax tree as a string with the given options
68+ fn display ( & self , options : SlangPrintOpts ) -> String {
69+ ffi:: print_tree ( self . clone ( ) , options)
9270 }
71+ }
9372
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- }
73+ /// Extension trait for SlangContext
74+ pub trait SlangContextExt {
75+ fn set_includes ( self , includes : & Vec < String > ) -> Self ;
76+ fn set_defines ( self , defines : & Vec < String > ) -> Self ;
77+ fn parse (
78+ & mut self ,
79+ path : & str ,
80+ ) -> Result < SharedPtr < ffi:: SyntaxTree > , Box < dyn std:: error:: Error > > ;
81+ }
9882
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
83+ impl SlangContextExt for UniquePtr < ffi:: SlangContext > {
84+ /// Sets the include directories
85+ fn set_includes ( mut self , includes : & Vec < String > ) -> Self {
86+ self . pin_mut ( ) . set_includes ( & includes) ;
87+ self
10788 }
10889
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 preprocessor defines
91+ fn set_defines ( mut self , defines : & Vec < String > ) -> Self {
92+ self . pin_mut ( ) . set_defines ( & defines) ;
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+ /// Parses a source file and returns the syntax tree
97+ fn parse (
98+ & mut self ,
99+ path : & str ,
100+ ) -> Result < SharedPtr < ffi:: SyntaxTree > , Box < dyn std:: error:: Error > > {
101+ Ok ( self . pin_mut ( ) . parse_file ( path) ?)
127102 }
103+ }
128104
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)
136- }
105+ /// Creates a new Slang session
106+ pub fn new_session ( ) -> UniquePtr < ffi:: SlangContext > {
107+ ffi:: new_slang_context ( )
137108}
0 commit comments