tepal_scan.l 4.95 KB
%{
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

#include <tepal_pars.h>
%}

%x			TOK

DIGITS   	[0-9]+
INT      	{DIGITS}
FLOAT    	{DIGITS}(.{DIGITS})?
STRING		('[^']*')|(\"[^\"]*\")
IDENT    	[a-zA-Z][_0-9a-zA-Z]*|_[0-9a-zA-Z]+
OPERATOR		[+\-*/%=,!:\[\]\(\);]

ICAST			int
FCAST			float
SCAST			string

S_TOK			[ \t]*<#
E_TOK			#>
TEXT			(\<[^#].*)|([^\<]*)

%%

{TEXT}				{
							char   tok[2];
							size_t len = strlen (yytext);

							yylval.cPtr = (char *) malloc (len + 2);
							memset (yylval.cPtr, 0, len + 2);
							memcpy (yylval.cPtr, yytext, len);

							tok[0] = input ();
							tok[1] = input ();

							if (tok[1] == '#' || tok[0] == EOF)
							{
								unput ('#');
								unput ('<');
							}
							else
							{
								if (tok[1] != EOF)
									unput (tok[1]);
								yylval.cPtr[len] = tok[0];
							}

							#ifdef DEBUG
							printf ("[TOKEN] HTML\n");
							#endif

							return HTML;
						}

{S_TOK}				{	
							BEGIN (TOK); 
							#ifdef DEBUG
							printf ("[TOKEN] STMT_END {[ \\t]*<#}\n");
							#endif
							return STMT_END;
						}

<TOK>{E_TOK}\n?	{ 
							BEGIN (INITIAL); 

							#ifdef DEBUG
							printf ("[TOKEN] STMT_END {#>\\n?}\n");
							#endif

							return STMT_END;
						}

<TOK>{OPERATOR}	{	
							#ifdef DEBUG
							printf ("[TOKEN] %c\n", yytext[0]);
							#endif
							yylval.iVal = yytext[0];
							return yytext[0]; 
						}

<TOK>==				{	
							#ifdef DEBUG
							printf ("[TOKEN] EQ\n");
							#endif
							
							return EQ;	
						}
<TOK>!=				{	
							#ifdef DEBUG
							printf ("[TOKEN] NE\n");
							#endif
							
							return NE;	
						}
<TOK>\<				{	
							#ifdef DEBUG
							printf ("[TOKEN] EQ\n");
							#endif
							
							return LT;	
						}
<TOK>\>				{	
							#ifdef DEBUG
							printf ("[TOKEN] EQ\n");
							#endif
							
							return GT;	
						}
<TOK>\<=				{	
							#ifdef DEBUG
							printf ("[TOKEN] EQ\n");
							#endif
							
							return LE;	
						}
<TOK>\>=				{	
							#ifdef DEBUG
							printf ("[TOKEN] EQ\n");
							#endif
							
							return GE;	
						}

<TOK>&&				{	
							#ifdef DEBUG
							printf ("[TOKEN] LOGAND\n");
							#endif
							
							return LOGAND;	
						}
<TOK>\|\|			{	
							#ifdef DEBUG
							printf ("[TOKEN] LOGOR\n");
							#endif
							
							return LOGOR;	
						}

<TOK>{INT}			{
							yylval.iVal = atol (yytext);

							#ifdef DEBUG
							printf ("[TOKEN] INT\n");
							#endif

							return INT;
						}

<TOK>{FLOAT}		{
							yylval.fVal = atof (yytext);

							#ifdef DEBUG
							printf ("[TOKEN] FLOAT\n");
							#endif

							return FLOAT;
						}

<TOK>{STRING}		{
							yylval.cPtr = (char *) malloc (strlen (yytext) - 1);
							memset (yylval.cPtr, 0, strlen (yytext) - 1);
							memcpy (yylval.cPtr, yytext + 1, strlen (yytext) - 2);

							#ifdef DEBUG
							printf ("[TOKEN] STRING\n");
							#endif

							return STRING;
						}

<TOK>repeat			{	
							#ifdef DEBUG
							printf ("[TOKEN] REPEAT\n"); 
							#endif
							
							return REPEAT;	
						}
<TOK>count			{	
							#ifdef DEBUG
							printf ("[TOKEN] COUNT\n"); 
							#endif

							return COUNT;	
						}

<TOK>foreach		{	
							#ifdef DEBUG
							printf ("[TOKEN] FOREACH\n"); 
							#endif
							
							return FOREACH;	
						}
<TOK>as				{	
							#ifdef DEBUG
							printf ("[TOKEN] AS\n"); 
							#endif
							
							return AS;	
						}

<TOK>if				{	
							#ifdef DEBUG
							printf ("[TOKEN] IF\n"); 
							#endif
							
							return IF;	
						}
<TOK>else			{	
							#ifdef DEBUG
							printf ("[TOKEN] ELSE\n"); 
							#endif
							
							return ELSE;	
						}

<TOK>end				{	
							#ifdef DEBUG
							printf ("[TOKEN] BLOCK_END\n"); 
							#endif
							
							return BLOCK_END;	
						}

<TOK>unset			{	
							#ifdef DEBUG
							printf ("[TOKEN] UNSET\n"); 
							#endif
							
							return UNSET;	
						}

<TOK>eol				{	
							yylval.cPtr = (char *) calloc (sizeof (char), 2);
							yylval.cPtr[0] = '\n';
							yylval.cPtr[1] = '\0';
							#ifdef DEBUG
							printf ("[TOKEN] EOL\n"); 
							#endif

							return EOL;	
						}

<TOK>parent			{
							#ifdef DEBUG
							printf ("[TOKEN] PARENT\n"); 
							#endif
							
							return PARENT;	
						}

<TOK>global			{
							#ifdef DEBUG
							printf ("[TOKEN] GLOBAL\n"); 
							#endif
							
							return GLOBAL;	
						}

<TOK>{ICAST}		{	
							#ifdef DEBUG
							printf ("[TOKEN] ICAST\n"); 
							#endif
							
							return ICAST;	
						}
<TOK>{FCAST}			{	
							#ifdef DEBUG
							printf ("[TOKEN] FCAST\n"); 
							#endif
							
							return FCAST;	
						}
<TOK>{SCAST}			{	
							#ifdef DEBUG
							printf ("[TOKEN] SCAST\n"); 
							#endif
							
							return SCAST;	
						}

<TOK>{IDENT}		{
							yylval.cPtr = (char *) malloc (strlen (yytext) + 1);
							strcpy (yylval.cPtr, yytext);

							#ifdef DEBUG
							printf ("[TOKEN] IDENT\n");
							#endif

							return IDENT;
						}

<TOK>[ \t\r\n]		{ /* eat tokens */ }