Skip to main content

draxl_parser/
lib.rs

1#![forbid(unsafe_code)]
2//! Language-dispatch facade for Draxl surface parsing.
3//!
4//! Today the crate exposes only the Rust backend, but the public parsing
5//! surface is language-aware so additional backends can be added behind the
6//! same facade over time.
7
8use draxl_ast::{
9    CommentNode, DocNode, Expr, Field, File, Item, LowerLanguage, MatchArm, Param, Pattern, Stmt,
10    Type, Variant,
11};
12
13pub use draxl_rust::ParseError;
14
15/// Parses Draxl source into the bootstrap AST using the selected language backend.
16pub fn parse_file_for_language(language: LowerLanguage, source: &str) -> Result<File, ParseError> {
17    match language {
18        LowerLanguage::Rust => draxl_rust::parse_file(source),
19    }
20}
21
22/// Parses Draxl source into the bootstrap AST.
23pub fn parse_file(source: &str) -> Result<File, ParseError> {
24    parse_file_for_language(LowerLanguage::Rust, source)
25}
26
27/// Parses a single item fragment for patch resolution using the selected language backend.
28pub fn parse_item_fragment_for_language(
29    language: LowerLanguage,
30    source: &str,
31) -> Result<Item, ParseError> {
32    match language {
33        LowerLanguage::Rust => draxl_rust::parse_item_fragment(source),
34    }
35}
36
37/// Parses a single item fragment for patch resolution.
38pub fn parse_item_fragment(source: &str) -> Result<Item, ParseError> {
39    parse_item_fragment_for_language(LowerLanguage::Rust, source)
40}
41
42/// Parses a single struct field fragment for patch resolution using the selected language backend.
43pub fn parse_field_fragment_for_language(
44    language: LowerLanguage,
45    source: &str,
46) -> Result<Field, ParseError> {
47    match language {
48        LowerLanguage::Rust => draxl_rust::parse_field_fragment(source),
49    }
50}
51
52/// Parses a single struct field fragment for patch resolution.
53pub fn parse_field_fragment(source: &str) -> Result<Field, ParseError> {
54    parse_field_fragment_for_language(LowerLanguage::Rust, source)
55}
56
57/// Parses a single enum variant fragment for patch resolution using the selected language backend.
58pub fn parse_variant_fragment_for_language(
59    language: LowerLanguage,
60    source: &str,
61) -> Result<Variant, ParseError> {
62    match language {
63        LowerLanguage::Rust => draxl_rust::parse_variant_fragment(source),
64    }
65}
66
67/// Parses a single enum variant fragment for patch resolution.
68pub fn parse_variant_fragment(source: &str) -> Result<Variant, ParseError> {
69    parse_variant_fragment_for_language(LowerLanguage::Rust, source)
70}
71
72/// Parses a single function parameter fragment for patch resolution using the selected language backend.
73pub fn parse_param_fragment_for_language(
74    language: LowerLanguage,
75    source: &str,
76) -> Result<Param, ParseError> {
77    match language {
78        LowerLanguage::Rust => draxl_rust::parse_param_fragment(source),
79    }
80}
81
82/// Parses a single function parameter fragment for patch resolution.
83pub fn parse_param_fragment(source: &str) -> Result<Param, ParseError> {
84    parse_param_fragment_for_language(LowerLanguage::Rust, source)
85}
86
87/// Parses a single statement fragment for patch resolution using the selected language backend.
88pub fn parse_stmt_fragment_for_language(
89    language: LowerLanguage,
90    source: &str,
91) -> Result<Stmt, ParseError> {
92    match language {
93        LowerLanguage::Rust => draxl_rust::parse_stmt_fragment(source),
94    }
95}
96
97/// Parses a single statement fragment for patch resolution.
98pub fn parse_stmt_fragment(source: &str) -> Result<Stmt, ParseError> {
99    parse_stmt_fragment_for_language(LowerLanguage::Rust, source)
100}
101
102/// Parses a single match arm fragment for patch resolution using the selected language backend.
103pub fn parse_match_arm_fragment_for_language(
104    language: LowerLanguage,
105    source: &str,
106) -> Result<MatchArm, ParseError> {
107    match language {
108        LowerLanguage::Rust => draxl_rust::parse_match_arm_fragment(source),
109    }
110}
111
112/// Parses a single match arm fragment for patch resolution.
113pub fn parse_match_arm_fragment(source: &str) -> Result<MatchArm, ParseError> {
114    parse_match_arm_fragment_for_language(LowerLanguage::Rust, source)
115}
116
117/// Parses a single expression fragment for patch resolution using the selected language backend.
118pub fn parse_expr_fragment_for_language(
119    language: LowerLanguage,
120    source: &str,
121) -> Result<Expr, ParseError> {
122    match language {
123        LowerLanguage::Rust => draxl_rust::parse_expr_fragment(source),
124    }
125}
126
127/// Parses a single expression fragment for patch resolution.
128pub fn parse_expr_fragment(source: &str) -> Result<Expr, ParseError> {
129    parse_expr_fragment_for_language(LowerLanguage::Rust, source)
130}
131
132/// Parses a single type fragment for patch resolution using the selected language backend.
133pub fn parse_type_fragment_for_language(
134    language: LowerLanguage,
135    source: &str,
136) -> Result<Type, ParseError> {
137    match language {
138        LowerLanguage::Rust => draxl_rust::parse_type_fragment(source),
139    }
140}
141
142/// Parses a single type fragment for patch resolution.
143pub fn parse_type_fragment(source: &str) -> Result<Type, ParseError> {
144    parse_type_fragment_for_language(LowerLanguage::Rust, source)
145}
146
147/// Parses a single pattern fragment for patch resolution using the selected language backend.
148pub fn parse_pattern_fragment_for_language(
149    language: LowerLanguage,
150    source: &str,
151) -> Result<Pattern, ParseError> {
152    match language {
153        LowerLanguage::Rust => draxl_rust::parse_pattern_fragment(source),
154    }
155}
156
157/// Parses a single pattern fragment for patch resolution.
158pub fn parse_pattern_fragment(source: &str) -> Result<Pattern, ParseError> {
159    parse_pattern_fragment_for_language(LowerLanguage::Rust, source)
160}
161
162/// Parses a single doc comment fragment for patch resolution using the selected language backend.
163pub fn parse_doc_fragment_for_language(
164    language: LowerLanguage,
165    source: &str,
166) -> Result<DocNode, ParseError> {
167    match language {
168        LowerLanguage::Rust => draxl_rust::parse_doc_fragment(source),
169    }
170}
171
172/// Parses a single doc comment fragment for patch resolution.
173pub fn parse_doc_fragment(source: &str) -> Result<DocNode, ParseError> {
174    parse_doc_fragment_for_language(LowerLanguage::Rust, source)
175}
176
177/// Parses a single line comment fragment for patch resolution using the selected language backend.
178pub fn parse_comment_fragment_for_language(
179    language: LowerLanguage,
180    source: &str,
181) -> Result<CommentNode, ParseError> {
182    match language {
183        LowerLanguage::Rust => draxl_rust::parse_comment_fragment(source),
184    }
185}
186
187/// Parses a single line comment fragment for patch resolution.
188pub fn parse_comment_fragment(source: &str) -> Result<CommentNode, ParseError> {
189    parse_comment_fragment_for_language(LowerLanguage::Rust, source)
190}