This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cc
1561 lines (1341 loc) · 36.1 KB
/
parse.cc
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// language parser
// Copyright (C) 2014-2015 Serguei Makarov
//
// This file is part of EBT, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#include <string>
#include <set>
#include <stack>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <assert.h>
using namespace std;
#include "ir.h"
#include "parse.h"
#include "util.h"
////////////////////////////////////
// TODOXXX Language Features to Add
// - special assignment operators += -= etc.
// - more sophisticated 'foreach' loop
// - expression for 'return' should be optional
// - more sophisticated 'func' declaration (e.g. with type annotations)
// - shadow memory tables
// - statistical aggregates
// - command line arguments
// - basic try/catch error handling as in SystemTap
// --- pretty printing functions ---
std::ostream&
operator << (std::ostream& o, const source_loc& loc)
{
return o << loc.file->name << ":" << loc.line << ":" << loc.col;
}
std::ostream&
operator << (std::ostream& o, const token& tok)
{
string ttype;
switch (tok.type)
{
case tok_op: ttype = "tok_op"; break;
case tok_ident: ttype = "tok_ident"; break;
case tok_str: ttype = "tok_str"; break;
case tok_num: ttype = "tok_num"; break;
}
o << "\"";
for (unsigned i = 0; i < tok.content.length(); i++)
{
char c = tok.content[i]; o << (isprint (c) ? c : '?');
}
o << "\" at " << tok.location;
return o;
}
// --- error handling ---
struct parse_error: public runtime_error
{
const token* tok;
bool suppress_tok;
parse_error (const string& msg, bool suppress_tok = false)
: runtime_error (msg), tok(0), suppress_tok(suppress_tok) {}
parse_error (const string& msg, const token* t, bool suppress_tok = false)
: runtime_error (msg), tok(t), suppress_tok(suppress_tok) {}
};
// ------------------------
// --- lexing functions ---
// ------------------------
class lexer
{
private:
ebt_module *m;
ebt_file *f;
string input_contents;
unsigned input_size;
unsigned cursor;
unsigned curr_line;
unsigned curr_col;
std::vector<unsigned> line_coords;
int input_get();
int input_peek(unsigned offset = 0);
public:
lexer(ebt_module* m, ebt_file *f, istream& i);
static set<string> keywords;
static set<string> operators;
// Used to extract a subrange of the source code (e.g. for diagnostics):
unsigned get_pos();
string source(unsigned start, unsigned end);
string source_line(const token *tok);
token *scan();
// Clear unused last_tok and next_tok; skips any lookahead tokens:
void swallow();
// XXX check and re-check memory management for our tokens!
};
set<string> lexer::keywords;
set<string> lexer::operators;
lexer::lexer(ebt_module* m, ebt_file *f, istream& input)
: m(m), f(f), cursor(0), curr_line(1), curr_col(1)
{
getline(input, input_contents, '\0');
input_size = input_contents.size();
line_coords.push_back(0); // start of first line
if (keywords.empty())
{
keywords.insert("probe");
keywords.insert("global");
keywords.insert("array");
// XXX keywords.insert("shadow");
keywords.insert("func");
keywords.insert("not");
keywords.insert("and");
keywords.insert("or");
#ifdef ONLY_BASIC_PROBES
// Not really keywords, thus should be checked with swallow_ident():
//keywords.insert("begin");
//keywords.insert("end");
//keywords.insert("insn");
//keywords.insert("function");
//keywords.insert("entry");
//keywords.insert("exit");
#endif
keywords.insert("return");
keywords.insert("if");
keywords.insert("else");
keywords.insert("while");
keywords.insert("for");
keywords.insert("foreach");
keywords.insert("in");
keywords.insert("break");
keywords.insert("continue");
keywords.insert("next");
}
if (operators.empty())
{
// XXX: all operators currently at most 2 characters
operators.insert("{");
operators.insert("}");
operators.insert("(");
operators.insert(")");
operators.insert("[");
operators.insert("]");
operators.insert(",");
operators.insert(";");
operators.insert(".");
operators.insert("::");
operators.insert("$");
operators.insert("@");
operators.insert("*");
operators.insert("/");
operators.insert("%");
operators.insert("+");
operators.insert("-");
operators.insert("!");
operators.insert("~");
operators.insert(">>");
operators.insert("<<");
operators.insert("<");
operators.insert(">");
operators.insert("<=");
operators.insert(">=");
operators.insert("==");
operators.insert("!=");
operators.insert("&");
operators.insert("^");
operators.insert("|");
operators.insert("&&");
operators.insert("||");
operators.insert("?");
operators.insert(":");
operators.insert("="); // -- imperative assignment operation
operators.insert("+=");
operators.insert("-=");
operators.insert("*=");
operators.insert("/=");
operators.insert("%=");
operators.insert("<<=");
operators.insert(">>=");
operators.insert("&=");
operators.insert("^=");
operators.insert("|=");
operators.insert("++");
operators.insert("--");
}
}
unsigned
lexer::get_pos()
{
return cursor;
}
string
lexer::source(unsigned start, unsigned end)
{
assert (end >= start);
return input_contents.substr(start, end - start);
}
string
lexer::source_line(const token *tok)
{
// TODOXXX When we implement multi-file support, will need to fix
// this (as well as a whole bunch of other diagnostics stuff)
// to print the line number from the approriate file for each token.
unsigned line_num, line_start, line_next;
line_num = tok ? tok->location.line : line_coords.size();
retry:
line_start = line_coords[line_num-1];
line_next = line_start >= input_contents.size() ?
input_contents.size() : input_contents.find('\n', line_start);
// XXX When we are printing the last line:
if (tok == NULL
&& (line_next == string::npos || line_next - line_start <= 0))
{
line_start = line_next;
if (line_num > 0) { line_num --; goto retry; }
}
return input_contents.substr(line_start, line_next - line_start);
}
int
lexer::input_peek(unsigned offset)
{
if (cursor + offset >= input_size)
return -1; // EOF or failed read
return input_contents[cursor + offset];
}
int
lexer::input_get()
{
char c = input_peek();
if (c < 0) return c; // EOF
// advance cursor
cursor++;
if (c == '\n')
{
line_coords.push_back(cursor);
// -- XXX Recall the cursor shows the next character to look,
// i.e. the first character of the new line.
curr_line++; curr_col = 1;
}
else
curr_col++;
return c;
}
token *
lexer::scan()
{
token *t = new token;
t->location.file = f;
skip:
t->location.line = curr_line;
t->location.col = curr_col;
int c = input_get();
if (c < 0)
{ // end of file
delete t;
return NULL;
}
if (isspace (c))
goto skip;
int c2 = input_peek();
int c3 = input_peek(1);
if (c == '/' && c2 == '*') // found C style comment
{
(void) input_get(); // consume '*'
c = input_get(); c2 = input_get();
while (c2 >= 0)
{
if (c == '*' && c2 == '/') break;
c = c2; c2 = input_get();
}
if (c2 < 0) throw parse_error("unclosed comment"); // XXX
goto skip;
}
else if (c == '#' || (c == '/' && c2 == '/')) // found C++ or shell style comment
{
unsigned initial_line = curr_line;
do { c = input_get(); } while (c >= 0 && curr_line == initial_line);
goto skip;
}
if (isalpha(c) || c == '_') // found identifier
{
t->type = tok_ident;
t->content.push_back(c);
while (isalnum (c2) || c2 == '_')
{
input_get ();
t->content.push_back(c2);
c2 = input_peek ();
}
if (keywords.count(t->content))
t->type = tok_op;
return t;
}
else if (isdigit(c)) // found integer literal
{
t->type = tok_num;
t->content.push_back(c);
// XXX slurp alphanumeric chars first, figure out if it's a valid number later
while (isalnum (c2))
{
input_get ();
t->content.push_back(c2);
c2 = input_peek ();
}
return t;
}
else if (c == '\"') // found string literal
{
t->type = tok_str;
for (;;)
{
c = input_get();
if (c < 0 || c == '\n')
{
throw parse_error("could not find matching closing quote", t);
}
if (c == '\"')
break;
else if (c == '\\')
{
c = input_get();
switch (c)
{
case 'a':
case 'b':
case 't':
case 'n':
case 'v':
case 'f':
case 'r':
case 'x':
case '0' ... '7': // an octal code
case '\\':
// XXX Maintain these escapes in the string as-is, in
// case we want to emit the string as a C literal.
t->content.push_back('\\');
default:
t->content.push_back(c);
break;
}
}
else
t->content.push_back(c);
}
return t;
}
else if (ispunct (c)) // found at least a one-character operator
{
t->type = tok_op;
string s, s2, s3;
s.push_back(c); s2.push_back(c);
s2.push_back(c2);
if (operators.count(s3)) // valid three-character operator
{
input_get(); input_get(); // consume c2,c3
t->content = s3;
}
if (operators.count(s2)) // valid two-character operator
{
input_get(); // consume c2
t->content = s2;
}
else if (operators.count(s)) // valid single-character operator
{
t->content = s;
}
else
{
t->content = ispunct(c2) && ispunct(c3) ? s3
: ispunct(c2) ? s2 : s;
throw parse_error("unrecognized operator", t);
}
return t;
}
else // found an unrecognized symbol
{
t->type = tok_op;
ostringstream s;
s << "\\x" << hex << setw(2) << setfill('0') << c;
t->content = s.str();
throw parse_error("unexpected junk symbol", t);
}
}
// -------------------------
// --- parsing functions ---
// -------------------------
// Grammar Overview for the Language
//
// script ::= declaration script
// script ::=
//
#ifndef ONLY_BASIC_PROBES
// declaration ::= "probe" event_expr "{" smts "}"
#else
// declaration ::= "probe" probe_type ["(" [conditions] ")"] "{" stmts "}"
#endif
// declaration ::= "global" IDENTIFIER ["=" expr]
// declaration ::= "array" IDENTIFIER
// declaration ::= "func" IDENTIFIER "(" [params_spec] ")" "{" stmts "}"
// XXX declaration ::= "shadow" IDENTIFIER ":" NUMBER
//
#ifndef ONLY_BASIC_PROBES
// event_expr ::= [event_expr "."] IDENTIFIER
// event_expr ::= event_expr "(" expr ")"
// event_expr ::= "not" event_expr
// event_expr ::= event_expr "and" event_expr
// event_expr ::= event_expr "or" event_expr
// event_expr ::= event_expr "::" event_expr
#else
// probe_type ::= "begin"
// probe_type ::= "end"
// probe_type ::= "insn"
// probe_type ::= "function" "." "entry"
// probe_type ::= "function" "." "exit"
// XXX probe_type ::= ...
// conditions ::= expr ["," conditions]
#endif
//
// stmts ::= stmt stmts
// stmts ::=
//
// stmt ::= ";"
// stmt ::= "{" stmts "}"
// stmt ::= expr
// stmt ::= "return" expr
// stmt ::= "if" "(" expr ")" stmt ["else" stmt]
// stmt ::= "while" "(" expr ")" stmt
// stmt ::= "for" "(" [expr] ";" [expr] ";" [expr] ")" stmt
// stmt ::= "foreach" "(" IDENTITIER "in" expr ")" stmt
// stmt ::= "break" | "continue" | "next"
//
// expr ::= ["$" | "@"] designator
// expr ::= NUMBER
// expr ::= STRING
// expr ::= "(" expr ")"
// expr ::= UNARY expr
// expr ::= expr ["++" | "--"]
// expr ::= expr BINARY expr
// expr ::= expr "?" expr ":" expr
// expr ::= IDENTIFIER "(" [params] ")"
//
// params ::= expr ["," params]
// params_spec ::= IDENTIFIER ["," params_spec]
//
// designator ::= IDENTIFIER
// designator ::= designator "." IDENTIFIER
// designator ::= designator "." NUMBER
// designator ::= designator "[" expr "]"
class parser
{
private:
ebt_module *m;
ebt_file *f;
lexer input;
void print_error(const parse_error& pe);
void throw_expect_error(const string& expected_what, token *t = NULL);
// use to store lookahead:
token *last_tok;
token *next_tok;
token *peek(); // -- preview the next token
token *next(); // -- get the next token and advance forward
void swallow(); // -- get the next token, delete it, and advance forward
bool finished();
// These return true iff the next token is op:
bool peek_op(const string& op, token* &t, bool throw_err = false);
bool next_op(const string& op, token* &t, bool throw_err = false);
bool swallow_op(const string& op, bool throw_err = true);
// These return true iff the next token is an identifier:
bool peek_ident(token* &t, bool throw_err = false);
bool next_ident(token* &t, bool throw_err = false);
bool swallow_ident(bool throw_err = true);
bool swallow_ident(const string& text, bool throw_err = false);
// -- require exact text
public:
parser(ebt_module* m, const string& name, istream& i)
: m(m), f(new ebt_file(name, m)), input(lexer(m, f, i)),
last_tok(NULL), next_tok(NULL) {}
void test_lexer();
ebt_file *parse();
// Precedence table for expressions:
// 0 - IDENTIFIER, LITERAL
// 1 left - unary $ @
// (1.5 - unary postfix ++ --)
// 2 right - unary + - ! ~, unary prefix ++ --
// 3 left - * / %
// 4 left - + -
// 5 left - << >>
// 6 left - < <= > >=
// 7 left - == !=
// 8 left - &
// 9 left - ^
// 10 left - |
// 11 left - in
// 12 left - &&
// 13 left - ||
// 20 right - ?: // -- so `a ? b : c ? d : e` becomes `(a ? b : (c ? d : e))
// 21 right - = += -= *= /= %= <<= >>= &= ^= |=
// 22 left - ,
// 1000 - (not an operator)
#define PREC_NONE 1000
unsigned prec_expr(const string &op) {
// For binary operations only:
if (op == "*" || op == "/" || op == "%") return 3;
else if (op == "+" || op == "-") return 4;
else if (op == "<<" || op == ">>") return 5;
else if (op == "<" || op == "<=" || op == ">" || op == ">=") return 6;
else if (op == "==" || op == "!=") return 7;
else if (op == "&") return 8;
else if (op == "^") return 9;
else if (op == "|") return 10;
else if (op == "in") return 11;
else if (op == "&&") return 12;
else if (op == "||") return 13;
else if (op == "="
|| op == "+=" || op == "-=" || op == "*=" || op == "/="
|| op == "%=" || op == "<<=" || op == ">>="
|| op == "&=" || op == "^=" || op == "|=")
return 21;
else if (op == ",") return 22;
else return PREC_NONE;
}
expr *parse_basic_expr();
expr *parse_unary();
expr *parse_binary(); // -- all binary operators except assignment
expr *parse_ternary();
expr *parse_assignment();
expr *parse_comma();
expr *parse_expr();
stmt *parse_expr_stmt();
stmt *parse_ifthen_stmt();
stmt *parse_while_loop();
stmt *parse_for_loop();
stmt *parse_foreach_loop();
stmt *parse_compound_stmt();
stmt *parse_stmt();
std::vector<stmt *> *parse_block(std::vector <stmt *> *stmts = NULL);
// Precedence table for events:
// 0 - IDENTIFIER
// 1 left - EVENT . IDENTIFIER
// 2 right - EVENT ( EXPR )
// 3 left - not EVENT
// 4 left - EVENT and EVENT
// 5 left - EVENT or EVENT
// 6 left - EVENT :: EVENT
// 1000 - (not an event operator)
unsigned prec_event(const string& op)
{
if (op == "and") return 4;
else if (op == "or") return 5;
else if (op == "::") return 6;
else return PREC_NONE;
}
event_expr *parse_basic_event ();
event_expr *parse_event_not ();
event_expr *parse_binary_event ();
event_expr *parse_event_expr ();
#ifdef ONLY_BASIC_PROBES
basic_probe *parse_basic_probe_decl ();
#endif
probe *parse_probe_decl ();
ebt_function *parse_func_decl();
ebt_global *parse_global_decl();
ebt_global *parse_array_decl();
};
ebt_file *
parse (ebt_module* m, istream& i, const string source_name)
{
const std::string& name = source_name == "" ? m->script_name : source_name;
parser p (m, name, i);
return p.parse();
}
ebt_file *
parse (ebt_module* m, const string& n, const string source_name)
{
const std::string& name = source_name == "" ? m->script_name : source_name;
istringstream i (n);
parser p (m, name, i);
return p.parse();
}
void
test_lexer (ebt_module* m, istream& i, const string source_name)
{
const std::string& name = source_name == "" ? m->script_name : source_name;
parser p (m, name, i);
p.test_lexer();
}
void
test_lexer (ebt_module* m, const string& n, const string source_name)
{
const std::string& name = source_name == "" ? m->script_name : source_name;
istringstream i (n);
parser p (m, name, i);
p.test_lexer();
}
void
parser::test_lexer()
{
try
{
token *t;
while (t = peek())
{
cerr << *t << endl; swallow();
}
}
catch (const parse_error& pe)
{
print_error(pe);
}
}
// --- parser error handling ---
void
parser::print_error(const parse_error& pe)
{
cerr << "parse error:" << ' ' << pe.what() << endl;
if (pe.tok)
{
if (!pe.suppress_tok)
cerr << " " << *pe.tok << endl;
cerr << input.source_line(pe.tok) << endl;
// Point out the position of the token in the line:
for (unsigned i = 0; i < pe.tok->location.col - 1; i++)
cerr << " ";
cerr << "^" << endl;
}
// If pe.tok is unavailable and we are at end of input, print the last line:
else if (!pe.tok && peek() == NULL)
{
string last_line = input.source_line(NULL);
cerr << last_line << endl;
// Point out the end of the line:
for (unsigned i = 0; i < last_line.size(); i++)
cerr << " ";
cerr << "^" << endl;
}
// XXX If pe.tok is unavailable, we may want to use parser.last_tok
// XXX Optionally this could benefit from some syntax coloring
cerr << endl;
}
void
parser::throw_expect_error(const string& expected_what, token *t)
{
// By default, print the upcoming token as what was 'found':
if (t == NULL)
t = peek();
ostringstream what("");
what << "expected " << expected_what;
if (t)
// XXX Aligned to "parse error:"
what << ",\n " << "found " << *t;
// what << ", found " << *t;
// XXX We may want to only print newline if expected_what is long enough.
else
what << ", found end of input";
throw parse_error(what.str(),t,true);
}
// --- basic parser state management ---
token *
parser::next()
{
if (! next_tok) next_tok = input.scan();
if (! next_tok) throw parse_error("unexpected end of file");
last_tok = next_tok;
next_tok = NULL;
return last_tok;
}
token *
parser::peek()
{
if (! next_tok) next_tok = input.scan();
last_tok = next_tok;
return last_tok;
}
void
parser::swallow()
{
assert (last_tok != NULL);
delete last_tok;
last_tok = next_tok = NULL;
}
bool
parser::finished()
{
return peek() == NULL;
}
// --- additional token handling utilities ---
bool
parser::peek_op(const string& op, token* &t, bool throw_err)
{
t = peek();
bool valid = (t != NULL && t->type == tok_op && t->content == op);
if (!valid && throw_err)
{
ostringstream s(""); s << "'" << op << "'";
throw_expect_error(s.str());
}
return valid;
}
bool
parser::next_op(const string& op, token* &t, bool throw_err)
{
bool valid = peek_op(op,t,throw_err);
if (valid) next();
return valid;
}
bool
parser::swallow_op(const string& op, bool throw_err)
{
token *t;
bool valid = peek_op(op,t,throw_err);
if (valid) swallow();
return valid;
}
bool
parser::peek_ident(token* &t, bool throw_err)
{
t = peek();
bool valid = (t != NULL && t->type == tok_ident);
if (!valid && throw_err)
throw_expect_error("identifier");
return valid;
}
bool
parser::next_ident(token* &t, bool throw_err)
{
bool valid = peek_ident(t,throw_err);
if (valid) next();
return valid;
}
// XXX We may eventually want an analogous peek_ident() variant:
bool
parser::swallow_ident(const string& text, bool throw_err)
{
token *t;
bool valid = peek_ident(t,false) && t->content == text;
if (valid) swallow();
else if (throw_err)
{
ostringstream s(""); s << "'" << text << "'";
throw_expect_error(s.str());
}
return valid;
}
bool
parser::swallow_ident(bool throw_err)
{
token *t;
bool valid = peek_ident(t,throw_err);
if (valid) swallow();
return valid;
}
// --- parsing toplevel declarations ---
ebt_file *
parser::parse()
{
try
{
for (;;)
{
token *t;
if (peek_op("probe", t))
#ifdef ONLY_BASIC_PROBES
f->resolved_probes.push_back(parse_basic_probe_decl());
#else
f->probes.push_back(parse_probe_decl());
#endif
else if (peek_op("func", t))
{
ebt_function *fn = parse_func_decl();
f->functions[fn->name] = fn;
}
else if (peek_op("global", t))
{
ebt_global *gl = parse_global_decl();
f->globals[gl->name] = gl;
}
else if (peek_op("array", t))
{
ebt_global *gl = parse_array_decl();
f->globals[gl->name] = gl;
}
else if (finished())
break;
else
throw_expect_error("probe, func or global declaration");
}
}
catch (const parse_error& pe)
{
print_error(pe); return NULL;
}
return f;
}
#ifdef ONLY_BASIC_PROBES
basic_probe *
parser::parse_basic_probe_decl()
{
// unsigned probe_start = input.get_pos();
basic_probe *p = new basic_probe;
next_op("probe", p->tok, true);
// Identify the probe point:
if (swallow_ident("begin",false))
p->mechanism = EV_BEGIN;
else if (swallow_ident("end",false))
p->mechanism = EV_END;
else if (swallow_ident("insn",false))
p->mechanism = EV_INSN;
else if (swallow_ident("function",false))
{
swallow_op(".");
if (swallow_ident("entry",false))
p->mechanism = EV_FENTRY;
else if (swallow_ident("exit",false))
p->mechanism = EV_FEXIT;
else
throw_expect_error("entry or exit");
}
else
throw_expect_error("basic probe type "
"(begin, end, insn, function.entry, function.exit)");
// Parse condition chain:
if (swallow_op("(",false))
{
token *t;
if (!peek_op(")",t))
while (true)
{
condition *c = new condition;
c->id = p->get_variable_ticket();
c->e = parse_assignment(); // no comma operator allowed
p->conditions.push_back(c);
if (!swallow_op(",",false)) break;
}
swallow_op(")");
}
// unsigned probe_end = input.get_pos();
p->body = new handler;
p->body->id = f->get_handler_ticket();
p->body->action = parse_compound_stmt();
// XXX Only works for oneliner probes: p->body->orig_source = "probe " + input.source(probe_start, probe_end) + " { ... }";
p->body->orig_source = "probe " + input.source_line(p->tok) + " ...";
return p;
}
#endif
probe *
parser::parse_probe_decl()
{
// unsigned probe_start = input.get_pos();
probe *p = new probe;
next_op("probe", p->tok, true);
p->probe_point = parse_event_expr();
// unsigned probe_end = input.get_pos();
p->body = new handler;
p->body->id = f->get_handler_ticket();
p->body->action = parse_compound_stmt();
// XXX Only works for oneliner probes: p->body->orig_source = "probe " + input.source(probe_start, probe_end) + " { ... }";
p->body->orig_source = "probe " + input.source_line(p->tok) + " ...";
return p;
}
ebt_function *
parser::parse_func_decl()
{
ebt_function *fn = new ebt_function;
next_op("func", fn->tok, true);
fn->id = f->get_global_ticket();
// Parse function name:
next_ident(fn->tok,true);
fn->name = fn->tok->content;
// Parse function arguments:
swallow_op("(");