| 1 |
%{ |
| 2 |
/* |
| 3 |
* Asterisk -- An open source telephony toolkit. |
| 4 |
* |
| 5 |
* Copyright (C) 2006, Digium, Inc. |
| 6 |
* |
| 7 |
* Steve Murphy <[email protected]> |
| 8 |
* |
| 9 |
* See http://www.asterisk.org for more information about |
| 10 |
* the Asterisk project. Please do not directly contact |
| 11 |
* any of the maintainers of this project for assistance; |
| 12 |
* the project provides a web site, mailing lists and IRC |
| 13 |
* channels for your use. |
| 14 |
* |
| 15 |
* This program is free software, distributed under the terms of |
| 16 |
* the GNU General Public License Version 2. See the LICENSE file |
| 17 |
* at the top of the source tree. |
| 18 |
*/ |
| 19 |
/*! \file |
| 20 |
* |
| 21 |
* \brief Bison Grammar description of AEL2. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#define ASTMM_LIBC ASTMM_REDIRECT |
| 26 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include "asterisk/logger.h" |
| 33 |
#include "asterisk/lock.h" |
| 34 |
#include "asterisk/hashtab.h" |
| 35 |
#include "asterisk/ael_structs.h" |
| 36 |
#include "asterisk/utils.h" |
| 37 |
|
| 38 |
extern struct ast_flags ast_compat; |
| 39 |
|
| 40 |
pval * linku1(pval *head, pval *tail); |
| 41 |
static void set_dads(pval *dad, pval *child_list); |
| 42 |
void reset_parencount(yyscan_t yyscanner); |
| 43 |
void reset_semicount(yyscan_t yyscanner); |
| 44 |
void reset_argcount(yyscan_t yyscanner ); |
| 45 |
|
| 46 |
#define YYLEX_PARAM ((struct parse_io *)parseio)->scanner |
| 47 |
#define YYERROR_VERBOSE 1 |
| 48 |
|
| 49 |
extern char *my_file; |
| 50 |
#ifdef AAL_ARGCHECK |
| 51 |
int ael_is_funcname(char *name); |
| 52 |
#endif |
| 53 |
static char *ael_token_subst(const char *mess); |
| 54 |
|
| 55 |
%} |
| 56 |
|
| 57 |
|
| 58 |
%union { |
| 59 |
int intval; /* integer value, typically flags */ |
| 60 |
char *str; /* strings */ |
| 61 |
struct pval *pval; /* full objects */ |
| 62 |
} |
| 63 |
|
| 64 |
%{ |
| 65 |
/* declaring these AFTER the union makes things a lot simpler! */ |
| 66 |
void yyerror(YYLTYPE *locp, struct parse_io *parseio, char const *s); |
| 67 |
int ael_yylex (YYSTYPE * yylval_param, YYLTYPE * yylloc_param , void * yyscanner); |
| 68 |
|
| 69 |
/* create a new object with start-end marker */ |
| 70 |
pval *npval(pvaltype type, int first_line, int last_line, |
| 71 |
int first_column, int last_column); |
| 72 |
|
| 73 |
/* create a new object with start-end marker, simplified interface. |
| 74 |
* Must be declared here because YYLTYPE is not known before |
| 75 |
*/ |
| 76 |
static pval *npval2(pvaltype type, YYLTYPE *first, YYLTYPE *last); |
| 77 |
|
| 78 |
/* another frontend for npval, this time for a string */ |
| 79 |
static pval *nword(char *string, YYLTYPE *pos); |
| 80 |
|
| 81 |
/* update end position of an object, return the object */ |
| 82 |
static pval *update_last(pval *, YYLTYPE *); |
| 83 |
%} |
| 84 |
|
| 85 |
|
| 86 |
%token KW_CONTEXT LC RC LP RP SEMI EQ COMMA COLON AMPER BAR AT |
| 87 |
%token KW_MACRO KW_GLOBALS KW_IGNOREPAT KW_SWITCH KW_IF KW_IFTIME KW_ELSE KW_RANDOM KW_ABSTRACT KW_EXTEND |
| 88 |
%token EXTENMARK KW_GOTO KW_JUMP KW_RETURN KW_BREAK KW_CONTINUE KW_REGEXTEN KW_HINT |
| 89 |
%token KW_FOR KW_WHILE KW_CASE KW_PATTERN KW_DEFAULT KW_CATCH KW_SWITCHES KW_ESWITCHES |
| 90 |
%token KW_INCLUDES KW_LOCAL |
| 91 |
|
| 92 |
%right BAR COMMA |
| 93 |
|
| 94 |
%token <str> word |
| 95 |
|
| 96 |
%type <pval>includes |
| 97 |
%type <pval>includeslist |
| 98 |
%type <pval>switchlist |
| 99 |
%type <pval>eswitches |
| 100 |
%type <pval>switches |
| 101 |
%type <pval>macro_statement |
| 102 |
%type <pval>macro_statements |
| 103 |
%type <pval>case_statement |
| 104 |
%type <pval>case_statements |
| 105 |
%type <pval>eval_arglist |
| 106 |
%type <pval>application_call |
| 107 |
%type <pval>application_call_head |
| 108 |
%type <pval>macro_call |
| 109 |
%type <pval>target jumptarget |
| 110 |
%type <pval>statement |
| 111 |
%type <pval>switch_statement |
| 112 |
|
| 113 |
%type <pval>if_like_head |
| 114 |
%type <pval>statements |
| 115 |
%type <pval>extension |
| 116 |
%type <pval>ignorepat |
| 117 |
%type <pval>element |
| 118 |
%type <pval>elements |
| 119 |
%type <pval>arglist |
| 120 |
%type <pval>assignment |
| 121 |
%type <pval>local_assignment |
| 122 |
%type <pval>global_statements |
| 123 |
%type <pval>globals |
| 124 |
%type <pval>macro |
| 125 |
%type <pval>context |
| 126 |
%type <pval>object |
| 127 |
%type <pval>objects |
| 128 |
%type <pval>file |
| 129 |
/* XXX lr changes */ |
| 130 |
%type <pval>opt_else |
| 131 |
%type <pval>timespec |
| 132 |
%type <pval>included_entry |
| 133 |
|
| 134 |
%type <str>opt_word |
| 135 |
%type <str>context_name |
| 136 |
%type <str>timerange |
| 137 |
|
| 138 |
%type <str>goto_word |
| 139 |
%type <str>word_list |
| 140 |
%type <str>word3_list hint_word |
| 141 |
%type <str>test_expr |
| 142 |
%type <str>opt_pri |
| 143 |
|
| 144 |
%type <intval>opt_abstract |
| 145 |
|
| 146 |
/* |
| 147 |
* OPTIONS |
| 148 |
*/ |
| 149 |
|
| 150 |
%locations /* track source location using @n variables (yylloc in flex) */ |
| 151 |
%pure-parser /* pass yylval and yylloc as arguments to yylex(). */ |
| 152 |
%name-prefix="ael_yy" |
| 153 |
/* |
| 154 |
* add an additional argument, parseio, to yyparse(), |
| 155 |
* which is then accessible in the grammar actions |
| 156 |
*/ |
| 157 |
%parse-param {struct parse_io *parseio} |
| 158 |
|
| 159 |
/* there will be two shift/reduce conflicts, they involve the if statement, where a single statement occurs not wrapped in curlies in the "true" section |
| 160 |
the default action to shift will attach the else to the preceeding if. */ |
| 161 |
%expect 30 |
| 162 |
%error-verbose |
| 163 |
|
| 164 |
/* |
| 165 |
* declare destructors for objects. |
| 166 |
* The former is for pval, the latter for strings. |
| 167 |
* NOTE: we must not have a destructor for a 'file' object. |
| 168 |
*/ |
| 169 |
%destructor { |
| 170 |
destroy_pval($$); |
| 171 |
prev_word=0; |
| 172 |
} includes includeslist switchlist eswitches switches |
| 173 |
macro_statement macro_statements case_statement case_statements |
| 174 |
eval_arglist application_call application_call_head |
| 175 |
macro_call target jumptarget statement switch_statement |
| 176 |
if_like_head statements extension |
| 177 |
ignorepat element elements arglist assignment local_assignment |
| 178 |
global_statements globals macro context object objects |
| 179 |
opt_else |
| 180 |
timespec included_entry |
| 181 |
|
| 182 |
%destructor { free($$);} word word_list goto_word word3_list opt_word context_name |
| 183 |
timerange |
| 184 |
test_expr |
| 185 |
opt_pri |
| 186 |
|
| 187 |
|
| 188 |
%% |
| 189 |
|
| 190 |
file : objects { $$ = parseio->pval = $1; } |
| 191 |
; |
| 192 |
|
| 193 |
objects : object {$$=$1;} |
| 194 |
| objects object { $$ = linku1($1, $2); } |
| 195 |
| objects error {$$=$1;} |
| 196 |
; |
| 197 |
|
| 198 |
object : context {$$=$1;} |
| 199 |
| macro {$$=$1;} |
| 200 |
| globals {$$=$1;} |
| 201 |
| SEMI {$$=0;/* allow older docs to be read */} |
| 202 |
; |
| 203 |
|
| 204 |
context_name : word { $$ = $1; } |
| 205 |
| KW_DEFAULT { $$ = strdup("default"); } |
| 206 |
; |
| 207 |
|
| 208 |
context : opt_abstract KW_CONTEXT context_name LC elements RC { |
| 209 |
$$ = npval2(PV_CONTEXT, &@1, &@6); |
| 210 |
$$->u1.str = $3; |
| 211 |
$$->u2.statements = $5; |
| 212 |
set_dads($$,$5); |
| 213 |
$$->u3.abstract = $1;} |
| 214 |
; |
| 215 |
|
| 216 |
/* optional "abstract" keyword XXX there is no regression test for this */ |
| 217 |
opt_abstract: KW_ABSTRACT { $$ = 1; } |
| 218 |
| /* nothing */ { $$ = 0; } |
| 219 |
| KW_EXTEND { $$ = 2; } |
| 220 |
| KW_EXTEND KW_ABSTRACT { $$=3; } |
| 221 |
| KW_ABSTRACT KW_EXTEND { $$=3; } |
| 222 |
; |
| 223 |
|
| 224 |
macro : KW_MACRO word LP arglist RP LC macro_statements RC { |
| 225 |
$$ = npval2(PV_MACRO, &@1, &@8); |
| 226 |
$$->u1.str = $2; $$->u2.arglist = $4; $$->u3.macro_statements = $7; |
| 227 |
set_dads($$,$7);} |
| 228 |
; |
| 229 |
|
| 230 |
globals : KW_GLOBALS LC global_statements RC { |
| 231 |
$$ = npval2(PV_GLOBALS, &@1, &@4); |
| 232 |
$$->u1.statements = $3; |
| 233 |
set_dads($$,$3);} |
| 234 |
; |
| 235 |
|
| 236 |
global_statements : { $$ = NULL; } |
| 237 |
| global_statements assignment {$$ = linku1($1, $2); } |
| 238 |
| error global_statements {$$=$2;} |
| 239 |
; |
| 240 |
|
| 241 |
assignment : word EQ { reset_semicount(parseio->scanner); } word SEMI { |
| 242 |
$$ = npval2(PV_VARDEC, &@1, &@5); |
| 243 |
$$->u1.str = $1; |
| 244 |
$$->u2.val = $4; } |
| 245 |
; |
| 246 |
|
| 247 |
local_assignment : KW_LOCAL word EQ { reset_semicount(parseio->scanner); } word SEMI { |
| 248 |
$$ = npval2(PV_LOCALVARDEC, &@1, &@6); |
| 249 |
$$->u1.str = $2; |
| 250 |
$$->u2.val = $5; } |
| 251 |
; |
| 252 |
|
| 253 |
/* XXX this matches missing arguments, is this desired ? */ |
| 254 |
arglist : /* empty */ { $$ = NULL; } |
| 255 |
| word { $$ = nword($1, &@1); } |
| 256 |
| arglist COMMA word { $$ = linku1($1, nword($3, &@3)); } |
| 257 |
| arglist error {$$=$1;} |
| 258 |
; |
| 259 |
|
| 260 |
elements : {$$=0;} |
| 261 |
| elements element { $$ = linku1($1, $2); } |
| 262 |
| error elements { $$=$2;} |
| 263 |
; |
| 264 |
|
| 265 |
element : extension {$$=$1;} |
| 266 |
| includes {$$=$1;} |
| 267 |
| switches {$$=$1;} |
| 268 |
| eswitches {$$=$1;} |
| 269 |
| ignorepat {$$=$1;} |
| 270 |
| assignment {$$=$1;} |
| 271 |
| local_assignment {$$=$1;} |
| 272 |
| word error {free($1); $$=0;} |
| 273 |
| SEMI {$$=0;/* allow older docs to be read */} |
| 274 |
; |
| 275 |
|
| 276 |
ignorepat : KW_IGNOREPAT EXTENMARK word SEMI { |
| 277 |
$$ = npval2(PV_IGNOREPAT, &@1, &@4); |
| 278 |
$$->u1.str = $3;} |
| 279 |
; |
| 280 |
|
| 281 |
extension : word EXTENMARK statement { |
| 282 |
$$ = npval2(PV_EXTENSION, &@1, &@3); |
| 283 |
$$->u1.str = $1; |
| 284 |
$$->u2.statements = $3; set_dads($$,$3);} |
| 285 |
| word AT word EXTENMARK statement { |
| 286 |
$$ = npval2(PV_EXTENSION, &@1, &@3); |
| 287 |
$$->u1.str = malloc(strlen($1)+strlen($3)+2); |
| 288 |
strcpy($$->u1.str,$1); |
| 289 |
strcat($$->u1.str,"@"); |
| 290 |
strcat($$->u1.str,$3); |
| 291 |
free($1); |
| 292 |
$$->u2.statements = $5; set_dads($$,$5);} |
| 293 |
| KW_REGEXTEN word EXTENMARK statement { |
| 294 |
$$ = npval2(PV_EXTENSION, &@1, &@4); |
| 295 |
$$->u1.str = $2; |
| 296 |
$$->u2.statements = $4; set_dads($$,$4); |
| 297 |
$$->u4.regexten=1;} |
| 298 |
| KW_HINT LP hint_word RP word EXTENMARK statement { |
| 299 |
$$ = npval2(PV_EXTENSION, &@1, &@7); |
| 300 |
$$->u1.str = $5; |
| 301 |
$$->u2.statements = $7; set_dads($$,$7); |
| 302 |
$$->u3.hints = $3;} |
| 303 |
| KW_REGEXTEN KW_HINT LP hint_word RP word EXTENMARK statement { |
| 304 |
$$ = npval2(PV_EXTENSION, &@1, &@8); |
| 305 |
$$->u1.str = $6; |
| 306 |
$$->u2.statements = $8; set_dads($$,$8); |
| 307 |
$$->u4.regexten=1; |
| 308 |
$$->u3.hints = $4;} |
| 309 |
; |
| 310 |
|
| 311 |
/* list of statements in a block or after a case label - can be empty */ |
| 312 |
statements : /* empty */ { $$ = NULL; } |
| 313 |
| statements statement { $$ = linku1($1, $2); } |
| 314 |
| error statements {$$=$2;} |
| 315 |
; |
| 316 |
|
| 317 |
/* hh:mm-hh:mm, due to the way the parser works we do not |
| 318 |
* detect the '-' but only the ':' as separator |
| 319 |
*/ |
| 320 |
timerange: word3_list COLON word3_list COLON word3_list { |
| 321 |
if (asprintf(&$$, "%s:%s:%s", $1, $3, $5) < 0) { |
| 322 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 323 |
$$ = NULL; |
| 324 |
} else { |
| 325 |
free($1); |
| 326 |
free($3); |
| 327 |
free($5); |
| 328 |
} |
| 329 |
} |
| 330 |
| word { $$ = $1; } |
| 331 |
; |
| 332 |
|
| 333 |
/* full time specification range|dow|*|* */ |
| 334 |
timespec : timerange BAR word3_list BAR word3_list BAR word3_list { |
| 335 |
$$ = nword($1, &@1); |
| 336 |
$$->next = nword($3, &@3); |
| 337 |
$$->next->next = nword($5, &@5); |
| 338 |
$$->next->next->next = nword($7, &@7); } |
| 339 |
; |
| 340 |
|
| 341 |
/* expression used in if, random, while, switch */ |
| 342 |
test_expr : LP { reset_parencount(parseio->scanner); } word_list RP { $$ = $3; } |
| 343 |
; |
| 344 |
|
| 345 |
/* 'if' like statements: if, iftime, random */ |
| 346 |
if_like_head : KW_IF test_expr { |
| 347 |
$$= npval2(PV_IF, &@1, &@2); |
| 348 |
$$->u1.str = $2; } |
| 349 |
| KW_RANDOM test_expr { |
| 350 |
$$ = npval2(PV_RANDOM, &@1, &@2); |
| 351 |
$$->u1.str=$2;} |
| 352 |
| KW_IFTIME LP timespec RP { |
| 353 |
$$ = npval2(PV_IFTIME, &@1, &@4); |
| 354 |
$$->u1.list = $3; |
| 355 |
prev_word = 0; } |
| 356 |
; |
| 357 |
|
| 358 |
/* word_list is a hack to fix a problem with context switching between bison and flex; |
| 359 |
by the time you register a new context with flex, you've already got a look-ahead token |
| 360 |
from the old context, with no way to put it back and start afresh. So, we kludge this |
| 361 |
and merge the words back together. */ |
| 362 |
|
| 363 |
word_list : word { $$ = $1;} |
| 364 |
| word word { |
| 365 |
if (asprintf(&($$), "%s%s", $1, $2) < 0) { |
| 366 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 367 |
$$ = NULL; |
| 368 |
} else { |
| 369 |
free($1); |
| 370 |
free($2); |
| 371 |
prev_word = $$; |
| 372 |
} |
| 373 |
} |
| 374 |
; |
| 375 |
|
| 376 |
hint_word : word { $$ = $1; } |
| 377 |
| hint_word word { |
| 378 |
if (asprintf(&($$), "%s %s", $1, $2) < 0) { |
| 379 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 380 |
$$ = NULL; |
| 381 |
} else { |
| 382 |
free($1); |
| 383 |
free($2); |
| 384 |
} |
| 385 |
} |
| 386 |
| hint_word COLON word { |
| 387 |
if (asprintf(&($$), "%s:%s", $1, $3) < 0) { |
| 388 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 389 |
$$ = NULL; |
| 390 |
} else { |
| 391 |
free($1); |
| 392 |
free($3); |
| 393 |
} |
| 394 |
} |
| 395 |
| hint_word AMPER word { /* there are often '&' in hints */ |
| 396 |
if (asprintf(&($$), "%s&%s", $1, $3) < 0) { |
| 397 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 398 |
$$ = NULL; |
| 399 |
} else { |
| 400 |
free($1); |
| 401 |
free($3); |
| 402 |
} |
| 403 |
} |
| 404 |
| hint_word AT word { |
| 405 |
if (asprintf(&($$), "%s@%s", $1, $3) < 0) { |
| 406 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 407 |
$$ = NULL; |
| 408 |
} else { |
| 409 |
free($1); |
| 410 |
free($3); |
| 411 |
} |
| 412 |
} |
| 413 |
; |
| 414 |
|
| 415 |
word3_list : word { $$ = $1;} |
| 416 |
| word word { |
| 417 |
if (asprintf(&($$), "%s%s", $1, $2) < 0) { |
| 418 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 419 |
$$ = NULL; |
| 420 |
} else { |
| 421 |
free($1); |
| 422 |
free($2); |
| 423 |
prev_word = $$; |
| 424 |
} |
| 425 |
} |
| 426 |
| word word word { |
| 427 |
if (asprintf(&($$), "%s%s%s", $1, $2, $3) < 0) { |
| 428 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 429 |
$$ = NULL; |
| 430 |
} else { |
| 431 |
free($1); |
| 432 |
free($2); |
| 433 |
free($3); |
| 434 |
prev_word=$$; |
| 435 |
} |
| 436 |
} |
| 437 |
; |
| 438 |
|
| 439 |
goto_word : word { $$ = $1;} |
| 440 |
| word word { |
| 441 |
if (asprintf(&($$), "%s%s", $1, $2) < 0) { |
| 442 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 443 |
$$ = NULL; |
| 444 |
} else { |
| 445 |
free($1); |
| 446 |
free($2); |
| 447 |
} |
| 448 |
} |
| 449 |
| goto_word COLON word { |
| 450 |
if (asprintf(&($$), "%s:%s", $1, $3) < 0) { |
| 451 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 452 |
$$ = NULL; |
| 453 |
} else { |
| 454 |
free($1); |
| 455 |
free($3); |
| 456 |
} |
| 457 |
} |
| 458 |
; |
| 459 |
|
| 460 |
switch_statement : KW_SWITCH test_expr LC case_statements RC { |
| 461 |
$$ = npval2(PV_SWITCH, &@1, &@5); |
| 462 |
$$->u1.str = $2; |
| 463 |
$$->u2.statements = $4; set_dads($$,$4);} |
| 464 |
; |
| 465 |
|
| 466 |
/* |
| 467 |
* Definition of a statement in our language |
| 468 |
*/ |
| 469 |
statement : LC statements RC { |
| 470 |
$$ = npval2(PV_STATEMENTBLOCK, &@1, &@3); |
| 471 |
$$->u1.list = $2; set_dads($$,$2);} |
| 472 |
| assignment { $$ = $1; } |
| 473 |
| local_assignment { $$ = $1; } |
| 474 |
| KW_GOTO target SEMI { |
| 475 |
$$ = npval2(PV_GOTO, &@1, &@3); |
| 476 |
$$->u1.list = $2;} |
| 477 |
| KW_JUMP jumptarget SEMI { |
| 478 |
$$ = npval2(PV_GOTO, &@1, &@3); |
| 479 |
$$->u1.list = $2;} |
| 480 |
| word COLON { |
| 481 |
$$ = npval2(PV_LABEL, &@1, &@2); |
| 482 |
$$->u1.str = $1; } |
| 483 |
| KW_FOR LP {reset_semicount(parseio->scanner);} word SEMI |
| 484 |
{reset_semicount(parseio->scanner);} word SEMI |
| 485 |
{reset_parencount(parseio->scanner);} word RP statement { /* XXX word_list maybe ? */ |
| 486 |
$$ = npval2(PV_FOR, &@1, &@12); |
| 487 |
$$->u1.for_init = $4; |
| 488 |
$$->u2.for_test=$7; |
| 489 |
$$->u3.for_inc = $10; |
| 490 |
$$->u4.for_statements = $12; set_dads($$,$12);} |
| 491 |
| KW_WHILE test_expr statement { |
| 492 |
$$ = npval2(PV_WHILE, &@1, &@3); |
| 493 |
$$->u1.str = $2; |
| 494 |
$$->u2.statements = $3; set_dads($$,$3);} |
| 495 |
| switch_statement { $$ = $1; } |
| 496 |
| AMPER macro_call SEMI { $$ = update_last($2, &@2); } |
| 497 |
| application_call SEMI { $$ = update_last($1, &@2); } |
| 498 |
| word SEMI { |
| 499 |
$$= npval2(PV_APPLICATION_CALL, &@1, &@2); |
| 500 |
$$->u1.str = $1;} |
| 501 |
| application_call EQ {reset_semicount(parseio->scanner);} word SEMI { |
| 502 |
char *bufx; |
| 503 |
int tot=0; |
| 504 |
pval *pptr; |
| 505 |
$$ = npval2(PV_VARDEC, &@1, &@5); |
| 506 |
$$->u2.val=$4; |
| 507 |
/* rebuild the original string-- this is not an app call, it's an unwrapped vardec, with a func call on the LHS */ |
| 508 |
/* string to big to fit in the buffer? */ |
| 509 |
tot+=strlen($1->u1.str); |
| 510 |
for(pptr=$1->u2.arglist;pptr;pptr=pptr->next) { |
| 511 |
tot+=strlen(pptr->u1.str); |
| 512 |
tot++; /* for a sep like a comma */ |
| 513 |
} |
| 514 |
tot+=4; /* for safety */ |
| 515 |
bufx = calloc(1, tot); |
| 516 |
strcpy(bufx,$1->u1.str); |
| 517 |
strcat(bufx,"("); |
| 518 |
/* XXX need to advance the pointer or the loop is very inefficient */ |
| 519 |
for (pptr=$1->u2.arglist;pptr;pptr=pptr->next) { |
| 520 |
if ( pptr != $1->u2.arglist ) |
| 521 |
strcat(bufx,","); |
| 522 |
strcat(bufx,pptr->u1.str); |
| 523 |
} |
| 524 |
strcat(bufx,")"); |
| 525 |
#ifdef AAL_ARGCHECK |
| 526 |
if ( !ael_is_funcname($1->u1.str) ) |
| 527 |
ast_log(LOG_WARNING, "==== File: %s, Line %d, Cols: %d-%d: Function call? The name %s is not in my internal list of function names\n", |
| 528 |
my_file, @1.first_line, @1.first_column, @1.last_column, $1->u1.str); |
| 529 |
#endif |
| 530 |
$$->u1.str = bufx; |
| 531 |
destroy_pval($1); /* the app call it is not, get rid of that chain */ |
| 532 |
prev_word = 0; |
| 533 |
} |
| 534 |
| KW_BREAK SEMI { $$ = npval2(PV_BREAK, &@1, &@2); } |
| 535 |
| KW_RETURN SEMI { $$ = npval2(PV_RETURN, &@1, &@2); } |
| 536 |
| KW_CONTINUE SEMI { $$ = npval2(PV_CONTINUE, &@1, &@2); } |
| 537 |
| if_like_head statement opt_else { |
| 538 |
$$ = update_last($1, &@2); |
| 539 |
$$->u2.statements = $2; set_dads($$,$2); |
| 540 |
$$->u3.else_statements = $3;set_dads($$,$3);} |
| 541 |
| SEMI { $$=0; } |
| 542 |
; |
| 543 |
|
| 544 |
opt_else : KW_ELSE statement { $$ = $2; } |
| 545 |
| { $$ = NULL ; } |
| 546 |
|
| 547 |
|
| 548 |
target : goto_word { $$ = nword($1, &@1); } |
| 549 |
| goto_word BAR goto_word { |
| 550 |
$$ = nword($1, &@1); |
| 551 |
$$->next = nword($3, &@3); } |
| 552 |
| goto_word COMMA goto_word { |
| 553 |
$$ = nword($1, &@1); |
| 554 |
$$->next = nword($3, &@3); } |
| 555 |
| goto_word BAR goto_word BAR goto_word { |
| 556 |
$$ = nword($1, &@1); |
| 557 |
$$->next = nword($3, &@3); |
| 558 |
$$->next->next = nword($5, &@5); } |
| 559 |
| goto_word COMMA goto_word COMMA goto_word { |
| 560 |
$$ = nword($1, &@1); |
| 561 |
$$->next = nword($3, &@3); |
| 562 |
$$->next->next = nword($5, &@5); } |
| 563 |
| KW_DEFAULT BAR goto_word BAR goto_word { |
| 564 |
$$ = nword(strdup("default"), &@1); |
| 565 |
$$->next = nword($3, &@3); |
| 566 |
$$->next->next = nword($5, &@5); } |
| 567 |
| KW_DEFAULT COMMA goto_word COMMA goto_word { |
| 568 |
$$ = nword(strdup("default"), &@1); |
| 569 |
$$->next = nword($3, &@3); |
| 570 |
$$->next->next = nword($5, &@5); } |
| 571 |
; |
| 572 |
|
| 573 |
opt_pri : /* empty */ { $$ = strdup("1"); } |
| 574 |
| COMMA word { $$ = $2; } |
| 575 |
; |
| 576 |
|
| 577 |
/* XXX please document the form of jumptarget */ |
| 578 |
jumptarget : goto_word opt_pri { /* ext[, pri] default 1 */ |
| 579 |
$$ = nword($1, &@1); |
| 580 |
$$->next = nword($2, &@2); } /* jump extension[,priority][@context] */ |
| 581 |
| goto_word opt_pri AT context_name { /* context, ext, pri */ |
| 582 |
$$ = nword($4, &@4); |
| 583 |
$$->next = nword($1, &@1); |
| 584 |
$$->next->next = nword($2, &@2); } |
| 585 |
; |
| 586 |
|
| 587 |
macro_call : word LP {reset_argcount(parseio->scanner);} eval_arglist RP { |
| 588 |
/* XXX original code had @2 but i think we need @5 */ |
| 589 |
$$ = npval2(PV_MACRO_CALL, &@1, &@5); |
| 590 |
$$->u1.str = $1; |
| 591 |
$$->u2.arglist = $4;} |
| 592 |
| word LP RP { |
| 593 |
$$= npval2(PV_MACRO_CALL, &@1, &@3); |
| 594 |
$$->u1.str = $1; } |
| 595 |
; |
| 596 |
|
| 597 |
/* XXX application_call_head must be revised. Having 'word LP { ...' |
| 598 |
* just as above should work fine, however it gives a different result. |
| 599 |
*/ |
| 600 |
application_call_head: word LP {reset_argcount(parseio->scanner);} { |
| 601 |
if (strcasecmp($1,"goto") == 0) { |
| 602 |
$$ = npval2(PV_GOTO, &@1, &@2); |
| 603 |
free($1); /* won't be using this */ |
| 604 |
ast_log(LOG_WARNING, "==== File: %s, Line %d, Cols: %d-%d: Suggestion: Use the goto statement instead of the Goto() application call in AEL.\n", my_file, @1.first_line, @1.first_column, @1.last_column ); |
| 605 |
} else { |
| 606 |
$$= npval2(PV_APPLICATION_CALL, &@1, &@2); |
| 607 |
$$->u1.str = $1; |
| 608 |
} } |
| 609 |
; |
| 610 |
|
| 611 |
application_call : application_call_head eval_arglist RP { |
| 612 |
$$ = update_last($1, &@3); |
| 613 |
if( $$->type == PV_GOTO ) |
| 614 |
$$->u1.list = $2; |
| 615 |
else |
| 616 |
$$->u2.arglist = $2; |
| 617 |
} |
| 618 |
| application_call_head RP { $$ = update_last($1, &@2); } |
| 619 |
; |
| 620 |
|
| 621 |
opt_word : word { $$ = $1 } |
| 622 |
| { $$ = strdup(""); } |
| 623 |
; |
| 624 |
|
| 625 |
eval_arglist : word_list { $$ = nword($1, &@1); } |
| 626 |
| /*nothing! */ { |
| 627 |
$$= npval(PV_WORD,0/*@1.first_line*/,0/*@1.last_line*/,0/* @1.first_column*/, 0/*@1.last_column*/); |
| 628 |
$$->u1.str = strdup(""); } |
| 629 |
| eval_arglist COMMA opt_word { $$ = linku1($1, nword($3, &@3)); } |
| 630 |
; |
| 631 |
|
| 632 |
case_statements: /* empty */ { $$ = NULL; } |
| 633 |
| case_statements case_statement { $$ = linku1($1, $2); } |
| 634 |
; |
| 635 |
|
| 636 |
case_statement: KW_CASE word COLON statements { |
| 637 |
$$ = npval2(PV_CASE, &@1, &@3); /* XXX 3 or 4 ? */ |
| 638 |
$$->u1.str = $2; |
| 639 |
$$->u2.statements = $4; set_dads($$,$4);} |
| 640 |
| KW_DEFAULT COLON statements { |
| 641 |
$$ = npval2(PV_DEFAULT, &@1, &@3); |
| 642 |
$$->u1.str = NULL; |
| 643 |
$$->u2.statements = $3;set_dads($$,$3);} |
| 644 |
| KW_PATTERN word COLON statements { |
| 645 |
$$ = npval2(PV_PATTERN, &@1, &@4); /* XXX@3 or @4 ? */ |
| 646 |
$$->u1.str = $2; |
| 647 |
$$->u2.statements = $4;set_dads($$,$4);} |
| 648 |
; |
| 649 |
|
| 650 |
macro_statements: /* empty */ { $$ = NULL; } |
| 651 |
| macro_statements macro_statement { $$ = linku1($1, $2); } |
| 652 |
; |
| 653 |
|
| 654 |
macro_statement : statement {$$=$1;} |
| 655 |
| includes { $$=$1;} |
| 656 |
| KW_CATCH word LC statements RC { |
| 657 |
$$ = npval2(PV_CATCH, &@1, &@5); |
| 658 |
$$->u1.str = $2; |
| 659 |
$$->u2.statements = $4; set_dads($$,$4);} |
| 660 |
; |
| 661 |
|
| 662 |
switches : KW_SWITCHES LC switchlist RC { |
| 663 |
$$ = npval2(PV_SWITCHES, &@1, &@2); |
| 664 |
$$->u1.list = $3; set_dads($$,$3);} |
| 665 |
; |
| 666 |
|
| 667 |
eswitches : KW_ESWITCHES LC switchlist RC { |
| 668 |
$$ = npval2(PV_ESWITCHES, &@1, &@2); |
| 669 |
$$->u1.list = $3; set_dads($$,$3);} |
| 670 |
; |
| 671 |
|
| 672 |
switchlist : /* empty */ { $$ = NULL; } |
| 673 |
| switchlist word SEMI { $$ = linku1($1,nword($2, &@2)); } |
| 674 |
| switchlist word AT word SEMI { |
| 675 |
char *x; |
| 676 |
if (asprintf(&x,"%s@%s", $2, $4) < 0) { |
| 677 |
ast_log(LOG_WARNING, "asprintf() failed\n"); |
| 678 |
$$ = NULL; |
| 679 |
} else { |
| 680 |
free($2); |
| 681 |
free($4); |
| 682 |
$$ = linku1($1,nword(x, &@2)); |
| 683 |
} |
| 684 |
} |
| 685 |
| error switchlist {$$=$2;} |
| 686 |
; |
| 687 |
|
| 688 |
included_entry : context_name { $$ = nword($1, &@1); } |
| 689 |
| context_name BAR timespec { |
| 690 |
$$ = nword($1, &@1); |
| 691 |
$$->u2.arglist = $3; |
| 692 |
prev_word=0; /* XXX sure ? */ } |
| 693 |
; |
| 694 |
|
| 695 |
/* list of ';' separated context names followed by optional timespec */ |
| 696 |
includeslist : included_entry SEMI { $$ = $1; } |
| 697 |
| includeslist included_entry SEMI { $$ = linku1($1, $2); } |
| 698 |
| includeslist error {$$=$1;} |
| 699 |
; |
| 700 |
|
| 701 |
includes : KW_INCLUDES LC includeslist RC { |
| 702 |
$$ = npval2(PV_INCLUDES, &@1, &@4); |
| 703 |
$$->u1.list = $3;set_dads($$,$3);} |
| 704 |
| KW_INCLUDES LC RC { |
| 705 |
$$ = npval2(PV_INCLUDES, &@1, &@3);} |
| 706 |
; |
| 707 |
|
| 708 |
|
| 709 |
%% |
| 710 |
|
| 711 |
static char *token_equivs1[] = |
| 712 |
{ |
| 713 |
"AMPER", |
| 714 |
"AT", |
| 715 |
"BAR", |
| 716 |
"COLON", |
| 717 |
"COMMA", |
| 718 |
"EQ", |
| 719 |
"EXTENMARK", |
| 720 |
"KW_BREAK", |
| 721 |
"KW_CASE", |
| 722 |
"KW_CATCH", |
| 723 |
"KW_CONTEXT", |
| 724 |
"KW_CONTINUE", |
| 725 |
"KW_DEFAULT", |
| 726 |
"KW_ELSE", |
| 727 |
"KW_ESWITCHES", |
| 728 |
"KW_FOR", |
| 729 |
"KW_GLOBALS", |
| 730 |
"KW_GOTO", |
| 731 |
"KW_HINT", |
| 732 |
"KW_IFTIME", |
| 733 |
"KW_IF", |
| 734 |
"KW_IGNOREPAT", |
| 735 |
"KW_INCLUDES" |
| 736 |
"KW_JUMP", |
| 737 |
"KW_MACRO", |
| 738 |
"KW_PATTERN", |
| 739 |
"KW_REGEXTEN", |
| 740 |
"KW_RETURN", |
| 741 |
"KW_SWITCHES", |
| 742 |
"KW_SWITCH", |
| 743 |
"KW_WHILE", |
| 744 |
"LC", |
| 745 |
"LP", |
| 746 |
"RC", |
| 747 |
"RP", |
| 748 |
"SEMI", |
| 749 |
}; |
| 750 |
|
| 751 |
static char *token_equivs2[] = |
| 752 |
{ |
| 753 |
"&", |
| 754 |
"@", |
| 755 |
"|", |
| 756 |
":", |
| 757 |
",", |
| 758 |
"=", |
| 759 |
"=>", |
| 760 |
"break", |
| 761 |
"case", |
| 762 |
"catch", |
| 763 |
"context", |
| 764 |
"continue", |
| 765 |
"default", |
| 766 |
"else", |
| 767 |
"eswitches", |
| 768 |
"for", |
| 769 |
"globals", |
| 770 |
"goto", |
| 771 |
"hint", |
| 772 |
"ifTime", |
| 773 |
"if", |
| 774 |
"ignorepat", |
| 775 |
"includes" |
| 776 |
"jump", |
| 777 |
"macro", |
| 778 |
"pattern", |
| 779 |
"regexten", |
| 780 |
"return", |
| 781 |
"switches", |
| 782 |
"switch", |
| 783 |
"while", |
| 784 |
"{", |
| 785 |
"(", |
| 786 |
"}", |
| 787 |
")", |
| 788 |
";", |
| 789 |
}; |
| 790 |
|
| 791 |
|
| 792 |
static char *ael_token_subst(const char *mess) |
| 793 |
{ |
| 794 |
/* calc a length, malloc, fill, and return; yyerror had better free it! */ |
| 795 |
int len=0,i; |
| 796 |
const char *p; |
| 797 |
char *res, *s,*t; |
| 798 |
int token_equivs_entries = sizeof(token_equivs1)/sizeof(char*); |
| 799 |
|
| 800 |
for (p=mess; *p; p++) { |
| 801 |
for (i=0; i<token_equivs_entries; i++) { |
| 802 |
if ( strncmp(p,token_equivs1[i],strlen(token_equivs1[i])) == 0 ) |
| 803 |
{ |
| 804 |
len+=strlen(token_equivs2[i])+2; |
| 805 |
p += strlen(token_equivs1[i])-1; |
| 806 |
break; |
| 807 |
} |
| 808 |
} |
| 809 |
len++; |
| 810 |
} |
| 811 |
res = calloc(1, len+1); |
| 812 |
res[0] = 0; |
| 813 |
s = res; |
| 814 |
for (p=mess; *p;) { |
| 815 |
int found = 0; |
| 816 |
for (i=0; i<token_equivs_entries; i++) { |
| 817 |
if ( strncmp(p,token_equivs1[i],strlen(token_equivs1[i])) == 0 ) { |
| 818 |
*s++ = '\''; |
| 819 |
for (t=token_equivs2[i]; *t;) { |
| 820 |
*s++ = *t++; |
| 821 |
} |
| 822 |
*s++ = '\''; |
| 823 |
p += strlen(token_equivs1[i]); |
| 824 |
found = 1; |
| 825 |
break; |
| 826 |
} |
| 827 |
} |
| 828 |
if( !found ) |
| 829 |
*s++ = *p++; |
| 830 |
} |
| 831 |
*s++ = 0; |
| 832 |
return res; |
| 833 |
} |
| 834 |
|
| 835 |
void yyerror(YYLTYPE *locp, struct parse_io *parseio, char const *s) |
| 836 |
{ |
| 837 |
char *s2 = ael_token_subst((char *)s); |
| 838 |
if (locp->first_line == locp->last_line) { |
| 839 |
ast_log(LOG_ERROR, "==== File: %s, Line %d, Cols: %d-%d: Error: %s\n", my_file, locp->first_line, locp->first_column, locp->last_column, s2); |
| 840 |
} else { |
| 841 |
ast_log(LOG_ERROR, "==== File: %s, Line %d Col %d to Line %d Col %d: Error: %s\n", my_file, locp->first_line, locp->first_column, locp->last_line, locp->last_column, s2); |
| 842 |
} |
| 843 |
free(s2); |
| 844 |
parseio->syntax_error_count++; |
| 845 |
} |
| 846 |
|
| 847 |
struct pval *npval(pvaltype type, int first_line, int last_line, |
| 848 |
int first_column, int last_column) |
| 849 |
{ |
| 850 |
pval *z = calloc(1, sizeof(struct pval)); |
| 851 |
z->type = type; |
| 852 |
z->startline = first_line; |
| 853 |
z->endline = last_line; |
| 854 |
z->startcol = first_column; |
| 855 |
z->endcol = last_column; |
| 856 |
z->filename = strdup(S_OR(my_file, "<none>")); |
| 857 |
return z; |
| 858 |
} |
| 859 |
|
| 860 |
static struct pval *npval2(pvaltype type, YYLTYPE *first, YYLTYPE *last) |
| 861 |
{ |
| 862 |
return npval(type, first->first_line, last->last_line, |
| 863 |
first->first_column, last->last_column); |
| 864 |
} |
| 865 |
|
| 866 |
static struct pval *update_last(pval *obj, YYLTYPE *last) |
| 867 |
{ |
| 868 |
obj->endline = last->last_line; |
| 869 |
obj->endcol = last->last_column; |
| 870 |
return obj; |
| 871 |
} |
| 872 |
|
| 873 |
/* frontend for npval to create a PV_WORD string from the given token */ |
| 874 |
static pval *nword(char *string, YYLTYPE *pos) |
| 875 |
{ |
| 876 |
pval *p = npval2(PV_WORD, pos, pos); |
| 877 |
if (p) |
| 878 |
p->u1.str = string; |
| 879 |
return p; |
| 880 |
} |
| 881 |
|
| 882 |
/* this routine adds a dad ptr to each element in the list */ |
| 883 |
static void set_dads(struct pval *dad, struct pval *child_list) |
| 884 |
{ |
| 885 |
struct pval *t; |
| 886 |
|
| 887 |
for(t=child_list;t;t=t->next) /* simple stuff */ |
| 888 |
t->dad = dad; |
| 889 |
} |