annotate lwbasic/lexer.c @ 25:87590f43e76d

Started lwbasic parser; checkpoint
author lost@l-w.ca
date Mon, 24 Jan 2011 20:08:09 -0700
parents
children 26aa76da75ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
1 /*
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
2 lexer.c
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
3
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2011 William Astle
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
5
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
7
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
11 version.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
12
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
16 more details.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
17
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
20 */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
21
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
22 /*
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
23 This handles the gritty details of parsing tokens
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
24 */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
26 #include <stdlib.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
27 #include <stdio.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
28 #include <string.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
29
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
30 #include <lw_alloc.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
31 #include <lw_string.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
32
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
33 #define __lexer_c_seen__
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
34 #include "lwbasic.h"
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
35
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
36 /*
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
37 A token idenfier is returned by lexer(). The actual string value
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
38 is found in state->lexer_lexer_token_string; if the token as an integer value,
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
39 it will be found in state->lexer_token_number in the appropriate "value"
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
40 slot.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
41 */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
42
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
43 struct token_list
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
44 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
45 char *string;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
46 int token;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
47 };
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
48
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
49 static struct token_list lexer_global_tokens[] =
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
50 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
51 { "function", token_kw_function },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
52 { "sub", token_kw_sub },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
53 { "public", token_kw_public },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
54 { "private", token_kw_private },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
55 { "as", token_kw_as },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
56 { "params", token_kw_params },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
57 { "returns", token_kw_returns },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
58 { NULL }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
59 };
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
60
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
61 static int lexer_getchar(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
62 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
63 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
64 c = input_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
65 if (c == -2)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
66 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
67 lwb_error("Error reading input stream.");
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
68 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
69 return c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
70 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
71
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
72 static void lexer_nextchar(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
73 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
74 state -> lexer_curchar = lexer_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
75 if (state -> lexer_curchar == state -> lexer_ignorechar)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
76 state -> lexer_curchar = lexer_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
77 state -> lexer_ignorechar = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
78 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
79
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
80 static int lexer_curchar(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
81 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
82 if (state -> lexer_curchar == -1)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
83 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
84 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
85 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
86
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
87 return state -> lexer_curchar;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
88 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
89
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
90 static void lexer_skip_white(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
91 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
92 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
93
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
94 for (;;)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
95 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
96 c = lexer_curchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
97 if (!(c == 0 || c == ' ' || c == '\t'))
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
98 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
99 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
100 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
101 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
102
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
103 /* must not be called unless the word will be non-zero length */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
104 static void lexer_word(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
105 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
106 int wordlen = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
107 int wordpos = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
108 char *word = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
109 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
110 struct token_list *tok = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
111
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
112 for (;;) {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
113 c = lexer_curchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
114 if (c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c >= 0x80)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
115 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
116 /* character is part of word */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
117 if (wordpos >= wordlen)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
118 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
119 word = lw_realloc(word, wordlen + 32);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
120 wordlen += 32;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
121 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
122 word[wordpos++] = c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
123 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
124 else
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
125 break;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
126
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
127 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
128 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
129
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
130 word[wordpos] = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
131 lw_free(state -> lexer_token_string);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
132 state -> lexer_token_string = lw_strdup(word);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
133
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
134 switch (state -> parser_state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
135 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
136 default:
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
137 tok = lexer_global_tokens;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
138 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
139
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
140 /* check for tokens if appropriate */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
141 /* force uppercase */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
142 if (tok)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
143 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
144 for (c = 0; word[c]; c++)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
145 if (word[c] >= 'A' && word[c] <= 'Z')
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
146 word[c] = word[c] + 0x20;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
147
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
148 while (tok -> string)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
149 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
150 if (strcmp(tok -> string, word) == 0)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
151 break;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
152 tok++;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
153 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
154 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
155
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
156 lw_free(word);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
157 if (tok && tok -> string)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
158 state -> lexer_token = tok -> token;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
159 else
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
160 state -> lexer_token = token_identifier;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
161 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
162
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
163 static void lexer_empty_token(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
164 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
165 lw_free(state -> lexer_token_string);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
166 state -> lexer_token_string = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
167 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
168
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
169 void lexer(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
170 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
171 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
172
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
173 lexer_skip_white(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
174
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
175 lexer_empty_token(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
176
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
177 c = lexer_curchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
178 if (c == -1)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
179 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
180 state -> lexer_token = token_eof;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
181 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
182 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
183
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
184 if (c == '\n')
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
185 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
186 /* LF */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
187 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
188 state -> lexer_ignorechar = '\r';
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
189 state -> lexer_token = token_eol;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
190 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
191 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
192
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
193 if (c == '\r')
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
194 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
195 /* CR */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
196 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
197 state -> lexer_ignorechar = '\n';
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
198 state -> lexer_token = token_eol;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
199 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
200 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
201
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
202 if (c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c >= 0x80)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
203 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
204 /* we have a word here; identifier, keyword, etc. */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
205 lexer_word(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
206 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
207 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
208
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
209 /* return the character if all else fails */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
210 state -> lexer_token_string = lw_realloc(state -> lexer_token_string, 2);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
211 state -> lexer_token_string[0] = c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
212 state -> lexer_token_string[1] = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
213 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
214 state -> lexer_token = token_char;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
215 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
216 }