rune/parse/
mod.rs

1//! Parsing utilities for Rune.
2
3mod expectation;
4mod id;
5mod lexer;
6mod parse;
7mod parser;
8mod peek;
9mod resolve;
10mod traits;
11
12use unicode_ident::{is_xid_continue, is_xid_start};
13
14pub use self::expectation::Expectation;
15pub(crate) use self::expectation::IntoExpectation;
16pub(crate) use self::id::NonZeroId;
17pub(crate) use self::lexer::{Lexer, LexerMode};
18pub use self::parse::Parse;
19pub use self::parser::{Parser, Peeker};
20pub use self::peek::Peek;
21pub(crate) use self::resolve::{Resolve, ResolveContext};
22pub(crate) use self::traits::Advance;
23
24use crate::compile;
25use crate::SourceId;
26
27/// Parse the given input as the given type that implements [Parse]. The
28/// specified `source_id` will be used when referencing any parsed elements.
29/// `shebang` indicates if the parser should try to parse a shebang or not.
30///
31/// This will raise an error through [Parser::eof] if the specified `source` is
32/// not fully consumed by the parser.
33pub fn parse_all<T>(source: &str, source_id: SourceId, shebang: bool) -> compile::Result<T>
34where
35    T: Parse,
36{
37    let mut parser = Parser::new(source, source_id, shebang);
38    let ast = parser.parse::<T>()?;
39    parser.eof()?;
40    Ok(ast)
41}
42
43/// Test if the given string is a valid identifier.
44pub(crate) fn is_ident(ident: &str) -> bool {
45    let mut chars = ident.chars();
46
47    let Some(c) = chars.next() else {
48        return false;
49    };
50
51    if !(c == '_' || is_xid_start(c)) {
52        return false;
53    }
54
55    for c in chars {
56        if !is_xid_continue(c) {
57            return false;
58        }
59    }
60
61    true
62}