Debian Codegraph @

third_party/rust/glsl/src/parser.rs

raw
1 //! GLSL parsing.
2 //!
3 //! This module gives you several functions and types to deal with GLSL parsing, transforming an
4 //! input source into an AST. The AST is defined in the [`syntax`] module.
5 //!
6 //! You want to use the [`Parse`]’s methods to get starting with parsing and pattern match on
7 //! the resulting [`Result`]. In case of an error, you can inspect the content of the [`ParseError`]
8 //! object in the `Err` variant.
9 //!
10 //! [`Parse`]: crate::parser::Parse
11 //! [`ParseError`]: crate::parser::ParseError
12
13 use nom::error::convert_error;
14 use nom::Err as NomErr;
15 use std::fmt;
16
17 use crate::parsers::ParserResult;
18 use crate::syntax;
19
20 /// A parse error. It contains a [`String`] giving information on the reason why the parser failed.
21 #[derive(Clone, Debug, Eq, PartialEq)]
22 pub struct ParseError {
23 pub info: String,
24 }
25
26 impl std::error::Error for ParseError {}
27
28 impl fmt::Display for ParseError {
29 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
30 write!(f, "error: {}", self.info)
31 }
32 }
33
34 /// Run a parser `P` on a given `[&str`] input.
35 pub(crate) fn run_parser<P, T>(source: &str, parser: P) -> Result<T, ParseError>
36 where
37 P: FnOnce(&str) -> ParserResult<T>,
38 {
39 match parser(source) {
40 Ok((_, x)) => Ok(x),
41
42 Err(e) => match e {
43 NomErr::Incomplete(_) => Err(ParseError {
44 info: "incomplete parser".to_owned(),
45 }),
46
47 NomErr::Error(err) | NomErr::Failure(err) => {
48 let info = convert_error(source, err);
49 Err(ParseError { info })
50 }
51 },
52 }
53 }
54
55 /// Class of types that can be parsed.
56 ///
57 /// This trait exposes the [`Parse::parse`] function that can be used to parse GLSL types.
58 ///
59 /// The methods from this trait are the standard way to parse data into GLSL ASTs.
60 pub trait Parse: Sized {
61 /// Parse from a string slice.
62 fn parse<B>(source: B) -> Result<Self, ParseError>
63 where
64 B: AsRef<str>;
65 }
66
67 /// Macro to implement Parse for a given type.
68 macro_rules! impl_parse {
69 ($type_name:ty, $parser_name:ident) => {
70 impl Parse for $type_name {
71 fn parse<B>(source: B) -> Result<Self, ParseError>
72 where
73 B: AsRef<str>,
74 {
75 run_parser(source.as_ref(), $crate::parsers::$parser_name)
76 }
77 }
78 };
79 }
80
81 impl_parse!(syntax::Identifier, identifier);
82 impl_parse!(syntax::TypeSpecifierNonArray, type_specifier_non_array);
83 impl_parse!(syntax::TypeSpecifier, type_specifier);
84 impl_parse!(syntax::UnaryOp, unary_op);
85 impl_parse!(syntax::StructFieldSpecifier, struct_field_specifier);
86 impl_parse!(syntax::StructSpecifier, struct_specifier);
87 impl_parse!(syntax::StorageQualifier, storage_qualifier);
88 impl_parse!(syntax::LayoutQualifier, layout_qualifier);
89 impl_parse!(syntax::PrecisionQualifier, precision_qualifier);
90 impl_parse!(syntax::InterpolationQualifier, interpolation_qualifier);
91 impl_parse!(syntax::TypeQualifier, type_qualifier);
92 impl_parse!(syntax::TypeQualifierSpec, type_qualifier_spec);
93 impl_parse!(syntax::FullySpecifiedType, fully_specified_type);
94 impl_parse!(syntax::ArraySpecifier, array_specifier);
95 impl_parse!(syntax::Expr, expr);
96 impl_parse!(syntax::Declaration, declaration);
97 impl_parse!(syntax::FunctionPrototype, function_prototype);
98 impl_parse!(syntax::InitDeclaratorList, init_declarator_list);
99 impl_parse!(syntax::SingleDeclaration, single_declaration);
100 impl_parse!(syntax::Initializer, initializer);
101 impl_parse!(syntax::FunIdentifier, function_identifier);
102 impl_parse!(syntax::AssignmentOp, assignment_op);
103 impl_parse!(syntax::SimpleStatement, simple_statement);
104 impl_parse!(syntax::ExprStatement, expr_statement);
105 impl_parse!(syntax::SelectionStatement, selection_statement);
106 impl_parse!(syntax::SwitchStatement, switch_statement);
107 impl_parse!(syntax::CaseLabel, case_label);
108 impl_parse!(syntax::IterationStatement, iteration_statement);
109 impl_parse!(syntax::JumpStatement, jump_statement);
110 impl_parse!(syntax::Condition, condition);
111 impl_parse!(syntax::Statement, statement);
112 impl_parse!(syntax::CompoundStatement, compound_statement);
113 impl_parse!(syntax::FunctionDefinition, function_definition);
114 impl_parse!(syntax::ExternalDeclaration, external_declaration);
115 impl_parse!(syntax::TranslationUnit, translation_unit);
116 impl_parse!(syntax::Preprocessor, preprocessor);
117 impl_parse!(syntax::PreprocessorVersion, pp_version);
118 impl_parse!(syntax::PreprocessorVersionProfile, pp_version_profile);
119 impl_parse!(syntax::PreprocessorExtensionName, pp_extension_name);
120 impl_parse!(syntax::PreprocessorExtensionBehavior, pp_extension_behavior);
121 impl_parse!(syntax::PreprocessorExtension, pp_extension);