#[repr(C)]
pub struct ParserOptions { pub buffer_name: String, pub decoder: Option<Decoder>, pub token_rewriter: Option<TokenRewriter>, pub record_tokens: bool, }
Expand description

Configuration of the parser

Fields§

§buffer_name: String

Name of the buffer. Used in all diagnostic messages

§decoder: Option<Decoder>

Custom decoder that can be used if the source is encoded in unknown encoding. Only UTF-8 and ASCII-8BIT/BINARY are supported out of the box.

Example

use lib_ruby_parser::source::{Decoder, DecoderResult, InputError};
use lib_ruby_parser::{Parser, ParserOptions, ParserResult};

fn decode(encoding: String, input: Vec<u8>) -> DecoderResult {
    if "US-ASCII" == encoding.to_uppercase() {
        // reencode and return Ok(result)
        return DecoderResult::Ok(b"# encoding: us-ascii\ndecoded".to_vec().into());
    }
    DecoderResult::Err(InputError::DecodingError(
        "only us-ascii is supported".into(),
    ))
}

let decoder = Decoder::new(Box::new(decode));
let options = ParserOptions {
    decoder: Some(decoder),
    ..Default::default()
};
let parser = Parser::new(b"# encoding: us-ascii\n3 + 3".to_vec(), options);
let ParserResult { ast, input, .. } = parser.do_parse();

assert_eq!(
    ast.unwrap().expression().source(&input).unwrap(),
    "decoded".to_string()
)
§token_rewriter: Option<TokenRewriter>

Optional token rewriter, see TokenRewriter API

Example

use lib_ruby_parser::{
    nodes::*,
    source::token_rewriter::*,
    Bytes, Node, Parser, ParserOptions, ParserResult, Token,
};
fn rewrite_foo_to_bar(mut token: Box<Token>, input: &[u8]) -> TokenRewriterResult {
    // simply rewrite all tokens "foo" to "bar"
    if token.to_string_lossy() == "foo" {
        token.token_value = Bytes::new(b"bar".to_vec());
    }

    // return token + keep it + keep lexer's state
    TokenRewriterResult {
        rewritten_token: token,
        token_action: RewriteAction::Keep,
        lex_state_action: LexStateAction::Keep,
    }
}
let token_rewriter = TokenRewriter::new(Box::new(rewrite_foo_to_bar));
let options = ParserOptions {
    token_rewriter: Some(token_rewriter),
    ..Default::default()
};
let ParserResult { ast, .. } = Parser::new(b"foo = 1".to_vec(), options).do_parse();

let ast = ast.unwrap();

let lvar_name = match &*ast {
    Node::Lvasgn(Lvasgn { name, .. }) => name,
    other => panic!("expected lvasgn node, got {:?}", other),
};
assert_eq!(*lvar_name, String::from("bar"));
§record_tokens: bool

When set to true Parser records tokens. When set to false ParserResult.tokens is guaranteed to be empty. If you don’t need tokens better set it to false to speed up parsing.

Trait Implementations§

source§

impl Debug for ParserOptions

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for ParserOptions

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.