pub struct RustParser<'config> { /* private fields */ }Expand description
A parser for the Rust programming language.
The RustParser is responsible for parsing Rust source code and building an Abstract Syntax Tree (AST).
It uses a Pratt parser for handling operator precedence in expressions and supports all Rust syntax features.
§Examples
Basic usage:
use oak_core::Parser;
use oak_rust::{RustLanguage, RustParser, SourceText};
let language = RustLanguage::default();
let parser = RustParser::new(&language);
let source = SourceText::new("fn main() { println!(\"Hello, world!\"); }");
let result = parser.parse(source, 0);
// The result contains the parsed AST
assert!(result.is_ok());Parsing a more complex Rust structure:
use oak_core::Parser;
use oak_rust::{RustLanguage, RustParser, SourceText};
let language = RustLanguage::default();
let parser = RustParser::new(&language);
let source = SourceText::new(
r#"
struct Point {
x: f64,
y: f64,
}
impl Point {
fn new(x: f64, y: f64) -> Self {
Point { x, y }
}
}
"#,
);
let result = parser.parse(source, 0);
// Verify that parsing succeeded
assert!(result.is_ok());Implementations§
Source§impl<'config> RustParser<'config>
impl<'config> RustParser<'config>
pub fn parse_param( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_block( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_stmt( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_let_stmt( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_expr_stmt( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_expr( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_use_item( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_const_item( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_mod_item( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
pub fn parse_pub_item( state: &mut ParserState<'_, impl Source, RustLanguage>, ) -> Result<Arc<GreenNode<RustSyntaxKind>>, OakError>
Source§impl<'config> RustParser<'config>
impl<'config> RustParser<'config>
Sourcepub fn new(config: &'config RustLanguage) -> Self
pub fn new(config: &'config RustLanguage) -> Self
Creates a new RustParser with the given language configuration.
§Parameters
config- A reference to theRustLanguageconfiguration that controls language-specific parsing behavior.
§Examples
use oak_rust::{RustLanguage, RustParser};
let language = RustLanguage::default();
let parser = RustParser::new(&language);Sourcepub fn operator_rules(&self) -> &PrattParser<RustLanguage>
pub fn operator_rules(&self) -> &PrattParser<RustLanguage>
Returns a reference to the Pratt parser containing operator precedence rules.
The Pratt parser defines the precedence and associativity of all Rust operators, which is used to correctly parse expressions with multiple operators.
Trait Implementations§
Source§impl<'config> Builder<RustLanguage> for RustParser<'config>
impl<'config> Builder<RustLanguage> for RustParser<'config>
Source§fn build_incremental(
&self,
text: impl Source,
changed: usize,
cache: IncrementalCache<'_, RustLanguage>,
) -> OakDiagnostics<RustRoot>
fn build_incremental( &self, text: impl Source, changed: usize, cache: IncrementalCache<'_, RustLanguage>, ) -> OakDiagnostics<RustRoot>
Builds a kind tree incrementally using an existing cache. Read more
Source§impl<'config> Clone for RustParser<'config>
impl<'config> Clone for RustParser<'config>
Source§fn clone(&self) -> RustParser<'config>
fn clone(&self) -> RustParser<'config>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<'config> Parser<RustLanguage> for RustParser<'config>
impl<'config> Parser<RustLanguage> for RustParser<'config>
Source§fn parse_incremental(
&self,
text: impl Source,
changed: usize,
cache: IncrementalCache<'_, RustLanguage>,
) -> OakDiagnostics<Arc<GreenNode<RustSyntaxKind>>>
fn parse_incremental( &self, text: impl Source, changed: usize, cache: IncrementalCache<'_, RustLanguage>, ) -> OakDiagnostics<Arc<GreenNode<RustSyntaxKind>>>
Parses source text incrementally using an existing cache. Read more
Source§fn parse(
&self,
text: impl Source,
) -> OakDiagnostics<Arc<GreenNode<<L as Language>::SyntaxKind>>>
fn parse( &self, text: impl Source, ) -> OakDiagnostics<Arc<GreenNode<<L as Language>::SyntaxKind>>>
Parses source text into a kind tree. Read more
Auto Trait Implementations§
impl<'config> Freeze for RustParser<'config>
impl<'config> RefUnwindSafe for RustParser<'config>
impl<'config> Send for RustParser<'config>
impl<'config> Sync for RustParser<'config>
impl<'config> Unpin for RustParser<'config>
impl<'config> UnwindSafe for RustParser<'config>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more