aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/photcal/parser/prlexer.x
blob: 35e6a4c77b295cfe5a95c20f228454bcce62a914 (plain) (blame)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
include <ctype.h>
include <lexnum.h>
include	"../lib/lexer.h"
include "../lib/prtoken.h"


# PR_LEXER - Lexical analizer for the parser of the configuration file.

int procedure pr_lexer (fd, yylval)

int	fd			# input file descriptor
pointer	yylval			# YYLVAL token pointer

char	key[SZ_LINE]
int	tok			# next token
int	ip, n

include	"lexer.com"

bool	strne()
int	ctor()
int	strdic(), strlen()
int	lexnum()
int	getline()

begin
	# Stay scanning the input file until a valid or
	# error token is found
	repeat {

	    # Skip whitespaces and blank lines
	    while (line[pos] == '\n' || IS_WHITE (line[pos])) {

		# If next character is a newline count
		# lines, and read a new line from file
		if (line[pos] == '\n') {
		    nlines = nlines + 1
		    pos = 1
		    if (getline (fd, line) == EOF) {
			call strcpy ("EOF", LEX_ID (yylval), SZ_LINE)
			return (EOFILE)
		    }
		} else
		    pos = pos + 1
	    }

	    # Test first valid character
	    if (IS_ALPHA (line[pos])) {	# identifier or keyword

		# Read identifier
		for (ip=1; IS_ALNUM (line[pos]); ip=ip+1) {
		    id[ip] = line[pos]
		    pos = pos + 1
		}
	        id[ip] = EOS

		# Check for keyword. Abreviations of keywords are allowed
		# to up to a certain number of characters, but the
		# identifier returned by the lexer should contain the full
		# keyword name.

		n = strdic (id, key, SZ_LINE, KEYWORDS)
		if (n > 0 && strlen (id) >= ABBREVIATE) {
		    call strcpy (key, id, SZ_LINE)
		    switch (n) {
		    case K_CATALOG:
			tok = CATSECT
			break
		    case K_OBSERVATION:
			tok = OBSSECT
			break
		    case K_EXTINCTION:
			tok = EXTSECT
			break
		    case K_TRANSFORMATION:
			tok = TRNSECT
			break
		    case K_FIT:
			tok = FITID
			break
		    case K_CONSTANT:
			tok = CONSTID
			break
		    case K_DELTA:
			tok = DELTAID
			break
		    case K_ERROR:
			tok = ERRORID
			break
		    case K_WEIGHT:
			tok = WEIGHTID
			break
		    case K_MIN:
			tok = MINID
			break
		    case K_MAX:
			tok = MAXID
			break
		    case K_SET:
			tok = SETID
			break
		    case K_DERIVATIVE:
			tok = DERIVID
			break
		    case K_PLOT:
			tok = PLOTID
			break
		    default:
			call error (0, "pr_lexxer: Unknown keyword token")
		    }
		}

		# Check for function. Anything abbreviated,
		# or not matching is and identifier.

		n = strdic (id, key, SZ_LINE, FUNCTIONS)
		if (n == 0) {
		    tok = IDENTIFIER
		    break
		} else if (strne (id, key)) {
		    tok = IDENTIFIER
		    break
		}
		switch (n) {
		case K_ABS:		# absolute value
		    tok = F_ABS
		    break
		case K_ACOS:		# arc cosine
		    tok = F_ACOS
		    break
		case K_ASIN:		# arc sine
		    tok = F_ASIN
		    break
		case K_ATAN:		# arc tangent
		    tok = F_ATAN
		    break
		case K_COS:		# cosine
		    tok = F_COS
		    break
		case K_EXP:		# exponential
		    tok = F_EXP
		    break
		case K_LOG:		# natural logarithm
		    tok = F_LOG
		    break
		case K_LOG10:		# decimal logarithm
		    tok = F_LOG10
		    break
		case K_SIN:		# sine
		    tok = F_SIN
		    break
		case K_SQRT:		# square root
		    tok = F_SQRT
		    break
		case K_TAN:		# tangent
		    tok = F_TAN
		    break
		default:
			call error (0, "pr_lexer: Unknown identifier")
		}

	    } else if (IS_DIGIT (line[pos]) || line[pos] == '.') {	# number

		# Process number
		switch (lexnum (line, pos, n)) {
		case LEX_DECIMAL:
		    tok = INUMBER
		case LEX_REAL:
		    tok = RNUMBER
		default:
		    tok = ERR
		}

		# Copy whatever was processed
		# to the identifier
		do ip = 1, n
		    id[ip] = line[pos + ip - 1]
		id[n + 1] = EOS

		# Advance to next token and
		# break the loop
		pos = pos + n
		break

	    } else if (line[pos] == '(') {	# left parenthesis

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy ("(", id, SZ_LINE)
		tok = LPAR
		pos = pos + 1
		break

	    } else if (line[pos] == ')') {	# right parenthesis

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy (")", id, SZ_LINE)
		tok = RPAR
		pos = pos + 1
		break

	    } else if (line[pos] == '+') {	# plus

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy ("+", id, SZ_LINE)
		tok = PLUS
		pos = pos + 1
		break

	    } else if (line[pos] == '-') {	# minus

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy ("-", id, SZ_LINE)
		tok = MINUS
		pos = pos + 1
		break

	    } else if (line[pos] == '*') {	# star and double star

		# Advance to next character to see if 
		# it's another star
		pos = pos + 1
		if (line[pos] == '*') {

		    # Copy input character to identifier, set
		    # token, and advance to next character before
		    # breaking the loop
		    call strcpy ("**", id, SZ_LINE)
		    tok = EXPON
		    pos = pos + 1
		    break

		} else {

		    # Copy input characters to identifier, set
		    # token, and break the loop
		    call strcpy ("*", id, SZ_LINE)
		    tok = STAR
		    break
		}

	    } else if (line[pos] == '/') {	# slash

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy ("/", id, SZ_LINE)
		tok = SLASH
		pos = pos + 1
		break

	    } else if (line[pos] == '=') {	# equal

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy ("=", id, SZ_LINE)
		tok = EQUAL
		pos = pos + 1
		break

	    } else if (line[pos] == ',') {	# comma

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy (",", id, SZ_LINE)
		tok = COMMA
		pos = pos + 1
		break

	    } else if (line[pos] == ':') {	# colon

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy (":", id, SZ_LINE)
		tok = COLON
		pos = pos + 1
		break

	    } else if (line[pos] == ';') {	# semicolon

		# Copy input character to identifier, set
		# token, and advance to next character before
		# breaking the loop
		call strcpy (";", id, SZ_LINE)
		tok = SEMICOLON
		pos = pos + 1
		break

	    } else if (line[pos] == '#') {	# comment

		# Skip current line
		pos = strlen (line)

	    } else {	# none of the above

		# All characters not included in the previous
		# categories are treated as errors
		id[1] = line[pos]
		id[2] = EOS
	        tok = ERR

		# Advance to next character before
		# breaking the loop
		pos = pos + 1
		break
	    }

	} # repeat

	# Update yylval structure
	LEX_TOK (yylval) = tok
	call strcpy (id, LEX_ID (yylval), SZ_LINE)
	if (tok == INUMBER || tok == RNUMBER) {
	    ip = 1
	    n = ctor (LEX_ID (yylval), ip, LEX_VAL (yylval))
	} else
	    LEX_VAL (yylval) = INDEFR

	# Debug
	#call eprintf ("(tok=%d) (id=%s) (rval=%g)\n")
	    #call pargi (LEX_TOK (yylval))
	    #call pargstr (LEX_ID (yylval))
	    #call pargi (LEX_VAL (yylval))

	# Return token value
	return (tok)
end