Struct lib_ruby_parser::ParserOptions [−][src]
Configuration of the parser
Fields
buffer_name: String
Name of the buffer. Used in all diagnostic messages
debug: Type
Controls which debug information is printed during parsing
Can be:
- lib_ruby_parser::debug_level::None
- lib_ruby_parser::debug_level::Parser
- lib_ruby_parser::debug_level::Lexer
- lib_ruby_parser::debug_level::Buffer
- or a combination of them (like
Lexer | Buffer
, these value is just a bitmask)
decoder: MaybePtr<CustomDecoder>
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::{InputError, CustomDecoder, CustomDecoderResult}; use lib_ruby_parser::{Parser, ParserOptions, ParserResult, debug_level}; fn decode(encoding: String, input: Vec<u8>) -> CustomDecoderResult { if "US-ASCII" == encoding.to_uppercase() { // reencode and return Ok(result) return CustomDecoderResult::Ok(b"# encoding: us-ascii\ndecoded".to_vec()); } CustomDecoderResult::Err(InputError::DecodingError( "only us-ascii is supported".to_string(), )) } let options = ParserOptions { decoder: Some(Box::new(decode)), debug: debug_level::PARSER, ..Default::default() }; let mut 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: TokenRewriter
Optional token rewriter, see TokenRewriter API
Example
use lib_ruby_parser::{Parser, Token, Node, nodes::*, ParserOptions, ParserResult, token_rewriter::*, Bytes}; fn rewrite_foo_to_bar(mut token: Box<Token>, input: &[u8]) -> (Box<Token>, RewriteAction, LexStateAction) { // 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 (token, RewriteAction::Keep, LexStateAction::Keep) } let options = ParserOptions { token_rewriter: TokenRewriter::new(rewrite_foo_to_bar), ..Default::default() }; let ParserResult { ast, .. } = Parser::new(b"foo = 1".to_vec(), options).do_parse(); let lvar_name = match *ast.unwrap() { 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
impl Debug for ParserOptions
[src]
impl Default for ParserOptions
[src]
Auto Trait Implementations
impl RefUnwindSafe for ParserOptions
impl Send for ParserOptions
impl Sync for ParserOptions
impl Unpin for ParserOptions
impl UnwindSafe for ParserOptions
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,