%{
#include <string.h>
#include "deflex.h"
#ifdef DMALLOC
#include <dmalloc.h>
#endif

char *token = NULL;  /* pointer to token */
char *s     = NULL;  /* to return WHOLE hyphenated-word */

YY_BUFFER_STATE buf = NULL; /* buffer to parse; it need for parse from string */

int lrlimit = -1;	/* for limiting read from filehandle ( -1 - unlimited read ) */
int bytestoread = 0;	/* for limiting read from filehandle */

/* redefine macro for read limited length */
#define YY_INPUT(buf,result,max_size) \
	if ( yy_current_buffer->yy_is_interactive ) { \
                int c = '*', n; \
                for ( n = 0; n < max_size && \
                             (c = getc( fts_yyin )) != EOF && c != '\n'; ++n ) \
                        buf[n] = (char) c; \
                if ( c == '\n' ) \
                        buf[n++] = (char) c; \
                if ( c == EOF && ferror( fts_yyin ) ) \
                        YY_FATAL_ERROR( "input in flex scanner failed" ); \
                result = n; \
        }  else { \
		if ( lrlimit == 0 ) \
			result=YY_NULL; \
		else { \
			if ( lrlimit>0 ) { \
				bytestoread = ( lrlimit > max_size ) ? max_size : lrlimit; \
				lrlimit -= bytestoread; \
			} else \
				bytestoread = max_size; \
        		if ( ((result = fread( buf, 1, bytestoread, fts_yyin )) == 0) \
                  		&& ferror( fts_yyin ) ) \
                		YY_FATAL_ERROR( "input in flex scanner failed" ); \
		} \
	}


%}

/* parser's state for parsing hyphenated-word */
%x DELIM  
/* parser's state for parsing URL*/
%x URL  
%x SERVER  

/* parser's state for parsing TAGS */
%x INTAG
%x QINTAG
%x INCOMMENT
%x INSCRIPT

/* cyrillic koi8 char */
CYRALNUM	[0-9\xc0-\xdf\xe0-\xff\xa3\xb3]
CYRALPHA	[\xc0-\xdf\xe0-\xff\xa3\xb3]
ALPHA		[a-zA-Z\xc0-\xdf\xe0-\xff\xa3\xb3]
ALNUM		[0-9a-zA-Z\xc0-\xdf\xe0-\xff\xa3\xb3]


HOSTNAME	([-_[:alnum:]]+\.)+[[:alpha:]]+
URI		[-_[:alnum:]/%,\.;=&?#]+

%%

"<"[Ss][Cc][Rr][Ii][Pp][Tt] { BEGIN INSCRIPT; }

<INSCRIPT>"</"[Ss][Cc][Rr][Ii][Pp][Tt]">" {
	BEGIN INITIAL; 
	*fts_yytext=' '; *(fts_yytext+1) = '\0'; 
	token = fts_yytext;
	return SPACE;
}

"<!--"	{ BEGIN INCOMMENT; }

<INCOMMENT>"-->"	{ 
	BEGIN INITIAL;
	*fts_yytext=' '; *(fts_yytext+1) = '\0'; 
	token = fts_yytext;
	return SPACE;
}


"<"[\![:alpha:]]	{ BEGIN INTAG; }

"</"[[:alpha:]]	{ BEGIN INTAG; }

<INTAG>"\""	{ BEGIN QINTAG; }

<QINTAG>"\\\""	;

<QINTAG>"\""	{ BEGIN INTAG; }

<INTAG>">"	{ 
	BEGIN INITIAL;
	token = fts_yytext;
	*fts_yytext=' '; *(fts_yytext+1) = '\0'; 
	token = fts_yytext;
	return TAG;
}

<QINTAG,INTAG,INCOMMENT,INSCRIPT>.|\n 	;

\&(quot|amp|nbsp|lt|gt)\;   {
	token = fts_yytext;
	return HTMLENTITY;
}

\&\#[0-9][0-9]?[0-9]?\; {
	token = fts_yytext;
	return HTMLENTITY;
}
 
[-_\.[:alnum:]]+@{HOSTNAME}  /* Emails */ { 
	token = fts_yytext; 
	return EMAIL; 
}

[+-]?[0-9]+(\.[0-9]+)?[eEdD][+-]?[0-9]+  /* float */ 	{ 
	token = fts_yytext; 
	return SCIENTIFIC; 
}

[0-9]+\.[0-9]+\.[0-9\.]*[0-9] {
	token = fts_yytext;
	return VERSIONNUMBER;
}

[+-]?[0-9]+\.[0-9]+ {
	token = fts_yytext;
	return DECIMAL;
}

[+-][0-9]+ { 
	token = fts_yytext; 
	return SIGNEDINT; 
}

<DELIM,INITIAL>[0-9]+ { 
	token = fts_yytext; 
	return UNSIGNEDINT; 
}

http"://"        { 
	BEGIN URL; 
	token = fts_yytext;
	return HTTP;
}

ftp"://"        { 
	BEGIN URL; 
	token = fts_yytext;
	return HTTP;
}

<URL,INITIAL>{HOSTNAME}[/:]{URI} { 
	BEGIN SERVER;
	if (s) { free(s); s=NULL; } 
	s = strdup( fts_yytext ); 
	yyless( 0 ); 
	token = s;
	return FURL;
}

<SERVER,URL,INITIAL>{HOSTNAME} {
	token = fts_yytext; 
	return HOST;
}

<SERVER>[/:]{URI} 	{
	token = fts_yytext;
	return URI;
}

[[:alnum:]\./_-]+"/"[[:alnum:]\./_-]+ {
	token = fts_yytext;
	return FILEPATH;
}

({CYRALPHA}+-)+{CYRALPHA}+ /* composite-word */	{
	BEGIN DELIM;
	if (s) { free(s); s=NULL; } 
	s = strdup( fts_yytext );
	yyless( 0 );
	token = s;
	return CYRHYPHENWORD;
}

([[:alpha:]]+-)+[[:alpha:]]+ /* composite-word */	{
	 BEGIN DELIM;
	if (s) { free(s); s=NULL; } 
	s = strdup( fts_yytext );
	yyless( 0 );
	token = s;
	return LATHYPHENWORD;
}

({ALNUM}+-)+{ALNUM}+ /* composite-word */	{
	BEGIN DELIM;
	if (s) { free(s); s=NULL; } 
	s = strdup( fts_yytext );
	yyless( 0 );
	token = s;
	return HYPHENWORD;
}

<DELIM>[0-9]+\.[0-9]+\.[0-9\.]*[0-9] {
	token = fts_yytext;
	return VERSIONNUMBER;
}

<DELIM>\+?[0-9]+\.[0-9]+ {
	token = fts_yytext;
	return DECIMAL;
}

<DELIM>{CYRALPHA}+  /* one word in composite-word */	{ 
	token = fts_yytext; 
	return CYRPARTHYPHENWORD; 
}

<DELIM>[[:alpha:]]+  /* one word in composite-word */	{ 
	token = fts_yytext; 
	return LATPARTHYPHENWORD; 
}

<DELIM>{ALNUM}+  /* one word in composite-word */	{ 
	token = fts_yytext; 
	return PARTHYPHENWORD; 
}

<DELIM>-  { 
	token = fts_yytext;
	return SPACE;
}

<DELIM,SERVER,URL>.|\n /* return in basic state */	{
	BEGIN INITIAL;
	yyless( 0 );
}

{CYRALPHA}+ /* normal word */	{ 
	token = fts_yytext; 
	return CYRWORD; 
}

[[:alpha:]]+ /* normal word */	{ 
	token = fts_yytext; 
	return LATWORD; 
}

{ALNUM}+ /* normal word */	{ 
	token = fts_yytext; 
	return UWORD; 
}

[ \r\n\t]+ {
	token = fts_yytext;
	return SPACE;
}

. {
	token = fts_yytext;
	return SPACE;
} 

%%

int fts_yywrap(void) {
	return 1;
}

/* clearing after parsing from string */
void end_parse() {
	if (s) { free(s); s=NULL; } 
	fts_yy_delete_buffer( buf );
	buf = NULL;
} 

/* start parse from string */
void start_parse_str(char* str) {
	if (buf) end_parse();
	buf = fts_yy_scan_string( str );
	fts_yy_switch_to_buffer( buf );
	BEGIN INITIAL;
}

/* start parse from filehandle */
void start_parse_fh( FILE* fh, int limit ) {
	if (buf) end_parse();
	lrlimit = ( limit ) ? limit : -1;
	buf = fts_yy_create_buffer( fh, YY_BUF_SIZE );
	fts_yy_switch_to_buffer( buf );
	BEGIN INITIAL;
}


