aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1993-12-22 19:15:16 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1993-12-22 19:15:16 -0200
commit2058cc1dd9fa0ec57fe9191f66ed8b2790b31a25 (patch)
treea032aa7889bac872f68a136deadbbc70c9f0912a
parentf65ebced503d321852395e8ac8ad71d68cbb60c9 (diff)
downloadlua-2058cc1dd9fa0ec57fe9191f66ed8b2790b31a25.tar.gz
lua-2058cc1dd9fa0ec57fe9191f66ed8b2790b31a25.tar.bz2
lua-2058cc1dd9fa0ec57fe9191f66ed8b2790b31a25.zip
analizador lexico de LUA
-rw-r--r--lex.c207
1 files changed, 207 insertions, 0 deletions
diff --git a/lex.c b/lex.c
new file mode 100644
index 00000000..81bf9f31
--- /dev/null
+++ b/lex.c
@@ -0,0 +1,207 @@
1char *rcs_lex = "$Id$";
2/*$Log$*/
3
4#include <ctype.h>
5#include <math.h>
6
7#include "opcode.h"
8#include "hash.h"
9#include "inout.h"
10#include "table.h"
11#include "y.tab.h"
12
13#define next() { current = input(); }
14#define save(x) { *yytextLast++ = (x); }
15#define save_and_next() { save(current); next(); }
16
17static int current;
18static char yytext[256];
19static char *yytextLast;
20
21static Input input;
22
23void lua_setinput (Input fn)
24{
25 current = ' ';
26 input = fn;
27}
28
29char *lua_lasttext (void)
30{
31 *yytextLast = 0;
32 return yytext;
33}
34
35
36static struct
37 {
38 char *name;
39 int token;
40 } reserved [] = {
41 {"and", AND},
42 {"do", DO},
43 {"else", ELSE},
44 {"elseif", ELSEIF},
45 {"end", END},
46 {"function", FUNCTION},
47 {"if", IF},
48 {"local", LOCAL},
49 {"nil", NIL},
50 {"not", NOT},
51 {"or", OR},
52 {"repeat", REPEAT},
53 {"return", RETURN},
54 {"then", THEN},
55 {"until", UNTIL},
56 {"while", WHILE} };
57
58#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
59
60
61int findReserved (char *name)
62{
63 int l = 0;
64 int h = RESERVEDSIZE - 1;
65 while (l <= h)
66 {
67 int m = (l+h)/2;
68 int comp = strcmp(name, reserved[m].name);
69 if (comp < 0)
70 h = m-1;
71 else if (comp == 0)
72 return reserved[m].token;
73 else
74 l = m+1;
75 }
76 return 0;
77}
78
79
80int yylex ()
81{
82 while (1)
83 {
84 yytextLast = yytext;
85 switch (current)
86 {
87 case 0: return 0;
88 case '\n': lua_linenumber++;
89 case ' ':
90 case '\t':
91 save_and_next();
92 continue;
93
94 case '-':
95 save_and_next();
96 if (current != '-') return '-';
97 do { save_and_next(); } while (current != '\n' && current != 0);
98 continue;
99
100 case '<':
101 save_and_next();
102 if (current != '=') return '<';
103 else { save_and_next(); return LE; }
104
105 case '>':
106 save_and_next();
107 if (current != '=') return '>';
108 else { save_and_next(); return GE; }
109
110 case '~':
111 save_and_next();
112 if (current != '=') return '~';
113 else { save_and_next(); return NE; }
114
115 case '"':
116 case '\'':
117 {
118 int del = current;
119 next(); /* skip the delimiter */
120 while (current != del)
121 {
122 switch (current)
123 {
124 case 0:
125 case '\n':
126 return WRONGTOKEN;
127 case '\\':
128 next(); /* do not save the '\' */
129 switch (current)
130 {
131 case 'n': save('\n'); next(); break;
132 case 't': save('\t'); next(); break;
133 case 'r': save('\r'); next(); break;
134 default : save('\\'); break;
135 }
136 break;
137 default:
138 save_and_next();
139 }
140 }
141 next(); /* skip the delimiter */
142 *yytextLast = 0;
143 yylval.vWord = lua_findconstant (yytext);
144 return STRING;
145 }
146
147 case 'a': case 'b': case 'c': case 'd': case 'e':
148 case 'f': case 'g': case 'h': case 'i': case 'j':
149 case 'k': case 'l': case 'm': case 'n': case 'o':
150 case 'p': case 'q': case 'r': case 's': case 't':
151 case 'u': case 'v': case 'w': case 'x': case 'y':
152 case 'z':
153 case 'A': case 'B': case 'C': case 'D': case 'E':
154 case 'F': case 'G': case 'H': case 'I': case 'J':
155 case 'K': case 'L': case 'M': case 'N': case 'O':
156 case 'P': case 'Q': case 'R': case 'S': case 'T':
157 case 'U': case 'V': case 'W': case 'X': case 'Y':
158 case 'Z':
159 case '_':
160 {
161 int res;
162 do { save_and_next(); } while (isalnum(current) || current == '_');
163 *yytextLast = 0;
164 res = findReserved(yytext);
165 if (res) return res;
166 yylval.vWord = lua_findsymbol(yytext);
167 return NAME;
168 }
169
170 case '.':
171 save_and_next();
172 if (current == '.')
173 {
174 save_and_next();
175 return CONC;
176 }
177 else if (!isdigit(current)) return '.';
178 /* current is a digit: goes through to number */
179 goto fraction;
180
181 case '0': case '1': case '2': case '3': case '4':
182 case '5': case '6': case '7': case '8': case '9':
183
184 do { save_and_next(); } while (isdigit(current));
185 if (current == '.') save_and_next();
186fraction: while (isdigit(current)) save_and_next();
187 if (current == 'e' || current == 'E')
188 {
189 save_and_next();
190 if (current == '+' || current == '-') save_and_next();
191 if (!isdigit(current)) return WRONGTOKEN;
192 do { save_and_next(); } while (isdigit(current));
193 }
194 *yytextLast = 0;
195 yylval.vFloat = atof(yytext);
196 return NUMBER;
197
198 default:
199 {
200 int temp = current;
201 save_and_next();
202 return temp;
203 }
204 }
205 }
206}
207