tok_t Token = { 0u };
const char* Token_Strings[TOK_MAX] = {
- "id", /* TOK_ID */
- "num", /* TOK_NUM */
+ "EOF", /* TOK_EOF */
+ "ID", /* TOK_ID */
+ "NUM", /* TOK_NUM */
+ "LPAREN", /* TOK_LPAR */
+ "RPAREN", /* TOK_RPAR */
+ "LBRACK", /* TOK_LBRACK */
+ "RBRACK", /* TOK_RBRACK */
+ "LBRACE", /* TOK_LBRACE */
+ "RBRACE", /* TOK_RBRACE */
+ "TERM", /* TOK_TERM */
};
tok_t next_token(void)
{
- (void)memset(&Token,0,sizeof(Token));
+ prepare_for_token();
if (!file_eof())
{
- consume_whitespace();
- record_position();
- if (digit())
+ if (matches_any("()[]{};"))
+ punctuation();
+ else if (digit())
number();
//else if (matches('\''))
// character();
// string();
else
identifier();
+
+ /* the keyword "end" is actually a TOK_TERM */
+ if (0 == strcmp(Token.str,"end"))
+ set_type(TOK_TERM);
}
return Token;
}
+void punctuation(void)
+{
+ switch (file_peek())
+ {
+ case '(': accept_char( TOK_LPAR ); break;
+ case ')': accept_char( TOK_RPAR ); break;
+ case '[': accept_char( TOK_LBRACK ); break;
+ case ']': accept_char( TOK_RBRACK ); break;
+ case '{': accept_char( TOK_LBRACE ); break;
+ case '}': accept_char( TOK_RBRACE ); break;
+ case ';': accept_char( TOK_TERM ); break;
+ default: identifier(); break;
+ }
+}
+
void number()
{
set_type(TOK_NUM);
void identifier()
{
set_type(TOK_ID);
- while (!token_end()) consume();
+ while (!token_end() && !matches_any("()[]{};")) consume();
accept();
}
buf_put( file_get() );
}
-void consume_whitespace(void)
+void prepare_for_token(void)
{
+ (void)memset(&Token,0,sizeof(Token));
while( whitespace() )
(void)file_get();
+ record_position();
+}
+
+void accept_char(tok_type_t type)
+{
+ set_type(type);
+ consume();
+ accept();
}
void accept()
} tok_t;
typedef enum {
- TOK_ID = 0,
- TOK_NUM = 1,
- TOK_MAX = 2,
+ TOK_EOF = 0,
+ TOK_ID = 1,
+ TOK_NUM = 2,
+ TOK_LPAR = 3,
+ TOK_RPAR = 4,
+ TOK_LBRACK = 5,
+ TOK_RBRACK = 6,
+ TOK_LBRACE = 7,
+ TOK_RBRACE = 8,
+ TOK_TERM = 9,
+ TOK_MAX = 10,
} tok_type_t;
tok_t next_token(void);
+void punctuation(void);
void record_position(void);
void identifier(void);
void number(void);
void set_type(tok_type_t type);
void consume(void);
-void consume_whitespace(void);
+void prepare_for_token(void);
+void accept_char(tok_type_t type);
void accept(void);
#endif /* LEX_H */
while (!file_eof())
{
tok_t token = next_token();
- fprintf(stdout, "%s %d %d %s\n", token.type, token.line, token.column, token.str);
+ if (token.type != NULL)
+ fprintf(outfile, "%s\t%d\t%d\t%s\n", token.type, token.line, token.column, token.str);
free(token.str);
}
return ret;