| 1 | //! Parse wrapper type following rust-analyzer's pattern for thread-safe storage in Salsa. |
| 2 | |
| 3 | use craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::{Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#, ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#, Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile#, ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#, PositionedParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#, Rulerust-analyzer cargo makefile-lossless 0.3.40 lossless/Rule#}; |
| 4 | use rowanrust-analyzer cargo rowan 0.16.1 crate/::astrust-analyzer cargo rowan 0.16.1 ast/::AstNoderust-analyzer cargo rowan 0.16.1 ast/AstNode#; |
| 5 | use rowanrust-analyzer cargo rowan 0.16.1 crate/::{GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#, SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#}; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#; |
| 7 | |
| 8 | /// The result of parsing: a syntax tree and a collection of errors. |
| 9 | /// |
| 10 | /// This type is designed to be stored in Salsa databases as it contains |
| 11 | /// the thread-safe `GreenNode` instead of the non-thread-safe `SyntaxNode`. |
| 12 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 13 | pub struct Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#<T> { |
| 14 | greenrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#green.: GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#, |
| 15 | errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#errors.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#>, |
| 16 | positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#positioned_errors.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PositionedParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#>, |
| 17 | _tyrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#_ty.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<T>, |
| 18 | } |
| 19 | |
| 20 | impl<T> Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#<T> { |
| 21 | /// Create a new Parse result from a GreenNode, errors, and positioned errors |
| 22 | pub fn newrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]new().( |
| 23 | green: GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#, |
| 24 | errors: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#>, |
| 25 | positioned_errors: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PositionedParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#>, |
| 26 | ) -> Selfrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`] { |
| 27 | Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse# { |
| 28 | greenrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#green., |
| 29 | errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#errors., |
| 30 | positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#positioned_errors., |
| 31 | _tyrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#_ty.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /// Get the green node (thread-safe representation) |
| 36 | pub fn greenrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]green().(&self) -> &GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode# { |
| 37 | &self.greenrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#green. |
| 38 | } |
| 39 | |
| 40 | /// Get the syntax errors |
| 41 | pub fn errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]errors().(&self) -> &[ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#] { |
| 42 | &self.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#errors. |
| 43 | } |
| 44 | |
| 45 | /// Get parse errors with position information |
| 46 | pub fn positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]positioned_errors().(&self) -> &[PositionedParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#] { |
| 47 | &self.positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#positioned_errors. |
| 48 | } |
| 49 | |
| 50 | /// Check if there are any errors |
| 51 | pub fn okrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]ok().(&self) -> bool { |
| 52 | self.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#errors..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() |
| 53 | } |
| 54 | |
| 55 | /// Convert to a Result, returning the tree if there are no errors |
| 56 | pub fn to_resultrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]to_result().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#> |
| 57 | where |
| 58 | T: AstNoderust-analyzer cargo rowan 0.16.1 ast/AstNode#<Languagerust-analyzer cargo rowan 0.16.1 ast/AstNode#Language# = craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::Langrust-analyzer cargo makefile-lossless 0.3.40 lossless/Lang#>, |
| 59 | { |
| 60 | if self.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#errors..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 61 | let node = SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#::new_root_mutrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]new_root_mut().(self.greenrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#green.); |
| 62 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(T::castrust-analyzer cargo rowan 0.16.1 ast/AstNode#cast().(node).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("root node has wrong type")) |
| 63 | } else { |
| 64 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#::Parserust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#Parse#(ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError# { |
| 65 | errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#errors.: self.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#errors., |
| 66 | })) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Get the parsed syntax tree |
| 71 | /// |
| 72 | /// Returns the tree even if there are parse errors. Use `errors()`, |
| 73 | /// `positioned_errors()`, or `ok()` to check for errors separately if needed. |
| 74 | /// This allows for error-resilient tooling that can work with partial/invalid input. |
| 75 | pub fn treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(&self) -> T |
| 76 | where |
| 77 | T: AstNoderust-analyzer cargo rowan 0.16.1 ast/AstNode#<Languagerust-analyzer cargo rowan 0.16.1 ast/AstNode#Language# = craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::Langrust-analyzer cargo makefile-lossless 0.3.40 lossless/Lang#>, |
| 78 | { |
| 79 | let node = SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#::new_root_mutrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]new_root_mut().(self.greenrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#green..clonerust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNode][Clone]clone().()); |
| 80 | T::castrust-analyzer cargo rowan 0.16.1 ast/AstNode#cast().(node).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("root node has wrong type") |
| 81 | } |
| 82 | |
| 83 | /// Get the syntax node |
| 84 | pub fn syntax_noderust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]syntax_node().(&self) -> SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#<craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::Langrust-analyzer cargo makefile-lossless 0.3.40 lossless/Lang#> { |
| 85 | SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#::new_rootrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]new_root().(self.greenrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#green..clonerust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNode][Clone]clone().()) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Implement Send + Sync since GreenNode is thread-safe |
| 90 | unsafe impl<T> Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# for Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#<T> {} |
| 91 | unsafe impl<T> Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# for Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#<T> {} |
| 92 | |
| 93 | impl Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#<Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile#> { |
| 94 | /// Parse makefile text, returning a Parse result |
| 95 | pub fn parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(text: &str) -> Selfrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`] { |
| 96 | let parsed = craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::parserust-analyzer cargo makefile-lossless 0.3.40 lossless/parse().(text, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 97 | Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::newrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]new().(parsed.green_noderust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#green_node., parsed.errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#errors., parsed.positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#positioned_errors.) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#<Rulerust-analyzer cargo makefile-lossless 0.3.40 lossless/Rule#> { |
| 102 | /// Parse rule text, returning a Parse result |
| 103 | pub fn parse_rulerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Rule>`]parse_rule().(text: &str) -> Selfrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Rule>`] { |
| 104 | let parsed = craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::parserust-analyzer cargo makefile-lossless 0.3.40 lossless/parse().(text, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 105 | Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::newrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]new().(parsed.green_noderust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#green_node., parsed.errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#errors., parsed.positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#positioned_errors.) |
| 106 | } |
| 107 | |
| 108 | /// Convert to a Result, extracting a single rule from the makefile |
| 109 | pub fn to_rule_resultrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Rule>`]to_rule_result().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Rulerust-analyzer cargo makefile-lossless 0.3.40 lossless/Rule#, Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#> { |
| 110 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#errors..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 111 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#::Parserust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#Parse#(ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError# { |
| 112 | errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#errors.: self.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#errors., |
| 113 | })); |
| 114 | } |
| 115 | |
| 116 | let makefile = |
| 117 | Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile#::castrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Makefile][AstNode]cast().(SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#::new_root_mutrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]new_root_mut().(self.greenrust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#green.)).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("root node has wrong type"); |
| 118 | let rules: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = makefile.rulesrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]rules().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 119 | |
| 120 | if rules.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 { |
| 121 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(rules.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().nextrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/into_iter/impl#[`IntoIter<T, A>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()) |
| 122 | } else { |
| 123 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#::Parserust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#Parse#(ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError# { |
| 124 | errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#errors.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo# { |
| 125 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message.: "expected a single rule".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 126 | linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line.: 1, |
| 127 | contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context.: "".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 128 | }], |
| 129 | })) |
| 130 | } |
| 131 | } |
| 132 | } |