summaryrefslogtreecommitdiffstats
path: root/dimension/tokenize.c
diff options
context:
space:
mode:
authortavianator <tavianator@antimatter.localdomain>2009-10-29 12:35:32 -0400
committertavianator <tavianator@antimatter.localdomain>2009-10-29 12:35:32 -0400
commit281a8741c0407e3cc504e98dee7a1e10782b46b2 (patch)
tree03061db1b4fffc8ee5fb021ab8461b5d555e5fe5 /dimension/tokenize.c
parent6aced77005b10311176a53d8db9862d4fff5dbc3 (diff)
downloaddimension-281a8741c0407e3cc504e98dee7a1e10782b46b2.tar.xz
Refactor tokenizer a bit.
Diffstat (limited to 'dimension/tokenize.c')
-rw-r--r--dimension/tokenize.c49
1 files changed, 23 insertions, 26 deletions
diff --git a/dimension/tokenize.c b/dimension/tokenize.c
index 4d53bdf..ec8c6ee 100644
--- a/dimension/tokenize.c
+++ b/dimension/tokenize.c
@@ -18,28 +18,14 @@
*************************************************************************/
#include "tokenize.h"
+#include "utility.h"
#include <stdlib.h> /* For strtoul(), etc. */
#include <string.h>
-#include <stdarg.h>
#include <ctype.h> /* For isalpha(), etc. */
#include <sys/mman.h> /* For mmap() */
#include <libgen.h> /* For dirname() */
#include <locale.h>
-static void
-dmnsn_diagnostic(const char *filename, unsigned int line, unsigned int col,
- const char *format, ...)
-{
- va_list ap;
- va_start(ap, format);
-
- fprintf(stderr, "%s:%u:%u: ", filename, line, col);
- vfprintf(stderr, format, ap);
- fprintf(stderr, "\n");
-
- va_end(ap);
-}
-
static int
dmnsn_tokenize_comment(const char *filename,
unsigned int *linep, unsigned int *colp,
@@ -596,31 +582,42 @@ dmnsn_delete_tokens(dmnsn_array *tokens)
dmnsn_delete_array(tokens);
}
-static const char *dmnsn_token_name(dmnsn_token_type token_type);
-
static void
-dmnsn_print_token(FILE *file, dmnsn_token *token)
+dmnsn_print_token(FILE *file, const dmnsn_token *token)
{
+ const char *tname;
+ if (token->type == DMNSN_T_LPAREN) {
+ tname = "\\(";
+ } else if (token->type == DMNSN_T_RPAREN) {
+ tname = "\\)";
+ } else {
+ tname = dmnsn_token_name(token->type);
+ }
+
if (token->value) {
- fprintf(file, "(%s \"%s\")", dmnsn_token_name(token->type), token->value);
+ fprintf(file, "(%s \"%s\")", tname, token->value);
} else {
- fprintf(file, "%s", dmnsn_token_name(token->type));
+ fprintf(file, "%s", tname);
}
}
void
-dmnsn_print_token_sexpr(FILE *file, dmnsn_array *tokens)
+dmnsn_print_token_sexpr(FILE *file, const dmnsn_array *tokens)
{
+ dmnsn_token token;
unsigned int i;
+
if (dmnsn_array_size(tokens) == 0) {
fprintf(file, "()");
} else {
fprintf(file, "(");
- dmnsn_print_token(file, dmnsn_array_at(tokens, 0));
+ dmnsn_array_get(tokens, 0, &token);
+ dmnsn_print_token(file, &token);
for (i = 1; i < dmnsn_array_size(tokens); ++i) {
fprintf(file, " ");
- dmnsn_print_token(file, dmnsn_array_at(tokens, i));
+ dmnsn_array_get(tokens, i, &token);
+ dmnsn_print_token(file, &token);
}
fprintf(file, ")");
@@ -629,7 +626,7 @@ dmnsn_print_token_sexpr(FILE *file, dmnsn_array *tokens)
fprintf(file, "\n");
}
-static const char *
+const char *
dmnsn_token_name(dmnsn_token_type token_type)
{
switch (token_type) {
@@ -641,8 +638,8 @@ dmnsn_token_name(dmnsn_token_type token_type)
/* Punctuation */
dmnsn_token_map(DMNSN_T_LBRACE, "{");
dmnsn_token_map(DMNSN_T_RBRACE, "}")
- dmnsn_token_map(DMNSN_T_LPAREN, "\\(");
- dmnsn_token_map(DMNSN_T_RPAREN, "\\)");
+ dmnsn_token_map(DMNSN_T_LPAREN, "(");
+ dmnsn_token_map(DMNSN_T_RPAREN, ")");
dmnsn_token_map(DMNSN_T_LBRACKET, "[");
dmnsn_token_map(DMNSN_T_RBRACKET, "]");
dmnsn_token_map(DMNSN_T_LT, "<");