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 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
52 static unsigned RawMode = 0; /* Raw token mode flag/counter */
56 /*****************************************************************************/
58 /*****************************************************************************/
62 static TokList* CollectTokens (unsigned Start, unsigned Count)
63 /* Read a list of tokens that is terminated by a right paren. For all tokens
64 * starting at the one with index Start, and ending at (Start+Count-1), place
65 * them into a token list, and return this token list.
68 /* Create the token list */
69 TokList* List = NewTokList ();
71 /* Read the token list */
74 while (Parens != 0 || Tok != TOK_RPAREN) {
76 /* Check for end of line or end of input */
77 if (Tok == TOK_SEP || Tok == TOK_EOF) {
78 Error (ERR_UNEXPECTED_EOL);
82 /* Collect tokens in the given range */
83 if (Current >= Start && Current < Start+Count) {
84 /* Add the current token to the list */
88 /* Check for and count parenthesii */
89 if (Tok == TOK_LPAREN) {
91 } else if (Tok == TOK_RPAREN) {
95 /* Get the next token */
100 /* Eat the closing paren */
103 /* Return the list of collected tokens */
109 static void FuncConcat (void)
110 /* Handle the .CONCAT function */
112 char Buf[MAX_STR_LEN+1];
120 /* Left paren expected */
123 /* Concatenate any number of strings */
129 /* Next token must be a string */
130 if (Tok != TOK_STRCON) {
131 Error (ERR_STRCON_EXPECTED);
136 /* Get the length of the string const and check total length */
138 if (Length + L > MAX_STR_LEN) {
139 Error (ERR_STRING_TOO_LONG);
145 /* Add the new string */
150 /* Skip the string token */
153 /* Comma means another argument */
154 if (Tok == TOK_COMMA) {
162 /* Terminate the string */
165 /* We expect a closing parenthesis, but will not skip it but replace it
166 * by the string token just created.
168 if (Tok != TOK_RPAREN) {
169 Error (ERR_RPAREN_EXPECTED);
178 static void FuncLeft (void)
179 /* Handle the .LEFT function */
187 /* Left paren expected */
191 Count = ConstExpression ();
192 if (Count < 0 || Count > 100) {
198 /* Read the token list */
199 List = CollectTokens (0, (unsigned) Count);
201 /* Since we want to insert the list before the now current token, we have
202 * to save the current token in some way and then skip it. To do this, we
203 * will add the current token at the end of the token list (so the list
204 * will never be empty), push the token list, and then skip the current
205 * token. This will replace the current token by the first token from the
206 * list (which will be the old current token in case the list was empty).
210 /* Insert it into the scanner feed */
211 PushTokList (List, ".LEFT");
213 /* Skip the current token */
219 static void FuncMid (void)
220 /* Handle the .MID function */
229 /* Left paren expected */
233 Start = ConstExpression ();
234 if (Start < 0 || Start > 100) {
241 Count = ConstExpression ();
242 if (Count < 0 || Count > 100) {
248 /* Read the token list */
249 List = CollectTokens ((unsigned) Start, (unsigned) Count);
251 /* Since we want to insert the list before the now current token, we have
252 * to save the current token in some way and then skip it. To do this, we
253 * will add the current token at the end of the token list (so the list
254 * will never be empty), push the token list, and then skip the current
255 * token. This will replace the current token by the first token from the
256 * list (which will be the old current token in case the list was empty).
260 /* Insert it into the scanner feed */
261 PushTokList (List, ".MID");
263 /* Skip the current token */
269 static void FuncRight (void)
270 /* Handle the .RIGHT function */
278 /* Left paren expected */
282 Count = ConstExpression ();
283 if (Count < 0 || Count > 100) {
289 /* Read the complete token list */
290 List = CollectTokens (0, 9999);
292 /* Delete tokens from the list until Count tokens are remaining */
293 while (List->Count > Count) {
294 /* Get the first node */
295 TokNode* T = List->Root;
297 /* Remove it from the list */
298 List->Root = List->Root->Next;
303 /* Corrent the token counter */
307 /* Since we want to insert the list before the now current token, we have
308 * to save the current token in some way and then skip it. To do this, we
309 * will add the current token at the end of the token list (so the list
310 * will never be empty), push the token list, and then skip the current
311 * token. This will replace the current token by the first token from the
312 * list (which will be the old current token in case the list was empty).
316 /* Insert it into the scanner feed */
317 PushTokList (List, ".RIGHT");
319 /* Skip the current token */
325 static void FuncString (void)
326 /* Handle the .STRING function */
328 char Buf[MAX_STR_LEN+1];
333 /* Left paren expected */
336 /* Accept identifiers or numeric expressions */
337 if (Tok == TOK_IDENT) {
338 /* Save the identifier, then skip it */
342 /* Numeric expression */
343 long Val = ConstExpression ();
344 sprintf (Buf, "%ld", Val);
347 /* We expect a closing parenthesis, but will not skip it but replace it
348 * by the string token just created.
350 if (Tok != TOK_RPAREN) {
351 Error (ERR_RPAREN_EXPECTED);
361 /* Get next token and handle token level functions */
363 /* Get the next raw token */
366 /* In raw mode, pass the token unchanged */
369 /* Execute token handling functions */
402 void Consume (enum Token Expected, unsigned ErrMsg)
403 /* Consume Expected, print an error if we don't find it */
405 if (Tok == Expected) {
414 void ConsumeSep (void)
415 /* Consume a separator token */
417 /* Accept an EOF as separator */
418 if (Tok != TOK_EOF) {
419 if (Tok != TOK_SEP) {
420 Error (ERR_TOO_MANY_CHARS);
430 void ConsumeLParen (void)
431 /* Consume a left paren */
433 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
438 void ConsumeRParen (void)
439 /* Consume a right paren */
441 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
446 void ConsumeComma (void)
447 /* Consume a comma */
449 Consume (TOK_COMMA, ERR_COMMA_EXPECTED);
454 void SkipUntilSep (void)
455 /* Skip tokens until we reach a line separator or end of file */
457 while (Tok != TOK_SEP && Tok != TOK_EOF) {
464 void EnterRawTokenMode (void)
465 /* Enter raw token mode. In raw mode, token handling functions are not
466 * executed, but the function tokens are passed untouched to the upper
467 * layer. Raw token mode is used when storing macro tokens for later
469 * Calls to EnterRawTokenMode and LeaveRawTokenMode may be nested.
477 void LeaveRawTokenMode (void)
478 /* Leave raw token mode. */
480 PRECONDITION (RawMode > 0);