1 /*****************************************************************************/
5 /* Get next token and handle token level functions */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 static unsigned RawMode = 0; /* Raw token mode flag/counter */
60 /*****************************************************************************/
62 /*****************************************************************************/
66 static TokList* CollectTokens (unsigned Start, unsigned Count)
67 /* Read a list of tokens that is terminated by a right paren. For all tokens
68 * starting at the one with index Start, and ending at (Start+Count-1), place
69 * them into a token list, and return this token list.
72 /* Create the token list */
73 TokList* List = NewTokList ();
75 /* Read the token list */
78 while (Parens != 0 || Tok != TOK_RPAREN) {
80 /* Check for end of line or end of input */
81 if (Tok == TOK_SEP || Tok == TOK_EOF) {
82 Error (ERR_UNEXPECTED_EOL);
86 /* Collect tokens in the given range */
87 if (Current >= Start && Current < Start+Count) {
88 /* Add the current token to the list */
92 /* Check for and count parenthesii */
93 if (Tok == TOK_LPAREN) {
95 } else if (Tok == TOK_RPAREN) {
99 /* Get the next token */
104 /* Eat the closing paren */
107 /* Return the list of collected tokens */
113 static void FuncConcat (void)
114 /* Handle the .CONCAT function */
116 char Buf[MAX_STR_LEN+1];
124 /* Left paren expected */
127 /* Concatenate any number of strings */
133 /* Next token must be a string */
134 if (Tok != TOK_STRCON) {
135 Error (ERR_STRCON_EXPECTED);
140 /* Get the length of the string const and check total length */
142 if (Length + L > MAX_STR_LEN) {
143 Error (ERR_STRING_TOO_LONG);
149 /* Add the new string */
154 /* Skip the string token */
157 /* Comma means another argument */
158 if (Tok == TOK_COMMA) {
166 /* Terminate the string */
169 /* We expect a closing parenthesis, but will not skip it but replace it
170 * by the string token just created.
172 if (Tok != TOK_RPAREN) {
173 Error (ERR_RPAREN_EXPECTED);
182 static void FuncLeft (void)
183 /* Handle the .LEFT function */
191 /* Left paren expected */
195 Count = ConstExpression ();
196 if (Count < 0 || Count > 100) {
202 /* Read the token list */
203 List = CollectTokens (0, (unsigned) Count);
205 /* Since we want to insert the list before the now current token, we have
206 * to save the current token in some way and then skip it. To do this, we
207 * will add the current token at the end of the token list (so the list
208 * will never be empty), push the token list, and then skip the current
209 * token. This will replace the current token by the first token from the
210 * list (which will be the old current token in case the list was empty).
214 /* Insert it into the scanner feed */
215 PushTokList (List, ".LEFT");
217 /* Skip the current token */
223 static void FuncMid (void)
224 /* Handle the .MID function */
233 /* Left paren expected */
237 Start = ConstExpression ();
238 if (Start < 0 || Start > 100) {
245 Count = ConstExpression ();
246 if (Count < 0 || Count > 100) {
252 /* Read the token list */
253 List = CollectTokens ((unsigned) Start, (unsigned) Count);
255 /* Since we want to insert the list before the now current token, we have
256 * to save the current token in some way and then skip it. To do this, we
257 * will add the current token at the end of the token list (so the list
258 * will never be empty), push the token list, and then skip the current
259 * token. This will replace the current token by the first token from the
260 * list (which will be the old current token in case the list was empty).
264 /* Insert it into the scanner feed */
265 PushTokList (List, ".MID");
267 /* Skip the current token */
273 static void FuncRight (void)
274 /* Handle the .RIGHT function */
282 /* Left paren expected */
286 Count = ConstExpression ();
287 if (Count < 0 || Count > 100) {
293 /* Read the complete token list */
294 List = CollectTokens (0, 9999);
296 /* Delete tokens from the list until Count tokens are remaining */
297 while (List->Count > Count) {
298 /* Get the first node */
299 TokNode* T = List->Root;
301 /* Remove it from the list */
302 List->Root = List->Root->Next;
307 /* Corrent the token counter */
311 /* Since we want to insert the list before the now current token, we have
312 * to save the current token in some way and then skip it. To do this, we
313 * will add the current token at the end of the token list (so the list
314 * will never be empty), push the token list, and then skip the current
315 * token. This will replace the current token by the first token from the
316 * list (which will be the old current token in case the list was empty).
320 /* Insert it into the scanner feed */
321 PushTokList (List, ".RIGHT");
323 /* Skip the current token */
329 static void FuncString (void)
330 /* Handle the .STRING function */
332 char Buf[MAX_STR_LEN+1];
337 /* Left paren expected */
340 /* Accept identifiers or numeric expressions */
341 if (Tok == TOK_IDENT) {
342 /* Save the identifier, then skip it */
346 /* Numeric expression */
347 long Val = ConstExpression ();
348 sprintf (Buf, "%ld", Val);
351 /* We expect a closing parenthesis, but will not skip it but replace it
352 * by the string token just created.
354 if (Tok != TOK_RPAREN) {
355 Error (ERR_RPAREN_EXPECTED);
365 /* Get next token and handle token level functions */
367 /* Get the next raw token */
370 /* In raw mode, pass the token unchanged */
373 /* Execute token handling functions */
406 void Consume (enum Token Expected, unsigned ErrMsg)
407 /* Consume Expected, print an error if we don't find it */
409 if (Tok == Expected) {
418 void ConsumeSep (void)
419 /* Consume a separator token */
421 /* Accept an EOF as separator */
422 if (Tok != TOK_EOF) {
423 if (Tok != TOK_SEP) {
424 Error (ERR_TOO_MANY_CHARS);
434 void ConsumeLParen (void)
435 /* Consume a left paren */
437 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
442 void ConsumeRParen (void)
443 /* Consume a right paren */
445 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
450 void ConsumeComma (void)
451 /* Consume a comma */
453 Consume (TOK_COMMA, ERR_COMMA_EXPECTED);
458 void SkipUntilSep (void)
459 /* Skip tokens until we reach a line separator or end of file */
461 while (Tok != TOK_SEP && Tok != TOK_EOF) {
468 void EnterRawTokenMode (void)
469 /* Enter raw token mode. In raw mode, token handling functions are not
470 * executed, but the function tokens are passed untouched to the upper
471 * layer. Raw token mode is used when storing macro tokens for later
473 * Calls to EnterRawTokenMode and LeaveRawTokenMode may be nested.
481 void LeaveRawTokenMode (void)
482 /* Leave raw token mode. */
484 PRECONDITION (RawMode > 0);