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 TokList* CollectTokens (unsigned Start, unsigned Count)
53 /* Read a list of tokens that is terminated by a right paren. For all tokens
54 * starting at the one with index Start, and ending at (Start+Count-1), place
55 * them into a token list, and return this token list.
58 /* Create the token list */
59 TokList* List = NewTokList ();
61 /* Read the token list */
64 while (Parens != 0 || Tok != TOK_RPAREN) {
66 /* Check for end of line or end of input */
67 if (Tok == TOK_SEP || Tok == TOK_EOF) {
68 Error (ERR_UNEXPECTED_EOL);
72 /* Collect tokens in the given range */
73 if (Current >= Start && Current < Start+Count) {
74 /* Add the current token to the list */
78 /* Check for and count parenthesii */
79 if (Tok == TOK_LPAREN) {
81 } else if (Tok == TOK_RPAREN) {
85 /* Get the next token */
90 /* Eat the closing paren */
93 /* Return the list of collected tokens */
99 static void FuncConcat (void)
100 /* Handle the .CONCAT function */
102 char Buf[MAX_STR_LEN+1];
110 /* Left paren expected */
113 /* Concatenate any number of strings */
119 /* Next token must be a string */
120 if (Tok != TOK_STRCON) {
121 Error (ERR_STRCON_EXPECTED);
126 /* Get the length of the string const and check total length */
128 if (Length + L > MAX_STR_LEN) {
129 Error (ERR_STRING_TOO_LONG);
135 /* Add the new string */
140 /* Skip the string token */
143 /* Comma means another argument */
144 if (Tok == TOK_COMMA) {
152 /* Terminate the string */
155 /* We expect a closing parenthesis, but will not skip it but replace it
156 * by the string token just created.
158 if (Tok != TOK_RPAREN) {
159 Error (ERR_RPAREN_EXPECTED);
168 static void FuncMid (void)
169 /* Handle the .MID function */
178 /* Left paren expected */
182 Start = ConstExpression ();
183 if (Start < 0 || Start > 100) {
190 Count = ConstExpression ();
191 if (Count < 0 || Count > 100) {
197 /* Read the token list */
198 List = CollectTokens ((unsigned) Start, (unsigned) Count);
200 /* Insert it into the scanner feed */
201 PushTokList (List, ".MID");
206 static void FuncString (void)
207 /* Handle the .STRING function */
209 char Buf[MAX_STR_LEN+1];
214 /* Left paren expected */
217 /* Accept identifiers or numeric expressions */
218 if (Tok == TOK_IDENT) {
219 /* Save the identifier, then skip it */
223 /* Numeric expression */
224 long Val = ConstExpression ();
225 sprintf (Buf, "%ld", Val);
228 /* We expect a closing parenthesis, but will not skip it but replace it
229 * by the string token just created.
231 if (Tok != TOK_RPAREN) {
232 Error (ERR_RPAREN_EXPECTED);
242 /* Get next token and handle token level functions */
244 /* Get the next raw token */
247 /* Check for token handling functions */
271 void Consume (enum Token Expected, unsigned ErrMsg)
272 /* Consume Expected, print an error if we don't find it */
274 if (Tok == Expected) {
283 void ConsumeSep (void)
284 /* Consume a separator token */
286 /* Accept an EOF as separator */
287 if (Tok != TOK_EOF) {
288 if (Tok != TOK_SEP) {
289 Error (ERR_TOO_MANY_CHARS);
299 void ConsumeLParen (void)
300 /* Consume a left paren */
302 Consume (TOK_LPAREN, ERR_LPAREN_EXPECTED);
307 void ConsumeRParen (void)
308 /* Consume a right paren */
310 Consume (TOK_RPAREN, ERR_RPAREN_EXPECTED);
315 void ConsumeComma (void)
316 /* Consume a comma */
318 Consume (TOK_COMMA, ERR_COMMA_EXPECTED);
323 void SkipUntilSep (void)
324 /* Skip tokens until we reach a line separator */
326 while (Tok != TOK_SEP && Tok != TOK_EOF) {