/*************************************************************************************************
** Project:     FSMGenerator
** Module:      FSMGenerator::scanner
** File:        scanner.l
** Date:        2002.08.01
** Author:      Pavel Bekkerman (chpavel@tx.technion.ac.il)
** Copyright:   Copyright (C) 2002 Pavel Bekkerman
** License:
**              This program 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.
**
**              This program 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 this program; if not, write to the Free Software
**              Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**
** Contact:     Pavel Bekkerman (chpavel@tx.technion.ac.il)
*************************************************************************************************/

%{ 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "FSMScanner.h"

void prepare_token( int nID );
int count_newlines( char* strText );

int g_nLine = 1;

%}

SQUARE_BRACET_LEFT	"["
SQUARE_BRACET_RIGHT	"]"
COLLON			":"
SEMICOLLON		";"
COMMA			","
DOT			"."
QUOTATION		"\""
ARROW			"-"">"
DIGIT    		[0-9]
SYMBOL   		[a-zA-Z_$@]
INTEGER			{DIGIT}*
FLOAT			{DIGIT}*{DOT}{DIGIT}+
NAME			{SYMBOL}({SYMBOL}|{DIGIT})*
STRING			{QUOTATION}({SYMBOL}|{DIGIT})*{QUOTATION}
COMMENT 		"/""*""*""/"|"/""*"([^"*"]|"*"[^"/"]|"/")*"*""/"
SECTION			{SQUARE_BRACET_LEFT}{NAME}{SQUARE_BRACET_RIGHT}

%%

{DOT}		{ prepare_token( TOKEN_DOT ); return( ERROR_SUCCESS ); }
{SEMICOLLON}	{ prepare_token( TOKEN_SEMICOLLON ); return( ERROR_SUCCESS ); }
{COLLON}	{ prepare_token( TOKEN_COLLON ); return( ERROR_SUCCESS ); }
{COMMA}		{ prepare_token( TOKEN_COMMA ); return( ERROR_SUCCESS ); }
{ARROW}		{ prepare_token( TOKEN_ARROW ); return( ERROR_SUCCESS ); }

{SECTION}	{ prepare_token( TOKEN_SECTION ); return( ERROR_SUCCESS ); }
{STRING}	{ prepare_token( TOKEN_STRING ); return( ERROR_SUCCESS ); }
{NAME}		{ prepare_token( TOKEN_NAME ); return( ERROR_SUCCESS ); }
{COMMENT}	{ prepare_token( TOKEN_COMMENT ); g_nLine += count_newlines(yytext); return( ERROR_SUCCESS ); }
[\t\f\ ]+	{}
"\n"		{ g_nLine++; }
.               { fprintf( stderr,"unexpected char '%c'!\n", yytext[0]); exit(-1); }

%%

int yywrap() 
{ 
  return 1; 
}

void prepare_token( int nID )
{
	yylval.id = nID;
	yylval.text = (char*) malloc( strlen(yytext) + 1 );
	yylval.line = g_nLine;
        strcpy( yylval.text, yytext );
}

int count_newlines( char* strText )
{
  int counter;

	if( !strText || !*strText )
		return 0;

	counter = 0;
	for( ; *strText; strText++ )
		if( *strText == '\n' )
			counter++;

  return counter;
}
