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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
// MRustC custom version of libproc_macro
//
// Unlike the rustc version, this one is designed to live complely detached from its compiler.
// See the
use std::fmt;
use std::str::FromStr;
pub struct TokenStream {
inner: Vec<Token>,
}
#[derive(Debug)]
enum Token {
Symbol(String),
Ident(String),
Lifetime(String),
String(String),
ByteString(Vec<u8>),
CharLit(char),
UnsignedInt(u128, u8), // Value, size (0=?,8,16,32,64,128)
SignedInt(i128, u8), // Value, size (0=?,8,16,32,64,128)
Float(f64, u8), // Value, size (0, 32, 64)
Fragment(FragmentType, u64), // Type and a key
}
#[repr(u8)]
#[derive(Copy,Clone,Debug)] // TODO: Is this just a mrustc thing?
enum FragmentType {
Ident = 0,
Tt = 1,
Path = 2,
Type = 3,
Expr = 4,
Statement = 5,
Block = 6,
Pattern = 7,
}
impl From<char> for Token {
fn from(v: char) -> Token {
Token::String(v.to_string())
}
}
macro_rules! some_else {
($e:expr => $alt:expr) => {match $e { Some(v) => v, None => $alt }};
}
struct CharStream<T: Iterator<Item=char>> {
inner: ::std::iter::Peekable<T>,
cur: Option<char>,
}
impl<T: Iterator<Item=char>> CharStream<T> {
fn new(i: T) -> Self {
CharStream {
inner: i.peekable(),
cur: Some(' '),
}
}
fn consume(&mut self) -> Option<char> {
self.cur = self.inner.next();
self.cur
}
fn is_complete(&self) -> bool {
self.cur.is_none()
}
fn cur(&self) -> char {
self.cur.unwrap()
}
fn next(&self) -> Option<char> {
self.inner.peek().cloned()
}
}
pub struct LexError {
inner: &'static str,
}
impl ::std::fmt::Debug for LexError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "LexError({})", self.inner)
}
}
impl FromStr for TokenStream {
type Err = LexError;
fn from_str(src: &str) -> Result<TokenStream, LexError> {
//eprintln!("TokenStream::from_str({:?})\r", src);
let rv = Vec::new();
let mut it = CharStream::new(src.chars());
fn err(s: &'static str) -> Result<TokenStream,LexError> {
Err(LexError { inner: s })
}
fn get_ident<T: Iterator<Item=char>>(it: &mut CharStream<T>, mut s: String) -> String
{
let mut c = it.cur();
while c.is_xid_continue() || c.is_digit(10)
{
s.push(c);
c = some_else!(it.consume() => break);
}
s
}
'outer: while ! it.is_complete()
{
let c = it.cur();
if c.is_whitespace() {
it.consume();
continue ;
}
let syms = [
b"!" as &[u8],
b"!=",
b"#",
b"$",
b"%", b"%=",
b"&", b"&&", b"&=",
b"(",
b")",
b"*", b"*=",
b"+", b"+=",
b",",
b"-", b"-=", b"->",
b".", b"..", b"...",
b"/", b"/=",
b":", b"::",
b";",
b"<", b"<-", b"<<", b"<<=", b"<=",
b"=", b"==", b"=>",
b">", b">=", b">>", b">>=",
b"?",
b"@",
b"[",
b"\\",
b"]",
b"^", b"^=",
b"`",
b"{",
b"|", b"|=", b"||",
b"}",
b"~",
];
if c == '\''
{
it.consume();
c = it.cur();
if c.is_xid_start() && it.next().map(|x| x != '\'').unwrap_or(true) {
// Lifetime
let ident = get_ident(&mut it, String::new());
rv.push(Token::Lifetime(ident))
}
else {
// Char lit
panic!("TODO: char literal");
}
}
else if c == '/' && it.next() == Some('/')
{
// Line comment
while it.consume() != Some('\n') {
}
}
else if c == '/' && it.next() == Some('*')
{
// Block comment
panic!("TODO: Block comment");
}
else
{
// byte or raw string literals
if c == 'b' || c == 'r' || c == '"' {
let mut c = c;
let is_byte = if c == 'b' {
c = some_else!(it.consume() => { rv.push(Token::Ident("b".into())); break });
true
} else {
false
};
if c == 'r'
{
// TODO: If this isn't a string, start parsing an ident instead.
let ident_str = if is_byte { "br" } else { "r" };
c = some_else!(it.consume() => { rv.push(Token::Ident(ident_str.into())); break });
let mut hashes = 0;
while c == '#' {
hashes += 1;
c = some_else!(it.consume() => return err("rawstr eof"));
}
if c != '"' {
if hashes == 0 {
let s = get_ident(&mut it, ident_str.to_string());
rv.push(Token::Ident(s));
}
else {
rv.push(Token::Ident(ident_str.into()));
}
while hashes > 0 {
rv.push(Token::Symbol("#".into()));
hashes -= 1;
}
continue 'outer;
}
let req_hashes = hashes;
let mut rawstr = String::new();
loop
{
c = some_else!(it.consume() => return err("Rawstr eof"));
if c == '"' {
let mut hashes = 0;
while hashes < req_hashes {
c = some_else!(it.consume() => return err("rawstr eof"));
if c != '#' { break ; }
hashes += 1;
}
if hashes != req_hashes {
rawstr.push('"');
for _ in 0 .. hashes {
rawstr.push('#');
}
rawstr.push(c);
}
else {
break ;
}
}
else {
rawstr.push(c);
}
}
rv.push(if is_byte { Token::ByteString(rawstr.into_bytes()) } else { Token::String(rawstr) });
continue 'outer;
}
else if c == '\''
{
// Byte character literal?
// NOTE: That b'foo is not `b` followed by `'foo`
assert!(is_byte);
panic!("TODO: Byte character literal");
}
else if c == '\"'
{
// String literal
let mut s = String::new();
loop
{
c = some_else!(it.consume() => return err("str eof"));
if c == '"' {
it.consume();
break ;
}
else if c == '\\' {
match some_else!(it.consume() => return err("str eof"))
{
c @ _ => panic!("Unknown escape in string {}", c),
}
}
else {
s.push(c);
}
}
rv.push(Token::String(s));
continue 'outer;
}
else
{
// Could be an ident starting with 'b', or it's just 'b'
// - Fall through
let ident = get_ident(&mut it, "b".into());
rv.push(Token::Ident(ident));
continue 'outer;
}
}
// Identifier.
if c.is_xid_start() || c == '_'
{
let ident = get_ident(&mut it, String::new());
if ident == "_" {
rv.push(Token::Symbol(ident));
}
else {
rv.push(Token::Ident(ident));
}
}
else if c.is_digit(10)
{
let base =
if c == '0' {
match it.consume()
{
Some('x') => { it.consume(); 16 },
Some('o') => { it.consume(); 8 },
Some('b') => { it.consume(); 2 },
_ => 10,
}
}
else {
10
};
let mut v = 0;
let mut c = it.cur();
'int: loop
{
while c == '_' {
c = some_else!( it.consume() => { break 'int; } );
}
if c == 'u' || c == 'i' {
let s = get_ident(&mut it, String::new());
match &*s
{
"u8" => rv.push(Token::UnsignedInt(v, 8)), "i8" => rv.push(Token::SignedInt(v as i128, 8)),
"u16" => rv.push(Token::UnsignedInt(v, 16)), "i16" => rv.push(Token::SignedInt(v as i128, 16)),
"u32" => rv.push(Token::UnsignedInt(v, 32)), "i32" => rv.push(Token::SignedInt(v as i128, 32)),
"u64" => rv.push(Token::UnsignedInt(v, 64)), "i64" => rv.push(Token::SignedInt(v as i128, 64)),
"u128" => rv.push(Token::UnsignedInt(v, 128)), "i128" => rv.push(Token::SignedInt(v as i128, 128)),
"usize" => rv.push(Token::UnsignedInt(v, 1)), "isize" => rv.push(Token::SignedInt(v as i128, 1)),
_ => return err("Unexpected integer suffix"),
}
continue 'outer;
}
else if let Some(d) = c.to_digit(base) {
v *= base as u128;
v += d as u128;
c = some_else!( it.consume() => { break 'int; } );
}
else if c == '.' {
panic!("TODO: Floating point");
}
else {
break;
}
}
rv.push(Token::UnsignedInt(v, 0));
continue 'outer;
}
// Punctuation?
else if c as u32 <= 0xFF
{
let mut start = match syms.iter().position(|v| v[0] == (c as u8))
{
Some(start) => start,
None => {
eprint!("Unknown operator character '{}'\r\n", c);
return err("Unknown operator")
},
};
let mut end = start+1;
while end < syms.len() && syms[end][0] == c as u8 {
end += 1;
}
let mut ofs = 1;
loop
{
let syms = &syms[start..end];
assert_eq!(ofs, syms[0].len(), "{:?}", syms[0]);
c = some_else!(it.consume() => break);
let step = match syms[1..].iter().position(|v| v[ofs] == (c as u8))
{
Some(s) => s+1,
None => break,
};
start += step;
end = start+1;
while end < syms.len() && syms[end][ofs] == c as u8 {
end += 1;
}
ofs += 1;
}
assert_eq!(syms[start].len(), ofs);
rv.push(Token::Symbol(::std::str::from_utf8(syms[start]).unwrap().into()));
}
else
{
return err("Unexpectec character");
}
}
}
Ok(TokenStream {
inner: rv,
})
}
}
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
impl fmt::Display for TokenStream {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for v in &self.inner
{
f.write_str(" ")?;
match v
{
&Token::Symbol(ref s) => s.fmt(f)?,
//&Token::Keyword(ref s) => s.fmt(f)?,
&Token::Ident(ref s) => s.fmt(f)?,
&Token::Lifetime(ref s) => write!(f, "'{}", s)?,
&Token::String(ref s) => write!(f, "{:?}", s)?,
&Token::ByteString(ref v) => {
f.write_str("b\"")?;
for &b in v {
if b != b'"' && b >= b' ' && b <= b'z' {
(b as char).fmt(f)?;
}
else {
write!(f, "\\x{:02x}", b)?;
}
}
f.write_str("\"")?;
},
&Token::CharLit(v) => match v
{
'\'' => f.write_str("'\\''")?,
'\0' => f.write_str("'\\0'")?,
'\n' => f.write_str("'\\n'")?,
' ' ... 'z' => write!(f, "'{}'", v)?,
_ => write!(f, "'\\u{:x}'", v as u32)?,
},
&Token::UnsignedInt(v,sz) => {
v.fmt(f)?;
if sz == 0 {
}
else if sz == 1 {
f.write_str("usize")?;
}
else {
f.write_str("u")?;
sz.fmt(f)?;
}
},
&Token::SignedInt(v,sz) => {
v.fmt(f)?;
if sz == 0 {
}
else if sz == 1 {
f.write_str("isize")?;
}
else {
f.write_str("i")?;
sz.fmt(f)?;
}
},
&Token::Float(v, sz) => {
v.fmt(f)?;
if sz == 0 {
}
else {
f.write_str("f")?;
sz.fmt(f)?;
}
},
&Token::Fragment(ty, key) => {
write!(f, "_{}_{:x}", ty as u8, key)?;
},
}
}
Ok( () )
}
}
/// Receive a token stream from the compiler
pub fn recv_token_stream() -> TokenStream
{
struct Reader<R> {
inner: R,
}
impl<R: ::std::io::Read> Reader<R> {
fn getb(&mut self) -> Option<u8> {
let mut b = [0];
match self.inner.read(&mut b)
{
Ok(1) => Some(b[0]),
Ok(0) => panic!("Unexpected EOF reading from stdin"),
Ok(_) => panic!("Bad byte count"),
Err(e) => panic!("Error reading from stdin - {}", e),
}
}
fn get_u128v(&mut self) -> u128 {
let mut ofs = 0;
let mut raw_rv = 0u128;
loop
{
let b = self.getb().unwrap();
raw_rv |= ((b & 0x7F) as u128) << ofs;
if b < 128 {
break;
}
assert!(ofs < 18); // at most 18 bytes needed for a i128
ofs += 7;
}
raw_rv
}
fn get_i128v(&mut self) -> i128 {
let raw_rv = self.get_u128v();
// Zig-zag encoding (0 = 0, 1 = -1, 2 = 1, ...)
if raw_rv & 1 != 0 {
-( (raw_rv >> 1) as i128 + 1 )
}
else {
(raw_rv >> 1) as i128
}
}
fn get_byte_vec(&mut self) -> Vec<u8> {
let size = self.get_u128v();
assert!(size < (1<<30));
let size = size as usize;
let mut buf = vec![0u8; size];
match self.inner.read_exact(&mut buf)
{
Ok(_) => {},
Err(e) => panic!("Error reading from stdin get_byte_vec({}) - {}", size, e),
}
buf
}
fn get_string(&mut self) -> String {
let raw = self.get_byte_vec();
String::from_utf8(raw).expect("Invalid UTF-8 passed from compiler")
}
fn get_f64(&mut self) -> f64 {
let mut buf = [0u8; 8];
match self.inner.read_exact(&mut buf)
{
Ok(_) => {},
Err(e) => panic!("Error reading from stdin - {}", e),
}
unsafe {
::std::mem::transmute(buf)
}
}
}
let mut s = Reader { inner: ::std::io::stdin().lock() };
let mut toks = Vec::new();
loop
{
let hdr_b = some_else!( s.getb() => break );
toks.push(match hdr_b
{
0 => {
let sym = s.get_string();
if sym == "" { break ; }
Token::Symbol( sym )
},
1 => Token::Ident( s.get_string() ),
2 => Token::Lifetime( s.get_string() ),
3 => Token::String( s.get_string() ),
4 => Token::ByteString( s.get_byte_vec() ),
5 => Token::CharLit(::std::char::from_u32(s.get_i128v() as u32).expect("char lit")),
6 => {
let ty = s.getb().expect("getb int ty");
Token::UnsignedInt(s.get_u128v(), ty)
},
7 => {
let ty = s.getb().expect("getb int ty");
Token::SignedInt(s.get_i128v(), ty)
},
8 => {
let ty = s.getb().expect("getb float ty");
Token::Float(s.get_f64(), ty)
}
});
eprintln!("> {:?}\r", toks.last().unwrap());
}
TokenStream {
inner: toks,
}
}
/// Send a token stream back to the compiler
pub fn send_token_stream(ts: TokenStream)
{
struct Writer<T> {
inner: T,
}
impl<T: ::std::io::Write> Writer<T> {
fn putb(&mut self, v: u8) {
let buf = [v];
self.inner.write(&buf).expect("");
}
fn put_u128v(&mut self, mut v: u128) {
while v > 128 {
self.putb( (v & 0x7F) as u8 | 0x80 );
v >>= 7;
}
self.putb( (v & 0x7F) as u8 );
}
fn put_i128v(&mut self, v: i128) {
if v < 0 {
self.put_u128v( (((v + 1) as u128) << 1) | 1 );
}
else {
self.put_u128v( (v as u128) << 1 );
}
}
fn put_bytes(&mut self, v: &[u8]) {
self.put_u128v(v.len() as u128);
self.inner.write(v).expect("");
}
fn put_f64(&mut self, v: f64) {
let buf: [u8; 8] = unsafe { ::std::mem::transmute(v) };
self.inner.write(&buf).expect("");
}
}
let mut s = Writer { inner: ::std::io::stdout().lock() };
for t in &ts.inner
{
//eprintln!("{:?}\r", t);
match t
{
&Token::Symbol(ref v) => { s.putb(0); s.put_bytes(v.as_bytes()); },
&Token::Ident(ref v) => { s.putb(1); s.put_bytes(v.as_bytes()); },
&Token::Lifetime(ref v) => { s.putb(2); s.put_bytes(v.as_bytes()); },
&Token::String(ref v) => { s.putb(3); s.put_bytes(v.as_bytes()); },
&Token::ByteString(ref v) => { s.putb(4); s.put_bytes(&v[..]); },
&Token::CharLit(v) => { s.putb(5); s.put_u128v(v as u32 as u128); },
&Token::UnsignedInt(v, sz) => { s.putb(6); s.putb(sz); s.put_u128v(v); },
&Token::SignedInt(v, sz) => { s.putb(7); s.putb(sz); s.put_i128v(v); },
&Token::Float(v, sz) => { s.putb(8); s.putb(sz); s.put_f64(v); },
&Token::Fragment(ty, key) => { s.putb(9); s.putb(ty as u8); s.put_u128v(key as u128); },
}
}
// Empty symbol indicates EOF
s.putb(0); s.putb(0);
drop(s);
::std::io::Write::flush(&mut ::std::io::stdout());
}
pub struct MacroDesc
{
name: &'static str,
handler: fn(TokenStream)->TokenStream,
}
pub fn main(macros: &[MacroDesc])
{
let mac_name = ::std::env::args().nth(1).expect("Was not passed a macro name");
eprintln!("Searching for macro {}\r", mac_name);
for m in macros
{
if m.name == mac_name {
use std::io::Write;
::std::io::stdout().write(&[0]);
::std::io::stdout().flush();
eprintln!("Waiting for input\r");
let input = recv_token_stream();
eprintln!("INPUT = `{}`\r", input);
let output = (m.handler)( input );
eprintln!("OUTPUT = `{}`\r", output);
send_token_stream(output);
eprintln!("Done");
return ;
}
}
panic!("Unknown macro name '{}'", mac_name);
}
|