-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcommand.rs
137 lines (126 loc) · 4.94 KB
/
command.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::io::Write;
use codemap::CodeMap;
use codemap_diagnostic::{ColorConfig, Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
use rust_sitter::errors::{ParseError, ParseErrorReason};
#[rust_sitter::grammar("command")]
pub mod grammar {
#[rust_sitter::language]
pub enum CommandExpr {
StepInto(#[rust_sitter::leaf(text = "t")] ()),
Go(#[rust_sitter::leaf(text = "g")] ()),
SetBreakpoint(#[rust_sitter::leaf(text = "bp")] (), Box<EvalExpr>),
ListBreakpoints(#[rust_sitter::leaf(text = "bl")] ()),
ClearBreakpoint(#[rust_sitter::leaf(text = "bc")] (), Box<EvalExpr>),
DisplayRegisters(#[rust_sitter::leaf(text = "r")] ()),
DisplayBytes(#[rust_sitter::leaf(text = "db")] (), Box<EvalExpr>),
Evaluate(#[rust_sitter::leaf(text = "?")] (), Box<EvalExpr>),
ListNearest(#[rust_sitter::leaf(text = "ln")] (), Box<EvalExpr>),
Quit(#[rust_sitter::leaf(text = "q")] ()),
}
#[rust_sitter::language]
pub enum EvalExpr {
Number(#[rust_sitter::leaf(pattern = r"(\d+|0x[0-9a-fA-F]+)", transform = parse_int)] u64),
Symbol(#[rust_sitter::leaf(pattern = r"(([a-zA-Z0-9_@#.]+!)?[a-zA-Z0-9_@#.]+)", transform = parse_sym)] String),
#[rust_sitter::prec_left(1)]
Add(
Box<EvalExpr>,
#[rust_sitter::leaf(text = "+")] (),
Box<EvalExpr>,
),
}
#[rust_sitter::extra]
struct Whitespace {
#[rust_sitter::leaf(pattern = r"\s")]
_whitespace: (),
}
fn parse_int(text: &str) -> u64 {
let text = text.trim();
if text.starts_with("0x") {
let text = text.split_at(2).1;
u64::from_str_radix(text, 16).unwrap()
} else {
text.parse().unwrap()
}
}
fn parse_sym(text: &str) -> String {
text.to_owned()
}
}
// This came from https://github.com/hydro-project/rust-sitter/blob/main/example/src/main.rs
fn convert_parse_error_to_diagnostics(
file_span: &codemap::Span,
error: &ParseError,
diagnostics: &mut Vec<Diagnostic>,
) {
match &error.reason {
ParseErrorReason::MissingToken(tok) => diagnostics.push(Diagnostic {
level: Level::Error,
message: format!("Missing token: \"{tok}\""),
code: Some("S000".to_string()),
spans: vec![SpanLabel {
span: file_span.subspan(error.start as u64, error.end as u64),
style: SpanStyle::Primary,
label: Some(format!("missing \"{tok}\"")),
}],
}),
ParseErrorReason::UnexpectedToken(tok) => diagnostics.push(Diagnostic {
level: Level::Error,
message: format!("Unexpected token: \"{tok}\""),
code: Some("S000".to_string()),
spans: vec![SpanLabel {
span: file_span.subspan(error.start as u64, error.end as u64),
style: SpanStyle::Primary,
label: Some(format!("unexpected \"{tok}\"")),
}],
}),
ParseErrorReason::FailedNode(errors) => {
if errors.is_empty() {
diagnostics.push(Diagnostic {
level: Level::Error,
message: "Failed to parse node".to_string(),
code: Some("S000".to_string()),
spans: vec![SpanLabel {
span: file_span.subspan(error.start as u64, error.end as u64),
style: SpanStyle::Primary,
label: Some("failed".to_string()),
}],
})
} else {
for error in errors {
convert_parse_error_to_diagnostics(file_span, error, diagnostics);
}
}
}
}
}
pub fn read_command() -> grammar::CommandExpr {
let stdin = std::io::stdin();
loop {
print!("> ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
stdin.read_line(&mut input).unwrap();
let input = input.trim().to_string();
if !input.is_empty() {
let cmd = grammar::parse(&input);
match cmd {
Ok(c) => return c,
Err(errs) => {
// This came from https://github.com/hydro-project/rust-sitter/blob/main/example/src/main.rs
let mut codemap = CodeMap::new();
let file_span = codemap.add_file("<input>".to_string(), input.to_string());
let mut diagnostics = vec![];
for error in errs {
convert_parse_error_to_diagnostics(
&file_span.span,
&error,
&mut diagnostics,
);
}
let mut emitter = Emitter::stderr(ColorConfig::Always, Some(&codemap));
emitter.emit(&diagnostics);
}
}
}
}
}