aboutsummaryrefslogtreecommitdiff
blob: 182f6c32c6f421308ac7abe74af9e3da5baa89b9 (plain)
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
/*
   Please use git log for copyright holder and year information

   This file is part of libbash.

   libbash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   libbash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
*/
tree grammar libbashWalker;

options
{
	language = C;
	tokenVocab = libbash;
	ASTLabelType = pANTLR3_BASE_TREE;
}

@includes{

	#include <memory>
	#include <string>
	#include <vector>

	class interpreter;
	void set_interpreter(std::shared_ptr<interpreter> w);

}

@postinclude{

	#include <iostream>

	#include <boost/format.hpp>

	#include "core/interpreter.h"

}

@members{

	static std::shared_ptr<interpreter> walker;

	void set_interpreter(std::shared_ptr<interpreter> w)
	{
		walker = w;
	}

	inline void set_index(const std::string& name, unsigned& index, int value)
	{
		if(value < 0)
			throw interpreter_exception((boost::format("Array index is less than 0: \%s[\%d]") \% name \% value).str());
		index = value;
	}

	// Recursively count number of nodes of curr
	static int count_nodes(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE curr)
	{
		int child_count = adaptor->getChildCount(adaptor, curr);
		if(child_count == 0)
		{
			// Leaf node
			return 1;
		}
		else
		{
			int result = 0;
			// Count every child
			for(int i = 0; i != child_count; ++i)
				result += count_nodes(adaptor, (pANTLR3_BASE_TREE)(adaptor->getChild(adaptor, curr, i)));
			// Add itself, DOWN and UP
			return result + 3;
		}
	}

}

start: list|EOF;

list: ^(LIST (function_def|logic_command_list)+);

variable_definitions: ^(VARIABLE_DEFINITIONS var_def+);

name_base returns[std::string libbash_value]
	:NAME { $libbash_value = walker->get_string($NAME); }
	|LETTER { $libbash_value = walker->get_string($LETTER); }
	|'_' { $libbash_value="_"; };

name returns[std::string libbash_value, unsigned index]
@init {
	$index = 0;
}
	:^(libbash_name=name_base value=arithmetics) {
		set_index(libbash_name, $index, value);
		$libbash_value = libbash_name;
	}
	|libbash_name=name_base {
		$libbash_value = libbash_name;
	};

num returns[std::string libbash_value]
options{ k=1; }
	:DIGIT { $libbash_value = walker->get_string($DIGIT); }
	|NUMBER { $libbash_value = walker->get_string($NUMBER); };

var_def
@declarations {
	std::map<int, std::string> values;
	unsigned index = 0;
	bool is_null = true;
}
	:^(EQUALS name (string_expr { is_null = false; })?) {
		walker->set_value($name.libbash_value, $string_expr.libbash_value, $name.index, is_null);
	}
	|^(EQUALS libbash_name=name_base ^(ARRAY (
										(expr=string_expr
										|^(EQUALS value=arithmetics {
												set_index(libbash_name, index, value);
											} expr=string_expr))
										{ values[index++] = expr.libbash_value; })*
									 )){
		walker->define(libbash_name, values);
	};

string_expr returns[std::string libbash_value, bool quoted]
	:^(STRING(
		(DOUBLE_QUOTED_STRING) =>
			^(DOUBLE_QUOTED_STRING (libbash_string=double_quoted_string { $libbash_value += libbash_string; })*) {
				$quoted = true;
			}
		|(ARITHMETIC_EXPRESSION) =>
			^(ARITHMETIC_EXPRESSION value=arithmetics {
				$libbash_value = boost::lexical_cast<std::string>(value); $quoted = false;
			})
		|(var_ref[false]) => libbash_string=var_ref[false] { $libbash_value = libbash_string; $quoted = false; }
		|(libbash_string=any_string { $libbash_value += libbash_string; $quoted = false; })+
	));

//double quoted string rule, allows expansions
double_quoted_string returns[std::string libbash_value]
	:(var_ref[true]) => libbash_string=var_ref[true] { $libbash_value = libbash_string; }
	|(ARITHMETIC_EXPRESSION) => ^(ARITHMETIC_EXPRESSION value=arithmetics) {
		$libbash_value = boost::lexical_cast<std::string>(value);
	}
	|libbash_string=any_string { $libbash_value = libbash_string; };

any_string returns[std::string libbash_value]
@declarations {
	pANTLR3_BASE_TREE any_token;
}
	:any_token=. { $libbash_value = walker->get_string(any_token); };

//Allowable variable names in the variable expansion
var_name returns[std::string libbash_value, unsigned index]
@init {
	$var_name.index = 0;
}
	:libbash_string=num { $libbash_value = libbash_string; }
	|name {
		$libbash_value = $name.libbash_value;
		$index = $name.index;
	};

var_expansion returns[std::string libbash_value]
	:^(USE_DEFAULT_WHEN_UNSET_OR_NULL var_name libbash_word=word) {
		libbash_value = walker->do_default_expansion($var_name.libbash_value, libbash_word, $var_name.index);
	}
	|^(ASSIGN_DEFAULT_WHEN_UNSET_OR_NULL var_name libbash_word=word) {
		libbash_value = walker->do_assign_expansion($var_name.libbash_value, libbash_word, $var_name.index);
	}
	|^(USE_ALTERNATE_WHEN_UNSET_OR_NULL var_name libbash_word=word) {
		libbash_value = walker->do_alternate_expansion($var_name.libbash_value, libbash_word, $var_name.index);
	}
	|(^(OFFSET var_name arithmetics arithmetics)) => ^(OFFSET var_name offset=arithmetics length=arithmetics) {
		libbash_value = walker->do_substring_expansion($var_name.libbash_value, offset, length, $var_name.index);
	}
	|^(OFFSET var_name offset=arithmetics) {
		libbash_value = walker->do_substring_expansion($var_name.libbash_value, offset, $var_name.index);
	}
	|^(POUND(
		var_name {
			libbash_value = boost::lexical_cast<std::string>(walker->get_length($var_name.libbash_value, $var_name.index));
		}
		|^(libbash_name=name_base ARRAY_SIZE) {
			libbash_value = boost::lexical_cast<std::string>(walker->get_array_length(libbash_name));
		}
	));

word returns[std::string libbash_value]
	:(num) => libbash_string=num { $libbash_value = libbash_string; }
	|string_expr { $libbash_value = $string_expr.libbash_value; }
	|value=arithmetics { $libbash_value = boost::lexical_cast<std::string>(value); };

//variable reference
var_ref [bool double_quoted] returns[std::string libbash_value]
	:^(VAR_REF name) {
		$libbash_value = walker->resolve<std::string>($name.libbash_value, $name.index);
	}
	|^(VAR_REF libbash_string=num) {
		$libbash_value = walker->resolve<std::string>(libbash_string);
	}
	|^(VAR_REF ^(libbash_string=name_base AT)) { walker->get_all_elements(libbash_string, $libbash_value); }
	|^(VAR_REF ^(libbash_string=name_base TIMES)) {
		if(double_quoted)
			walker->get_all_elements_IFS_joined(libbash_string, $libbash_value);
		else
			walker->get_all_elements(libbash_string, $libbash_value);
	}
	|^(VAR_REF libbash_string=var_expansion) { $libbash_value = libbash_string; };

command
	:variable_definitions
	|simple_command;

simple_command
@declarations {
	std::vector<std::string> libbash_args;
}
	:^(COMMAND string_expr (argument[libbash_args])* var_def*) {
		if(!walker->call($string_expr.libbash_value,
						 libbash_args,
						 ctx,
						 compound_command))
			std::cerr << $string_expr.libbash_value << " is not implemented yet" << std::endl;
	};

argument[std::vector<std::string>& args]
	: string_expr {
		if($string_expr.quoted)
		{
			args.push_back($string_expr.libbash_value);
		}
		else
		{
			std::vector<std::string> arguments;
			walker->split_word($string_expr.libbash_value, arguments);
			args.insert(args.end(), arguments.begin(), arguments.end());
		}
	};

logic_command_list
	:command
	|^(LOGICAND command command)
	|^(LOGICOR command command);

command_list: ^(LIST logic_command_list+);

compound_command: ^(CURRENT_SHELL command_list);

function_def returns[int placeholder]
	:^(FUNCTION ^(STRING name) {
		// Define the function with current index
		walker->define_function($name.libbash_value, INDEX());
		// Skip the AST for function body, minus one is needed to make the offset right.
		// LT(1) is the function body. It should match the compound_command rule.
		SEEK(INDEX() + count_nodes(ADAPTOR, LT(1)) - 1);
		// After seeking ahead, we need to call CONSUME to eat all the nodes we've skipped.
		CONSUME();
	});

// Only used in arithmetic expansion
primary returns[std::string libbash_value, unsigned index]
	:(^(VAR_REF name)) => ^(VAR_REF name) {
		$libbash_value = $name.libbash_value;
		$index = $name.index;
	}
	|name {
		$libbash_value = $name.libbash_value;
		$index = $name.index;
	}
	// array[@] and array[*] is meaningless to arithmetic expansion so true/false are both ok.
	|^(VAR_REF libbash_string=var_ref[false]) {
		$libbash_value = libbash_string;
		$index = 0;
	};

// shell arithmetic
arithmetics returns[int value]
	:^(LOGICOR l=arithmetics r=arithmetics) { $value = walker->logicor(l, r); }
	|^(LOGICAND l=arithmetics r=arithmetics) { $value = walker->logicand(l, r); }
	|^(PIPE l=arithmetics r=arithmetics) { $value = walker->bitwiseor(l, r); }
	|^(CARET l=arithmetics r=arithmetics) { $value = walker->bitwisexor(l, r); }
	|^(AMP l=arithmetics r=arithmetics) { $value = walker->bitwiseand(l, r); }
	|^(LEQ l=arithmetics r=arithmetics) { $value = walker->less_equal_than(l, r); }
	|^(GEQ l=arithmetics r=arithmetics) { $value = walker->greater_equal_than(l, r); }
	|^(LESS_THAN l=arithmetics r=arithmetics) { $value = walker->less_than(l, r); }
	|^(GREATER_THAN l=arithmetics r=arithmetics) { $value = walker->greater_than(l, r); }
	|^(LSHIFT l=arithmetics r=arithmetics) { $value = walker->left_shift(l, r); }
	|^(RSHIFT l=arithmetics r=arithmetics) { $value = walker->right_shift(l, r); }
	|^(PLUS l=arithmetics r=arithmetics) { $value = walker->plus(l, r); }
	|^(PLUS_SIGN l=arithmetics) { $value = l; }
	|^(MINUS l=arithmetics r=arithmetics) { $value = walker->minus(l, r); }
	|^(MINUS_SIGN l=arithmetics) { $value = -l; }
	|^(TIMES l=arithmetics r=arithmetics) { $value = walker->multiply(l, r); }
	|^(SLASH l=arithmetics r=arithmetics) { $value = walker->divide(l, r); }
	|^(PCT l=arithmetics r=arithmetics) { $value = walker->mod(l, r); }
	|^(EXP l=arithmetics r=arithmetics) { $value = walker->exp(l, r); }
	|^(BANG l=arithmetics) { $value = walker->negation(l); }
	|^(TILDE l=arithmetics) { $value = walker->bitwise_negation(l); }
	|^(ARITHMETIC_CONDITION cnd=arithmetics l=arithmetics r=arithmetics) {
		$value = walker->arithmetic_condition(cnd, l, r);
	}
	|primary {
		$value = walker->resolve<int>($primary.libbash_value, $primary.index);
	}
	|^(PRE_INCR primary) { $value = walker->pre_incr($primary.libbash_value, $primary.index); }
	|^(PRE_DECR primary) { $value = walker->pre_decr($primary.libbash_value, $primary.index); }
	|^(POST_INCR primary) { $value = walker->post_incr($primary.libbash_value, $primary.index); }
	|^(POST_DECR primary) { $value = walker->post_decr($primary.libbash_value, $primary.index); }
	|^(EQUALS primary l=arithmetics) {
		$value = walker->set_value($primary.libbash_value, l, $primary.index);
	}
	|^(MUL_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::multiply, $primary.libbash_value, l, $primary.index);
	}
	|^(DIVIDE_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::divide, $primary.libbash_value, l, $primary.index);
	}
	|^(MOD_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::mod, $primary.libbash_value, l, $primary.index);
	}
	|^(PLUS_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::plus, $primary.libbash_value, l, $primary.index);
	}
	|^(MINUS_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::minus, $primary.libbash_value, l, $primary.index);
	}
	|^(LSHIFT_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::left_shift, $primary.libbash_value, l, $primary.index);
	}
	|^(RSHIFT_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::right_shift, $primary.libbash_value, l, $primary.index);
	}
	|^(AND_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::bitwiseand, $primary.libbash_value, l, $primary.index);
	}
	|^(XOR_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::bitwisexor, $primary.libbash_value, l, $primary.index);
	}
	|^(OR_ASSIGN primary l=arithmetics) {
		$value = walker->assign(&interpreter::bitwiseor, $primary.libbash_value, l, $primary.index);
	}
	| NUMBER { $value = walker->parse_int($NUMBER);}
	| DIGIT { $value = walker->parse_int($DIGIT);}
	;