1use std::result;
3
4use crate::kdiagnostic::KDiagnInfo;
5use crate::kproc_macros::KTokenStream;
6use crate::proc_macro::TokenTree;
7
8pub trait KParserTracer {
11 fn log(&self, msg: &str);
12}
13
14pub struct DummyTracer;
17
18impl KParserTracer for DummyTracer {
19 fn log(&self, _: &str) {}
20}
21
22pub type Result<T> = result::Result<T, KParserError>;
23
24#[derive(Debug)]
29pub struct KParserError {
30 dig: KDiagnInfo,
31}
32
33impl KParserError {
34 pub fn new(dig: KDiagnInfo) -> Self {
35 KParserError { dig }
36 }
37
38 pub fn with_msg(tok: TokenTree, msg: &str, line: String, file: String) -> Self {
39 let diag = KDiagnInfo::new(msg, tok, line, file);
40 Self::new(diag)
41 }
42
43 pub fn expect(expect_tok: &str, tok: &TokenTree, line: String, file: String) -> Result<()> {
44 if expect_tok != &tok.to_string() {
45 let msg = format!("expected `{expect_tok}` but got `{tok}`");
46 return Err(KParserError {
47 dig: KDiagnInfo::new(&msg, tok.to_owned(), line, file),
48 });
49 }
50 Ok(())
51 }
52
53 pub fn emit(self) {
54 self.dig.emit()
55 }
56
57 pub fn span(&self) -> TokenTree {
58 self.dig.span()
59 }
60}
61
62pub trait KParser {
65 fn parse<E>(&self, stream: &mut KTokenStream, tracer: &dyn KParserTracer) -> Result<E>;
68}