aboutsummaryrefslogtreecommitdiff
path: root/lib/forlanlex.l
blob: 5281e616d2da544f750aa1894350a40262fa7c7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
%{
/* This file is part of Eclat.
   Copyright (C) 2012-2023 Sergey Poznyakoff.
 
   Eclat 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 3, or (at your option)
   any later version.
 
   Eclat 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 Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "libeclat.h"
#include <sysexits.h>
#include <ctype.h>
#include <grecs.h>
#include <grecs/locus.h>
#include "forlangrm.h"
#include "forlan.h"

static const char *forlan_input_base;
static size_t forlan_input_len;
static size_t forlan_input_pos;

#undef YY_INPUT
#define YY_INPUT(buf,result,max_size)					\
	do {								\
		if (forlan_input_base) {				\
			size_t __s = forlan_input_len - forlan_input_pos; \
			if (__s > max_size)				\
				__s = max_size;				\
			if (__s > 0) {					\
				memcpy(buf, forlan_input_base, __s);	\
				forlan_input_pos += __s;		\
			}						\
			result = __s;					\
		} else							\
			result = fread(buf, 1, max_size, yyin);		\
	} while(0)

#define YY_USER_ACTION do {						\
		if (YYSTATE == 0) {					\
			yylloc.beg = grecs_current_locus_point;		\
			yylloc.beg.col++;				\
		}							\
  		grecs_current_locus_point.col += yyleng;		\
 		yylloc.end = grecs_current_locus_point;			\
   	} while (0);

static int yywrap(void);

%}

%option noinput
%option nounput

WS [ \t\f][ \t\f]*
ID [a-zA-Z_][a-zA-Z_0-9-]*
%x COMMENT ML STR
%%
         /* Comments */
"/*"         BEGIN(COMMENT);
<COMMENT>[^*\n]*        /* eat anything that's not a '*' */
<COMMENT>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<COMMENT>\n          grecs_locus_point_advance_line(grecs_current_locus_point);
<COMMENT>"*"+"/"     BEGIN(INITIAL);
"//".*               ;
         /* Decimal numbers */
-?[0-9][0-9]*         { grecs_line_begin();
                        grecs_line_add(yytext, yyleng);
                        yylval.string = grecs_line_finish();
	                return STRING; }
         /* Keywords */ 
if                    return IF;
else                  return ELSE;
last                  return LAST;
for                   return FOR;
let                   return LET;
in                    return IN;
break                 return BREAK;
continue              return CONTINUE;
!                     return NOT;
"&&"                  return AND;
"||"                  return OR;
"=="                  return EQ;
"!="                  return NE;
{ID}                  { grecs_line_begin();
                        grecs_line_add(yytext, yyleng);
                        yylval.string = grecs_line_finish();
	                return IDENT; }
         /* Quoted strings */
\"[^\\"\n]*\"         { grecs_line_begin();
                        grecs_line_add(yytext + 1, yyleng - 2);
                        yylval.string = grecs_line_finish();
                        return STRING; }
\"[^\\"\n]*\\\n        { BEGIN(STR);
                         grecs_line_begin();
		         grecs_line_acc_grow_unescape_last(yytext + 1,
                                                           yyleng - 1,
							   &yylloc);
                         grecs_locus_point_advance_line(grecs_current_locus_point); }
\"[^\\"\n]*\\.         { BEGIN(STR);
                         grecs_line_begin();
		         grecs_line_acc_grow_unescape_last(yytext + 1,
                                                           yyleng - 1,
                                                           &yylloc); }
<STR>\"[^\\"\n]*\\\n  { grecs_line_acc_grow_unescape_last(yytext, yyleng,
							  &yylloc);
                        grecs_locus_point_advance_line(grecs_current_locus_point); }
<STR>[^\\"\n]*\\.     { grecs_line_acc_grow_unescape_last(yytext, yyleng, &yylloc); }
<STR>[^\\"\n]*\"      { BEGIN(INITIAL);
                        if (yyleng > 1) 
				grecs_line_add(yytext, yyleng - 1); 
                        yylval.string = grecs_line_finish();
		        return STRING; }
         /* Other tokens */
{WS}     ;
\n       { grecs_locus_point_advance_line(grecs_current_locus_point); } 
[.,;{}()\[\]=\*%] return yytext[0];
.        { if (isascii(yytext[0]) && isprint(yytext[0]))
		grecs_error(&yylloc, 0,
			     _("stray character %c"), yytext[0]);
           else
		grecs_error(&yylloc, 0,
			     _("stray character \\%03o"),
			       (unsigned char) yytext[0]); }
%%

static int
yywrap()
{
	return 1;
}

void
forlan_lex_from_buffer(const char *input, size_t length,
		       struct grecs_locus_point *pt)
{
	forlan_input_base = input;
	forlan_input_len = length;
	grecs_current_locus_point = *pt;
	yy_flex_debug = debug_level(forlan_dbg) >= FORLAN_DBG_LEX;
	grecs_line_acc_create();
}

void
forlan_lex_from_file(FILE *fp, struct grecs_locus_point *pt)
{
	yyin = fp;
	forlan_input_base = NULL;
	grecs_current_locus_point = *pt;
	yy_flex_debug = debug_level(forlan_dbg) >= FORLAN_DBG_LEX;
	grecs_line_acc_create();
}

void
forlan_lex_end()
{
	grecs_line_acc_free();
}





Return to:

Send suggestions and report system problems to the System administrator.