scanner.icl 64.3 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
implementation module scanner

import	StdEnv, compare_constructor, StdCompare, general

from utilities import revCharListToString, isSpecialChar

/*
Known bug:
functions names starting with '->' require a ';' after the type. Solutions:
1) Make '->' an ordinary token. This implies that we have to write 'a-> .b' instead 
   of 'a->.b'.
2) re-scan token in new context. Requires substantial changes.
3) Determine offsides before token is generated. Tricky since we do not know the
   actual context of the new token or/and have to take care of generating the right
   amount of offsides. 
*/
17
18
19
20
21
22
// RWS Proof ... ::	SearchPaths	:== [String]
:: SearchPaths = 
	{ sp_locations   :: [(String, String)]       // (module, path)
	, sp_paths       :: [String]
	}
// ... RWS
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
23

clean's avatar
clean committed
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
::	*ScanState = ScanState !RScanState

instance getFilename ScanState
where
	getFilename (ScanState scan_state)
		# (file_name,scan_state) = getFilename scan_state
		= (file_name,ScanState scan_state)

instance tokenBack ScanState
where
	tokenBack (ScanState scan_state) = ScanState (tokenBack scan_state)

instance nextToken ScanState
where
	nextToken context (ScanState scan_state)
		# (token,scan_state) = nextToken context scan_state
		= (token,ScanState scan_state) 

instance currentToken ScanState
where
	currentToken (ScanState scan_state)
		# (token,scan_state) = currentToken scan_state
		= (token,ScanState scan_state) 

instance insertToken ScanState
where
	insertToken token context (ScanState scan_state) = ScanState (insertToken token context scan_state)

instance replaceToken ScanState
where
	replaceToken token (ScanState scan_state) = ScanState (replaceToken token scan_state)

instance getPosition ScanState
where
	getPosition (ScanState scan_state)
		# (position,scan_state) = getPosition scan_state
		= (position,ScanState scan_state) 

::	* RScanState =
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
63
64
65
66
67
68
69
	{	ss_input		::	ScanInput
	,	ss_offsides		::	! [(Int, Bool) ]	// (column, defines newDefinition)
	,	ss_useLayout	::	! Bool
	,	ss_tokenBuffer	::	! Buffer LongToken
	}

::	* ScanInput
Pieter Koopman's avatar
Pieter Koopman committed
70
	=	Input	Input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
71
72
73
	|	PushedToken		LongToken ScanInput

::	* Input =
Pieter Koopman's avatar
Pieter Koopman committed
74
	{	inp_stream		::	! * InputStream
clean's avatar
clean committed
75
	,	inp_filename	::	!String
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
76
77
78
79
80
	,	inp_pos			::	! FilePosition
	,	inp_tabsize		::	! Int
	}

::	* InputStream
Pieter Koopman's avatar
Pieter Koopman committed
81
82
83
84
	=	InFile			* File
	|	OldLine !Int !{#Char} !InputStream
//	|	OldChar			! Char ! FilePosition ! InputStream
//	|	OldChars		! *[Char] ! InputStream
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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

::	FilePosition =
	{	fp_line			::	! Int
	,	fp_col			::	! Int
	}

::	LongToken =
	{	lt_position		::	! FilePosition	// Start position of this token
	,	lt_token		::	! Token			// The token itself
//	,	lt_chars		::	! [Char]		// The chars in this token
//	,	lt_context		::	! Context		// The context of the scanning of this token
	}

::	Buffer x
	=	Buffer0
	|	Buffer1 x
	|	Buffer2 x x
	|	Buffer3 x x x // buffer size is 3.

::	Token
	= 	IdentToken ! .String	//		an identifier
	|	IntToken !.String		//		an integer
	|	RealToken !.String		//		a real
	|	StringToken !.String	//		a string
	|	CharToken !.String		//		a character
	|	CharListToken !.String	//		a character list '{char}*'
	|	BoolToken !Bool			//		a boolean
	|	OpenToken				//		(
	|	CloseToken				//		)
	|	CurlyOpenToken			//		{
	|	CurlyCloseToken			//		}
	|	SquareOpenToken			//		[
	|	SquareCloseToken		//		]

	|	DotToken				//		.
	|	SemicolonToken			//		;
	|	ColonToken				//		:
	|	DoubleColonToken		//		::
	|	CommaToken				//		,
	|	ExclamationToken		//		!
	|	BarToken				//		|
	|	ArrowToken				//		->
	|	DoubleArrowToken		//		=>
	|	EqualToken				//		=
	|	DefinesColonToken		//		=:
	|	ColonDefinesToken		//		:==
	|	WildCardToken			//		_
	|	BackSlashToken			//		\
	|	DoubleBackSlashToken	//		\\
	|	LeftArrowToken			//		<-
	|	LeftArrowColonToken		//		<-:
	|	DotDotToken				//		..
	|	AndToken				//		&
	|	HashToken				//		#
	|	AsteriskToken			//		*
	|	LessThanOrEqualToken	//		<=

	|	ModuleToken				//		module
	|	ImpModuleToken			//		implementation
	|	DefModuleToken			//		definition
	|	SysModuleToken			//		system

	|	ImportToken				//		import
	|	FromToken				//		from
	|	SpecialToken			//		special

	|	IntTypeToken			//		Int
	|	CharTypeToken			//		Char
	|	RealTypeToken			//		Real
	|	BoolTypeToken			//		Bool
	|	StringTypeToken			//		String
	|	FileTypeToken			//		File
	|	WorldTypeToken			//		World
	|	VoidTypeToken			//		Void
	|	LeftAssocToken			//		left
	|	RightAssocToken			//		right
	|	ClassToken				//		class
	|	InstanceToken			//		instance
	|	OtherwiseToken			//		otherwise

	|	IfToken					//		if
	|	WhereToken				//		where
	|	WithToken				//		with
	|	CaseToken				//		case
	|	OfToken					//		of
	|	LetToken Bool			//		let!, let
	|	SeqLetToken Bool		//		#!, #
	|	InToken					//		in

	|	DynamicToken			//		dynamic
	|	DynamicTypeToken		//		Dynamic

	|	PriorityToken Priority	//		infixX N

	|	CodeToken				//		code
	|	InlineToken				//		inline
	|	CodeBlockToken [String]	//		{...}

	|	NewDefinitionToken		//		generated automatically, OffsideToken.
	|	EndGroupToken			//		generated automatically
	|	EndOfFileToken			//		end of file
	|	ErrorToken String		//		an error has occured

::	Context
	=	GeneralContext
	|	TypeContext
	|	FunctionContext
	|	CodeContext

instance == Context
where
	(==) co1 co2 = equal_constructor co1 co2

::	Assoc
	=	LeftAssoc
	|	RightAssoc
	|	NoAssoc

::	Priority
	=	Prio Assoc Int
	|	NoPrio

//
//	Macros for error messages
//
ScanErrIllegal	:== "illegal char in input"
ScanErrCharErr	:== "wrong character denotation"
ScanErrNLString	:== "new line in string denotation"
ScanErrWild		:== "ident should not start with _"

class getFilename state :: !*state -> (!String,!*state)

instance getFilename ScanInput
where
	getFilename (Input input)
		# (filename,input) = input!inp_filename
		= (filename,Input input)
	getFilename (PushedToken tok input)
		# (filename,input) = getFilename input
		= (filename,PushedToken tok input)

clean's avatar
clean committed
226
instance getFilename RScanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
227
228
229
230
231
232
233
where
	getFilename scanState=:{ss_input}
		# (filename,ss_input) = getFilename ss_input
		= (filename,{scanState & ss_input = ss_input })

class getPosition state :: !*state -> (!FilePosition,!*state)  // Position of current Token (or Char)

clean's avatar
clean committed
234
instance getPosition RScanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
235
236
237
238
239
240
241
242
243
244
245
246
247
where
	getPosition scanState=:{ss_tokenBuffer}
		| isEmptyBuffer ss_tokenBuffer
			= getCharPosition scanState
		# (ltok,_) = get ss_tokenBuffer
		= (ltok.lt_position, scanState)

instance getPosition Input
where
	getPosition input=:{inp_pos} = (inp_pos, input)

class getCharPosition state :: !*state -> (FilePosition,!*state)

clean's avatar
clean committed
248
instance getCharPosition RScanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
249
250
251
252
253
254
255
256
257
258
259
260
where
	getCharPosition scanState=:{ss_input=Input input}
		# (pos,input) = getPosition input
		= (pos,{ scanState & ss_input = Input input })
	getCharPosition scanState=:{ss_input=PushedToken longToken _}
		= (longToken.lt_position,scanState)

instance getCharPosition Input
where getCharPosition input=:{inp_pos} = (inp_pos, input)

class nextToken state :: !Context !*state -> (!Token, !*state)

clean's avatar
clean committed
261
instance nextToken RScanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
262
where
Pieter Koopman's avatar
Pieter Koopman committed
263
//	nextToken newContext {ss_input=PushedToken token=:{lt_position,lt_token} rest,ss_tokenBuffer,ss_offsides,ss_useLayout}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
264
265
	nextToken newContext scanState=:{ss_input=input=:PushedToken token=:{lt_position,lt_token/*,lt_context*/} rest,ss_tokenBuffer}
//		| lt_context == newContext || ~ (contextDependent lt_token) || isGeneratedToken lt_token
Pieter Koopman's avatar
Pieter Koopman committed
266

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
267
		=	(	lt_token
Pieter Koopman's avatar
Pieter Koopman committed
268
269
			,	{ scanState & ss_input			= rest , ss_tokenBuffer	= store token ss_tokenBuffer }
//			,	{ ss_input = rest , ss_tokenBuffer = store token ss_tokenBuffer,ss_useLayout=ss_useLayout, ss_offsides=ss_offsides }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
270
			)  //-->> ("nextToken: pushed token", lt_token)
Pieter Koopman's avatar
Pieter Koopman committed
271
272
/*
		=	nextToken newContext { scanState & ss_input = pushTokensBack input}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
273
274
275
276
277
278
279
280
	where
		pushTokensBack input=:(Input _) = input
		pushTokensBack (PushedToken token input)
			# (Input input=:{inp_stream}) = pushTokensBack input
			= Input
				{ input
				& inp_stream = OldToken token inp_stream
				}  //-->> ("pushTokensBack",token)
Pieter Koopman's avatar
Pieter Koopman committed
281
282
*/
	nextToken context {ss_input=Input inp,ss_tokenBuffer,ss_offsides,ss_useLayout}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
283
284
		# (error, c, inp) 	= SkipWhites inp
		= case error of
Pieter Koopman's avatar
Pieter Koopman committed
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
			Yes string
				#! (pos, inp)		= inp!inp_pos
				->	( ErrorToken string
							,	{	ss_tokenBuffer	= store
												{	lt_position 	= pos
												,	lt_token		= ErrorToken string
											//	,	lt_chars		= []
											//	,	lt_context		= context
												}
												ss_tokenBuffer,
									ss_input=Input inp,
									ss_offsides=ss_offsides,	ss_useLayout=ss_useLayout
								}
							) -->> ("Error token generated",string)
			no
				#	(eof, inp)	= EndOfInput inp
				#!	(pos, inp)	= inp!inp_pos
				|	eof && c == NewLineChar
					#	newToken	= EndOfFileToken
					->	checkOffside pos newToken
							{ ss_tokenBuffer	= store
													{	lt_position 	= pos
													,	lt_token		= newToken
												//	,	lt_chars		= []
												//	,	lt_context		= context
													}
													ss_tokenBuffer
							, ss_input = Input inp,
							ss_offsides=ss_offsides,	ss_useLayout=ss_useLayout
							} // -->> ("Token", EndOfFileToken,pos)
				// otherwise // ~ (eof && c == NewLineChar)
					#	(token, inp)	= Scan c inp /* {inp & inp_curToken = [c]}*/ context
				//	#	(chars, inp)	= inp!inp_curToken
					-> checkOffside pos token
						{ ss_input 			= Input inp
						, ss_tokenBuffer	= store
												{	lt_position 	= pos
												,	lt_token		= token
											//	,	lt_chars		= reverse chars
											//	,	lt_context		= context
												}
												ss_tokenBuffer,
							ss_offsides=ss_offsides,	ss_useLayout=ss_useLayout
						}	 //-->> (token,pos)

/*
		#! (pos, inp)		= inp!inp_pos
		#!  scanState			= {scanState & ss_input = Input inp }
//		#! scanState = {ss_input=Input inp, ss_offsides=ss_offsides,	ss_useLayout=ss_useLayout, ss_tokenBuffer=ss_tokenBuffer }
		= case error of
			Yes string
				->	( ErrorToken string
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
337
338
339
340
341
342
343
344
345
346
							,	{	scanState
								&	ss_tokenBuffer	= store
												{	lt_position 	= pos
												,	lt_token		= ErrorToken string
											//	,	lt_chars		= []
											//	,	lt_context		= context
												}
												scanState.ss_tokenBuffer
								}
							) -->> ("Error token generated",string)
Pieter Koopman's avatar
Pieter Koopman committed
347
348
			no
				->	determineToken c pos scanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
349
	where
Pieter Koopman's avatar
Pieter Koopman committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
			determineToken c pos scanState=:{ss_input=Input inp}
				#	(eof, inp)	= EndOfInput inp
	//			#!	(pos, inp)	= inp!inp_pos
				|	eof && c == NewLineChar
					#	newToken	= EndOfFileToken
					=	checkOffside pos newToken
							{ scanState
							& ss_tokenBuffer	= store
													{	lt_position 	= pos
													,	lt_token		= newToken
												//	,	lt_chars		= []
												//	,	lt_context		= context
													}
													scanState.ss_tokenBuffer
							, ss_input = Input inp
							} // -->> ("Token", EndOfFileToken,pos)
				// otherwise // ~ (eof && c == NewLineChar)
					#	(token, inp)	= Scan c inp /* {inp & inp_curToken = [c]}*/ context
				//	#	(chars, inp)	= inp!inp_curToken
					= checkOffside pos token
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
370
						{ scanState
Pieter Koopman's avatar
Pieter Koopman committed
371
372
						& ss_input 			= Input inp
						, ss_tokenBuffer	= store
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
373
												{	lt_position 	= pos
Pieter Koopman's avatar
Pieter Koopman committed
374
375
												,	lt_token		= token
											//	,	lt_chars		= reverse chars
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
376
377
											//	,	lt_context		= context
												}
Pieter Koopman's avatar
Pieter Koopman committed
378
379
380
												ss_tokenBuffer
						}	 //-->> (token,pos)
*/
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
381
382
383
384
	nextToken _ _ = abort "Scanner: Error in nextToken"

class tokenBack state :: !*state -> !*state

clean's avatar
clean committed
385
instance tokenBack RScanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
386
387
388
389
390
391
392
where
	tokenBack scanState=:{ss_tokenBuffer, ss_input}
		| isEmptyBuffer ss_tokenBuffer = abort "tokenBack with empty token buffer"
		# (tok, buf) = get ss_tokenBuffer
		=	{ scanState
			& ss_tokenBuffer	= buf
			, ss_input			= PushedToken tok ss_input
Pieter Koopman's avatar
Pieter Koopman committed
393
		} // -->> ("tokenBack", tok, buf)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
394
395
396

class currentToken state :: !*state -> (!Token, !*state)

clean's avatar
clean committed
397
instance currentToken RScanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
398
399
400
401
402
403
404
where currentToken scanState=:{ss_tokenBuffer}
		| isEmptyBuffer ss_tokenBuffer
			= (ErrorToken "dummy", scanState)
			= ((head ss_tokenBuffer).lt_token, scanState)

class insertToken state :: !Token !Context !*state -> *state

clean's avatar
clean committed
405
instance insertToken RScanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
where
	insertToken t c scanState
/*		#	chars = if (isGeneratedToken t)
						[]
						(fromString (toString t))
*/		#	(pos, scanState=:{ss_input}) = getPosition scanState
		=	{ scanState
			& ss_input = PushedToken
							{ lt_position	= pos
							, lt_token		= t
						//	, lt_chars		= chars
						//	, lt_context	= c
							}
							ss_input
			}
/*
isGeneratedToken :: !Token -> Bool
isGeneratedToken NewDefinitionToken = True
isGeneratedToken EndGroupToken      = True
isGeneratedToken (CodeBlockToken _) = True
isGeneratedToken _					= False
*/

class replaceToken state :: !Token !*state -> *state

clean's avatar
clean committed
431
instance replaceToken RScanState
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
432
433
434
435
436
437
438
439
where
	replaceToken tok scanState=:{ss_tokenBuffer}
		# (longToken,buffer) = get ss_tokenBuffer
		= { scanState
		  & ss_tokenBuffer = store { longToken & lt_token = tok } buffer
		  }

SkipWhites :: !Input -> (!Optional String, !Char, !Input)
Pieter Koopman's avatar
Pieter Koopman committed
440
441
442
SkipWhites {inp_stream=OldLine i line stream,inp_pos={fp_line,fp_col},inp_tabsize,inp_filename}
	| i<size line
		= skip_whites_in_line i fp_col fp_line line inp_tabsize stream inp_filename
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
443
444
445
446
447
448
SkipWhites input
	# (eof, c, input)		= ReadChar input
	| eof					= (No, NewLineChar, input) // -->> "EOF in SkipWhites"
	| IsWhiteSpace c		= SkipWhites input
							= TryScanComment c input

Pieter Koopman's avatar
Pieter Koopman committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
skip_whites_in_line :: !Int !Int !Int !{#Char} !Int !*InputStream !String -> *(!Optional String,!Char,!*Input);
skip_whites_in_line i fp_col fp_line line tabsize stream inp_filename
	| i<size line
		# c=line.[i]
		| c==' ' || c == '\f' || c == '\v'
			= skip_whites_in_line (i+1) (fp_col+1) fp_line line tabsize stream inp_filename
		| c=='\t'
			= skip_whites_in_line (i+1) (tabsize * (fp_col / tabsize + 1)) fp_line line tabsize stream inp_filename
		| c==LFChar || c==CRChar
			# pos = {fp_line = fp_line + 1, fp_col = 0}
//			#	(c,stream)	= correctNewline_OldLine c i tabsize line stream
			=	SkipWhites  {
						inp_filename=inp_filename,inp_tabsize=tabsize,
						inp_stream = stream
					,	inp_pos	= pos
					}
			#	pos = {fp_line=fp_line,fp_col = fp_col + 1}
			=	TryScanComment c {
						inp_filename=inp_filename,inp_tabsize=tabsize,
						inp_stream = OldLine (i+1) line stream
					,	inp_pos	= pos
					}
	#	pos = {fp_line=fp_line, fp_col = fp_col}
	= SkipWhites {
				inp_filename=inp_filename,inp_tabsize=tabsize,
				inp_stream = stream
			,	inp_pos	= pos
			}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
478
479
TryScanComment :: !Char !Input -> (!Optional String, !Char, !Input)
TryScanComment c1=:'/' input
Pieter Koopman's avatar
Pieter Koopman committed
480
	# (eof,c2, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
481
482
483
484
485
486
487
	| eof					= (No, c1, input)
	= case c2 of
		'/' -> SkipWhites (SkipToEndOfLine input)
		'*' -> case ScanComment input of
				(No,input)	-> SkipWhites input
				(er,input)	-> (er, c1, input)
		_   -> (No, c1, charBack input)
488
			
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
489
490
491
492
TryScanComment c input
	= (No, c, input)

ScanComment	:: !Input -> (!Optional String, !Input)
Pieter Koopman's avatar
Pieter Koopman committed
493
494
495
ScanComment {inp_stream=OldLine i line stream,inp_pos={fp_line,fp_col},inp_tabsize,inp_filename}
	| i<size line
		= scan_comment_in_line i fp_col fp_line line inp_tabsize stream inp_filename
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
496
497
498
ScanComment input
	# (eof1, c1, input)	= ReadChar input
	| eof1				= (Yes "end of file encountered inside comment", input)
Pieter Koopman's avatar
Pieter Koopman committed
499
500
501
502
		= ScanComment2 c1 input;

ScanComment2	:: !Char !Input -> (!Optional String, !Input)
ScanComment2 c1 input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
503
504
505
506
507
508
509
510
511
512
	| c1 == '/'
		# (eof2, c2, input)	= ReadChar input
		| eof2				= (Yes "end of file encountered inside comment", input)
							= case c2 of
								'/'	->	ScanComment (SkipToEndOfLine input)
								'*'	->	case ScanComment input of
											(No, input) -> ScanComment input
											error		-> error
								_	->	ScanComment input
	| c1 == '*'
Pieter Koopman's avatar
Pieter Koopman committed
513
514
515
516
517
518
		# (eol2, c2, input)	= ReadNormalChar input
		| eol2	
			# (eof2, c2, input)	= ReadChar input
			| eof2
			= (Yes "end of file encountered inside comment", input)
			= ScanComment input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
519
		| c2 == '/'			= (No, input)
Pieter Koopman's avatar
Pieter Koopman committed
520
521
522
		| c2 == '*'
//					= ScanComment (charBack input)
					= ScanComment2 c2 input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
523
524
525
							= ScanComment input
	| otherwise				= ScanComment input

Pieter Koopman's avatar
Pieter Koopman committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
scan_comment_in_line :: !Int !Int !Int !{#Char} !Int !*InputStream !String -> (!Optional String, !Input)
scan_comment_in_line i fp_col fp_line line tabsize stream inp_filename
	| i<size line
		# c=line.[i]
		| c=='\t'
			= scan_comment_in_line (i+1) (tabsize * (fp_col / tabsize + 1)) fp_line line tabsize stream inp_filename
		| c==LFChar || c==CRChar
			# pos = {fp_line = fp_line + 1, fp_col = 0}
//			#	(c,stream)	= correctNewline_OldLine c i tabsize line stream
			=	ScanComment {
						inp_filename=inp_filename,inp_tabsize=tabsize,
						inp_stream = stream
					,	inp_pos	= pos
					}
		| c=='/' || c=='*'
			= ScanComment2 c {
								inp_filename=inp_filename,inp_tabsize=tabsize,
								inp_stream = OldLine (i+1) line stream
							,	inp_pos	= {fp_line=fp_line, fp_col = fp_col+1}
							}
			= scan_comment_in_line (i+1) (fp_col+1) fp_line line tabsize stream inp_filename
	= ScanComment {
				inp_filename=inp_filename,inp_tabsize=tabsize,
				inp_stream = stream
			,	inp_pos	= {fp_line=fp_line, fp_col = fp_col}
			}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
553
SkipToEndOfLine	:: !Input -> !Input
Pieter Koopman's avatar
Pieter Koopman committed
554
555
556
SkipToEndOfLine input=:{inp_stream=OldLine i line stream,inp_pos={fp_line,fp_col}}
	| i<size line
		= {input & inp_stream=stream,inp_pos={fp_line=fp_line+1,fp_col=0}}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
557
558
559
560
561
SkipToEndOfLine input
	# (eof, c, input)	= ReadChar input
	| eof				= input
	| c==NewLineChar	= input
			= SkipToEndOfLine input
Pieter Koopman's avatar
Pieter Koopman committed
562

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
563
Scan :: !Char !Input !Context -> (!Token, !Input)
Pieter Koopman's avatar
Pieter Koopman committed
564

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
565
566
567
568
569
570
571
572
Scan '(' input co			= (OpenToken, input)
Scan ')' input co			= (CloseToken, input)
Scan '{' input CodeContext	= ScanCodeBlock input
Scan '{' input co			= (CurlyOpenToken, input)
Scan '}' input co			= (CurlyCloseToken, input)
Scan '[' input co			= (SquareOpenToken, input)
Scan ']' input co			= (SquareCloseToken, input)
Scan c0=:'|' input co
Pieter Koopman's avatar
Pieter Koopman committed
573
	# (eof, c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
574
575
576
577
578
579
580
581
582
583
	| eof					= (BarToken, input)
	| isSpecialChar c1		= ScanOperator 1 input [c1, c0] co
							= (BarToken, charBack input)
Scan ',' input co			= (CommaToken, input)
Scan ';' input co			= (SemicolonToken, input)
Scan '#' input TypeContext	= (HashToken, input)
Scan c0=:'#' input co
	# (strict, input)		= determineStrictness input
	| strict
		= (SeqLetToken strict, input)
Pieter Koopman's avatar
Pieter Koopman committed
584
	# (eof,c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
585
586
587
588
589
590
591
592
	| eof
		= (SeqLetToken False, input)
	| isSpecialChar c1
		= ScanOperator 1 input [c1, c0] co
	// otherwise
		= (SeqLetToken strict, charBack input)
Scan '*' input TypeContext	= (AsteriskToken, input)
Scan c0=:'&' input co
Pieter Koopman's avatar
Pieter Koopman committed
593
	# (eof, c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
594
595
596
597
598
599
600
	| eof					= (AndToken, input)
	| isSpecialChar c1		= ScanOperator 1 input [c1, c0] co
							= (AndToken, charBack input)
Scan c0=:'.' input co
	= case co of
		TypeContext
						-> (DotToken, input)
Pieter Koopman's avatar
Pieter Koopman committed
601
		_	# (eof, c1, input) = ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
602
			| eof		-> (DotToken, input)
Pieter Koopman's avatar
Pieter Koopman committed
603
			| c1 == '.'
Pieter Koopman's avatar
Pieter Koopman committed
604
				# (eof, c2, input) = ReadNormalChar input
Pieter Koopman's avatar
Pieter Koopman committed
605
606
				| eof		-> (DotDotToken, input)
				| isSpecialChar c2
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
607
608
						-> ScanOperator 2 input [c2, c1, c0] co
						-> (DotDotToken, charBack input)
Pieter Koopman's avatar
Pieter Koopman committed
609
610
611
			| isSpecialChar c1
						-> ScanOperator 1 input [c1, c0] co
						-> (DotToken, charBack input)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
612
613
Scan '!' input TypeContext	= (ExclamationToken, input)
Scan '\\' input co
Pieter Koopman's avatar
Pieter Koopman committed
614
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
615
616
617
618
	| eof					= (BackSlashToken, input)
	| c == '\\'				= (DoubleBackSlashToken, input)
							= (BackSlashToken, charBack input)
Scan c0=:'_' input co
Pieter Koopman's avatar
Pieter Koopman committed
619
	# (eof, c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
620
	| eof					= (WildCardToken, input)
Pieter Koopman's avatar
Pieter Koopman committed
621
622
623
	| IsIdentChar c1	co
//		= ScanIdent 1 input [c1, c0] co
		= ScanIdentFast 2 input co
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
624
625
626
//	| isSpecialChar c1		= ScanOperator 1 input [c1, c0] co
							= (WildCardToken, charBack input)
Scan c0=:'<' input TypeContext
Pieter Koopman's avatar
Pieter Koopman committed
627
	# (eof, c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
628
629
630
631
	| eof					= (ErrorToken "< just before end of file in TypeContext", input)
	| c1 == '='				= (LessThanOrEqualToken, input)
							=  ScanOperator 0 (charBack input) [c0] TypeContext
Scan c0=:'<' input co
Pieter Koopman's avatar
Pieter Koopman committed
632
	# (eof, c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
633
634
	| eof 					= (IdentToken "<", input)
	| c1 <> '-'				= ScanOperator 0 (charBack input) [c0] co
Pieter Koopman's avatar
Pieter Koopman committed
635
	# (eof, c2, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
636
637
	| eof					= (LeftArrowToken, input)
	| c2 == ':'	
Pieter Koopman's avatar
Pieter Koopman committed
638
		# (eof, c3, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
639
640
641
642
643
644
		| eof					= (LeftArrowColonToken, input)
		| isSpecialChar c3		= ScanOperator 3 input [c3, c2, c1, c0] co
								= (LeftArrowColonToken, charBack input)
	| isSpecialChar c2		= ScanOperator 2 input [c2, c1, c0] co
							= (LeftArrowToken, charBack input)
Scan c0=:'-' input co
Pieter Koopman's avatar
Pieter Koopman committed
645
646
647
	# (previous_char,input) = GetPreviousChar input;

	# (eof, c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
648
	| eof					= (IdentToken "-", input)
Pieter Koopman's avatar
Pieter Koopman committed
649
650
651
652
653
654
//	# new					= newExp input.inp_charBuffer
//	| IsDigit c1 && new		= ScanNumeral 1 input [c1,c0]

	| IsDigit c1 && new_exp_char previous_char
		= ScanNumeral 1 input [c1,c0]

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
655
656
657
	| c1 <> '>'				= ScanOperator 0 (charBack input) [c0] co
	| co == TypeContext		= (ArrowToken, input)	// -> is a reserved symbol in a type context
							// Can cause an error when token (like ->.) is read in wrong context
Pieter Koopman's avatar
Pieter Koopman committed
658
	# (eof, c2, input)		= ReadNormalChar input		
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
659
660
661
662
	| eof					= (ArrowToken, input)
	| isSpecialChar c2		= ScanOperator 2 input [c2, c1, c0] co
							= (ArrowToken, charBack input)
Scan c0=:'+' input co
Pieter Koopman's avatar
Pieter Koopman committed
663
664
665
	# (previous_char,input) = GetPreviousChar input;

	# (eof, c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
666
	| eof					= (IdentToken "+", input)
Pieter Koopman's avatar
Pieter Koopman committed
667
668
669
670
671
672
//	# new					= newExp input.inp_charBuffer
//	| IsDigit c1 && new		= ScanNumeral 1 input [c1,c0]

	| IsDigit c1 && new_exp_char previous_char
		= ScanNumeral 1 input [c1,c0]

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
673
674
							= ScanOperator 0 (charBack input) [c0] co
Scan c0=:'=' input co
Pieter Koopman's avatar
Pieter Koopman committed
675
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
676
677
678
679
680
681
	| eof					= (EqualToken, input)
	| c == ':'				= (DefinesColonToken, input)
	| c == '>'				= (DoubleArrowToken, input)
	| isSpecialChar c		= ScanOperator 1 input [c, c0] co
							= (EqualToken, charBack input)
Scan c0=:':' input co
Pieter Koopman's avatar
Pieter Koopman committed
682
	# (eof,c1, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
683
684
685
686
687
	| eof					= (ColonToken, input)
	| c1 == ':'				= (DoubleColonToken, input)
	| c1 <> '='
		| isSpecialChar c1	= ScanOperator 1 input [c1, c0] co
							= (ColonToken, charBack input)
Pieter Koopman's avatar
Pieter Koopman committed
688
	# (eof, c2, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
689
690
691
692
693
	| eof					= ScanOperator 1 input [c1, c0] co
	| c2 == '='				= (ColonDefinesToken, input)
							= ScanOperator 1 (charBack input) [c1, c0] co
Scan c0=:'\'' input co		= ScanChar input [c0]
Scan c0=:'\"' input co		= ScanString 0 input [c0]
Pieter Koopman's avatar
Pieter Koopman committed
694

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
695
696
Scan c    input co
	| IsDigit c				= ScanNumeral 0 input [c]
Pieter Koopman's avatar
Pieter Koopman committed
697
698
699
	| IsIdentChar c	co	
		= ScanIdentFast 1 input co
//		= ScanIdent 0 input [c] co
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
700
701
	| isSpecialChar c		= ScanOperator 0 input [c] co
							= (ErrorToken ScanErrIllegal, input)
Pieter Koopman's avatar
Pieter Koopman committed
702
/*
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
703
704
705
706
707
708
709
newExp :: !(Buffer (Char,FilePosition)) -> Bool
newExp buffer
	# (c, _)	= case buffer of
					Buffer3 _ _ cp	-> cp
					_				-> (' ',{fp_line=0,fp_col=0})
	= new_exp_char c
where
Pieter Koopman's avatar
Pieter Koopman committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
*/
new_exp_char ',' = True
new_exp_char '[' = True
new_exp_char '(' = True
new_exp_char '/' = True // to handle end of comment symbol: */
new_exp_char c	 = isSpace c

ScanIdentFast :: !Int !Input !Context -> (!Token, !Input)
ScanIdentFast n input=:{inp_stream=OldLine i line stream,inp_pos} co
	# end_i = ScanIdentCharsInString i line co
		with
			ScanIdentCharsInString :: !Int !{#Char} !Context -> Int
			ScanIdentCharsInString i line co
				| i<size line && IsIdentChar line.[i] co
					= ScanIdentCharsInString (i+1) line co
					= i
	# pos = {inp_pos & fp_col = inp_pos.fp_col + (end_i-i)}
	# input =  {input & inp_stream=OldLine end_i line stream,inp_pos=pos}
	= CheckReserved co (line % (i-n,end_i-1)) input
/*
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
730
731
ScanIdent :: !Int !Input ![Char] !Context -> (!Token, !Input)
ScanIdent n input token co
Pieter Koopman's avatar
Pieter Koopman committed
732
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
733
734
735
	| eof					= CheckReserved co (revCharListToString n token) input
	| IsIdentChar c co		= ScanIdent (n + 1) input [c:token] co
							= CheckReserved co (revCharListToString n token) (charBack input)
Pieter Koopman's avatar
Pieter Koopman committed
736
*/
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
737
738
ScanOperator :: !Int !Input ![Char] !Context -> (!Token, !Input)
ScanOperator n input token co
Pieter Koopman's avatar
Pieter Koopman committed
739
	#  (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
	| eof					= (IdentToken (revCharListToString n token), input)
	| isSpecialChar c		= ScanOperator (n + 1) input [c:token] co
							= CheckReserved co (revCharListToString n token) (charBack input)

CheckReserved :: !Context !String !Input -> (!Token, !Input)
CheckReserved GeneralContext    s i = CheckGeneralContext s i
CheckReserved TypeContext       s i = CheckTypeContext s i
CheckReserved FunctionContext	s i = CheckFunctContext s i
CheckReserved CodeContext		s i = CheckCodeContext s i

CheckGeneralContext	:: !String !Input -> (!Token, !Input)
CheckGeneralContext s input
 = case s of
	"module"     		-> (ModuleToken		, input)
	"definition"  		-> (DefModuleToken	, input)
	"implementation"	-> (ImpModuleToken	, input)
	"system"			-> (SysModuleToken	, input)
	"from" 				-> (FromToken		, input)
	"in"  	    		-> (InToken			, input)
	s					-> CheckEveryContext s input

CheckEveryContext :: !String !Input -> (!Token, !Input)
CheckEveryContext s input
 = case s of
	"where"		->	(WhereToken			, input)
	"with"		->	(WithToken			, input)
	"class" 	->	(ClassToken			, input)
	"instance"	->	(InstanceToken		, input)
	"otherwise"	->	(OtherwiseToken		, input)
769
	"!"			->	(ExclamationToken	, input)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//	"::"		->	(DoubleColonToken	, input)
	"*/"		->	(ErrorToken "Unexpected end of comment, */", input)
	"infixr"	#	(error, n, input) = GetPrio  input
				->	case error of
						Yes err -> (ErrorToken err						, input)  //-->> ("Error token generated: "+err)
						No		-> (PriorityToken (Prio RightAssoc n)	, input)
	"infixl"	#	(error, n, input) = GetPrio  input
				->	case error of
						Yes err -> (ErrorToken err						, input)  //-->> ("Error token generated: "+err)
						No		-> (PriorityToken (Prio LeftAssoc n)	, input)
	"infix"		#	(error, n, input) = GetPrio  input
				->	case error of
						Yes err -> (ErrorToken err						, input)  //-->> ("Error token generated: "+err)
						No		-> (PriorityToken (Prio NoAssoc n)		, input)
Pieter Koopman's avatar
Pieter Koopman committed
784
785
	"import" -> (ImportToken,input)
   	s			->	(IdentToken s		, input)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832

CheckTypeContext :: !String !Input -> (!Token, !Input)
CheckTypeContext s input
 = case s of
 	"Int"		->	(IntTypeToken		, input)
	"Char"		->	(CharTypeToken		, input)
	"Real"		->	(RealTypeToken		, input)
	"Bool"		->	(BoolTypeToken		, input)
	"String"	->	(StringTypeToken	, input)
	"File"		->	(FileTypeToken		, input)
	"World"		->	(WorldTypeToken		, input)
	"Dynamic"	->	(DynamicTypeToken	, input)
	"special"	->	(SpecialToken		, input)
	"from" 		->	(FromToken			, input)
	s			->	CheckEveryContext s input

CheckFunctContext :: !String !Input -> (!Token, !Input)
CheckFunctContext s input
 = case s of
	"if"		->	(IfToken			, input)
	"True"		->	(BoolToken True		, input)
	"False"		->	(BoolToken False	, input)
	"case"		->	(CaseToken			, input)
	"of"		->	(OfToken			, input)
	"system"	->	(SysModuleToken		, input)
	"from"		->	(FromToken			, input)
	"let" 	    #	(strict, input) = determineStrictness input
				->	(LetToken strict, input)
//	"Let" 	    #	(strict, input) = determineStrictness input
//				->	(SeqLetToken strict	, input)
	"in"  	    ->	(InToken			, input)
	"dynamic"  	->	(DynamicToken		, input)
	"code"		->	(CodeToken			, input)
	s			->	CheckEveryContext s input

CheckCodeContext :: !String !Input -> (!Token, !Input)
CheckCodeContext s input
 = case s of
	"inline"	->	(InlineToken		, input)
	s			->	CheckEveryContext s input	

GetPrio :: !Input -> (!Optional String, !Int, !Input)
GetPrio input
	# (error, c, input) = SkipWhites input
	| IsDigit c
		= (error, digitToInt c, input)
		= (error, defaultPrio , charBack input)
833
where defaultPrio = 9
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
834
835
836

determineStrictness :: !Input -> (!Bool, !Input)
determineStrictness input
Pieter Koopman's avatar
Pieter Koopman committed
837
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
	| eof					= (False, input)
	| c == '!'				= (True, input)
							= (False, charBack input)

ScanCodeBlock :: !Input -> (!Token, !Input)
ScanCodeBlock input
	= scan_code_block [] input
where
	scan_code_block :: ![String] !Input -> (!Token,!Input)
	scan_code_block acc input
		# (eof, c, input)	= ReadChar input
		| c == '}'
			= (CodeBlockToken (reverse acc), input)
		| isNewLine c
			| eof
				= (ErrorToken "eof in code block", input)
				= scan_code_block acc input
		| IsWhiteSpace c
				= scan_code_block acc input
		# (line, input)		= ReadLine input
		= scan_code_block [toString c+stripNewline line:acc] input

stripNewline :: !String -> String
stripNewline string
	# size = size string
	= case size of
		0 -> string
		1 | isNewLine string.[0]
			-> ""
			-> string
		_ | isNewLine string.[size-1]
			| isNewLine string.[size-2]
				-> string%(0,size-3)
				-> string%(0,size-2)
			-> string

ScanNumeral	:: !Int !Input [Char] -> (!Token, !Input)
ScanNumeral n input chars=:['0':r]
	| isEmpty r || r == ['+']
Pieter Koopman's avatar
Pieter Koopman committed
877
		# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
878
879
		| eof					= (IntToken (revCharListToString n chars), input)
		| c == 'x'
Pieter Koopman's avatar
Pieter Koopman committed
880
			# (eof, c1, input)	= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
881
882
883
884
885
886
887
			| eof				= (IntToken "0", charBack input)
			| isHexDigit c1		= ScanHexNumeral (hexDigitToInt c1) input
								= (IntToken "0", charBack (charBack input))
		| isOctDigit c			= ScanOctNumeral (digitToInt c) input
		| c == '.'				= TestFraction n input chars
								= (IntToken "0", charBack input)
	| r == ['-']
Pieter Koopman's avatar
Pieter Koopman committed
888
		# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
889
890
		| eof					= (IntToken (revCharListToString n chars), input)
		| c == 'x'
Pieter Koopman's avatar
Pieter Koopman committed
891
			# (eof, c1, input)	= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
892
893
894
895
896
897
898
			| eof				= (IntToken "0", charBack input)
			| isHexDigit c1		= ScanHexNumeral (~ (hexDigitToInt c1)) input
								= (IntToken "0", charBack (charBack input))
		| isOctDigit c			= ScanOctNumeral (~ (digitToInt c)) input
		| c == '.'				= TestFraction n input chars
								= (IntToken "0", charBack input)
ScanNumeral n input chars
Pieter Koopman's avatar
Pieter Koopman committed
899
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
900
901
902
903
904
905
906
907
	| eof					= (IntToken (revCharListToString n chars), input)
	| IsDigit c				= ScanNumeral (n + 1) input [c:chars]
	| c == 'E'				= ScanExponentSign (n + 1) input [c:chars]
	| c == '.'				= TestFraction n input chars
							= (IntToken (revCharListToString n chars), charBack input)

TestFraction :: !Int !Input ![Char] -> (!Token, !Input)
TestFraction n input chars
Pieter Koopman's avatar
Pieter Koopman committed
908
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
909
910
911
912
	| eof					= (ErrorToken ("Incorrect Real at end of file: "+(revCharListToString (n+1) ['.':chars])), input)
	| IsDigit c				= ScanFraction (n + 2) input [c,'.':chars]
			 				= (IntToken (revCharListToString n chars), charBack (charBack input))

913

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
914
915
ScanFraction :: !Int !Input ![Char] -> (!Token, !Input)
ScanFraction n input chars
Pieter Koopman's avatar
Pieter Koopman committed
916
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
917
918
	| eof					= (RealToken (revCharListToString n chars), input)
	| c == 'E'				= case chars of
919
920
								[c:_] | IsDigit c	-> ScanExponentSign (n + 1) input ['E':chars] /* Sjaak, was [c:chars] */
								_					-> ScanExponentSign (n + 2) input ['E','0':chars] /* Sjaak, idem */
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
921
922
923
924
925
926
927
	| IsDigit c				= ScanFraction (n + 1) input [c:chars]
							= case chars of
								[c:_] | IsDigit c	-> (RealToken (revCharListToString n chars), charBack input)
								_					-> (RealToken (revCharListToString (n+1) ['0':chars]), charBack input)

ScanExponentSign :: !Int !Input ![Char] -> (!Token, !Input)
ScanExponentSign n input chars
Pieter Koopman's avatar
Pieter Koopman committed
928
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
929
930
931
932
933
934
935
	| eof					= (RealToken (revCharListToString n chars), input)
	| c == '+'				= ScanExponent n input chars
	| c == '-' || IsDigit c	= ScanExponent (n+1) input [c:chars]
	| otherwise				= (ErrorToken ("Digit or sign expected after "+revCharListToString n chars), charBack input)

ScanExponent :: !Int !Input ![Char] -> (!Token, !Input)
ScanExponent n input chars
Pieter Koopman's avatar
Pieter Koopman committed
936
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
937
938
939
940
941
942
943
944
	| eof					= (RealToken (revCharListToString n chars), input)
	| IsDigit c				= ScanExponent (n + 1) input [c:chars]
							= case chars of
								[c:_] | IsDigit c	-> (RealToken (revCharListToString n chars), charBack input)
								_					-> (ErrorToken ("Digit expected after "+revCharListToString n chars), charBack input)

ScanHexNumeral	:: !Int !Input -> (!Token, !Input)
ScanHexNumeral n input
Pieter Koopman's avatar
Pieter Koopman committed
945
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
946
947
948
949
950
951
	| eof					= (IntToken (toString n), input)
	| isHexDigit c			= ScanHexNumeral (n*16+hexDigitToInt c) input
							= (IntToken (toString n), charBack input)

ScanOctNumeral	:: !Int !Input -> (!Token, !Input)
ScanOctNumeral n input
Pieter Koopman's avatar
Pieter Koopman committed
952
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
953
954
955
956
957
958
	| eof					= (IntToken (toString n), input)
	| isOctDigit c			= ScanOctNumeral (n*8+digitToInt c) input
							= (IntToken (toString n), charBack input)

ScanChar :: !Input ![Char] -> (!Token, !Input)
ScanChar input chars
Pieter Koopman's avatar
Pieter Koopman committed
959
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
960
961
962
963
964
965
966
	| eof					= (ErrorToken "End of file inside Char denotation", input)
	| '\\' <> c				= ScanEndOfChar 1 [c: chars] input
	# (chars, n, input)		= ScanBSChar 0 chars input
	= ScanEndOfChar n chars input

ScanBSChar :: !Int ![Char] !Input -> (![Char], !Int, !Input)
ScanBSChar n chars input
Pieter Koopman's avatar
Pieter Koopman committed
967
	# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
968
969
970
971
972
	| eof					= (chars, n, input)
	= case c of
		'n'		->	(['n','\\':chars], n + 2, input)
		'r'		->	(['r','\\':chars], n + 2, input)
		'f'		->	(['f','\\':chars], n + 2, input)
973
974
975
// RWS ...		'b'		->	(['b','\\':chars], n + 2, input)
		'b'		->	to_chars '\b' n input
// ... RWS
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
976
		't'		->	(['t','\\':chars], n + 2, input)
977
978
979
// RWS ...		'v'		->	(['v','\\':chars], n + 2, input)
		'v'		->	to_chars '\v' n input
// ... RWS
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
		'\\'	->	(['\\','\\':chars], n + 2, input)
		'"'		->	(['"' ,'\\':chars], n + 2, input)
		'\''	->	(['\'','\\':chars], n + 2, input)
		'x'		#	(cc,input) = ScanNumChar Hex isHexDigit 2 0 input // max 2 characters
				->	to_chars cc n input
		'd'		#	(cc,input) = ScanNumChar Dec isDigit 3 0 input // max 3 characters
				->	to_chars cc n input
		c | IsOct c
				#	(cc,input) = ScanNumChar Oct IsOct 2 (digitToInt c) input // max 3 characters, including current
				->	to_chars cc n input
				->	([c:chars], n + 1, input)
where
	ScanNumChar base valid 0 acc input
		= (acc, input)
	ScanNumChar base valid n acc input
Pieter Koopman's avatar
Pieter Koopman committed
995
		# (eof, c, input)		= ReadNormalChar input
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
996
		| eof					= (acc, input)
997
998
999
// RWS ...		| valid c				= ScanNumChar base valid (n-1) (base*acc+digitToInt c) input
		| valid c				= ScanNumChar base valid (n-1) (base*acc+hexDigitToInt c) input
// ... RWS
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1000
								= (acc, charBack input)	
For faster browsing, not all history is shown. View entire blame