3 * Asterisk -- An open source telephony toolkit.
5 * Copyright (C) 2006, Digium, Inc.
7 * Steve Murphy <murf@parsetree.com>
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.
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.
21 * \brief Bison Grammar description of AEL2.
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/logger.h"
34 #include "asterisk/lock.h"
35 #include "asterisk/hashtab.h"
36 #include "asterisk/ael_structs.h"
37 #include "asterisk/utils.h"
39 extern struct ast_flags ast_compat;
41 pval * linku1(pval *head, pval *tail);
42 static void set_dads(pval *dad, pval *child_list);
43 void reset_parencount(yyscan_t yyscanner);
44 void reset_semicount(yyscan_t yyscanner);
45 void reset_argcount(yyscan_t yyscanner );
47 #define YYLEX_PARAM ((struct parse_io *)parseio)->scanner
48 #define YYERROR_VERBOSE 1
52 int ael_is_funcname(char *name);
54 static char *ael_token_subst(const char *mess);
60 int intval; /* integer value, typically flags */
61 char *str; /* strings */
62 struct pval *pval; /* full objects */
66 /* declaring these AFTER the union makes things a lot simpler! */
67 void yyerror(YYLTYPE *locp, struct parse_io *parseio, char const *s);
68 int ael_yylex (YYSTYPE * yylval_param, YYLTYPE * yylloc_param , void * yyscanner);
70 /* create a new object with start-end marker */
71 pval *npval(pvaltype type, int first_line, int last_line,
72 int first_column, int last_column);
74 /* create a new object with start-end marker, simplified interface.
75 * Must be declared here because YYLTYPE is not known before
77 static pval *npval2(pvaltype type, YYLTYPE *first, YYLTYPE *last);
79 /* another frontend for npval, this time for a string */
80 static pval *nword(char *string, YYLTYPE *pos);
82 /* update end position of an object, return the object */
83 static pval *update_last(pval *, YYLTYPE *);
87 %token KW_CONTEXT LC RC LP RP SEMI EQ COMMA COLON AMPER BAR AT
88 %token KW_MACRO KW_GLOBALS KW_IGNOREPAT KW_SWITCH KW_IF KW_IFTIME KW_ELSE KW_RANDOM KW_ABSTRACT KW_EXTEND
89 %token EXTENMARK KW_GOTO KW_JUMP KW_RETURN KW_BREAK KW_CONTINUE KW_REGEXTEN KW_HINT
90 %token KW_FOR KW_WHILE KW_CASE KW_PATTERN KW_DEFAULT KW_CATCH KW_SWITCHES KW_ESWITCHES
91 %token KW_INCLUDES KW_LOCAL
98 %type <pval>includeslist
99 %type <pval>switchlist
100 %type <pval>eswitches
102 %type <pval>macro_statement
103 %type <pval>macro_statements
104 %type <pval>case_statement
105 %type <pval>case_statements
106 %type <pval>eval_arglist
107 %type <pval>application_call
108 %type <pval>application_call_head
109 %type <pval>macro_call
110 %type <pval>target jumptarget
111 %type <pval>statement
112 %type <pval>switch_statement
114 %type <pval>if_like_head
115 %type <pval>statements
116 %type <pval>extension
117 %type <pval>ignorepat
121 %type <pval>assignment
122 %type <pval>local_assignment
123 %type <pval>global_statements
133 %type <pval>included_entry
136 %type <str>context_name
141 %type <str>word3_list hint_word
145 %type <intval>opt_abstract
151 %locations /* track source location using @n variables (yylloc in flex) */
152 %pure-parser /* pass yylval and yylloc as arguments to yylex(). */
153 %name-prefix="ael_yy"
155 * add an additional argument, parseio, to yyparse(),
156 * which is then accessible in the grammar actions
158 %parse-param {struct parse_io *parseio}
160 /* 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
161 the default action to shift will attach the else to the preceeding if. */
166 * declare destructors for objects.
167 * The former is for pval, the latter for strings.
168 * NOTE: we must not have a destructor for a 'file' object.
173 } includes includeslist switchlist eswitches switches
174 macro_statement macro_statements case_statement case_statements
175 eval_arglist application_call application_call_head
176 macro_call target jumptarget statement switch_statement
177 if_like_head statements extension
178 ignorepat element elements arglist assignment local_assignment
179 global_statements globals macro context object objects
181 timespec included_entry
183 %destructor { free($$);} word word_list goto_word word3_list opt_word context_name
191 file : objects { $$ = parseio->pval = $1; }
194 objects : object {$$=$1;}
195 | objects object { $$ = linku1($1, $2); }
196 | objects error {$$=$1;}
199 object : context {$$=$1;}
202 | SEMI {$$=0;/* allow older docs to be read */}
205 context_name : word { $$ = $1; }
206 | KW_DEFAULT { $$ = strdup("default"); }
209 context : opt_abstract KW_CONTEXT context_name LC elements RC {
210 $$ = npval2(PV_CONTEXT, &@1, &@6);
212 $$->u2.statements = $5;
214 $$->u3.abstract = $1;}
217 /* optional "abstract" keyword XXX there is no regression test for this */
218 opt_abstract: KW_ABSTRACT { $$ = 1; }
219 | /* nothing */ { $$ = 0; }
220 | KW_EXTEND { $$ = 2; }
221 | KW_EXTEND KW_ABSTRACT { $$=3; }
222 | KW_ABSTRACT KW_EXTEND { $$=3; }
225 macro : KW_MACRO word LP arglist RP LC macro_statements RC {
226 $$ = npval2(PV_MACRO, &@1, &@8);
227 $$->u1.str = $2; $$->u2.arglist = $4; $$->u3.macro_statements = $7;
231 globals : KW_GLOBALS LC global_statements RC {
232 $$ = npval2(PV_GLOBALS, &@1, &@4);
233 $$->u1.statements = $3;
237 global_statements : { $$ = NULL; }
238 | assignment global_statements {$$ = linku1($1, $2); }
239 | error global_statements {$$=$2;}
242 assignment : word EQ { reset_semicount(parseio->scanner); } word SEMI {
243 $$ = npval2(PV_VARDEC, &@1, &@5);
248 local_assignment : KW_LOCAL word EQ { reset_semicount(parseio->scanner); } word SEMI {
249 $$ = npval2(PV_LOCALVARDEC, &@1, &@6);
254 /* XXX this matches missing arguments, is this desired ? */
255 arglist : /* empty */ { $$ = NULL; }
256 | word { $$ = nword($1, &@1); }
257 | arglist COMMA word { $$ = linku1($1, nword($3, &@3)); }
258 | arglist error {$$=$1;}
262 | element elements { $$ = linku1($1, $2); }
263 | error elements { $$=$2;}
266 element : extension {$$=$1;}
271 | assignment {$$=$1;}
272 | local_assignment {$$=$1;}
273 | word error {free($1); $$=0;}
274 | SEMI {$$=0;/* allow older docs to be read */}
277 ignorepat : KW_IGNOREPAT EXTENMARK word SEMI {
278 $$ = npval2(PV_IGNOREPAT, &@1, &@4);
282 extension : word EXTENMARK statement {
283 $$ = npval2(PV_EXTENSION, &@1, &@3);
285 $$->u2.statements = $3; set_dads($$,$3);}
286 | word AT word EXTENMARK statement {
287 $$ = npval2(PV_EXTENSION, &@1, &@3);
288 $$->u1.str = malloc(strlen($1)+strlen($3)+2);
289 strcpy($$->u1.str,$1);
290 strcat($$->u1.str,"@");
291 strcat($$->u1.str,$3);
293 $$->u2.statements = $5; set_dads($$,$5);}
294 | KW_REGEXTEN word EXTENMARK statement {
295 $$ = npval2(PV_EXTENSION, &@1, &@4);
297 $$->u2.statements = $4; set_dads($$,$4);
299 | KW_HINT LP hint_word RP word EXTENMARK statement {
300 $$ = npval2(PV_EXTENSION, &@1, &@7);
302 $$->u2.statements = $7; set_dads($$,$7);
304 | KW_REGEXTEN KW_HINT LP hint_word RP word EXTENMARK statement {
305 $$ = npval2(PV_EXTENSION, &@1, &@8);
307 $$->u2.statements = $8; set_dads($$,$8);
312 /* list of statements in a block or after a case label - can be empty */
313 statements : /* empty */ { $$ = NULL; }
314 | statement statements { $$ = linku1($1, $2); }
315 | error statements {$$=$2;}
318 /* hh:mm-hh:mm, due to the way the parser works we do not
319 * detect the '-' but only the ':' as separator
321 timerange: word3_list COLON word3_list COLON word3_list {
322 asprintf(&$$, "%s:%s:%s", $1, $3, $5);
329 /* full time specification range|dow|*|* */
330 timespec : timerange BAR word3_list BAR word3_list BAR word3_list {
332 $$->next = nword($3, &@3);
333 $$->next->next = nword($5, &@5);
334 $$->next->next->next = nword($7, &@7); }
337 /* expression used in if, random, while, switch */
338 test_expr : LP { reset_parencount(parseio->scanner); } word_list RP { $$ = $3; }
341 /* 'if' like statements: if, iftime, random */
342 if_like_head : KW_IF test_expr {
343 $$= npval2(PV_IF, &@1, &@2);
345 | KW_RANDOM test_expr {
346 $$ = npval2(PV_RANDOM, &@1, &@2);
348 | KW_IFTIME LP timespec RP {
349 $$ = npval2(PV_IFTIME, &@1, &@4);
354 /* word_list is a hack to fix a problem with context switching between bison and flex;
355 by the time you register a new context with flex, you've already got a look-ahead token
356 from the old context, with no way to put it back and start afresh. So, we kludge this
357 and merge the words back together. */
359 word_list : word { $$ = $1;}
361 asprintf(&($$), "%s%s", $1, $2);
367 hint_word : word { $$ = $1; }
369 asprintf(&($$), "%s %s", $1, $2);
372 | hint_word COLON word {
373 asprintf(&($$), "%s:%s", $1, $3);
376 | hint_word AMPER word { /* there are often '&' in hints */
377 asprintf(&($$), "%s&%s", $1, $3);
382 word3_list : word { $$ = $1;}
384 asprintf(&($$), "%s%s", $1, $2);
389 asprintf(&($$), "%s%s%s", $1, $2, $3);
396 goto_word : word { $$ = $1;}
398 asprintf(&($$), "%s%s", $1, $2);
401 | goto_word COLON word {
402 asprintf(&($$), "%s:%s", $1, $3);
407 switch_statement : KW_SWITCH test_expr LC case_statements RC {
408 $$ = npval2(PV_SWITCH, &@1, &@5);
410 $$->u2.statements = $4; set_dads($$,$4);}
414 * Definition of a statememt in our language
416 statement : LC statements RC {
417 $$ = npval2(PV_STATEMENTBLOCK, &@1, &@3);
418 $$->u1.list = $2; set_dads($$,$2);}
419 | assignment { $$ = $1; }
420 | local_assignment { $$ = $1; }
421 | KW_GOTO target SEMI {
422 $$ = npval2(PV_GOTO, &@1, &@3);
424 | KW_JUMP jumptarget SEMI {
425 $$ = npval2(PV_GOTO, &@1, &@3);
428 $$ = npval2(PV_LABEL, &@1, &@2);
430 | KW_FOR LP {reset_semicount(parseio->scanner);} word SEMI
431 {reset_semicount(parseio->scanner);} word SEMI
432 {reset_parencount(parseio->scanner);} word RP statement { /* XXX word_list maybe ? */
433 $$ = npval2(PV_FOR, &@1, &@12);
434 $$->u1.for_init = $4;
436 $$->u3.for_inc = $10;
437 $$->u4.for_statements = $12; set_dads($$,$12);}
438 | KW_WHILE test_expr statement {
439 $$ = npval2(PV_WHILE, &@1, &@3);
441 $$->u2.statements = $3; set_dads($$,$3);}
442 | switch_statement { $$ = $1; }
443 | AMPER macro_call SEMI { $$ = update_last($2, &@2); }
444 | application_call SEMI { $$ = update_last($1, &@2); }
446 $$= npval2(PV_APPLICATION_CALL, &@1, &@2);
448 | application_call EQ {reset_semicount(parseio->scanner);} word SEMI {
452 $$ = npval2(PV_VARDEC, &@1, &@5);
454 /* rebuild the original string-- this is not an app call, it's an unwrapped vardec, with a func call on the LHS */
455 /* string to big to fit in the buffer? */
456 tot+=strlen($1->u1.str);
457 for(pptr=$1->u2.arglist;pptr;pptr=pptr->next) {
458 tot+=strlen(pptr->u1.str);
459 tot++; /* for a sep like a comma */
461 tot+=4; /* for safety */
462 bufx = calloc(1, tot);
463 strcpy(bufx,$1->u1.str);
465 /* XXX need to advance the pointer or the loop is very inefficient */
466 for (pptr=$1->u2.arglist;pptr;pptr=pptr->next) {
467 if ( pptr != $1->u2.arglist )
469 strcat(bufx,pptr->u1.str);
473 if ( !ael_is_funcname($1->u1.str) )
474 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",
475 my_file, @1.first_line, @1.first_column, @1.last_column, $1->u1.str);
478 destroy_pval($1); /* the app call it is not, get rid of that chain */
481 | KW_BREAK SEMI { $$ = npval2(PV_BREAK, &@1, &@2); }
482 | KW_RETURN SEMI { $$ = npval2(PV_RETURN, &@1, &@2); }
483 | KW_CONTINUE SEMI { $$ = npval2(PV_CONTINUE, &@1, &@2); }
484 | if_like_head statement opt_else {
485 $$ = update_last($1, &@2);
486 $$->u2.statements = $2; set_dads($$,$2);
487 $$->u3.else_statements = $3;set_dads($$,$3);}
491 opt_else : KW_ELSE statement { $$ = $2; }
495 target : goto_word { $$ = nword($1, &@1); }
496 | goto_word BAR goto_word {
498 $$->next = nword($3, &@3); }
499 | goto_word COMMA goto_word {
501 $$->next = nword($3, &@3); }
502 | goto_word BAR goto_word BAR goto_word {
504 $$->next = nword($3, &@3);
505 $$->next->next = nword($5, &@5); }
506 | goto_word COMMA goto_word COMMA goto_word {
508 $$->next = nword($3, &@3);
509 $$->next->next = nword($5, &@5); }
510 | KW_DEFAULT BAR goto_word BAR goto_word {
511 $$ = nword(strdup("default"), &@1);
512 $$->next = nword($3, &@3);
513 $$->next->next = nword($5, &@5); }
514 | KW_DEFAULT COMMA goto_word COMMA goto_word {
515 $$ = nword(strdup("default"), &@1);
516 $$->next = nword($3, &@3);
517 $$->next->next = nword($5, &@5); }
520 opt_pri : /* empty */ { $$ = strdup("1"); }
521 | COMMA word { $$ = $2; }
524 /* XXX please document the form of jumptarget */
525 jumptarget : goto_word opt_pri { /* ext[, pri] default 1 */
527 $$->next = nword($2, &@2); } /* jump extension[,priority][@context] */
528 | goto_word opt_pri AT context_name { /* context, ext, pri */
530 $$->next = nword($1, &@1);
531 $$->next->next = nword($2, &@2); }
534 macro_call : word LP {reset_argcount(parseio->scanner);} eval_arglist RP {
535 /* XXX original code had @2 but i think we need @5 */
536 $$ = npval2(PV_MACRO_CALL, &@1, &@5);
538 $$->u2.arglist = $4;}
540 $$= npval2(PV_MACRO_CALL, &@1, &@3);
544 /* XXX application_call_head must be revised. Having 'word LP { ...'
545 * just as above should work fine, however it gives a different result.
547 application_call_head: word LP {reset_argcount(parseio->scanner);} {
548 if (strcasecmp($1,"goto") == 0) {
549 $$ = npval2(PV_GOTO, &@1, &@2);
550 free($1); /* won't be using this */
551 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 );
553 $$= npval2(PV_APPLICATION_CALL, &@1, &@2);
558 application_call : application_call_head eval_arglist RP {
559 $$ = update_last($1, &@3);
560 if( $$->type == PV_GOTO )
565 | application_call_head RP { $$ = update_last($1, &@2); }
568 opt_word : word { $$ = $1 }
569 | { $$ = strdup(""); }
572 eval_arglist : word_list { $$ = nword($1, &@1); }
574 $$= npval(PV_WORD,0/*@1.first_line*/,0/*@1.last_line*/,0/* @1.first_column*/, 0/*@1.last_column*/);
575 $$->u1.str = strdup(""); }
576 | eval_arglist COMMA opt_word { $$ = linku1($1, nword($3, &@3)); }
579 case_statements: /* empty */ { $$ = NULL; }
580 | case_statement case_statements { $$ = linku1($1, $2); }
583 case_statement: KW_CASE word COLON statements {
584 $$ = npval2(PV_CASE, &@1, &@3); /* XXX 3 or 4 ? */
586 $$->u2.statements = $4; set_dads($$,$4);}
587 | KW_DEFAULT COLON statements {
588 $$ = npval2(PV_DEFAULT, &@1, &@3);
590 $$->u2.statements = $3;set_dads($$,$3);}
591 | KW_PATTERN word COLON statements {
592 $$ = npval2(PV_PATTERN, &@1, &@4); /* XXX@3 or @4 ? */
594 $$->u2.statements = $4;set_dads($$,$4);}
597 macro_statements: /* empty */ { $$ = NULL; }
598 | macro_statement macro_statements { $$ = linku1($1, $2); }
601 macro_statement : statement {$$=$1;}
603 | KW_CATCH word LC statements RC {
604 $$ = npval2(PV_CATCH, &@1, &@5);
606 $$->u2.statements = $4; set_dads($$,$4);}
609 switches : KW_SWITCHES LC switchlist RC {
610 $$ = npval2(PV_SWITCHES, &@1, &@2);
611 $$->u1.list = $3; set_dads($$,$3);}
614 eswitches : KW_ESWITCHES LC switchlist RC {
615 $$ = npval2(PV_ESWITCHES, &@1, &@2);
616 $$->u1.list = $3; set_dads($$,$3);}
619 switchlist : /* empty */ { $$ = NULL; }
620 | word SEMI switchlist { $$ = linku1(nword($1, &@1), $3); }
621 | word AT word SEMI switchlist { char *x; asprintf(&x,"%s@%s", $1,$3); free($1); free($3);
622 $$ = linku1(nword(x, &@1), $5);}
623 | error switchlist {$$=$2;}
626 included_entry : context_name { $$ = nword($1, &@1); }
627 | context_name BAR timespec {
630 prev_word=0; /* XXX sure ? */ }
633 /* list of ';' separated context names followed by optional timespec */
634 includeslist : included_entry SEMI { $$ = $1; }
635 | includeslist included_entry SEMI { $$ = linku1($1, $2); }
636 | includeslist error {$$=$1;}
639 includes : KW_INCLUDES LC includeslist RC {
640 $$ = npval2(PV_INCLUDES, &@1, &@4);
641 $$->u1.list = $3;set_dads($$,$3);}
642 | KW_INCLUDES LC RC {
643 $$ = npval2(PV_INCLUDES, &@1, &@3);}
649 static char *token_equivs1[] =
689 static char *token_equivs2[] =
730 static char *ael_token_subst(const char *mess)
732 /* calc a length, malloc, fill, and return; yyerror had better free it! */
736 int token_equivs_entries = sizeof(token_equivs1)/sizeof(char*);
738 for (p=mess; *p; p++) {
739 for (i=0; i<token_equivs_entries; i++) {
740 if ( strncmp(p,token_equivs1[i],strlen(token_equivs1[i])) == 0 )
742 len+=strlen(token_equivs2[i])+2;
743 p += strlen(token_equivs1[i])-1;
749 res = calloc(1, len+1);
754 for (i=0; i<token_equivs_entries; i++) {
755 if ( strncmp(p,token_equivs1[i],strlen(token_equivs1[i])) == 0 ) {
757 for (t=token_equivs2[i]; *t;) {
761 p += strlen(token_equivs1[i]);
773 void yyerror(YYLTYPE *locp, struct parse_io *parseio, char const *s)
775 char *s2 = ael_token_subst((char *)s);
776 if (locp->first_line == locp->last_line) {
777 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);
779 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);
782 parseio->syntax_error_count++;
785 struct pval *npval(pvaltype type, int first_line, int last_line,
786 int first_column, int last_column)
788 pval *z = calloc(1, sizeof(struct pval));
790 z->startline = first_line;
791 z->endline = last_line;
792 z->startcol = first_column;
793 z->endcol = last_column;
794 z->filename = strdup(my_file);
798 static struct pval *npval2(pvaltype type, YYLTYPE *first, YYLTYPE *last)
800 return npval(type, first->first_line, last->last_line,
801 first->first_column, last->last_column);
804 static struct pval *update_last(pval *obj, YYLTYPE *last)
806 obj->endline = last->last_line;
807 obj->endcol = last->last_column;
811 /* frontend for npval to create a PV_WORD string from the given token */
812 static pval *nword(char *string, YYLTYPE *pos)
814 pval *p = npval2(PV_WORD, pos, pos);
820 /* this routine adds a dad ptr to each element in the list */
821 static void set_dads(struct pval *dad, struct pval *child_list)
825 for(t=child_list;t;t=t->next) /* simple stuff */