Skip to main content

oslquery_petite/parser/
reader.rs

1//! OSO file reader that orchestrates the parsing
2
3use std::fs;
4use std::path::Path;
5
6use super::types::{BaseType, ParsedParameter, SymType, TypeSpec};
7use super::{ParseError, hint, oso};
8use crate::query::OslQuery;
9
10/// OSO file reader that parses OSO format line by line.
11///
12/// This reader implements a token-based parsing approach where each line
13/// is first tokenized, then tokens are parsed sequentially. This matches
14/// the behavior of the OpenShadingLanguage C++ parser.
15pub struct OsoReader {
16    /// Current line number for error reporting
17    line_no: usize,
18    /// Current parameter being read
19    current_param: Option<ParsedParameter>,
20    /// Whether we're reading a parameter
21    reading_param: bool,
22}
23
24impl Default for OsoReader {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30impl OsoReader {
31    /// Create a new OSO reader
32    pub fn new() -> Self {
33        OsoReader {
34            line_no: 1,
35            current_param: None,
36            reading_param: false,
37        }
38    }
39
40    /// Parse an OSO file from disk
41    pub fn parse_file<P: AsRef<Path>>(self, path: P) -> Result<OslQuery, ParseError> {
42        let content = fs::read_to_string(path)?;
43        self.parse_string(&content)
44    }
45
46    /// Parse OSO content from a string
47    pub fn parse_string(mut self, content: &str) -> Result<OslQuery, ParseError> {
48        let mut query = OslQuery::new();
49        let lines = content.lines();
50
51        for line in lines {
52            // Don't trim the line - preserve tabs for proper parsing
53
54            // Skip empty lines and comments (# at start of line)
55            if line.trim().is_empty() || line.trim_start().starts_with('#') {
56                self.line_no += 1;
57                continue;
58            }
59
60            // Try to parse different directives
61            if let Ok((_, version)) = oso::parse_version(line) {
62                // Check version compatibility - support 1.00 and above
63                if version.0 < 1 {
64                    return Err(ParseError::UnsupportedVersion {
65                        major: version.0,
66                        minor: version.1,
67                    });
68                }
69            } else if line.starts_with("shader ")
70                || line.starts_with("surface ")
71                || line.starts_with("displacement ")
72                || line.starts_with("volume ")
73            {
74                // Parse shader declaration - handles both "shader name" and "surface name" formats
75                if let Ok((rest, (shader_type, shader_name))) = oso::parse_shader(line) {
76                    query.set_shader_info(shader_type, shader_name);
77                    // Parse any hints on the same line
78                    let rest_tokens = oso::tokenize_line(rest);
79                    for token in rest_tokens {
80                        if token.starts_with('%') {
81                            self.handle_hint(&mut query, token)?;
82                        }
83                    }
84                }
85            } else if self.try_parse_symbol_line(&mut query, line)? {
86                // Symbol line was successfully parsed
87            } else if line.starts_with("code") {
88                // End of current parameter, start of code section
89                self.finish_current_param(&mut query);
90                // For now, we stop parsing at code section
91                // In a full implementation, we'd parse bytecode here
92                break;
93            } else if line.starts_with('%') {
94                // Standalone hint line (metadata for shader or current param)
95                self.handle_hint(&mut query, line)?;
96            }
97
98            self.line_no += 1;
99        }
100
101        // Make sure to add the last parameter if any
102        self.finish_current_param(&mut query);
103
104        Ok(query)
105    }
106
107    /// Handle symbol declaration
108    fn handle_symbol(
109        &mut self,
110        query: &mut OslQuery,
111        symtype: SymType,
112        typespec: TypeSpec,
113        name: &str,
114    ) -> Result<(), ParseError> {
115        // Finish any previous parameter
116        self.finish_current_param(query);
117
118        match symtype {
119            SymType::Param | SymType::OutputParam => {
120                let mut param = ParsedParameter::new(name, typespec.simpletype);
121                param.is_output = symtype == SymType::OutputParam;
122                param.is_struct = typespec.is_structure();
123                param.varlen_array = typespec.is_unsized_array();
124
125                self.current_param = Some(param);
126                self.reading_param = true;
127            }
128            _ => {
129                // Not a parameter, ignore for now
130                self.reading_param = false;
131            }
132        }
133
134        Ok(())
135    }
136
137    /// Try to parse a symbol line using tokenization
138    fn try_parse_symbol_line(
139        &mut self,
140        query: &mut OslQuery,
141        line: &str,
142    ) -> Result<bool, ParseError> {
143        let tokens = oso::tokenize_line(line);
144        if tokens.is_empty() {
145            return Ok(false);
146        }
147
148        // Check if first token is a valid symtype
149        let symtype = match oso::parse_symtype(tokens[0]) {
150            Ok((_, st)) => st,
151            _ => return Ok(false),
152        };
153
154        // Need at least 3 tokens: symtype, typespec, identifier
155        if tokens.len() < 3 {
156            return Ok(false);
157        }
158
159        // Parse typespec from second token(s)
160        // Handle "closure color" as two tokens
161        let (typespec, next_token_idx) = if tokens[1] == "closure" {
162            // Need at least 4 tokens for closure: symtype, "closure", typename, identifier
163            if tokens.len() < 4 {
164                return Err(ParseError::ParseError {
165                    line: self.line_no,
166                    message: "Incomplete closure type specification".to_string(),
167                    token_info: Some((tokens[1].to_string(), 1)),
168                });
169            }
170            // Parse "closure typename" as a single typespec
171            let closure_spec = format!("{} {}", tokens[1], tokens[2]);
172            match oso::parse_typespec(&closure_spec) {
173                Ok((_, ts)) => (ts, 3), // Next token is at index 3
174                _ => {
175                    return Err(ParseError::ParseError {
176                        line: self.line_no,
177                        message: format!("Invalid closure type: {} {}", tokens[1], tokens[2]),
178                        token_info: Some((tokens[1].to_string(), 1)),
179                    });
180                }
181            }
182        } else {
183            // Regular single-token typespec
184            match oso::parse_typespec(tokens[1]) {
185                Ok((_, ts)) => (ts, 2), // Next token is at index 2
186                _ => {
187                    return Err(ParseError::ParseError {
188                        line: self.line_no,
189                        message: format!("Invalid type specification: {}", tokens[1]),
190                        token_info: Some((tokens[1].to_string(), 1)),
191                    });
192                }
193            }
194        };
195
196        // Next token is the identifier
197        let name = tokens[next_token_idx];
198
199        // Handle the symbol
200        self.handle_symbol(query, symtype, typespec, name)?;
201
202        // Process remaining tokens as default values and hints
203        let mut token_idx = next_token_idx + 1;
204
205        // Parse default values (everything until we hit a % token)
206        while token_idx < tokens.len() && !tokens[token_idx].starts_with('%') {
207            if let Some(default) = oso::parse_default_token(tokens[token_idx])
208                && let Some(ref mut param) = self.current_param
209            {
210                match default {
211                    oso::DefaultValue::Int(i) => {
212                        // For float-based types (float, color, point, vector, normal, matrix),
213                        // store integers as floats
214                        match param.type_desc.basetype {
215                            BaseType::Float
216                            | BaseType::Color
217                            | BaseType::Point
218                            | BaseType::Vector
219                            | BaseType::Normal
220                            | BaseType::Matrix => {
221                                param.fdefault.push(i as f32);
222                            }
223                            _ => {
224                                param.idefault.push(i);
225                            }
226                        }
227                    }
228                    oso::DefaultValue::Float(f) => {
229                        param.fdefault.push(f);
230                    }
231                    oso::DefaultValue::String(s) => {
232                        param.sdefault.push(s);
233                    }
234                }
235                param.valid_default = true;
236            }
237            token_idx += 1;
238        }
239
240        // Process hint tokens
241        while token_idx < tokens.len() {
242            if tokens[token_idx].starts_with('%') {
243                self.handle_hint(query, tokens[token_idx])?;
244            }
245            token_idx += 1;
246        }
247
248        Ok(true)
249    }
250
251    /// Handle hint directive
252    fn handle_hint(&mut self, query: &mut OslQuery, hint_str: &str) -> Result<(), ParseError> {
253        // Parse metadata hints
254        if hint_str.starts_with("%meta{") {
255            self.parse_metadata(query, hint_str)?;
256        } else if self.reading_param && hint_str.starts_with("%structfields{") {
257            self.parse_struct_fields(hint_str)?;
258        } else if self.reading_param && hint_str.starts_with("%struct{") {
259            self.parse_struct_name(hint_str)?;
260        } else if self.reading_param && hint_str.starts_with("%space{") {
261            self.parse_space_hint(hint_str)?;
262        } else if self.reading_param && hint_str.starts_with("%default{") {
263            self.parse_default_hint(hint_str)?;
264        } else if self.reading_param
265            && hint_str == "%initexpr"
266            && let Some(ref mut param) = self.current_param
267        {
268            param.valid_default = false;
269        }
270        // Ignore other hints like %read{...} %write{...} which are bytecode related
271
272        Ok(())
273    }
274
275    /// Parse metadata hint
276    fn parse_metadata(&mut self, query: &mut OslQuery, hint_str: &str) -> Result<(), ParseError> {
277        if let Ok((_, meta)) = hint::parse_metadata_hint(hint_str) {
278            if self.reading_param {
279                if let Some(ref mut param) = self.current_param {
280                    param.metadata.push(meta);
281                }
282            } else {
283                // Convert ParsedParameter metadata to Metadata
284                use crate::types::MetadataValue;
285                let value = if !meta.idefault.is_empty() {
286                    if meta.idefault.len() == 1 {
287                        MetadataValue::Int(meta.idefault[0])
288                    } else {
289                        MetadataValue::IntArray(meta.idefault)
290                    }
291                } else if !meta.fdefault.is_empty() {
292                    if meta.fdefault.len() == 1 {
293                        MetadataValue::Float(meta.fdefault[0])
294                    } else {
295                        MetadataValue::FloatArray(meta.fdefault)
296                    }
297                } else if !meta.sdefault.is_empty() {
298                    if meta.sdefault.len() == 1 {
299                        MetadataValue::String(meta.sdefault[0].clone())
300                    } else {
301                        MetadataValue::StringArray(meta.sdefault)
302                    }
303                } else {
304                    return Ok(());
305                };
306                query.add_metadata(crate::types::Metadata {
307                    name: meta.name,
308                    value,
309                });
310            }
311        }
312        Ok(())
313    }
314
315    /// Parse struct fields hint
316    fn parse_struct_fields(&mut self, hint_str: &str) -> Result<(), ParseError> {
317        if let Some(ref mut param) = self.current_param
318            && let Some(fields) = hint::parse_structfields_hint(hint_str)
319        {
320            param.fields = fields;
321        }
322        Ok(())
323    }
324
325    /// Parse struct name hint
326    fn parse_struct_name(&mut self, hint_str: &str) -> Result<(), ParseError> {
327        if let Some(ref mut param) = self.current_param {
328            param.structname = hint::parse_struct_hint(hint_str);
329        }
330        Ok(())
331    }
332
333    /// Parse space hint for geometric types
334    fn parse_space_hint(&mut self, hint_str: &str) -> Result<(), ParseError> {
335        if let Some(ref mut param) = self.current_param
336            && let Some(space) = hint::parse_space_hint(hint_str)
337        {
338            param.spacename.push(space);
339        }
340        Ok(())
341    }
342
343    /// Parse default hint (alternative default value format)
344    fn parse_default_hint(&mut self, hint_str: &str) -> Result<(), ParseError> {
345        if let Some(ref mut param) = self.current_param
346            && let Some(values) = hint::parse_default_hint(hint_str)
347        {
348            match param.type_desc.basetype {
349                BaseType::Int => {
350                    param
351                        .idefault
352                        .extend(values.iter().filter_map(|v| v.parse::<i32>().ok()));
353                }
354                BaseType::Float
355                | BaseType::Color
356                | BaseType::Point
357                | BaseType::Vector
358                | BaseType::Normal
359                | BaseType::Matrix => {
360                    param
361                        .fdefault
362                        .extend(values.iter().filter_map(|v| v.parse::<f32>().ok()));
363                }
364                BaseType::String => {
365                    param.sdefault.extend(values);
366                }
367                _ => {}
368            }
369
370            param.valid_default = true;
371        }
372        Ok(())
373    }
374
375    /// Finish processing the current parameter and add it to the query
376    fn finish_current_param(&mut self, query: &mut OslQuery) {
377        if let Some(parsed_param) = self.current_param.take() {
378            // Convert ParsedParameter to final Parameter type
379            match parsed_param.try_into() {
380                Ok(param) => query.add_parameter(param),
381                Err(e) => eprintln!("Failed to convert parameter: {}", e),
382            }
383        }
384        self.reading_param = false;
385    }
386}
387
388#[cfg(test)]
389mod tests {
390    use super::*;
391
392    #[test]
393    fn test_parse_simple_shader() {
394        let oso_content = r#"
395OpenShadingLanguage 1.12
396surface simple
397param float Kd 0.5
398code ___main___
399"#;
400
401        let reader = OsoReader::new();
402        let query = reader.parse_string(oso_content).unwrap();
403
404        assert_eq!(query.shader_type(), "surface");
405        assert_eq!(query.shader_name(), "simple");
406        assert_eq!(query.param_count(), 1);
407
408        let param = query.param_by_name("Kd").unwrap();
409        assert_eq!(param.name.as_str(), "Kd");
410        assert!(!param.is_output());
411        // Check the typed parameter for default value
412        use crate::TypedParameter;
413        match param.typed_param() {
414            TypedParameter::Float { default: Some(val) } => {
415                assert_eq!(*val, 0.5);
416            }
417            _ => panic!("Expected Float parameter with default"),
418        }
419    }
420
421    #[test]
422    fn test_parse_shader_with_tabs() {
423        let oso_content = r#"
424OpenShadingLanguage 1.12
425surface _3DelightMaterial
426param	int	coating_on	0	%meta{string,page,"Coating"} %meta{string,label,"On"}
427param	color	coating_color	1 1 1	%meta{string,label,"Color"}
428code ___main___
429"#;
430
431        let reader = OsoReader::new();
432        let query = reader.parse_string(oso_content).unwrap();
433
434        assert_eq!(query.shader_type(), "surface");
435        assert_eq!(query.shader_name(), "_3DelightMaterial");
436        assert_eq!(query.param_count(), 2);
437
438        let param = query.param_by_name("coating_on").unwrap();
439        assert_eq!(param.name.as_str(), "coating_on");
440        assert!(!param.is_output());
441        use crate::TypedParameter;
442        match param.typed_param() {
443            TypedParameter::Int { default: Some(val) } => {
444                assert_eq!(*val, 0);
445            }
446            _ => panic!("Expected Int parameter with default"),
447        }
448
449        let param = query.param_by_name("coating_color").unwrap();
450        assert_eq!(param.name.as_str(), "coating_color");
451        match param.typed_param() {
452            TypedParameter::Color {
453                default: Some([r, g, b]),
454                ..
455            } => {
456                assert_eq!(*r, 1.0);
457                assert_eq!(*g, 1.0);
458                assert_eq!(*b, 1.0);
459            }
460            _ => panic!("Expected Color parameter with default"),
461        }
462    }
463}