1 : // -*- C++ -*-
2 : // Copyright (C) 2005-2007 Red Hat Inc.
3 : // Copyright (C) 2007 Bull S.A.S
4 : //
5 : // This file is part of systemtap, and is free software. You can
6 : // redistribute it and/or modify it under the terms of the GNU General
7 : // Public License (GPL); either version 2, or (at your option) any
8 : // later version.
9 :
10 :
11 : #ifndef PARSE_H
12 : #define PARSE_H
13 :
14 : #include <string>
15 : #include <fstream>
16 : #include <iostream>
17 : #include <vector>
18 : #include <stdexcept>
19 :
20 :
21 : struct source_loc
22 61314712 : {
23 : std::string file;
24 : unsigned line;
25 : unsigned column;
26 : };
27 :
28 : std::ostream& operator << (std::ostream& o, const source_loc& loc);
29 :
30 : enum parse_context
31 : {
32 : con_unknown, con_probe, con_global, con_function, con_embedded
33 : };
34 :
35 :
36 : enum token_type
37 : {
38 : tok_junk, tok_identifier, tok_operator, tok_string, tok_number,
39 : tok_embedded, tok_keyword
40 : };
41 :
42 :
43 : struct token
44 61314712 : {
45 : source_loc location;
46 : token_type type;
47 : std::string content;
48 : };
49 :
50 :
51 : std::ostream& operator << (std::ostream& o, const token& t);
52 :
53 :
54 : struct parse_error: public std::runtime_error
55 121 : {
56 : const token* tok;
57 : bool skip_some;
58 105 : parse_error (const std::string& msg):
59 105 : runtime_error (msg), tok (0), skip_some (true) {}
60 14 : parse_error (const std::string& msg, const token* t):
61 14 : runtime_error (msg), tok (t), skip_some (true) {}
62 2 : parse_error (const std::string& msg, bool skip):
63 2 : runtime_error (msg), tok (0), skip_some (skip) {}
64 : };
65 :
66 :
67 : struct systemtap_session;
68 :
69 : class lexer
70 42204 : {
71 : public:
72 : token* scan (bool wildcard=false, bool expand_args=true);
73 : lexer (std::istream&, const std::string&, systemtap_session&);
74 :
75 : private:
76 : int input_get ();
77 : void input_put (int);
78 : void input_put (const std::string&);
79 : int input_peek (unsigned n=0);
80 : std::istream& input;
81 : std::string input_name;
82 : std::vector<int> lookahead;
83 : unsigned cursor_suspend_count;
84 : unsigned cursor_line;
85 : unsigned cursor_column;
86 : systemtap_session& session;
87 : };
88 :
89 :
90 : struct stapfile;
91 : struct probe;
92 : struct probe_alias;
93 : struct vardecl;
94 : struct functiondecl;
95 : struct embeddedcode;
96 : struct probe_point;
97 : struct literal;
98 : struct block;
99 : struct for_loop;
100 : struct statement;
101 : struct if_statement;
102 : struct foreach_loop;
103 : struct expr_statement;
104 : struct return_statement;
105 : struct delete_statement;
106 : struct break_statement;
107 : struct next_statement;
108 : struct continue_statement;
109 : struct indexable;
110 : struct expression;
111 : struct hist_op;
112 :
113 : class parser
114 : {
115 : public:
116 : parser (systemtap_session& s, std::istream& i, bool p);
117 : parser (systemtap_session& s, const std::string& n, bool p);
118 : ~parser ();
119 :
120 : stapfile* parse ();
121 :
122 : static stapfile* parse (systemtap_session& s, std::istream& i, bool privileged);
123 : static stapfile* parse (systemtap_session& s, const std::string& n, bool privileged);
124 :
125 : private:
126 : systemtap_session& session;
127 : std::string input_name;
128 : std::istream* free_input;
129 : lexer input;
130 : bool privileged;
131 : parse_context context;
132 :
133 : // preprocessing subordinate
134 : std::vector<const token*> enqueued_pp;
135 : const token* scan_pp (bool wildcard=false, bool expand_args=true);
136 :
137 : // scanning state
138 : const token* last ();
139 : const token* next (bool wildcard=false);
140 : const token* peek (bool wildcard=false);
141 :
142 : const token* last_t; // the last value returned by peek() or next()
143 : const token* next_t; // lookahead token
144 :
145 : // expectations
146 : const token* expect_known (token_type tt, std::string const & expected);
147 : const token* expect_unknown (token_type tt, std::string & target);
148 : const token* expect_unknown2 (token_type tt1, token_type tt2,
149 : std::string & target);
150 :
151 : // convenience forms
152 : const token* expect_op (std::string const & expected);
153 : const token* expect_kw (std::string const & expected);
154 : const token* expect_number (int64_t & expected);
155 : const token* expect_ident (std::string & target);
156 : const token* expect_ident_or_keyword (std::string & target);
157 : bool peek_op (std::string const & op);
158 : bool peek_kw (std::string const & kw);
159 :
160 : void print_error (const parse_error& pe);
161 : unsigned num_errors;
162 :
163 : private: // nonterminals
164 : void parse_probe (std::vector<probe*>&, std::vector<probe_alias*>&);
165 : void parse_global (std::vector<vardecl*>&, std::vector<probe*>&);
166 : void parse_functiondecl (std::vector<functiondecl*>&);
167 : embeddedcode* parse_embeddedcode ();
168 : probe_point* parse_probe_point ();
169 : literal* parse_literal ();
170 : block* parse_stmt_block ();
171 : statement* parse_statement ();
172 : if_statement* parse_if_statement ();
173 : for_loop* parse_for_loop ();
174 : for_loop* parse_while_loop ();
175 : foreach_loop* parse_foreach_loop ();
176 : expr_statement* parse_expr_statement ();
177 : return_statement* parse_return_statement ();
178 : delete_statement* parse_delete_statement ();
179 : next_statement* parse_next_statement ();
180 : break_statement* parse_break_statement ();
181 : continue_statement* parse_continue_statement ();
182 : indexable* parse_indexable ();
183 : const token *parse_hist_op_or_bare_name (hist_op *&hop, std::string &name);
184 : expression* parse_expression ();
185 : expression* parse_assignment ();
186 : expression* parse_ternary ();
187 : expression* parse_logical_or ();
188 : expression* parse_logical_and ();
189 : expression* parse_boolean_or ();
190 : expression* parse_boolean_xor ();
191 : expression* parse_boolean_and ();
192 : expression* parse_array_in ();
193 : expression* parse_comparison ();
194 : expression* parse_shift ();
195 : expression* parse_concatenation ();
196 : expression* parse_additive ();
197 : expression* parse_multiplicative ();
198 : expression* parse_unary ();
199 : expression* parse_crement ();
200 : expression* parse_value ();
201 : expression* parse_symbol ();
202 : };
203 :
204 :
205 :
206 : #endif // PARSE_H
|