Skip to main content

brush_core/shell/
parsing.rs

1//! Parsing for shell instances.
2
3use std::io::Read;
4
5use crate::{Shell, extensions, trace_categories};
6
7impl<SE: extensions::ShellExtensions> Shell<SE> {
8    /// Parses the given reader as a shell program, returning the resulting Abstract Syntax Tree
9    /// for the program.
10    pub fn parse<R: Read>(
11        &self,
12        reader: R,
13    ) -> Result<brush_parser::ast::Program, brush_parser::ParseError> {
14        let mut parser = create_parser(reader, &self.parser_options());
15
16        tracing::debug!(target: trace_categories::PARSE, "Parsing reader as program...");
17        parser.parse_program()
18    }
19
20    /// Parses the given string as a shell program, returning the resulting Abstract Syntax Tree
21    /// for the program.
22    ///
23    /// # Arguments
24    ///
25    /// * `s` - The string to parse as a program.
26    pub fn parse_string<S: Into<String>>(
27        &self,
28        s: S,
29    ) -> Result<brush_parser::ast::Program, brush_parser::ParseError> {
30        parse_string_impl(s.into(), self.parser_options())
31    }
32
33    /// Returns the options that should be used for parsing shell programs; reflects
34    /// the current configuration state of the shell and may change over time.
35    pub const fn parser_options(&self) -> brush_parser::ParserOptions {
36        brush_parser::ParserOptions {
37            enable_extended_globbing: self.options.extended_globbing,
38            posix_mode: self.options.posix_mode,
39            sh_mode: self.options.sh_mode,
40            tilde_expansion_at_word_start: true,
41            tilde_expansion_after_colon: false,
42            parser_impl: self.parser_impl,
43        }
44    }
45}
46
47#[cached::proc_macro::cached(size = 64, result = true)]
48fn parse_string_impl(
49    s: String,
50    parser_options: brush_parser::ParserOptions,
51) -> Result<brush_parser::ast::Program, brush_parser::ParseError> {
52    let mut parser = create_parser(s.as_bytes(), &parser_options);
53
54    tracing::debug!(target: trace_categories::PARSE, "Parsing string as program...");
55    parser.parse_program()
56}
57
58pub(super) fn create_parser<R: Read>(
59    r: R,
60    parser_options: &brush_parser::ParserOptions,
61) -> brush_parser::Parser<std::io::BufReader<R>> {
62    let reader = std::io::BufReader::new(r);
63    brush_parser::Parser::new(reader, parser_options)
64}