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};
17pub use source_map::RdSourceMap;
18
19/// Parse a UTF-8 Rd source document.
20pub fn parse(input: &[u8]) -> Result<Parsed, ParseError> {
21    if input.len() > MAX_INPUT_SIZE {
22        return Err(ParseError::InputTooLarge);
23    }
24    if let Some(offset) = input.iter().position(|byte| *byte == 0) {
25        return Err(ParseError::NulByte { offset });
26    }
27    let source = std::str::from_utf8(input).map_err(|error| ParseError::InvalidUtf8 {
28        valid_up_to: error.valid_up_to(),
29        error_len: error.error_len(),
30    })?;
31    parser::Parser::new(input, source).parse()
32}