File: external_l.l

package info (click to toggle)
gnubg 1.08.003-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,308 kB
  • sloc: ansic: 104,162; xml: 15,451; sh: 5,292; pascal: 820; yacc: 700; makefile: 586; python: 538; lex: 286; sql: 236; awk: 26
file content (210 lines) | stat: -rw-r--r-- 6,184 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
%{
/*
 * Copyright (C) 2014-2015 Michael Petch <mpetch@gnubg.org>
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 *
 * $Id: external_l.l,v 1.33 2023/06/11 21:20:12 plm Exp $
 */

#include "common.h"
#include "external.h"

#include "external_y.h"

extern int ext_parse(yyscan_t scanner);

void escapes(const char *cp, char *tp);

#define PROCESS_YYTEXT_STRING \
        if (yytext[0] == '\'' || yytext[0] == '\"') { \
            yytext[strlen(yytext) - 1] = '\0'; \
            escapes(yytext+1, yytext); \
        } \
        yylval->str = g_string_new(yytext)

%}

%option prefix="ext_"
%option outfile="lex.yy.c"
%option reentrant
%option bison-bridge
%option noyywrap
%option noinput
%option nounput
%option 8bit
%option case-insensitive

%x SBOARDP1 
%x SBOARDP2
%x OPTIONS
%x VALLIST

QSTRING                 (\"[^\"\n]*\")|(\'[^\'\n]*\')
EOT                     [\t\r\n ]+
PLAYERSTR               [[:alnum:]_]+
FLOATSTR                [-+]?([0-9]*\.[0-9]+([eE][-+]?[0-9]+)?)
SIGNEDINTSTR            [-+]?[0-9]+
UNSIGNEDINTSTR          [0-9]+
%%

board:                  {   BEGIN(SBOARDP1);
                            return FIBSBOARD;
                        }

prompt{EOT}             {   return PROMPT; }
new{EOT}                {   return NEW; }
old{EOT}                {   return OLD; }
interface{EOT}          {   return E_INTERFACE; }
help{EOT}               {   return HELP; }
set{EOT}                {   return SET; }
debug{EOT}              {   return DEBUG; }
version{EOT}            {   return INTERFACEVERSION; }
(quit|exit){EOT}        {   return EXIT; }
evaluation{EOT}         {   return EVALUATION; }
fibsboard{EOT}          {   return FIBSBOARD; }

<*>(yes|on|true){EOT}   {   yylval->boolean = 1; 
                            return (E_BOOLEAN);
                        }
<*>(no|off|false){EOT}  {   yylval->boolean = 0; 
                            return (E_BOOLEAN);
                        }


{QSTRING}               {
                            PROCESS_YYTEXT_STRING;
                            return E_STRING;
                        }
		
<*>{FLOATSTR}           {
                            yylval->floatnum = (float) atof(yytext);
                            return E_FLOAT;
                        }
		
<*>{SIGNEDINTSTR}       {   yylval->intnum = atoi(yytext); 
                            return E_INTEGER; 
                        }
                        
[\[\],\(\)\{\}]         return (yytext[0]);

<OPTIONS>{
resignation{EOT}        {   return RESIGNATION; }
beavers{EOT}            {   return BEAVERS; }
crawfordrule{EOT}       {   return CRAWFORDRULE; }
cube{EOT}               {   return CUBE; }
cubeful{EOT}            {   return CUBEFUL; }
cubeless{EOT}           {   return CUBELESS; }
deterministic{EOT}      {   return DETERMINISTIC; }
jacobyrule{EOT}         {   return JACOBYRULE; }
noise{EOT}              {   return NOISE; }
plies{EOT}              {   return PLIES; }
prune{EOT}              {   return PRUNE; }
}
                        
<VALLIST,SBOARDP1,SBOARDP2>: {
                            return (yytext[0]);
                        }
                        
<VALLIST,SBOARDP1,SBOARDP2>{EOT} {
                            BEGIN(OPTIONS);
                            return FIBSBOARDEND;
                        }

<SBOARDP1>{PLAYERSTR}|{QSTRING}   {
                            PROCESS_YYTEXT_STRING;
                            BEGIN(SBOARDP2);
                            return E_STRING;
                        }
<SBOARDP2>{PLAYERSTR}|{QSTRING}   {
                            PROCESS_YYTEXT_STRING;
                            BEGIN(VALLIST);
                            return E_STRING;
                        }

<*>[\t\r\n ]+             ; /* ignore whitespace */

<<EOF>>                 {   BEGIN(INITIAL); 
                            return (EOL); 
                        }

<*>.                    {   BEGIN(INITIAL);
                            yylval->character = yytext[0]; 
                            return (E_CHARACTER);
                        }

%%

void escapes(const char *cp, char *tp)
{
    while (*cp)
    {
	int cval = 0;

	if (*cp == '\\' && strchr("0123456789xX", cp[1]))
	{
	    char *dp;
	    const char *hex = "00112233445566778899aAbBcCdDeEfF";
	    int dcount = 0;

	    if (*++cp == 'x' || *cp == 'X')
		for (++cp; (dp = strchr(hex, *cp)) && (dcount++ < 2); cp++)
		    cval = (cval * 16) + (int) (dp - hex) / 2;
	    else if (*cp == '0')
		while (strchr("01234567",*cp) != (char*)NULL && (dcount++ < 3))
		    cval = (cval * 8) + (*cp++ - '0');
	    else
		while ((strchr("0123456789",*cp)!=(char*)NULL)&&(dcount++ < 3))
		    cval = (cval * 10) + (*cp++ - '0');
	}
	else if (*cp == '\\')		/* C-style character escapes */
	{
	    switch (*++cp)
	    {
	    case '\\': cval = '\\'; break;
	    case 'n': cval = '\n'; break;
	    case 't': cval = '\t'; break;
	    case 'b': cval = '\b'; break;
	    case 'r': cval = '\r'; break;
	    default: cval = *cp;
	    }
	    cp++;
	}
	else
	    cval = *cp++;
	*tp++ = (char) cval;
    }
    *tp = '\0';
}

void ExtStartParse(yyscan_t scanner, const char* szCommand)
{
    struct yyguts_t * yyg = (struct yyguts_t *) scanner;
    YY_BUFFER_STATE buf_state = NULL;
    BEGIN(INITIAL);    
    buf_state = yy_scan_string(szCommand, scanner);
    ext_parse(scanner);
    yy_flush_buffer(buf_state, scanner);
    yy_delete_buffer(buf_state, scanner);
}

int ExtInitParse(yyscan_t *scancontext)
{
    return ext_lex_init_extra(scancontext, scancontext);
}

void ExtDestroyParse(yyscan_t scancontext)
{
    yylex_destroy(scancontext);
}