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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 static unsigned RawMode = 0; /* Raw token mode flag/counter */
61 /*****************************************************************************/
63 /*****************************************************************************/
67 static TokList* CollectTokens (unsigned Start, unsigned Count)
68 /* Read a list of tokens that is terminated by a right paren. For all tokens
69 * starting at the one with index Start, and ending at (Start+Count-1), place
70 * them into a token list, and return this token list.
73 /* Create the token list */
74 TokList* List = NewTokList ();
76 /* Read the token list */
79 while (Parens != 0 || Tok != TOK_RPAREN) {
81 /* Check for end of line or end of input */
82 if (Tok == TOK_SEP || Tok == TOK_EOF) {
83 Error (ERR_UNEXPECTED_EOL);
87 /* Collect tokens in the given range */
88 if (Current >= Start && Current < Start+Count) {
89 /* Add the current token to the list */
93 /* Check for and count parenthesii */
94 if (Tok == TOK_LPAREN) {
96 } else if (Tok == TOK_RPAREN) {
100 /* Get the next token */
105 /* Eat the closing paren */
108 /* Return the list of collected tokens */
114 static void FuncConcat (void)
115 /* Handle the .CONCAT function */
117 char Buf[MAX_STR_LEN+1];
125 /* Left paren expected */
128 /* Concatenate any number of strings */
134 /* Next token must be a string */
135 if (Tok != TOK_STRCON) {
136 Error (ERR_STRCON_EXPECTED);
141 /* Get the length of the string const and check total length */
143 if (Length + L > MAX_STR_LEN) {
144 Error (ERR_STRING_TOO_LONG);
150 /* Add the new string */
155 /* Skip the string token */
158 /* Comma means another argument */
159 if (Tok == TOK_COMMA) {
167 /* Terminate the string */
170 /* We expect a closing parenthesis, but will not skip it but replace it
171 * by the string token just created.
173 if (Tok != TOK_RPAREN) {
174 Error (ERR_RPAREN_EXPECTED);
183 static void FuncLeft (void)
184 /* Handle the .LEFT function */
192 /* Left paren expected */
196 Count = ConstExpression ();
197 if (Count < 0 || Count > 100) {
203 /* Read the token list */
204 List = CollectTokens (0, (unsigned) Count);
206 /* Since we want to insert the list before the now current token, we have
207 * to save the current token in some way and then skip it. To do this, we
208 * will add the current token at the end of the token list (so the list
209 * will never be empty), push the token list, and then skip the current
210 * token. This will replace the current token by the first token from the
211 * list (which will be the old current token in case the list was empty).
215 /* Insert it into the scanner feed */
216 PushTokList (List, ".LEFT");
218 /* Skip the current token */
224 static void FuncMid (void)
225 /* Handle the .MID function */
234 /* Left paren expected */
238 Start = ConstExpression ();
239 if (Start < 0 || Start > 100) {
246 Count = ConstExpression ();
247 if (Count < 0 || Count > 100) {
253 /* Read the token list */
254 List = CollectTokens ((unsigned) Start, (unsigned) Count);
256 /* Since we want to insert the list before the now current token, we have
257 * to save the current token in some way and then skip it. To do this, we
258 * will add the current token at the end of the token list (so the list
259 * will never be empty), push the token list, and then skip the current
260 * token. This will replace the current token by the first token from the
261 * list (which will be the old current token in case the list was empty).
265 /* Insert it into the scanner feed */
266 PushTokList (List, ".MID");
268 /* Skip the current token */
274 static void FuncRight (void)
275 /* Handle the .RIGHT function */
283 /* Left paren expected */
287 Count = ConstExpression ();
288 if (Count < 0 || Count > 100) {
294 /* Read the complete token list */
295 List = CollectTokens (0, 9999);
297 /* Delete tokens from the list until Count tokens are remaining */
298 while (List->Count > Count) {
299 /* Get the first node */
300 TokNode* T = List->Root;
302 /* Remove it from the list */
303 List->Root = List->Root->Next;
308 /* Corrent the token counter */
312 /* Since we want to insert the list before the now current token, we have
313 * to save the current token in some way and then skip it. To do this, we
314 * will add the current token at the end of the token list (so the list
315 * will never be empty), push the token list, and then skip the current
316 * token. This will replace the current token by the first token from the
317 * list (which will be the old current token in case the list was empty).
321 /* Insert it into the scanner feed */
322 PushTokList (List, ".RIGHT");
324 /* Skip the current token */
330 static void FuncString (void)
331 /* Handle the .STRING function */
333 char Buf[MAX_STR_LEN+1];
338 /* Left paren expected */
341 /* Accept identifiers or numeric expressions */
342 if (Tok == TOK_IDENT) {
343 /* Save the identifier, then skip it */
347 /* Numeric expression */
348 long Val = ConstExpression ();
349 sprintf (Buf, "%ld", Val);
352 /* We expect a closing parenthesis, but will not skip it but replace it
353 * by the string token just created.
355 if (Tok != TOK_RPAREN) {
356 Error (ERR_RPAREN_EXPECTED);
366 /* Get next token and handle token level functions */
368 /* Get the next raw token */
371 /* In raw mode, pass the token unchanged */
374 /* Execute token handling functions */
407 void Consume (enum Token Expected, unsigned ErrMsg)
408 /* Consume Expected, print an error if we don't find it */
410 if (Tok == Expected) {
419 void ConsumeSep (void)
420 /* Consume a separator token */
422 /* Accept an EOF as separator */
423 if (Tok != TOK_EOF) {
424 if (Tok != TOK_SEP) {
425 Error (ERR_TOO_MANY_CHARS);
435 void ConsumeLParen (void)
436 /* Consume a left paren */
438 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
443 void ConsumeRParen (void)
444 /* Consume a right paren */
446 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
451 void ConsumeComma (void)
452 /* Consume a comma */
454 Consume (TOK_COMMA, ERR_COMMA_EXPECTED);
459 void SkipUntilSep (void)
460 /* Skip tokens until we reach a line separator or end of file */
462 while (Tok != TOK_SEP && Tok != TOK_EOF) {
469 void EnterRawTokenMode (void)
470 /* Enter raw token mode. In raw mode, token handling functions are not
471 * executed, but the function tokens are passed untouched to the upper
472 * layer. Raw token mode is used when storing macro tokens for later
474 * Calls to EnterRawTokenMode and LeaveRawTokenMode may be nested.
482 void LeaveRawTokenMode (void)
483 /* Leave raw token mode. */
485 PRECONDITION (RawMode > 0);