]> git.saurik.com Git - wxWidgets.git/blame - src/common/lexer.l
fine-tuned API from the ICO patch
[wxWidgets.git] / src / common / lexer.l
CommitLineData
cfe780fb
JS
1SIGN [+-]
2DIGIT [0-9]
3ALPHA [a-zA-Z_]
4ALPHADIGIT [a-zA-Z_0-9]
5STRINGCHAR [^"\\]
6WORDCHAR [^'\\]
7
8%{
9/*
10 * File: lexer.l
11 * Description: Lexical analyser for PROLOGIO; can be used with
12 * either lex and flex.
13 */
14#include <string.h>
bbe0af5b 15#include <unistd.h>
cfe780fb
JS
16
17/* +++steve162e: added, otherwise, PROIO_input will be undefined (at least under LINUX)
18 please check, if this is also TRUE under other UNIXes.
19 */
20
21#if defined(FLEX_SCANNER) && defined(_LINUX)
22#define PROIO_input my_input
23#endif
24/* ---steve162e */
25
26#include "wx/expr.h"
27
cfe780fb
JS
28#define Return(x) return x;
29
30#if defined(VMS) && !defined(strdup)
31#define strdup(s) (strcpy((char *)malloc(strlen(s)+1), s));
32#endif
33
34static size_t lex_buffer_length = 0;
35static const char *lex_buffer = NULL;
36static size_t lex_string_ptr = 0;
37static int lex_read_from_string = 0;
38
39static int my_input(void);
cfe780fb
JS
40
41#ifdef FLEX_SCANNER
88ac883a 42# undef YY_INPUT
cfe780fb
JS
43# define YY_INPUT(buf,result,max_size) \
44 if (lex_read_from_string) \
45 { int c = my_input(); result = (c == 0) ? YY_NULL : ((buf)[0]=(c), 1); } \
46 else \
47 if ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \
48 YY_FATAL_ERROR( "read() in flex scanner failed" );
49#else
50# undef unput
51# define unput(_c) my_unput(_c)
88ac883a 52 static int my_unput(char);
cfe780fb
JS
53#endif
54
55%}
56
57%%
58
59{SIGN}?{DIGIT}+ {yylval.s = strdup(yytext); Return(INTEGER);}
60
61"e" Return(EXP);
62
63{ALPHA}{ALPHADIGIT}* {yylval.s = strdup(yytext); Return(WORD);}
64
65"'"{WORDCHAR}*"'" {int len = strlen(yytext);
66 yytext[len-1] = 0;
004fd0c8 67 yylval.s = strdup(yytext+1);
cfe780fb
JS
68 Return(WORD);}
69
70\"({STRINGCHAR}|\\\"|\|\\\\|\\)*\" {yylval.s = strdup(yytext); Return(STRING);}
71
72"(" Return(OPEN);
73
74")" Return(CLOSE);
75
76"," Return(COMMA);
77
78"[" Return(OPEN_SQUARE);
79
80"]" Return(CLOSE_SQUARE);
81
82"=" Return(EQUALS);
83
84"." Return(PERIOD);
85
86[ \t] ;
87
88\n ;
89
90"/*" { loop:
91#ifdef __cplusplus
92 while (yyinput() != '*');
93 switch (yyinput())
94#else
95 while (input() != '*');
96 switch (input())
97#endif
98 {
99 case '/': break;
100 case '*': unput('*');
101 default: goto loop;
102 }
103 }
104
105. Return(ERROR);
106
107%%
108
109
110#ifdef FLEX_SCANNER
111static int lex_input() {
112 return input();
113}
114#else /* BSD/AT&T lex */
115#ifndef input
116# error "Sorry, but need either flex or AT&T lex"
117#endif
118static int lex_input() {
119 return input();
120}
cfe780fb
JS
121
122# undef input
123# define input() my_input()
124static int my_unput(char c)
125{
004fd0c8 126 if (lex_read_from_string != 0) {
cfe780fb
JS
127 /* Make sure we have something */
128 if (lex_string_ptr) {
129 if (c == '\n') yylineno--;
130 lex_string_ptr--;
131 }
132 } else {
133 yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;
134/* unput(c); Causes infinite recursion! */
135 }
136 return c;
137}
138
139#endif
140
004fd0c8 141/* Public */
cfe780fb
JS
142void LexFromFile(FILE *fd)
143{
144 lex_read_from_string = 0;
145 yyin = fd;
146 /* Don't know why this is necessary, but otherwise
147 * lex only works _once_!
148 */
149#ifdef FLEX_SCANNER
150 yyrestart(fd);
151 yy_init = 1;
152#endif
153}
154
155void LexFromString(char *buffer)
156{
157 lex_read_from_string = 1;
158 lex_buffer = buffer;
159 lex_buffer_length = strlen(buffer);
160 lex_string_ptr = 0;
161 /* Don't know why this is necessary, but otherwise
162 * lex only works _once_!
163 */
164#ifdef FLEX_SCANNER
165 yy_init = 1;
166#endif
167}
168
169static int my_input( void )
170{
171 if (lex_read_from_string) {
172 if (lex_string_ptr == lex_buffer_length)
173 return 0;
174 else {
175 char c = lex_buffer[lex_string_ptr++];
176#ifndef FLEX_SCANNER
177 if (c == '\n') yylineno++;
178#endif
179 return c;
180 }
181 } else {
182 return lex_input();
183 }
184}
185
186void wxExprCleanUp()
187{
750b78ba 188#ifdef FLEX_SCANNER
cfe780fb
JS
189 if (yy_current_buffer)
190 yy_delete_buffer(yy_current_buffer);
750b78ba 191#endif
cfe780fb 192}