lib_ruby_parser/parser_options.rs
1use crate::source::Decoder;
2
3/// Configuration of the parser
4#[derive(Debug)]
5#[repr(C)]
6pub struct ParserOptions {
7 /// Name of the buffer. Used in all diagnostic messages
8 pub buffer_name: String,
9
10 /// Custom decoder that can be used if the source is encoded
11 /// in unknown encoding. Only UTF-8 and ASCII-8BIT/BINARY are
12 /// supported out of the box.
13 ///
14 /// # Example
15 /// ```rust
16 /// use lib_ruby_parser::source::{Decoder, DecoderResult, InputError};
17 /// use lib_ruby_parser::{Parser, ParserOptions, ParserResult, LocExt};
18 ///
19 /// fn decode(encoding: String, input: Vec<u8>) -> DecoderResult {
20 /// if "US-ASCII" == encoding.to_uppercase() {
21 /// // reencode and return Ok(result)
22 /// return DecoderResult::Ok(b"# encoding: us-ascii\ndecoded".to_vec().into());
23 /// }
24 /// DecoderResult::Err(InputError::DecodingError(
25 /// "only us-ascii is supported".into(),
26 /// ))
27 /// }
28 ///
29 /// let decoder = Decoder::new(Box::new(decode));
30 /// let options = ParserOptions {
31 /// decoder: Some(decoder),
32 /// ..Default::default()
33 /// };
34 /// let parser = Parser::new(b"# encoding: us-ascii\n3 + 3".to_vec(), options);
35 /// let ParserResult { ast, input, .. } = parser.do_parse();
36 ///
37 /// assert_eq!(
38 /// ast.unwrap().expression().source(&input).unwrap(),
39 /// "decoded".to_string()
40 /// )
41 /// ```
42 pub decoder: Option<Decoder>,
43
44 /// When set to true Parser records tokens.
45 /// When set to false `ParserResult.tokens` is guaranteed to be empty.
46 /// If you don't need tokens better set it to false to speed up parsing.
47 pub record_tokens: bool,
48}
49
50const DEFAULT_BUFFER_NAME: &str = "(eval)";
51
52impl Default for ParserOptions {
53 fn default() -> Self {
54 Self {
55 buffer_name: DEFAULT_BUFFER_NAME.to_string(),
56 decoder: None,
57 record_tokens: true,
58 }
59 }
60}