1 /*****************************************************************************/
5 /* Small scanner for input from a StrBuf */
9 /* (C) 2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
43 #include "scanstrbuf.h"
47 /*****************************************************************************/
48 /* Helper functions */
49 /*****************************************************************************/
53 static char ParseChar (StrBuf* B)
54 /* Parse a character. Converts \n into EOL, etc. */
59 /* Check for escape chars */
60 if ((C = SB_Get (B)) == '\\') {
88 /* Hex character constant */
89 C = HexVal (SB_Get (B)) << 4;
90 C |= HexVal (SB_Get (B));
100 while (SB_Peek (B) >= '0' && SB_Peek (B) <= '7' && I++ < 4) {
101 C = (C << 3) | (SB_Get (B) - '0');
105 Error ("Illegal character constant");
111 /* Return the character */
121 /*****************************************************************************/
123 /*****************************************************************************/
127 void SB_SkipWhite (StrBuf* B)
128 /* Skip whitespace in the string buffer */
130 while (IsBlank (SB_Peek (B))) {
137 int SB_GetSym (StrBuf* B, char* S)
138 /* Get a symbol from the string buffer. S must be able to hold MAX_IDENTLEN
139 * characters. Returns 1 if a symbol was found and 0 otherwise.
142 if (IsIdent (SB_Peek (B))) {
144 char C = SB_Peek (B);
146 if (I < MAX_IDENTLEN) {
152 } while (IsIdent (C) || IsDigit (C));
162 int SB_GetString (StrBuf* B, StrBuf* S)
163 /* Get a string from the string buffer. S will be initialized by the function
164 * and will return the correctly terminated string on return. The function
165 * returns 1 if a string was found and 0 otherwise.
171 *S = AUTO_STRBUF_INITIALIZER;
172 if (SB_Peek (B) == '\"') {
174 /* String follows, be sure to concatenate strings */
175 while (SB_Peek (B) == '\"') {
177 /* Skip the quote char */
180 /* Read the actual string contents */
181 while ((C = SB_Peek (B)) != '\"') {
183 Error ("Unexpected end of string");
186 SB_AppendChar (S, ParseChar (B));
189 /* Skip the closing quote char if there was one */
192 /* Skip white space, read new input */
196 /* Terminate the string */