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
138
use crate::{diag, diagnostics::Diagnostics, parser::syntax::make_loc};
use move_ir_types::location::*;
use move_symbol_pool::Symbol;
struct Context {
filename: Symbol,
start_offset: usize,
diags: Diagnostics,
}
impl Context {
fn new(filename: Symbol, start_offset: usize) -> Self {
Self {
filename,
start_offset,
diags: Diagnostics::new(),
}
}
fn error(&mut self, start: usize, end: usize, err_text: String) {
let loc = make_loc(
self.filename,
self.start_offset + 2 + start, self.start_offset + 2 + end,
);
self.diags
.add(diag!(Syntax::InvalidByteString, (loc, err_text)))
}
fn has_diags(&self) -> bool {
!self.diags.is_empty()
}
fn get_diags(self) -> Diagnostics {
self.diags
}
}
pub fn decode(loc: Loc, text: &str) -> Result<Vec<u8>, Diagnostics> {
let filename = loc.file();
let start_offset = loc.start() as usize;
let mut context = Context::new(filename, start_offset);
let mut buffer = vec![];
let chars: Vec<_> = text.chars().collect();
decode_(&mut context, &mut buffer, chars);
if !context.has_diags() {
Ok(buffer)
} else {
Err(context.get_diags())
}
}
fn decode_(context: &mut Context, buffer: &mut Vec<u8>, chars: Vec<char>) {
let len = chars.len();
let mut i = 0;
macro_rules! next_char {
() => {{
let c = chars[i];
i += 1;
c
}};
}
macro_rules! next_char_opt {
() => {{
if i < len {
Some(next_char!())
} else {
None
}
}};
}
while i < len {
let cur = i;
let c = next_char!();
if c != '\\' {
push(buffer, c);
continue;
}
match next_char!() {
'n' => push(buffer, '\n'),
'r' => push(buffer, '\r'),
't' => push(buffer, '\t'),
'\\' => push(buffer, '\\'),
'0' => push(buffer, '\0'),
'"' => push(buffer, '"'),
'x' => {
let d0_opt = next_char_opt!();
let d1_opt = next_char_opt!();
let hex = match (d0_opt, d1_opt) {
(Some(d0), Some(d1)) => {
let mut hex = String::new();
hex.push(d0);
hex.push(d1);
hex
}
(d0_opt @ Some(_), None) | (d0_opt @ None, None) => {
let h = match d0_opt {
Some(d0) => format!("{}", d0),
None => "".to_string(),
};
let err_text = format!(
"Invalid escape: '\\x{}'. Hex literals are represented by two \
symbols: [\\x00-\\xFF].",
h
);
context.error(cur, len, err_text);
return;
}
(None, Some(_)) => unreachable!(),
};
match hex::decode(hex) {
Ok(hex_buffer) => buffer.extend(hex_buffer),
Err(hex::FromHexError::InvalidHexCharacter { c, index }) => {
let err_text = format!("Invalid hexadecimal character: '{}'", c);
context.error(cur + 2 + index, cur + 2 + index, err_text);
}
Err(_) => unreachable!("ICE unexpected error parsing hex byte string value"),
}
}
c => {
context.error(cur, cur + 2, format!("Invalid escape sequence: '\\{}'", c));
}
}
}
}
fn push(buffer: &mut Vec<u8>, ch: char) {
assert!(ch.is_ascii(), "ICE ascii-only support is gated at parsing");
buffer.extend(vec![ch as u8]);
}