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/// v1 implementation limit for the complete UTF-8 input, in bytes.
9pub const MAX_INPUT_SIZE: usize = 64 * 1024 * 1024;
10
11pub use diagnostic::{
12    Diagnostic, DiagnosticCode, ParseError, Parsed, Severity, SourcePosition, SourceSpan,
13};
14
15/// Parse a UTF-8 Rd source document.
16pub fn parse(input: &[u8]) -> Result<Parsed, ParseError> {
17    if input.len() > MAX_INPUT_SIZE {
18        return Err(ParseError::InputTooLarge);
19    }
20    if let Some(offset) = input.iter().position(|byte| *byte == 0) {
21        return Err(ParseError::NulByte { offset });
22    }
23    let source = std::str::from_utf8(input).map_err(|error| ParseError::InvalidUtf8 {
24        valid_up_to: error.valid_up_to(),
25        error_len: error.error_len(),
26    })?;
27    parser::Parser::new(input, source).parse()
28}