pub struct LexerOfString<P, T, E>where
P: PosnInCharStream,{ /* private fields */ }
Expand description
This provides a type that wraps an allocated String, and which tracks the lines within the string. It then provides a method to create a LexerOfStr that borrows the text, and which can the be used as a crate::Lexer.
This type also implements the FmtContext trait, which allows for pretty-printing the text between certain lines, to highlight certain characters or regions of the text.
Should the LexerOfStr return an error while parsing then the FmtContext implementation can be used to generate a very useful error message with the context of the error.
This also applies should the LexerOfStr output be used in a further grammar - the methods to pretty-print the text require just start and end positions of the position type, which must support PosnInCharStream.
The LexerOfString may be used more than once, replacing its text if necessary before a new parse is initiated. The String can also be retrieved, which is useful for droppign the LexerOfString but retaining the text for future parse or compilation stages
Implementations§
Source§impl<P, T, E> LexerOfString<P, T, E>
impl<P, T, E> LexerOfString<P, T, E>
Sourcepub fn set_text<S: Into<String>>(self, text: S) -> Self
pub fn set_text<S: Into<String>>(self, text: S) -> Self
Set the text
Examples found in repository?
151fn main() -> Result<(), String> {
152 let args: Vec<String> = env::args().collect();
153 if args.len() < 2 {
154 return Err(format!("Usage: {} <expression>", args[0]));
155 }
156 let args_as_string = args[1..].join(" ");
157 let c = CalcTokenParser::new();
158 let l = LexerOfString::default().set_text(args_as_string);
159 let ts = l.lexer();
160
161 // let ts = TextStream::new(&args_as_string);
162
163 println!("Parsing");
164 let tokens = c.iter(&ts);
165 for t in tokens {
166 let t = {
167 match t {
168 Err(e) => {
169 println!();
170 let mut s = String::new();
171 l.fmt_context(&mut s, &e.pos, &e.pos).unwrap();
172 eprintln!("{}", s);
173 return Err(format!("{}", e));
174 }
175 Ok(t) => t,
176 }
177 };
178 print!("{}", t);
179 }
180 println!();
181 println!("Text parsed okay");
182 Ok(())
183}
More examples
201fn main() -> Result<(), String> {
202 let args: Vec<String> = env::args().collect();
203 if args.len() < 2 {
204 return Err(format!("Usage: {} <expression>", args[0]));
205 }
206 let args_as_string = args[1..].join(" ");
207
208 let mut parsers = ParserVec::new();
209 parsers.add_parser(|a, b, c| LexToken::parse_whitespace(a, b, c));
210 parsers.add_parser(|a, b, c| LexToken::parse_comment_line(a, b, c));
211 parsers.add_parser(|a, b, c| LexToken::parse_digits(a, b, c));
212 parsers.add_parser(|a, b, c| LexToken::parse_char(a, b, c));
213
214 let l = LexerOfString::default().set_text(args_as_string);
215 let ts = l.lexer();
216 let tokens = ts.iter(&parsers.parsers);
217
218 println!("Parsing");
219 for t in tokens {
220 let t = {
221 match t {
222 Err(e) => {
223 println!();
224 let mut s = String::new();
225 l.fmt_context(&mut s, &e.pos, &e.pos).unwrap();
226 eprintln!("{}", s);
227 return Err(format!("{}", e));
228 }
229 Ok(t) => t,
230 }
231 };
232 println!("{:?}", t);
233 }
234 println!();
235 println!("Text parsed okay");
236 Ok(())
237}
Sourcepub fn take_text(&mut self) -> String
pub fn take_text(&mut self) -> String
Take the text as a String out of the LexerOfString
Sourcepub fn lexer(&self) -> LexerOfStr<'_, P, T, E>
pub fn lexer(&self) -> LexerOfStr<'_, P, T, E>
Create a LexerOfStr that will parse the text
Examples found in repository?
151fn main() -> Result<(), String> {
152 let args: Vec<String> = env::args().collect();
153 if args.len() < 2 {
154 return Err(format!("Usage: {} <expression>", args[0]));
155 }
156 let args_as_string = args[1..].join(" ");
157 let c = CalcTokenParser::new();
158 let l = LexerOfString::default().set_text(args_as_string);
159 let ts = l.lexer();
160
161 // let ts = TextStream::new(&args_as_string);
162
163 println!("Parsing");
164 let tokens = c.iter(&ts);
165 for t in tokens {
166 let t = {
167 match t {
168 Err(e) => {
169 println!();
170 let mut s = String::new();
171 l.fmt_context(&mut s, &e.pos, &e.pos).unwrap();
172 eprintln!("{}", s);
173 return Err(format!("{}", e));
174 }
175 Ok(t) => t,
176 }
177 };
178 print!("{}", t);
179 }
180 println!();
181 println!("Text parsed okay");
182 Ok(())
183}
More examples
201fn main() -> Result<(), String> {
202 let args: Vec<String> = env::args().collect();
203 if args.len() < 2 {
204 return Err(format!("Usage: {} <expression>", args[0]));
205 }
206 let args_as_string = args[1..].join(" ");
207
208 let mut parsers = ParserVec::new();
209 parsers.add_parser(|a, b, c| LexToken::parse_whitespace(a, b, c));
210 parsers.add_parser(|a, b, c| LexToken::parse_comment_line(a, b, c));
211 parsers.add_parser(|a, b, c| LexToken::parse_digits(a, b, c));
212 parsers.add_parser(|a, b, c| LexToken::parse_char(a, b, c));
213
214 let l = LexerOfString::default().set_text(args_as_string);
215 let ts = l.lexer();
216 let tokens = ts.iter(&parsers.parsers);
217
218 println!("Parsing");
219 for t in tokens {
220 let t = {
221 match t {
222 Err(e) => {
223 println!();
224 let mut s = String::new();
225 l.fmt_context(&mut s, &e.pos, &e.pos).unwrap();
226 eprintln!("{}", s);
227 return Err(format!("{}", e));
228 }
229 Ok(t) => t,
230 }
231 };
232 println!("{:?}", t);
233 }
234 println!();
235 println!("Text parsed okay");
236 Ok(())
237}