rune/grammar/
mod.rs
1mod classify;
6pub(crate) use self::classify::{classify, NodeClass};
7
8mod grammar;
9pub(crate) use self::grammar::{object_key, ws};
10
11mod parser;
12use self::parser::Checkpoint;
13
14mod tree;
15use self::tree::{inner_token, InternalChildren};
16pub(crate) use self::tree::{
17 Ignore, MaybeNode, Node, NodeAt, NodeId, Remaining, Stream, StreamBuf, Tree,
18};
19
20mod flavor;
21use self::flavor::Flavor;
22
23use crate::ast::Kind;
24use crate::macros::TokenStream;
25use crate::parse::Lexer;
26use crate::{compile, SourceId};
27
28use self::parser::{Parser, Source};
29
30pub(crate) fn text(source_id: SourceId, input: &str) -> Prepare<'_> {
32 Prepare::new(Input::Text(source_id, input))
33}
34
35pub(crate) fn token_stream(token_stream: &TokenStream) -> Prepare<'_> {
37 Prepare::new(Input::TokenStream(token_stream))
38}
39
40pub(crate) fn node(tree: Node<'_>) -> Prepare<'_> {
42 Prepare::new(Input::Node(tree))
43}
44
45enum Input<'a> {
46 Text(SourceId, &'a str),
47 TokenStream(&'a TokenStream),
48 Node(Node<'a>),
49}
50
51pub(crate) struct Prepare<'a> {
53 input: Input<'a>,
54 without_processing: bool,
55 include_whitespace: bool,
56 shebang: bool,
57}
58
59impl<'a> Prepare<'a> {
60 fn new(input: Input<'a>) -> Self {
61 Self {
62 input,
63 without_processing: false,
64 include_whitespace: false,
65 shebang: true,
66 }
67 }
68
69 #[cfg(feature = "fmt")]
71 pub(crate) fn without_processing(mut self) -> Self {
72 self.without_processing = true;
73 self
74 }
75
76 #[cfg(feature = "fmt")]
78 pub(crate) fn include_whitespace(mut self) -> Self {
79 self.include_whitespace = true;
80 self
81 }
82
83 pub(crate) fn root(self) -> compile::Result<Tree> {
85 let mut p = self.into_parser();
86 self::grammar::root(&mut p)?;
87 p.build()
88 }
89
90 pub(crate) fn exprs(self, separator: Kind) -> compile::Result<Tree> {
92 let mut p = self.into_parser();
93 self::grammar::exprs(&mut p, separator)?;
94 p.build()
95 }
96
97 pub(crate) fn format(self) -> compile::Result<Tree> {
99 let mut p = self.into_parser();
100 self::grammar::format(&mut p)?;
101 p.build()
102 }
103
104 fn into_parser(self) -> Parser<'a> {
105 let source = match self.input {
106 Input::Text(source_id, source) => {
107 let mut lexer = Lexer::new(source, source_id, self.shebang);
108
109 if self.without_processing {
110 lexer = lexer.without_processing();
111 }
112
113 Source::lexer(lexer)
114 }
115 Input::TokenStream(token_stream) => Source::token_stream(token_stream.iter()),
116 Input::Node(node) => Source::node(node),
117 };
118
119 let mut p = Parser::new(source);
120 p.include_whitespace(self.include_whitespace);
121 p
122 }
123}