1 /* Copyright (c) 2003, David Leonard. All rights reserved. */
2 
3 #ifndef _SEE_h_input_
4 #define _SEE_h_input_
5 
6 /*
7  * UCS-32 character stream inputs,
8  * used by the lexical analyser.
9  *
10  * Supported streams:
11  *	- ASCII stdio file
12  *	- SEE_string
13  *	- UTF-8 C-strings
14  *
15  * Inputs can also implement filters.
16  * Supported filters:
17  *	- n-character lookahead
18  *
19  */
20 
21 #include <stdio.h>
22 
23 #include <see/type.h>
24 
25 struct SEE_input;
26 struct SEE_string;
27 struct SEE_interpreter;
28 
29 struct SEE_inputclass {
30 	/* Returns the next character on the stream. Invalid if !eof */
31 	SEE_unicode_t	(*next)(struct SEE_input *);
32 	/* Releases system resources allocated to input */
33 	void		(*close)(struct SEE_input *);
34 };
35 
36 struct SEE_input {
37 	struct SEE_inputclass *inputclass;
38 	SEE_boolean_t	   eof;		/* True means next() will be invalid */
39 	SEE_unicode_t	   lookahead;	/* UCS-32 - what next() will return */
40 	struct SEE_string *filename;	/* source origin desc (or NULL) */
41 	int 		   first_lineno;
42 	struct SEE_interpreter *interpreter;
43 };
44 
45 #define SEE_INPUT_NEXT(i)	(*(i)->inputclass->next)(i)
46 #define SEE_INPUT_CLOSE(i)	(*(i)->inputclass->close)(i)
47 
48 /*
49  * These input filters are intended for testing and demonstration.
50  * Host applications will normally provide their own input class if they
51  * want to support embedded JavaScript. Typically, you will have your
52  * program text in a buffer that your application owns, and your
53  * input class will return characters from that buffer.
54  * Remember: ASCII chars 0x80-0xff are not Unicode!
55  */
56 
57 /* Creates an input from a stdio file. Unicode markers are understood */
58 struct SEE_input   *SEE_input_file(struct SEE_interpreter *i,
59 			FILE *f, const char *filename, const char *encoding);
60 
61 /* Creates an input from an interpreter string */
62 struct SEE_input   *SEE_input_string(struct SEE_interpreter *i,
63 			struct SEE_string *s);
64 
65 /* Creates an input from a C string */
66 struct SEE_input   *SEE_input_utf8(struct SEE_interpreter *i, const char *s);
67 
68 /* Creates an input filter around another input, keeping a lookahead buffer */
69 struct SEE_input   *SEE_input_lookahead(struct SEE_input *i, int maxlookahead);
70 
71 /* Copy out the lookahead buffer */
72 int SEE_input_lookahead_copy(struct SEE_input *li,
73 			SEE_unicode_t *buf, int buflen);
74 
75 /* Unicode character returned when source is malformed */
76 #define SEE_INPUT_BADCHAR	((SEE_unicode_t)0x100000)
77 
78 #endif /* _SEE_h_input_ */
79