| 1 | //! Ungrammar -- a DSL for specifying concrete syntax tree grammar. |
| 2 | //! |
| 3 | //! Producing a parser is an explicit non-goal -- it's ok for this grammar to be |
| 4 | //! ambiguous, non LL, non LR, etc. |
| 5 | //! |
| 6 | //! See this |
| 7 | //! [introductory post](https://rust-analyzer.github.io/blog/2020/10/24/introducing-ungrammar.html) |
| 8 | //! for details. |
| 9 | |
| 10 | #![deny(missing_debug_implementations)] |
| 11 | #![deny(missing_docs)] |
| 12 | #![deny(rust_2018_idioms)] |
| 13 | |
| 14 | mod errorrust-analyzer cargo ungrammar 1.16.1 error/; |
| 15 | mod lexerrust-analyzer cargo ungrammar 1.16.1 lexer/; |
| 16 | mod parserrust-analyzer cargo ungrammar 1.16.1 parser/; |
| 17 | |
| 18 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/, strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#}; |
| 19 | |
| 20 | pub use errorrust-analyzer cargo ungrammar 1.16.1 error/::{Errorrust-analyzer cargo ungrammar 1.16.1 error/Error#, Resultrust-analyzer cargo ungrammar 1.16.1 error/Result#}; |
| 21 | |
| 22 | /// Returns a Rust grammar. |
| 23 | pub fn rust_grammarrust-analyzer cargo ungrammar 1.16.1 rust_grammar().() -> Grammarrust-analyzer cargo ungrammar 1.16.1 Grammar# { |
| 24 | let src = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("../rust.ungram"); |
| 25 | src.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 26 | } |
| 27 | |
| 28 | /// A node, like `A = 'b' | 'c'`. |
| 29 | /// |
| 30 | /// Indexing into a [`Grammar`] with a [`Node`] returns a reference to a |
| 31 | /// [`NodeData`]. |
| 32 | #[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:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, 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:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 33 | pub struct Noderust-analyzer cargo ungrammar 1.16.1 Node#(usize); |
| 34 | |
| 35 | /// A token, denoted with single quotes, like `'+'` or `'struct'`. |
| 36 | /// |
| 37 | /// Indexing into a [`Grammar`] with a [`Token`] returns a reference to a |
| 38 | /// [`TokenData`]. |
| 39 | #[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:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, 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:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 40 | pub struct Tokenrust-analyzer cargo ungrammar 1.16.1 Token#(usize); |
| 41 | |
| 42 | /// An Ungrammar grammar. |
| 43 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 44 | pub struct Grammarrust-analyzer cargo ungrammar 1.16.1 Grammar# { |
| 45 | nodesrust-analyzer cargo ungrammar 1.16.1 Grammar#nodes.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<NodeDatarust-analyzer cargo ungrammar 1.16.1 NodeData#>, |
| 46 | tokensrust-analyzer cargo ungrammar 1.16.1 Grammar#tokens.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TokenDatarust-analyzer cargo ungrammar 1.16.1 TokenData#>, |
| 47 | } |
| 48 | |
| 49 | impl FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for Grammarrust-analyzer cargo ungrammar 1.16.1 Grammar# { |
| 50 | type Errrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar][FromStr]Err# = Errorrust-analyzer cargo ungrammar 1.16.1 error/Error#; |
| 51 | fn from_strrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar][FromStr]from_str().(s: &str) -> Resultrust-analyzer cargo ungrammar 1.16.1 error/Result#<Selfrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar][FromStr]> { |
| 52 | let tokens = lexerrust-analyzer cargo ungrammar 1.16.1 lexer/::tokenizerust-analyzer cargo ungrammar 1.16.1 lexer/tokenize().(s)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 53 | parserrust-analyzer cargo ungrammar 1.16.1 parser/::parserust-analyzer cargo ungrammar 1.16.1 parser/parse().(tokens) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl Grammarrust-analyzer cargo ungrammar 1.16.1 Grammar# { |
| 58 | /// Returns an iterator over all nodes in the grammar. |
| 59 | pub fn iterrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar]iter().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = Noderust-analyzer cargo ungrammar 1.16.1 Node#> + '_ { |
| 60 | (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#self.nodesrust-analyzer cargo ungrammar 1.16.1 Grammar#nodes..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Noderust-analyzer cargo ungrammar 1.16.1 Node#) |
| 61 | } |
| 62 | |
| 63 | /// Returns an iterator over all tokens in the grammar. |
| 64 | pub fn tokensrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar]tokens().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = Tokenrust-analyzer cargo ungrammar 1.16.1 Token#> + '_ { |
| 65 | (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#self.tokensrust-analyzer cargo ungrammar 1.16.1 Grammar#tokens..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Tokenrust-analyzer cargo ungrammar 1.16.1 Token#) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Indexrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#<Noderust-analyzer cargo ungrammar 1.16.1 Node#> for Grammarrust-analyzer cargo ungrammar 1.16.1 Grammar# { |
| 70 | type Outputrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar][`Index<Node>`]Output# = NodeDatarust-analyzer cargo ungrammar 1.16.1 NodeData#; |
| 71 | fn indexrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar][`Index<Node>`]index().(&self, Noderust-analyzer cargo ungrammar 1.16.1 Node#(index): Noderust-analyzer cargo ungrammar 1.16.1 Node#) -> &NodeDatarust-analyzer cargo ungrammar 1.16.1 NodeData# { |
| 72 | &self.nodesrust-analyzer cargo ungrammar 1.16.1 Grammar#nodes.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Indexrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#<Tokenrust-analyzer cargo ungrammar 1.16.1 Token#> for Grammarrust-analyzer cargo ungrammar 1.16.1 Grammar# { |
| 77 | type Outputrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar][`Index<Token>`]Output# = TokenDatarust-analyzer cargo ungrammar 1.16.1 TokenData#; |
| 78 | fn indexrust-analyzer cargo ungrammar 1.16.1 impl#[Grammar][`Index<Token>`]index().(&self, Tokenrust-analyzer cargo ungrammar 1.16.1 Token#(index): Tokenrust-analyzer cargo ungrammar 1.16.1 Token#) -> &TokenDatarust-analyzer cargo ungrammar 1.16.1 TokenData# { |
| 79 | &self.tokensrust-analyzer cargo ungrammar 1.16.1 Grammar#tokens.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Data about a node. |
| 84 | #[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:)] |
| 85 | pub struct NodeDatarust-analyzer cargo ungrammar 1.16.1 NodeData# { |
| 86 | /// The name of the node. |
| 87 | /// |
| 88 | /// In the rule `A = 'b' | 'c'`, this is `"A"`. |
| 89 | pub namerust-analyzer cargo ungrammar 1.16.1 NodeData#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 90 | /// The rule for this node. |
| 91 | /// |
| 92 | /// In the rule `A = 'b' | 'c'`, this represents `'b' | 'c'`. |
| 93 | pub rulerust-analyzer cargo ungrammar 1.16.1 NodeData#rule.: Rulerust-analyzer cargo ungrammar 1.16.1 Rule#, |
| 94 | } |
| 95 | |
| 96 | /// Data about a token. |
| 97 | #[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:)] |
| 98 | pub struct TokenDatarust-analyzer cargo ungrammar 1.16.1 TokenData# { |
| 99 | /// The name of the token. |
| 100 | pub namerust-analyzer cargo ungrammar 1.16.1 TokenData#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 101 | } |
| 102 | |
| 103 | /// A production rule. |
| 104 | #[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:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 105 | pub enum Rulerust-analyzer cargo ungrammar 1.16.1 Rule# { |
| 106 | /// A labeled rule, like `a:B` (`"a"` is the label, `B` is the rule). |
| 107 | Labeledrust-analyzer cargo ungrammar 1.16.1 Rule#Labeled# { |
| 108 | /// The label. |
| 109 | labelrust-analyzer cargo ungrammar 1.16.1 Rule#Labeled#label.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 110 | /// The rule. |
| 111 | rulerust-analyzer cargo ungrammar 1.16.1 Rule#Labeled#rule.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<Rulerust-analyzer cargo ungrammar 1.16.1 Rule#>, |
| 112 | }, |
| 113 | /// A node, like `A`. |
| 114 | Noderust-analyzer cargo ungrammar 1.16.1 Rule#Node#(Noderust-analyzer cargo ungrammar 1.16.1 Node#), |
| 115 | /// A token, like `'struct'`. |
| 116 | Tokenrust-analyzer cargo ungrammar 1.16.1 Rule#Token#(Tokenrust-analyzer cargo ungrammar 1.16.1 Token#), |
| 117 | /// A sequence of rules, like `'while' '(' Expr ')' Stmt`. |
| 118 | Seqrust-analyzer cargo ungrammar 1.16.1 Rule#Seq#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Rulerust-analyzer cargo ungrammar 1.16.1 Rule#>), |
| 119 | /// An alternative between many rules, like `'+' | '-' | '*' | '/'`. |
| 120 | Altrust-analyzer cargo ungrammar 1.16.1 Rule#Alt#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Rulerust-analyzer cargo ungrammar 1.16.1 Rule#>), |
| 121 | /// An optional rule, like `A?`. |
| 122 | Optrust-analyzer cargo ungrammar 1.16.1 Rule#Opt#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<Rulerust-analyzer cargo ungrammar 1.16.1 Rule#>), |
| 123 | /// A repeated rule, like `A*`. |
| 124 | Reprust-analyzer cargo ungrammar 1.16.1 Rule#Rep#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<Rulerust-analyzer cargo ungrammar 1.16.1 Rule#>), |
| 125 | } |
| 126 | |
| 127 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 128 | fn smokerust-analyzer cargo ungrammar 1.16.1 smoke().() { |
| 129 | let grammar = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("../ungrammar.ungram"); |
| 130 | let grammar = grammar.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<Grammarrust-analyzer cargo ungrammar 1.16.1 Grammar#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 131 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(grammar) |
| 132 | } |
| 133 | |
| 134 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 135 | fn test_rust_grammarrust-analyzer cargo ungrammar 1.16.1 test_rust_grammar().() { |
| 136 | let _ = rust_grammarrust-analyzer cargo ungrammar 1.16.1 rust_grammar().(); |
| 137 | } |