aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@home.transmeta.com>2003-03-13 12:53:56 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-07 20:59:14 -0700
commit3ece2ef7c0a3d5975f65aa09911e1944e4125c45 (patch)
treeaf5b6217bec44bbad283f7d35c8959f1c1ed930b /test-lexing.c
downloadsparse-3ece2ef7c0a3d5975f65aa09911e1944e4125c45.tar.gz
sparse-3ece2ef7c0a3d5975f65aa09911e1944e4125c45.tar.bz2
sparse-3ece2ef7c0a3d5975f65aa09911e1944e4125c45.zip
Yaah. I'm a retard, but I want to at least try to see how hard it is
to do a semantic parser that is smaller than gcc is. Right now this is just the lexer, though, along with a test app to print the results back out to verify the thing.
Diffstat (limited to 'test-lexing.c')
-rw-r--r--test-lexing.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/test-lexing.c b/test-lexing.c
new file mode 100644
index 0000000..419a3bc
--- /dev/null
+++ b/test-lexing.c
@@ -0,0 +1,126 @@
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "token.h"
+
+void die(const char *fmt, ...)
+{
+ va_list args;
+ static char buffer[512];
+
+ va_start(args, fmt);
+ vsnprintf(buffer, sizeof(buffer), fmt, args);
+ va_end(args);
+
+ fprintf(stderr, "%s\n", buffer);
+ exit(1);
+}
+
+static char *show_value(struct value *value)
+{
+ static char buffer[256];
+
+ switch (value->type) {
+ case TOKEN_ERROR:
+ return "syntax error";
+
+ case TOKEN_IDENT: {
+ struct ident *ident = value->ident;
+ sprintf(buffer, "%.*s", ident->len, ident->name);
+ return buffer;
+ }
+
+ case TOKEN_STRING: {
+ char *ptr;
+ int i;
+ struct string *string = value->string;
+
+ ptr = buffer;
+ *ptr++ = '"';
+ for (i = 0; i < string->length; i++) {
+ unsigned char c = string->data[i];
+ if (isprint(c) && c != '"') {
+ *ptr++ = c;
+ continue;
+ }
+ *ptr++ = '\\';
+ switch (c) {
+ case '\n':
+ *ptr++ = 'n';
+ continue;
+ case '\t':
+ *ptr++ = 't';
+ continue;
+ case '"':
+ *ptr++ = '"';
+ continue;
+ }
+ if (!isdigit(string->data[i+1])) {
+ ptr += sprintf(ptr, "%o", c);
+ continue;
+ }
+
+ ptr += sprintf(ptr, "%03o", c);
+ }
+ *ptr++ = '"';
+ *ptr = '\0';
+ return buffer;
+ }
+
+ case TOKEN_INTEGER: {
+ char *ptr;
+ ptr = buffer + sprintf(buffer, "%llu", value->intval);
+ return buffer;
+ }
+
+ case TOKEN_FP: {
+ sprintf(buffer, "%f", value->fpval);
+ return buffer;
+ }
+
+ case TOKEN_SPECIAL: {
+ int val = value->special;
+ static const char *combinations[] = COMBINATION_STRINGS;
+ buffer[0] = val;
+ buffer[1] = 0;
+ if (val >= SPECIAL_BASE)
+ strcpy(buffer, combinations[val - SPECIAL_BASE]);
+ return buffer;
+ }
+
+ default:
+ return "WTF???";
+ }
+}
+
+void callback(struct token *token)
+{
+ printf("%s ", show_value(&token->value));
+}
+
+int main(int argc, char **argv)
+{
+ int line;
+ int fd = open(argv[1], O_RDONLY);
+ struct token *token;
+
+ if (fd < 0)
+ die("No such file: %s", argv[1]);
+ token = tokenize(argv[1], fd);
+ line = token->line;
+ while (token) {
+ callback(token);
+ token = token->next;
+ if (token && token->line != line) {
+ line = token->line;
+ putchar('\n');
+ }
+ }
+ putchar('\n');
+ print_ident_stat();
+ return 0;
+}