Skip to main content

rd_source/
lib.rs

1//! Parse R documentation source into the common `rd_ast` document model.
2
3mod diagnostic;
4mod lexer;
5mod parser;
6mod source_map;
7
8#[doc(hidden)]
9pub mod unstable_rlike;
10
11/// v1 implementation limit for the complete UTF-8 input, in bytes.
12pub const MAX_INPUT_SIZE: usize = 64 * 1024 * 1024;
13
14pub use diagnostic::{
15    Diagnostic, DiagnosticCode, ParseError, Parsed, Severity, SourcePosition, SourceSpan,
16};
17
18/// Parse a UTF-8 Rd source document.
19pub fn parse(input: &[u8]) -> Result<Parsed, ParseError> {
20    if input.len() > MAX_INPUT_SIZE {
21        return Err(ParseError::InputTooLarge);
22    }
23    if let Some(offset) = input.iter().position(|byte| *byte == 0) {
24        return Err(ParseError::NulByte { offset });
25    }
26    let source = std::str::from_utf8(input).map_err(|error| ParseError::InvalidUtf8 {
27        valid_up_to: error.valid_up_to(),
28        error_len: error.error_len(),
29    })?;
30    parser::Parser::new(input, source).parse()
31}