2006-04-24 17:41:27 +00:00
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2006, Digium, Inc.
*
* Steve Murphy <murf@parsetree.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Flex scanner description of tokens used in AEL2 .
2006-04-26 18:40:09 +00:00
*
2006-04-27 21:09:52 +00:00
*/
/*
* Start with flex options:
*
* %x describes the contexts we have: paren, semic and argg, plus INITIAL
*/
%x paren semic argg
/* prefix used for various globally-visible functions and variables.
* This renames also yywrap, but since we do not use it, we just
* add option noyywrap to remove it.
*/
%option prefix="ael_yy"
%option noyywrap
/* batch gives a bit more performance if we are using it in
* a non-interactive mode. We probably don't care much.
*/
%option batch
/* outfile is the filename to be used instead of lex.yy.c */
%option outfile="ael_lex.c"
/*
* These are not supported in flex 2.5.4, but we need them
* at the moment:
* reentrant produces a thread-safe parser. Not 100% sure that
* we require it, though.
* bison-bridge passes an additional yylval argument to yylex().
* bison-locations is probably not needed.
*/
%option reentrant
%option bison-bridge
%option bison-locations
%{
#include <sys/types.h>
2006-04-24 17:41:27 +00:00
#include <sys/stat.h>
#include <unistd.h>
#include "asterisk.h"
#include "asterisk/logger.h"
2006-04-25 17:08:41 +00:00
#include "ael/ael.tab.h"
2006-04-24 17:41:27 +00:00
#include "asterisk/ael_structs.h"
2006-04-27 20:08:42 +00:00
/*
* A stack to keep track of matching brackets ( [ { } ] )
*/
2006-04-26 22:56:18 +00:00
static char pbcstack[400]; /* XXX missing size checks */
2006-04-24 17:41:27 +00:00
static int pbcpos = 0;
2006-04-30 09:24:04 +00:00
static void pbcpush(char x);
static int pbcpop(char x);
2006-04-24 17:41:27 +00:00
static int parencount = 0;
2006-04-27 20:08:42 +00:00
/*
* current line, column and filename, updated as we read the input.
*/
2006-04-27 17:00:09 +00:00
static int my_lineno = 1; /* current line in the source */
2006-04-28 15:24:30 +00:00
static int my_col = 1; /* current column in the source */
2006-04-27 20:08:42 +00:00
char *my_file = 0; /* used also in the bison code */
char *prev_word; /* XXX document it */
2006-04-24 17:41:27 +00:00
#define MAX_INCLUDE_DEPTH 50
2006-04-27 20:08:42 +00:00
/*
* flex is not too smart, and generates global functions
* without prototypes so the compiler may complain.
* To avoid that, we declare the prototypes here,
* even though these functions are not used.
*/
2006-04-24 17:41:27 +00:00
int ael_yyget_column (yyscan_t yyscanner);
void ael_yyset_column (int column_no , yyscan_t yyscanner);
2006-04-27 20:08:42 +00:00
2006-04-24 17:41:27 +00:00
int ael_yyparse (struct parse_io *);
2006-04-27 20:08:42 +00:00
/*
* A stack to process include files.
* As we switch into the new file we need to store the previous
* state to restore it later.
*/
2006-04-26 18:40:09 +00:00
struct stackelement {
char *fname;
int lineno;
int colno;
YY_BUFFER_STATE bufstate;
2006-04-24 17:41:27 +00:00
};
2006-04-27 20:08:42 +00:00
2006-04-26 23:08:47 +00:00
static struct stackelement include_stack[MAX_INCLUDE_DEPTH];
static int include_stack_index = 0;
2006-04-24 17:41:27 +00:00
2006-04-27 17:00:09 +00:00
/*
* if we use the @n feature of bison, we must supply the start/end
* location of tokens in the structure pointed by yylloc.
* Simple tokens are just assumed to be on the same line, so
* the line number is constant, and the column is incremented
* by the length of the token.
*/
2006-04-27 21:09:52 +00:00
#ifdef FLEX_BETA /* set for 2.5.33 */
/* compute the total number of lines and columns in the text
* passed as argument.
*/
static void pbcwhere(const char *text, int *line, int *col )
{
int loc_line = *line;
int loc_col = *col;
char c;
while ( (c = *text++) ) {
2006-04-28 15:33:05 +00:00
if ( c == '\t' ) {
loc_col += 8 - (loc_col % 8);
} else if ( c == '\n' ) {
2006-04-27 21:09:52 +00:00
loc_line++;
2006-04-28 15:33:05 +00:00
loc_col = 1;
} else
loc_col++;
2006-04-27 21:09:52 +00:00
}
*line = loc_line;
*col = loc_col;
}
2006-04-26 23:18:03 +00:00
#define STORE_POS do { \
yylloc->first_line = yylloc->last_line = my_lineno; \
yylloc->first_column=my_col; \
2006-04-27 17:00:09 +00:00
yylloc->last_column=my_col+yyleng-1; \
2006-04-26 23:18:03 +00:00
my_col+=yyleng; \
} while (0)
2006-04-27 17:10:15 +00:00
2006-04-28 15:42:13 +00:00
#define STORE_LOC do { \
2006-04-27 17:10:15 +00:00
yylloc->first_line = my_lineno; \
yylloc->first_column=my_col; \
pbcwhere(yytext, &my_lineno, &my_col); \
yylloc->last_line = my_lineno; \
2006-04-28 15:24:30 +00:00
yylloc->last_column = my_col - 1; \
2006-04-27 17:10:15 +00:00
} while (0)
2006-04-27 21:09:52 +00:00
#else
#define STORE_POS
2006-04-28 15:33:05 +00:00
#define STORE_LOC
2006-04-27 21:09:52 +00:00
#endif
2006-04-24 17:41:27 +00:00
%}
2006-04-27 01:07:24 +00:00
NOPARENS [^()\[\]\{\}]*
NOARGG [^(),\{\}\[\]]*
NOSEMIC [^;()\{\}\[\]]*
2006-04-24 17:41:27 +00:00
%%
2006-04-27 01:07:24 +00:00
2006-04-26 23:18:03 +00:00
\{ { STORE_POS; return LC;}
\} { STORE_POS; return RC;}
\( { STORE_POS; return LP;}
\) { STORE_POS; return RP;}
\; { STORE_POS; return SEMI;}
\= { STORE_POS; return EQ;}
\, { STORE_POS; return COMMA;}
\: { STORE_POS; return COLON;}
\& { STORE_POS; return AMPER;}
\| { STORE_POS; return BAR;}
\=\> { STORE_POS; return EXTENMARK;}
\@ { STORE_POS; return AT;}
\/\/[^\n]* {/*comment*/}
context { STORE_POS; return KW_CONTEXT;}
abstract { STORE_POS; return KW_ABSTRACT;}
macro { STORE_POS; return KW_MACRO;};
globals { STORE_POS; return KW_GLOBALS;}
ignorepat { STORE_POS; return KW_IGNOREPAT;}
switch { STORE_POS; return KW_SWITCH;}
if { STORE_POS; return KW_IF;}
ifTime { STORE_POS; return KW_IFTIME;}
random { STORE_POS; return KW_RANDOM;}
regexten { STORE_POS; return KW_REGEXTEN;}
hint { STORE_POS; return KW_HINT;}
else { STORE_POS; return KW_ELSE;}
goto { STORE_POS; return KW_GOTO;}
jump { STORE_POS; return KW_JUMP;}
return { STORE_POS; return KW_RETURN;}
break { STORE_POS; return KW_BREAK;}
continue { STORE_POS; return KW_CONTINUE;}
for { STORE_POS; return KW_FOR;}
while { STORE_POS; return KW_WHILE;}
case { STORE_POS; return KW_CASE;}
default { STORE_POS; return KW_DEFAULT;}
pattern { STORE_POS; return KW_PATTERN;}
catch { STORE_POS; return KW_CATCH;}
switches { STORE_POS; return KW_SWITCHES;}
eswitches { STORE_POS; return KW_ESWITCHES;}
includes { STORE_POS; return KW_INCLUDES;}
2006-04-24 17:41:27 +00:00
2006-04-28 14:17:03 +00:00
\n { my_lineno++; my_col = 1; }
2006-04-26 23:36:05 +00:00
[ ]+ { my_col += yyleng; }
2006-04-28 14:17:03 +00:00
[\t]+ { my_col += (yyleng*8)-(my_col%8); }
2006-04-24 17:41:27 +00:00
[-a-zA-Z0-9'"_/.\<\>\*\+!$#\[\]][-a-zA-Z0-9'"_/.!\*\+\<\>\{\}$#\[\]]* {
2006-04-26 23:18:03 +00:00
STORE_POS;
2006-04-26 18:40:09 +00:00
yylval->str = strdup(yytext);
prev_word = yylval->str;
return word;
}
2006-04-27 01:35:52 +00:00
2006-05-01 00:20:11 +00:00
/*
* context used for arguments of if_head, random_head, switch_head,
* for (last statement), while (XXX why not iftime_head ?).
* End with the matching parentheses.
* A comma at the top level is valid here, unlike in argg where it
* is an argument separator so it must be returned as a token.
*/
2006-04-27 01:07:24 +00:00
<paren>{NOPARENS}\) {
2006-04-27 01:27:07 +00:00
if ( pbcpop(')') ) { /* error */
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-04-27 01:27:07 +00:00
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched ')' in expression: %s !\n", my_file, my_lineno, my_col, yytext);
2006-04-26 18:40:09 +00:00
BEGIN(0);
yylval->str = strdup(yytext);
prev_word = 0;
return word;
}
parencount--;
if ( parencount >= 0) {
yymore();
} else {
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-04-26 18:40:09 +00:00
yylval->str = strdup(yytext);
2006-04-30 23:31:43 +00:00
yylval->str[yyleng-1] = '\0'; /* trim trailing ')' */
2006-04-26 18:40:09 +00:00
unput(')');
BEGIN(0);
2006-04-30 23:21:49 +00:00
return word;
2006-04-26 18:40:09 +00:00
}
}
2006-04-27 11:20:26 +00:00
<paren>{NOPARENS}[\(\[\{] {
char c = yytext[yyleng-1];
if (c == '(')
parencount++;
pbcpush(c);
2006-04-27 01:07:24 +00:00
yymore();
}
2006-04-24 17:41:27 +00:00
2006-04-27 11:20:26 +00:00
<paren>{NOPARENS}[\]\}] {
char c = yytext[yyleng-1];
if ( pbcpop(c)) { /* error */
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-04-27 11:20:26 +00:00
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched '%c' in expression!\n",
my_file, my_lineno, my_col, c);
2006-04-26 18:40:09 +00:00
BEGIN(0);
yylval->str = strdup(yytext);
return word;
}
yymore();
}
2006-05-01 00:20:11 +00:00
/*
* handlers for arguments to a macro or application calls.
* We enter this context when we find the initial '(' and
* stay here until we close all matching parentheses,
* and find the comma (argument separator) or the closing ')'
* of the (external) call, which happens when parencount == 0
* before the decrement.
*/
2006-04-27 11:34:44 +00:00
<argg>{NOARGG}[\(\[\{] {
char c = yytext[yyleng-1];
if (c == '(')
parencount++;
pbcpush(c);
2006-04-27 01:35:52 +00:00
yymore();
}
2006-04-27 01:07:24 +00:00
<argg>{NOARGG}\) {
2006-04-27 01:27:07 +00:00
if ( pbcpop(')') ) { /* error */
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-04-27 01:27:07 +00:00
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched ')' in expression!\n", my_file, my_lineno, my_col);
2006-04-26 18:40:09 +00:00
BEGIN(0);
yylval->str = strdup(yytext);
return word;
}
parencount--;
if( parencount >= 0){
yymore();
} else {
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-04-26 18:40:09 +00:00
BEGIN(0);
2006-04-30 23:31:43 +00:00
if ( !strcmp(yytext, ")") )
2006-04-30 23:21:49 +00:00
return RP;
2006-04-30 23:31:43 +00:00
yylval->str = strdup(yytext);
yylval->str[yyleng-1] = '\0'; /* trim trailing ')' */
unput(')');
return word;
2006-04-26 18:40:09 +00:00
}
}
2006-04-27 01:07:24 +00:00
<argg>{NOARGG}\, {
2006-04-28 15:42:13 +00:00
if( parencount != 0) { /* printf("Folding in a comma!\n"); */
2006-04-26 18:40:09 +00:00
yymore();
} else {
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-05-01 00:31:47 +00:00
if( !strcmp(yytext,"," ) )
2006-04-26 18:40:09 +00:00
return COMMA;
2006-05-01 00:31:47 +00:00
yylval->str = strdup(yytext);
yylval->str[yyleng-1] = '\0';
unput(',');
return word;
2006-04-26 18:40:09 +00:00
}
}
2006-04-24 17:41:27 +00:00
2006-04-27 11:43:34 +00:00
<argg>{NOARGG}[\]\}] {
char c = yytext[yyleng-1];
if ( pbcpop(c) ) { /* error */
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-04-27 11:43:34 +00:00
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched '%c' in expression!\n", my_file, my_lineno, my_col, c);
2006-04-26 18:40:09 +00:00
BEGIN(0);
yylval->str = strdup(yytext);
return word;
}
yymore();
}
2006-04-24 17:41:27 +00:00
2006-05-01 00:20:11 +00:00
/*
* context used to find tokens in the right hand side of assignments,
* or in the first and second operand of a 'for'. As above, match
* commas and use ';' as a separator (hence return it as a separate token).
*/
2006-04-27 11:34:44 +00:00
<semic>{NOSEMIC}[\(\[\{] {
char c = yytext[yyleng-1];
2006-04-27 01:07:24 +00:00
yymore();
2006-04-27 11:34:44 +00:00
pbcpush(c);
2006-04-27 17:16:41 +00:00
}
2006-04-26 18:40:09 +00:00
2006-04-27 11:43:34 +00:00
<semic>{NOSEMIC}[\)\]\}] {
char c = yytext[yyleng-1];
if ( pbcpop(c) ) { /* error */
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-04-27 11:43:34 +00:00
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched '%c' in expression!\n", my_file, my_lineno, my_col, c);
2006-04-26 18:40:09 +00:00
BEGIN(0);
yylval->str = strdup(yytext);
return word;
}
yymore();
}
2006-04-27 01:07:24 +00:00
<semic>{NOSEMIC}; {
2006-04-28 15:42:13 +00:00
STORE_LOC;
2006-04-26 18:40:09 +00:00
yylval->str = strdup(yytext);
2006-05-01 00:27:27 +00:00
yylval->str[yyleng-1] = '\0';
2006-04-26 18:40:09 +00:00
unput(';');
BEGIN(0);
return word;
}
2006-04-24 17:41:27 +00:00
\#include[ \t]+\"[^\"]+\" {
2006-04-26 18:40:09 +00:00
FILE *in1;
char fnamebuf[1024],*p1,*p2;
2006-04-27 19:29:14 +00:00
int error = 1; /* don't use the file if set */
p1 = strchr(yytext,'"');
p2 = strrchr(yytext,'"');
2006-04-26 18:40:09 +00:00
if ( include_stack_index >= MAX_INCLUDE_DEPTH ) {
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Includes nested too deeply! Wow!!! How did you do that?\n", my_file, my_lineno, my_col);
2006-04-27 19:29:14 +00:00
} else if ( (int)(p2-p1) > sizeof(fnamebuf) - 1 ) {
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Filename is incredibly way too long (%d chars!). Inclusion ignored!\n", my_file, my_lineno, my_col, yyleng - 10);
2006-04-26 18:40:09 +00:00
} else {
2006-04-27 19:29:14 +00:00
int i;
strncpy(fnamebuf, p1, p2-p1);
fnamebuf[p2-p1] = 0;
for (i=0; i<include_stack_index; i++) {
if ( !strcmp(fnamebuf,include_stack[i].fname )) {
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Nice Try!!! But %s has already been included (perhaps by another file), and would cause an infinite loop of file inclusions!!! Include directive ignored\n",
my_file, my_lineno, my_col, fnamebuf);
break;
2006-04-26 18:40:09 +00:00
}
}
2006-04-27 19:29:14 +00:00
if (i == include_stack_index)
error = 0; /* we can use this file */
}
if ( !error ) { /* valid file name */
*p2 = 0;
/* relative vs. absolute */
if ( *(p1+1) != '/' ) {
/* XXX must check overflows */
strcpy(fnamebuf,ast_config_AST_CONFIG_DIR);
strcat(fnamebuf,"/");
strcat(fnamebuf,p1+1);
} else
strcpy(fnamebuf,p1+1);
in1 = fopen( fnamebuf, "r" );
if ( ! in1 ) {
ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Couldn't find the include file: %s; ignoring the Include directive!\n", my_file, my_lineno, my_col, fnamebuf);
} else {
char *buffer;
struct stat stats;
stat(fnamebuf, &stats);
buffer = (char*)malloc(stats.st_size+1);
fread(buffer, 1, stats.st_size, in1);
buffer[stats.st_size] = 0;
ast_log(LOG_NOTICE," --Read in included file %s, %d chars\n",fnamebuf, (int)stats.st_size);
fclose(in1);
include_stack[include_stack_index].fname = my_file;
my_file = strdup(fnamebuf);
include_stack[include_stack_index].lineno = my_lineno;
include_stack[include_stack_index].colno = my_col+yyleng;
include_stack[include_stack_index++].bufstate = YY_CURRENT_BUFFER;
yy_switch_to_buffer(ael_yy_scan_string (buffer ,yyscanner),yyscanner);
free(buffer);
my_lineno = 1;
my_col = 1;
BEGIN(INITIAL);
}
2006-04-26 18:40:09 +00:00
}
}
2006-04-24 17:41:27 +00:00
2006-04-26 18:40:09 +00:00
<<EOF>> {
if ( --include_stack_index < 0 ) {
yyterminate();
} else {
free(my_file);
yy_delete_buffer( YY_CURRENT_BUFFER, yyscanner );
yy_switch_to_buffer(include_stack[include_stack_index].bufstate, yyscanner );
my_lineno = include_stack[include_stack_index].lineno;
my_col = include_stack[include_stack_index].colno;
my_file = include_stack[include_stack_index].fname;
}
}
2006-04-24 17:41:27 +00:00
%%
static void pbcpush(char x)
{
pbcstack[pbcpos++] = x;
}
static int pbcpop(char x)
{
if ( ( x == ')' && pbcstack[pbcpos-1] == '(' )
|| ( x == ']' && pbcstack[pbcpos-1] == '[' )
|| ( x == '}' && pbcstack[pbcpos-1] == '{' )) {
pbcpos--;
return 0;
}
2006-04-26 18:40:09 +00:00
return 1; /* error */
2006-04-24 17:41:27 +00:00
}
2006-04-27 10:13:39 +00:00
static int c_prevword(void)
2006-04-24 17:41:27 +00:00
{
2006-04-26 18:40:09 +00:00
char *c = prev_word;
2006-04-27 11:20:26 +00:00
if (c == NULL)
return 0;
while ( *c ) {
2006-04-26 18:40:09 +00:00
switch (*c) {
2006-04-27 11:20:26 +00:00
case '{':
case '[':
case '(':
pbcpush(*c);
break;
case '}':
case ']':
case ')':
if (pbcpop(*c))
return 1;
break;
2006-04-26 18:40:09 +00:00
}
2006-04-24 17:41:27 +00:00
c++;
}
return 0;
}
2006-04-30 09:06:28 +00:00
/*
* The following three functions, reset_*, are used in the bison
* code to switch context. As a consequence, we need to
* declare them global and add a prototype so that the
* compiler does not complain.
*
* NOTE: yyg is declared because it is used in the BEGIN macros,
* though that should be hidden as the macro changes
* depending on the flex options that we use - in particular,
* %reentrant changes the way the macro is declared;
* without %reentrant, BEGIN uses yystart instead of yyg
*/
2006-04-27 00:05:05 +00:00
void reset_parencount(yyscan_t yyscanner );
void reset_parencount(yyscan_t yyscanner )
2006-04-24 17:41:27 +00:00
{
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
parencount = 0;
pbcpos = 0;
2006-05-01 00:20:11 +00:00
pbcpush('('); /* push '(' so the last pcbpop (parencount= -1) will succeed */
2006-04-24 17:41:27 +00:00
c_prevword();
BEGIN(paren);
}
2006-04-27 00:05:05 +00:00
void reset_semicount(yyscan_t yyscanner );
void reset_semicount(yyscan_t yyscanner )
2006-04-24 17:41:27 +00:00
{
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
pbcpos = 0;
BEGIN(semic);
}
2006-04-27 00:05:05 +00:00
void reset_argcount(yyscan_t yyscanner );
void reset_argcount(yyscan_t yyscanner )
2006-04-24 17:41:27 +00:00
{
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
parencount = 0;
pbcpos = 0;
2006-05-01 00:20:11 +00:00
pbcpush('('); /* push '(' so the last pcbpop (parencount= -1) will succeed */
2006-04-24 17:41:27 +00:00
c_prevword();
BEGIN(argg);
}
2006-04-26 22:41:16 +00:00
/* used elsewhere, but some local vars */
2006-04-24 17:41:27 +00:00
struct pval *ael2_parse(char *filename, int *errors)
{
struct pval *pval;
struct parse_io *io;
char *buffer;
struct stat stats;
FILE *fin;
/* extern int ael_yydebug; */
io = calloc(sizeof(struct parse_io),1);
/* reset the global counters */
prev_word = 0;
my_lineno = 1;
include_stack_index=0;
my_col = 0;
/* ael_yydebug = 1; */
ael_yylex_init(&io->scanner);
fin = fopen(filename,"r");
if ( !fin ) {
ast_log(LOG_ERROR,"File %s could not be opened\n", filename);
*errors = 1;
return 0;
}
my_file = strdup(filename);
stat(filename, &stats);
buffer = (char*)malloc(stats.st_size+2);
fread(buffer, 1, stats.st_size, fin);
buffer[stats.st_size]=0;
fclose(fin);
2006-04-26 18:40:09 +00:00
2006-04-24 17:41:27 +00:00
ael_yy_scan_string (buffer ,io->scanner);
ael_yyset_lineno(1 , io->scanner);
/* ael_yyset_in (fin , io->scanner); OLD WAY */
ael_yyparse(io);
pval = io->pval;
*errors = io->syntax_error_count;
ael_yylex_destroy(io->scanner);
free(buffer);
free(io);
return pval;
}