syntax_rs/
lib.rs

1// TODO: Make different presets for languages with a preset-<language> feature.
2// TODO: Add no_std feature.
3// TODO: Add some benchmarks.
4
5use parse::{Parse, ParseStream};
6
7pub mod compiler;
8pub mod cursor;
9pub mod debug;
10pub mod macros;
11pub mod parse;
12pub mod snapshot;
13pub mod spec;
14mod utf8;
15
16#[cfg(feature = "span")]
17#[derive(Debug, Default, PartialEq, Eq, Hash, Copy, Clone)]
18pub struct Span {
19    pub begin: usize,
20    pub end: usize,
21}
22
23#[cfg(feature = "span")]
24pub trait Spanned {
25    fn span(&self) -> Span;
26    fn span_ref_mut(&mut self) -> &mut Span;
27}
28
29pub type Result<T> = std::result::Result<T, &'static str>;
30
31// TODO: Make the new method private.
32#[inline]
33pub fn parse_stream(input: &str) -> ParseStream {
34    ParseStream::new(input)
35}
36
37#[inline]
38pub fn parse<T: Parse>(input: &str) -> Result<T> {
39    T::parse(&mut parse_stream(input))
40}
41
42/// Parses until the stream is empty or there are only whitespaces left.
43pub fn exhaustive_parse<T: Parse>(input: &str) -> Result<Vec<T>> {
44    let mut stream = parse_stream(input);
45    let mut results = Vec::new();
46    while !stream.is_empty() && !stream.is_only_whitespaces() {
47        results.push(stream.parse::<T>()?);
48    }
49    Ok(results)
50}