]> git.sur5r.net Git - cc65/blob - src/ca65/nexttok.c
Fix problematic code. Use more stuff from the shared modules.
[cc65] / src / ca65 / nexttok.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 nexttok.c                                 */
4 /*                                                                           */
5 /*              Get next token and handle token level functions              */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include "error.h"
37 #include "expr.h"
38 #include "scanner.h"
39 #include "toklist.h"
40 #include "nexttok.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   Code                                    */
46 /*****************************************************************************/
47
48
49
50 static TokList* CollectTokens (unsigned Start, unsigned Count)
51 /* Read a list of tokens that is terminated by a right paren. For all tokens
52  * starting at the one with index Start, and ending at (Start+Count-1), place
53  * them into a token list, and return this token list.
54  */
55 {
56     /* Create the token list */
57     TokList* List = NewTokList ();
58
59     /* Read the token list */
60     unsigned Current = 0;
61     unsigned Parens  = 0;
62     while (Parens != 0 && Tok != TOK_RPAREN) {
63
64         /* Check for end of line or end of input */
65         if (Tok == TOK_SEP || Tok == TOK_EOF) {
66             Error (ERR_UNEXPECTED_EOL);
67             return List;
68         }
69
70         /* Collect tokens in the given range */
71         if (Current >= Start && Current < Start+Count) {
72             /* Add the current token to the list */
73             AddCurTok (List);
74         }
75
76         /* Check for and count parenthesii */
77         if (Tok == TOK_LPAREN) {
78             ++Parens;
79         } else if (Tok == TOK_RPAREN) {
80             --Parens;
81         }
82
83         /* Get the next token */
84         NextTok ();
85     }
86
87     /* Eat the closing paren */
88     ConsumeRParen ();
89
90     /* Return the list of collected tokens */
91     return List;
92 }
93
94
95
96 static void FuncMid (void)
97 /* Handle the .MID function */
98 {
99     long        Start;
100     long        Count;
101     TokList*    List;
102
103     /* Skip it */
104     NextTok ();
105
106     /* Left paren expected */
107     ConsumeRParen ();
108
109     /* Start argument */
110     Start = ConstExpression ();
111     if (Start < 0 || Start > 100) {
112         Error (ERR_RANGE);
113         Start = 0;
114     }
115     ConsumeComma ();
116
117     /* Count argument */
118     Count = ConstExpression ();
119     if (Count > 100) {
120         Error (ERR_RANGE);
121         Count = 1;
122     }
123     ConsumeComma ();
124
125     /* Read the token list */
126     List = CollectTokens ((unsigned) Start, (unsigned) Count);
127
128     /* Insert it into the scanner feed */
129
130
131
132 }
133
134
135
136 void NextTok (void)
137 /* Get next token and handle token level functions */
138 {
139     /* Get the next raw token */
140     NextRawTok ();
141
142     /* Check for token handling functions */
143     switch (Tok) {
144
145         case TOK_MID:
146             FuncMid ();
147             break;
148
149         default:
150             /* Quiet down gcc */
151             break;
152
153     }
154 }
155
156
157
158 void Consume (enum Token Expected, unsigned ErrMsg)
159 /* Consume Expected, print an error if we don't find it */
160 {
161     if (Tok == Expected) {
162         NextTok ();
163     } else {
164         Error (ErrMsg);
165     }
166 }
167
168
169
170 void ConsumeSep (void)
171 /* Consume a separator token */
172 {
173     /* Accept an EOF as separator */
174     if (Tok != TOK_EOF) {
175         if (Tok != TOK_SEP) {
176             Error (ERR_TOO_MANY_CHARS);
177             SkipUntilSep ();
178         } else {
179             NextTok ();
180         }
181     }
182 }
183
184
185
186 void ConsumeLParen (void)
187 /* Consume a left paren */
188 {
189     Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
190 }
191
192
193
194 void ConsumeRParen (void)
195 /* Consume a right paren */
196 {
197     Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
198 }
199
200
201
202 void ConsumeComma (void)
203 /* Consume a comma */
204 {
205     Consume (TOK_COMMA, ERR_COMMA_EXPECTED);
206 }
207
208
209
210 void SkipUntilSep (void)
211 /* Skip tokens until we reach a line separator */
212 {
213     while (Tok != TOK_SEP && Tok != TOK_EOF) {
214         NextTok ();
215     }
216 }
217
218
219