APC Scanner 1.0
[henge/webcc.git] / src / apc / lexer.c
1 /*!@file
2 \brief lexical analyzer implementation for APC
3 \details The lexer manages two FIFO stacks. One for maintaining tokens, the
4 other for maintaining a list of files to be scanned. During
5 execution, the lexer will return a token from its token queue if any
6 are present. If not, the lexer will will pop an element from its
7 file queue to 'scanner' to be tokenized. If the file queue is empty,
8 the lexer will instead call 'parsedir' to traverse the directory tree
9 and tokenize the results. If 'parsedir' does not generate any new
10 tokens, we are done.
11 \author Jordan Lavatai
12 \date Aug 2016
13 ----------------------------------------------------------------------------*/
14 /* Standard */
15 #include <stdio.h>
16 #include <string.h>
17 #include <errno.h>
18 /* Posix */
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <dirent.h>
22 /* Local */
23 //#include "parser.tab.h"
24 #ifndef DE_STACKSIZE
25 #define DE_STACKSIZE 1024
26 #endif
27 #ifndef TK_STACKSIZE
28 #define TK_STACKSIZE 1024
29 #endif
30 /* Public */
31 int lexer_init(void);
32 int lexer(void);
33 int lexer_lex(const char*);
34 void lexer_pushtok(int, int);
35 struct dirent* lexer_direntpa[DE_STACKSIZE];
36 /* Private */
37 extern //scanner.c
38 int scanner_init(void);
39 extern //scanner.c
40 int scanner(void);
41 static inline
42 int dredge_current_depth(void);
43 extern //bison
44 int yylval;
45 static
46 struct tok
47 { int lval;
48 int tok;
49 } token_stack[TK_STACKSIZE];
50 static
51 union tokp
52 { int* i;
53 struct tok* t;
54 } tks, tkx;
55 static
56 struct dirent** dps;
57
58 /* Directory Entity Array/Stack
59 Simple array for keeping track of dirents yet to be processed by the scanner.
60 If this list is empty and there are no tokens, the lexer is done.
61 This array is populated by the scanner as an array, and popped locally by the
62 lexer as a stack.
63 */
64 #define DE_STACK (lexer_direntpa)
65 #define DE_STACKP (dps)
66 #define DE_LEN() (DE_STACKP - DE_STACK)
67 #define DE_INIT() (DE_STACKP = DE_STACK)
68 #define DE_POP() (*--DE_STACKP)
69
70 /* Token Stack
71 This is a FIFO stack whose pointers are a union of either a pointer to an
72 integer, or a pointer to two integers (a struct tok). This way, integers may
73 be added or removed from the stack either singularly (IPUSH/IPOP), or as a
74 full token of two integers (PUSH/POP).
75 An alignment error will occur if IPOP or IPUSH are used a non-even number of
76 times in a sequence!
77 */
78 #define TK_STACK (token_stack)
79 #define TK_STACKP (tks.t)
80 #define TK_STACKPI (tks.i)
81 #define TK_STACKX (tkx.t)
82 #define TK_STACKXI (tkx.i)
83 #define TK_LEN() (TK_STACKP - TK_STACKX)
84 #define TK_INIT() (TK_STACKP = TK_STACKX = TK_STACK)
85 #define TK_POP() (*TK_STACKP++)
86 #define TK_POPI() (*TK_STACKPI++);
87 #define TK_PUSH(T,L) (*TK_STACKX++ = (struct tok){L,T})
88
89 /* Initializer
90 The initializer returns boolean true if an error occurs, which may be handled with standard errno.
91 */
92 int lexer_init
93 ()
94 { TK_INIT();
95 DE_INIT();
96 return scanner_init();
97 }
98
99 /* Lexer
100 If the token buffer is empty, 'lexer' will initialize the token buffer and
101 call 'lexer_scandir'. If #SCANDIR_ERROR is returned, an error is printed
102 before sending a null return to bison. If 0 tokens are generated, the error
103 printing is skipped. In all other cases, 'yylval' is set, and the token's
104 integer representation is returned.
105 */
106 int lexer
107 #define SCAN_ERROR -1
108 #define TK_EMPTY (TK_STACKP == TK_STACKX)
109 ()
110 { if (TK_EMPTY)
111 { TK_INIT();
112 if (scanner() == 0)
113 { yylval = 0;
114 return 0;
115 }
116 }
117 yylval = TK_POPI();
118 return TK_POPI();
119 }
120
121 /* Lexical Analysis
122 Ragel state machine for tokenizing text.
123 */
124 int lexer_lex
125 (const char* str)
126 { lexer_pushtok(1, 2);
127 printf (str);
128 return 1;
129 }
130
131
132 /* Token Receiver
133 This receiver takes a struct tok and pushes it to the FIFO stack.
134 */
135 void lexer_pushtok
136 #define S(S)#S //stringifier
137 #define ERR_TK "Fatal: Generated over " S(TK_STACKSIZE) " tokens in one pass."
138 ( int tok, int lval )
139 { if (TK_LEN() >= TK_STACKSIZE)
140 { fprintf(stderr, ERR_TK);
141 exit(EXIT_FAILURE);
142 }
143 TK_PUSH(tok, lval);
144 }
145 /* init_file:
146 if (lsp != NULL)
147 while ((c = *lsp++) == *csp)
148 { switch (c)
149 { case DELIM:
150 delimeters_skipped++;
151 default:
152 csp++; //delayed to ensure csp is the start of scannable text
153 break;
154 }
155 }
156 last_string = string;
157 scan_text:
158 return scanner_tokenize(csp);
159 */