71 HaskellValue::Number(value) => write!(f, "{}", value), |
71 HaskellValue::Number(value) => write!(f, "{}", value), |
72 HaskellValue::String(value) => write_text(f, value), |
72 HaskellValue::String(value) => write_text(f, value), |
73 HaskellValue::Tuple(items) => write_sequence(f, b"()", items.iter()), |
73 HaskellValue::Tuple(items) => write_sequence(f, b"()", items.iter()), |
74 HaskellValue::List(items) => write_sequence(f, b"[]", items.iter()), |
74 HaskellValue::List(items) => write_sequence(f, b"[]", items.iter()), |
75 HaskellValue::AnonStruct { name, fields } => { |
75 HaskellValue::AnonStruct { name, fields } => { |
76 write!(f, "{} ", name); |
76 write!(f, "{} ", name)?; |
77 write_sequence(f, b" \0", fields.iter()) |
77 write_sequence(f, b" \0", fields.iter()) |
78 } |
78 } |
79 HaskellValue::Struct { name, fields } => { |
79 HaskellValue::Struct { name, fields } => { |
80 write!(f, "{} {{", name); |
80 write!(f, "{} {{", name)?; |
81 let fields = fields.iter().collect::<Vec<_>>(); |
81 let fields = fields.iter().collect::<Vec<_>>(); |
82 let mut items = fields.iter(); |
82 let mut items = fields.iter(); |
83 while let Some((field_name, value)) = items.next() { |
83 while let Some((field_name, value)) = items.next() { |
84 write!(f, "{} = {}", field_name, value); |
84 write!(f, "{} = {}", field_name, value)?; |
85 if !items.as_slice().is_empty() { |
85 if !items.as_slice().is_empty() { |
86 write!(f, ", ")?; |
86 write!(f, ", ")?; |
87 } |
87 } |
88 } |
88 } |
89 write!(f, "}}") |
89 write!(f, "}}") |
124 |
124 |
125 fn number(input: &[u8]) -> HaskellResult<HaskellValue> { |
125 fn number(input: &[u8]) -> HaskellResult<HaskellValue> { |
126 map(number_raw, HaskellValue::Number)(input) |
126 map(number_raw, HaskellValue::Number)(input) |
127 } |
127 } |
128 |
128 |
129 const BYTES: &[u8] = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; |
129 enum Escape { |
130 |
130 Empty, |
131 fn string_escape(input: &[u8]) -> HaskellResult<&[u8]> { |
131 Byte(u8), |
|
132 } |
|
133 |
|
134 impl ExtendInto for Escape { |
|
135 type Item = u8; |
|
136 type Extender = Vec<u8>; |
|
137 |
|
138 fn new_builder(&self) -> Self::Extender { |
|
139 Vec::new() |
|
140 } |
|
141 |
|
142 fn extend_into(&self, acc: &mut Self::Extender) { |
|
143 if let Escape::Byte(b) = self { |
|
144 acc.push(*b); |
|
145 } |
|
146 } |
|
147 } |
|
148 |
|
149 impl Extend<Escape> for Vec<u8> { |
|
150 fn extend<T: IntoIterator<Item = Escape>>(&mut self, iter: T) { |
|
151 for item in iter { |
|
152 item.extend_into(self); |
|
153 } |
|
154 } |
|
155 } |
|
156 |
|
157 fn string_escape(input: &[u8]) -> HaskellResult<Escape> { |
|
158 use Escape::*; |
132 alt(( |
159 alt(( |
133 map(number_raw, |n| &BYTES[n as usize..(n + 1) as usize]), |
160 map(number_raw, |n| Byte(n)), |
134 alt(( |
161 alt(( |
135 map(tag("\\"), |_| &b"\\"[..]), |
162 map(tag("\\"), |_| Byte(b'\\')), |
136 map(tag("\""), |_| &b"\""[..]), |
163 map(tag("\""), |_| Byte(b'\"')), |
137 map(tag("'"), |_| &b"'"[..]), |
164 map(tag("'"), |_| Byte(b'\'')), |
138 map(tag("n"), |_| &b"\n"[..]), |
165 map(tag("n"), |_| Byte(b'\n')), |
139 map(tag("r"), |_| &b"\r"[..]), |
166 map(tag("r"), |_| Byte(b'\r')), |
140 map(tag("t"), |_| &b"\t"[..]), |
167 map(tag("t"), |_| Byte(b'\t')), |
141 map(tag("a"), |_| &b"\x07"[..]), |
168 map(tag("a"), |_| Byte(b'\x07')), |
142 map(tag("b"), |_| &b"\x08"[..]), |
169 map(tag("b"), |_| Byte(b'\x08')), |
143 map(tag("v"), |_| &b"\x0B"[..]), |
170 map(tag("v"), |_| Byte(b'\x0B')), |
144 map(tag("f"), |_| &b"\x0C"[..]), |
171 map(tag("f"), |_| Byte(b'\x0C')), |
145 map(tag("&"), |_| &b""[..]), |
172 map(tag("&"), |_| Empty), |
146 map(tag("NUL"), |_| &b"\x00"[..]), |
173 map(tag("NUL"), |_| Byte(b'\x00')), |
147 map(tag("SOH"), |_| &b"\x01"[..]), |
174 map(tag("SOH"), |_| Byte(b'\x01')), |
148 map(tag("STX"), |_| &b"\x02"[..]), |
175 map(tag("STX"), |_| Byte(b'\x02')), |
149 map(tag("ETX"), |_| &b"\x03"[..]), |
176 map(tag("ETX"), |_| Byte(b'\x03')), |
150 map(tag("EOT"), |_| &b"\x04"[..]), |
177 map(tag("EOT"), |_| Byte(b'\x04')), |
151 map(tag("ENQ"), |_| &b"\x05"[..]), |
178 map(tag("ENQ"), |_| Byte(b'\x05')), |
152 map(tag("ACK"), |_| &b"\x06"[..]), |
179 map(tag("ACK"), |_| Byte(b'\x06')), |
153 )), |
180 )), |
154 alt(( |
181 alt(( |
155 map(tag("SO"), |_| &b"\x0E"[..]), |
182 map(tag("SO"), |_| Byte(b'\x0E')), |
156 map(tag("SI"), |_| &b"\x0F"[..]), |
183 map(tag("SI"), |_| Byte(b'\x0F')), |
157 map(tag("DLE"), |_| &b"\x10"[..]), |
184 map(tag("DLE"), |_| Byte(b'\x10')), |
158 map(tag("DC1"), |_| &b"\x11"[..]), |
185 map(tag("DC1"), |_| Byte(b'\x11')), |
159 map(tag("DC2"), |_| &b"\x12"[..]), |
186 map(tag("DC2"), |_| Byte(b'\x12')), |
160 map(tag("DC3"), |_| &b"\x13"[..]), |
187 map(tag("DC3"), |_| Byte(b'\x13')), |
161 map(tag("DC4"), |_| &b"\x14"[..]), |
188 map(tag("DC4"), |_| Byte(b'\x14')), |
162 map(tag("NAK"), |_| &b"\x15"[..]), |
189 map(tag("NAK"), |_| Byte(b'\x15')), |
163 map(tag("SYN"), |_| &b"\x16"[..]), |
190 map(tag("SYN"), |_| Byte(b'\x16')), |
164 map(tag("ETB"), |_| &b"\x17"[..]), |
191 map(tag("ETB"), |_| Byte(b'\x17')), |
165 map(tag("CAN"), |_| &b"\x18"[..]), |
192 map(tag("CAN"), |_| Byte(b'\x18')), |
166 map(tag("EM"), |_| &b"\x19"[..]), |
193 map(tag("EM"), |_| Byte(b'\x19')), |
167 map(tag("SUB"), |_| &b"\x1A"[..]), |
194 map(tag("SUB"), |_| Byte(b'\x1A')), |
168 map(tag("ESC"), |_| &b"\x1B"[..]), |
195 map(tag("ESC"), |_| Byte(b'\x1B')), |
169 map(tag("FS"), |_| &b"\x1C"[..]), |
196 map(tag("FS"), |_| Byte(b'\x1C')), |
170 map(tag("GS"), |_| &b"\x1D"[..]), |
197 map(tag("GS"), |_| Byte(b'\x1D')), |
171 map(tag("RS"), |_| &b"\x1E"[..]), |
198 map(tag("RS"), |_| Byte(b'\x1E')), |
172 map(tag("US"), |_| &b"\x1F"[..]), |
199 map(tag("US"), |_| Byte(b'\x1F')), |
173 map(tag("DEL"), |_| &b"\x7F"[..]), |
200 map(tag("DEL"), |_| Byte(b'\x7F')), |
174 )), |
201 )), |
175 ))(input) |
202 ))(input) |
176 } |
203 } |
177 |
204 |
178 fn string_content(mut input: &[u8]) -> HaskellResult<String> { |
205 fn string_content(mut input: &[u8]) -> HaskellResult<String> { |