1
0
mirror of https://github.com/cc65/cc65.git synced 2025-01-11 11:30:13 +00:00

Move more stuff from scanner.c into the new module token.c.

git-svn-id: svn://svn.cc65.org/cc65/trunk@3801 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2007-08-28 21:14:21 +00:00
parent aa4df9f98b
commit 3894b074a6
11 changed files with 138 additions and 83 deletions

View File

@ -333,7 +333,7 @@ static ExprNode* FuncBlank (void)
/* We have a list of tokens that ends with the closing paren. Skip /* We have a list of tokens that ends with the closing paren. Skip
* the tokens, and count them. Allow optionally curly braces. * the tokens, and count them. Allow optionally curly braces.
*/ */
enum Token Term = GetTokListTerm (TOK_RPAREN); Token Term = GetTokListTerm (TOK_RPAREN);
unsigned Count = 0; unsigned Count = 0;
while (Tok != Term) { while (Tok != Term) {
@ -437,7 +437,7 @@ static ExprNode* DoMatch (enum TC EqualityLevel)
* single linked list of tokens including attributes. The list is * single linked list of tokens including attributes. The list is
* either enclosed in curly braces, or terminated by a comma. * either enclosed in curly braces, or terminated by a comma.
*/ */
enum Token Term = GetTokListTerm (TOK_COMMA); Token Term = GetTokListTerm (TOK_COMMA);
while (Tok != Term) { while (Tok != Term) {
/* We may not end-of-line of end-of-file here */ /* We may not end-of-line of end-of-file here */
@ -717,7 +717,7 @@ static ExprNode* FuncTCount (void)
/* We have a list of tokens that ends with the closing paren. Skip /* We have a list of tokens that ends with the closing paren. Skip
* the tokens, and count them. Allow optionally curly braces. * the tokens, and count them. Allow optionally curly braces.
*/ */
enum Token Term = GetTokListTerm (TOK_RPAREN); Token Term = GetTokListTerm (TOK_RPAREN);
int Count = 0; int Count = 0;
while (Tok != Term) { while (Tok != Term) {
@ -978,7 +978,7 @@ static ExprNode* Term (void)
ExprNode* Right; ExprNode* Right;
/* Remember the token and skip it */ /* Remember the token and skip it */
enum Token T = Tok; Token T = Tok;
NextTok (); NextTok ();
/* Move root to left side and read the right side */ /* Move root to left side and read the right side */
@ -1079,7 +1079,7 @@ static ExprNode* SimpleExpr (void)
ExprNode* Right; ExprNode* Right;
/* Remember the token and skip it */ /* Remember the token and skip it */
enum Token T = Tok; Token T = Tok;
NextTok (); NextTok ();
/* Move root to left side and read the right side */ /* Move root to left side and read the right side */
@ -1141,7 +1141,7 @@ static ExprNode* BoolExpr (void)
ExprNode* Right; ExprNode* Right;
/* Remember the token and skip it */ /* Remember the token and skip it */
enum Token T = Tok; Token T = Tok;
NextTok (); NextTok ();
/* Move root to left side and read the right side */ /* Move root to left side and read the right side */
@ -1208,7 +1208,7 @@ static ExprNode* Expr2 (void)
ExprNode* Right; ExprNode* Right;
/* Remember the token and skip it */ /* Remember the token and skip it */
enum Token T = Tok; Token T = Tok;
NextTok (); NextTok ();
/* Move root to left side and read the right side */ /* Move root to left side and read the right side */
@ -1267,7 +1267,7 @@ static ExprNode* Expr1 (void)
ExprNode* Right; ExprNode* Right;
/* Remember the token and skip it */ /* Remember the token and skip it */
enum Token T = Tok; Token T = Tok;
NextTok (); NextTok ();
/* Move root to left side and read the right side */ /* Move root to left side and read the right side */

View File

@ -6,8 +6,8 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998-2004 Ullrich von Bassewitz */ /* (C) 1998-2007 Ullrich von Bassewitz */
/* Römerstraße 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
@ -662,7 +662,7 @@ static void StartExpClassic (Macro* M)
/* Start expanding the classic macro M */ /* Start expanding the classic macro M */
{ {
MacExp* E; MacExp* E;
enum Token Term; Token Term;
/* Skip the macro name */ /* Skip the macro name */
@ -764,7 +764,7 @@ static void StartExpDefine (Macro* M)
TokNode* Last; TokNode* Last;
/* The macro may optionally be enclosed in curly braces */ /* The macro may optionally be enclosed in curly braces */
enum Token Term = GetTokListTerm (TOK_COMMA); Token Term = GetTokListTerm (TOK_COMMA);
/* Check if there is really a parameter */ /* Check if there is really a parameter */
if (TokIsSep (Tok) || Tok == Term) { if (TokIsSep (Tok) || Tok == Term) {

View File

@ -53,6 +53,7 @@ OBJS = anonname.o \
symentry.o \ symentry.o \
symbol.o \ symbol.o \
symtab.o \ symtab.o \
token.o \
toklist.o \ toklist.o \
ulabel.o ulabel.o

View File

@ -97,6 +97,7 @@ OBJS = anonname.obj \
symbol.obj \ symbol.obj \
symentry.obj \ symentry.obj \
symtab.obj \ symtab.obj \
token.obj \
toklist.obj \ toklist.obj \
ulabel.obj ulabel.obj

View File

@ -6,8 +6,8 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2004 Ullrich von Bassewitz */ /* (C) 2000-2007 Ullrich von Bassewitz */
/* Römerstraße 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
@ -102,7 +102,7 @@ static TokList* CollectTokens (unsigned Start, unsigned Count)
TokList* List = NewTokList (); TokList* List = NewTokList ();
/* Determine if the list is enclosed in curly braces. */ /* Determine if the list is enclosed in curly braces. */
enum Token Term = GetTokListTerm (TOK_RPAREN); Token Term = GetTokListTerm (TOK_RPAREN);
/* Read the token list */ /* Read the token list */
unsigned Current = 0; unsigned Current = 0;
@ -218,10 +218,10 @@ static void NoIdent (void)
static void FuncIdent (void) static void FuncIdent (void)
/* Handle the .IDENT function */ /* Handle the .IDENT function */
{ {
char Buf[sizeof(SVal)]; char Buf[sizeof(SVal)];
enum Token Id; Token Id;
unsigned Len; unsigned Len;
unsigned I; unsigned I;
/* Skip it */ /* Skip it */
NextTok (); NextTok ();
@ -337,7 +337,7 @@ static void FuncMid (void)
/* Left paren expected */ /* Left paren expected */
ConsumeLParen (); ConsumeLParen ();
/* Start argument. Since the start argument can get negative with /* Start argument. Since the start argument can get negative with
* expressions like ".tcount(arg)-2", we correct it to zero silently. * expressions like ".tcount(arg)-2", we correct it to zero silently.
*/ */
Start = ConstExpression (); Start = ConstExpression ();
@ -726,7 +726,7 @@ void NextTok (void)
void Consume (enum Token Expected, const char* ErrMsg) void Consume (Token Expected, const char* ErrMsg)
/* Consume Expected, print an error if we don't find it */ /* Consume Expected, print an error if we don't find it */
{ {
if (Tok == Expected) { if (Tok == Expected) {

View File

@ -6,8 +6,8 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2004 Ullrich von Bassewitz */ /* (C) 2000-2007 Ullrich von Bassewitz */
/* Römerstraße 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
@ -51,7 +51,7 @@
void NextTok (void); void NextTok (void);
/* Get next token and handle token level functions */ /* Get next token and handle token level functions */
void Consume (enum Token Expected, const char* ErrMsg); void Consume (Token Expected, const char* ErrMsg);
/* Consume Token, print an error if we don't find it */ /* Consume Token, print an error if we don't find it */
void ConsumeSep (void); void ConsumeSep (void);

View File

@ -70,7 +70,7 @@
enum Token Tok = TOK_NONE; /* Current token */ Token Tok = TOK_NONE; /* Current token */
int WS; /* Flag: Whitespace before token */ int WS; /* Flag: Whitespace before token */
long IVal; /* Integer token attribute */ long IVal; /* Integer token attribute */
char SVal[MAX_STR_LEN+1]; /* String token attribute */ char SVal[MAX_STR_LEN+1]; /* String token attribute */
@ -84,7 +84,7 @@ typedef struct InputFile InputFile;
struct InputFile { struct InputFile {
FILE* F; /* Input file descriptor */ FILE* F; /* Input file descriptor */
FilePos Pos; /* Position in file */ FilePos Pos; /* Position in file */
enum Token Tok; /* Last token */ Token Tok; /* Last token */
int C; /* Last character */ int C; /* Last character */
char Line[256]; /* The current input line */ char Line[256]; /* The current input line */
InputFile* Next; /* Linked list of input files */ InputFile* Next; /* Linked list of input files */
@ -96,7 +96,7 @@ struct InputData {
char* Text; /* Pointer to the text data */ char* Text; /* Pointer to the text data */
const char* Pos; /* Pointer to current position */ const char* Pos; /* Pointer to current position */
int Malloced; /* Memory was malloced */ int Malloced; /* Memory was malloced */
enum Token Tok; /* Last token */ Token Tok; /* Last token */
int C; /* Last character */ int C; /* Last character */
InputData* Next; /* Linked list of input data */ InputData* Next; /* Linked list of input data */
}; };
@ -115,7 +115,7 @@ struct CharSourceFunctions {
/* Input source: Either file or data */ /* Input source: Either file or data */
struct CharSource { struct CharSource {
CharSource* Next; /* Linked list of char sources */ CharSource* Next; /* Linked list of char sources */
enum Token Tok; /* Last token */ Token Tok; /* Last token */
int C; /* Last character */ int C; /* Last character */
const CharSourceFunctions* Func; /* Pointer to function table */ const CharSourceFunctions* Func; /* Pointer to function table */
union { union {
@ -135,7 +135,7 @@ int ForcedEnd = 0;
/* List of dot keywords with the corresponding tokens */ /* List of dot keywords with the corresponding tokens */
struct DotKeyword { struct DotKeyword {
const char* Key; /* MUST be first field */ const char* Key; /* MUST be first field */
enum Token Tok; Token Tok;
} DotKeywords [] = { } DotKeywords [] = {
{ ".A16", TOK_A16 }, { ".A16", TOK_A16 },
{ ".A8", TOK_A8 }, { ".A8", TOK_A8 },
@ -810,7 +810,7 @@ Again:
} }
} }
/* Read the number */ /* Read the number */
IVal = 0; IVal = 0;
while (IsXDigit (C)) { while (IsXDigit (C)) {
if (IVal & 0xF0000000) { if (IVal & 0xF0000000) {
@ -835,7 +835,7 @@ Again:
Error ("Binary digit expected"); Error ("Binary digit expected");
} }
/* Read the number */ /* Read the number */
IVal = 0; IVal = 0;
while (IsBDigit (C)) { while (IsBDigit (C)) {
if (IVal & 0x80000000) { if (IVal & 0x80000000) {
@ -910,7 +910,7 @@ Again:
/* This is an integer constant */ /* This is an integer constant */
Tok = TOK_INTCON; Tok = TOK_INTCON;
return; return;
} }
/* Control command? */ /* Control command? */
@ -935,7 +935,7 @@ Again:
Tok = FindDotKeyword (); Tok = FindDotKeyword ();
if (Tok == TOK_NONE) { if (Tok == TOK_NONE) {
/* Not found */ /* Not found */
if (!LeadingDotInIdents) { if (!LeadingDotInIdents) {
/* Invalid pseudo instruction */ /* Invalid pseudo instruction */
Error ("`%s' is not a recognized control command", SVal); Error ("`%s' is not a recognized control command", SVal);
@ -1035,7 +1035,7 @@ Again:
} }
break; break;
default: default:
break; break;
} }
@ -1085,7 +1085,7 @@ CharAgain:
case '^': case '^':
NextChar (); NextChar ();
Tok = TOK_XOR; Tok = TOK_XOR;
return; return;
case '&': case '&':
@ -1110,7 +1110,7 @@ CharAgain:
case ':': case ':':
NextChar (); NextChar ();
switch (C) { switch (C) {
case ':': case ':':
NextChar (); NextChar ();
@ -1160,7 +1160,7 @@ CharAgain:
case '#': case '#':
NextChar (); NextChar ();
Tok = TOK_HASH; Tok = TOK_HASH;
return; return;
case '(': case '(':
@ -1185,7 +1185,7 @@ CharAgain:
case '{': case '{':
NextChar (); NextChar ();
Tok = TOK_LCURLY; Tok = TOK_LCURLY;
return; return;
case '}': case '}':
@ -1210,7 +1210,7 @@ CharAgain:
return; return;
case '=': case '=':
NextChar (); NextChar ();
Tok = TOK_EQ; Tok = TOK_EQ;
return; return;
@ -1235,7 +1235,7 @@ CharAgain:
case '~': case '~':
NextChar (); NextChar ();
Tok = TOK_NOT; Tok = TOK_NOT;
return; return;
case '\'': case '\'':
/* Hack: If we allow ' as terminating character for strings, read /* Hack: If we allow ' as terminating character for strings, read
@ -1285,7 +1285,7 @@ CharAgain:
goto Again; goto Again;
} }
} }
break; break;
case '\n': case '\n':
NextChar (); NextChar ();
@ -1314,22 +1314,6 @@ CharAgain:
int TokHasSVal (enum Token Tok)
/* Return true if the given token has an attached SVal */
{
return (Tok == TOK_IDENT || TOK_LOCAL_IDENT || Tok == TOK_STRCON);
}
int TokHasIVal (enum Token Tok)
/* Return true if the given token has an attached IVal */
{
return (Tok == TOK_INTCON || Tok == TOK_CHARCON || Tok == TOK_REG);
}
int GetSubKey (const char** Keys, unsigned Count) int GetSubKey (const char** Keys, unsigned Count)
/* Search for a subkey in a table of keywords. The current token must be an /* Search for a subkey in a table of keywords. The current token must be an
* identifier and all keys must be in upper case. The identifier will be * identifier and all keys must be in upper case. The identifier will be

View File

@ -40,7 +40,6 @@
/* common */ /* common */
#include "filepos.h" #include "filepos.h"
#include "inline.h"
/* ca65 */ /* ca65 */
#include "token.h" #include "token.h"
@ -56,7 +55,7 @@
/* Scanner variables */ /* Scanner variables */
#define MAX_INPUT_FILES 254 /* No more than this files total */ #define MAX_INPUT_FILES 254 /* No more than this files total */
#define MAX_STR_LEN 255 /* Maximum length of any string */ #define MAX_STR_LEN 255 /* Maximum length of any string */
extern enum Token Tok; /* Current token */ extern Token Tok; /* Current token */
extern int WS; /* Flag: Whitespace before token */ extern int WS; /* Flag: Whitespace before token */
extern long IVal; /* Integer token attribute */ extern long IVal; /* Integer token attribute */
extern char SVal[MAX_STR_LEN+1]; /* String token attribute */ extern char SVal[MAX_STR_LEN+1]; /* String token attribute */
@ -93,22 +92,6 @@ void UpcaseSVal (void);
void NextRawTok (void); void NextRawTok (void);
/* Read the next raw token from the input stream */ /* Read the next raw token from the input stream */
int TokHasSVal (enum Token Tok);
/* Return true if the given token has an attached SVal */
int TokHasIVal (enum Token Tok);
/* Return true if the given token has an attached IVal */
#if defined(HAVE_INLINE)
INLINE int TokIsSep (enum Token T)
/* Return true if this is a separator token */
{
return (T == TOK_SEP || T == TOK_EOF);
}
#else
# define TokIsSep(T) (T == TOK_SEP || T == TOK_EOF)
#endif
int GetSubKey (const char** Keys, unsigned Count); int GetSubKey (const char** Keys, unsigned Count);
/* Search for a subkey in a table of keywords. The current token must be an /* Search for a subkey in a table of keywords. The current token must be an
* identifier and all keys must be in upper case. The identifier will be * identifier and all keys must be in upper case. The identifier will be
@ -136,3 +119,4 @@ void DoneScanner (void);

62
src/ca65/token.c Normal file
View File

@ -0,0 +1,62 @@
/*****************************************************************************/
/* */
/* token.c */
/* */
/* Token list for the ca65 macro assembler */
/* */
/* */
/* */
/* (C) 2007 Ullrich von Bassewitz */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* warranty. In no event will the authors be held liable for any damages */
/* arising from the use of this software. */
/* */
/* Permission is granted to anyone to use this software for any purpose, */
/* including commercial applications, and to alter it and redistribute it */
/* freely, subject to the following restrictions: */
/* */
/* 1. The origin of this software must not be misrepresented; you must not */
/* claim that you wrote the original software. If you use this software */
/* in a product, an acknowledgment in the product documentation would be */
/* appreciated but is not required. */
/* 2. Altered source versions must be plainly marked as such, and must not */
/* be misrepresented as being the original software. */
/* 3. This notice may not be removed or altered from any source */
/* distribution. */
/* */
/*****************************************************************************/
/* ca65 */
#include "token.h"
/*****************************************************************************/
/* Code */
/*****************************************************************************/
int TokHasSVal (Token Tok)
/* Return true if the given token has an attached SVal */
{
return (Tok == TOK_IDENT || TOK_LOCAL_IDENT || Tok == TOK_STRCON);
}
int TokHasIVal (Token Tok)
/* Return true if the given token has an attached IVal */
{
return (Tok == TOK_INTCON || Tok == TOK_CHARCON || Tok == TOK_REG);
}

View File

@ -1,6 +1,6 @@
/*****************************************************************************/ /*****************************************************************************/
/* */ /* */
/* token.c */ /* token.h */
/* */ /* */
/* Token list for the ca65 macro assembler */ /* Token list for the ca65 macro assembler */
/* */ /* */
@ -38,6 +38,11 @@
/* common */
#include "inline.h"
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
@ -45,7 +50,7 @@
/* Tokens */ /* Tokens */
enum Token { typedef enum Token {
TOK_NONE, /* Start value, invalid */ TOK_NONE, /* Start value, invalid */
TOK_EOF, /* End of input file */ TOK_EOF, /* End of input file */
TOK_SEP, /* Separator (usually newline) */ TOK_SEP, /* Separator (usually newline) */
@ -98,7 +103,7 @@ enum Token {
TOK_HASH, /* # */ TOK_HASH, /* # */
TOK_COLON, /* : */ TOK_COLON, /* : */
TOK_LPAREN, /* ( */ TOK_LPAREN, /* ( */
TOK_RPAREN, /* ) */ TOK_RPAREN, /* ) */
TOK_LBRACK, /* [ */ TOK_LBRACK, /* [ */
TOK_RBRACK, /* ] */ TOK_RBRACK, /* ] */
TOK_LCURLY, /* { */ TOK_LCURLY, /* { */
@ -241,7 +246,7 @@ enum Token {
TOK_LASTPSEUDO = TOK_ZEROPAGE, TOK_LASTPSEUDO = TOK_ZEROPAGE,
TOK_COUNT /* Count of tokens */ TOK_COUNT /* Count of tokens */
}; } Token;
@ -251,6 +256,24 @@ enum Token {
int TokHasSVal (Token Tok);
/* Return true if the given token has an attached SVal */
int TokHasIVal (Token Tok);
/* Return true if the given token has an attached IVal */
#if defined(HAVE_INLINE)
INLINE int TokIsSep (enum Token T)
/* Return true if this is a separator token */
{
return (T == TOK_SEP || T == TOK_EOF);
}
#else
# define TokIsSep(T) (T == TOK_SEP || T == TOK_EOF)
#endif
/* End of token.h */ /* End of token.h */
#endif #endif

View File

@ -6,8 +6,8 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2000-2004 Ullrich von Bassewitz */ /* (C) 2000-2007 Ullrich von Bassewitz */
/* Römerstraße 52 */ /* Roemerstrasse 52 */
/* D-70794 Filderstadt */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
@ -52,7 +52,7 @@
typedef struct TokNode TokNode; typedef struct TokNode TokNode;
struct TokNode { struct TokNode {
TokNode* Next; /* For single linked list */ TokNode* Next; /* For single linked list */
enum Token Tok; /* Token value */ Token Tok; /* Token value */
int WS; /* Whitespace before token? */ int WS; /* Whitespace before token? */
long IVal; /* Integer token attribute */ long IVal; /* Integer token attribute */
char SVal [1]; /* String attribute, dyn. allocated */ char SVal [1]; /* String attribute, dyn. allocated */
@ -109,7 +109,7 @@ TokList* NewTokList (void);
void FreeTokList (TokList* T); void FreeTokList (TokList* T);
/* Delete the token list including all token nodes */ /* Delete the token list including all token nodes */
enum Token GetTokListTerm (enum Token Term); Token GetTokListTerm (Token Term);
/* Determine if the following token list is enclosed in curly braces. This is /* Determine if the following token list is enclosed in curly braces. This is
* the case if the next token is the opening brace. If so, skip it and return * the case if the next token is the opening brace. If so, skip it and return
* a closing brace, otherwise return Term. * a closing brace, otherwise return Term.