Remove expensive assertion checks in st-array.h
[panda.git] / tests / test-lexer.c
blob24ae0614babc58d50467718ede90ff075d2aaf64
3 #include <st-lexer.h>
5 #include <stdio.h>
6 #include <stdbool.h>
8 static const char *const token_names[] = {
9 "TOKEN_INVALID",
10 "TOKEN_LPAREN",
11 "TOKEN_RPAREN",
12 "TOKEN_BLOCK_BEGIN",
13 "TOKEN_BLOCK_END",
14 "TOKEN_COMMA",
15 "TOKEN_SEMICOLON",
16 "TOKEN_PERIOD",
17 "TOKEN_RETURN",
18 "TOKEN_COLON",
19 "TOKEN_ASSIGN",
20 "TOKEN_TUPLE_BEGIN",
21 "TOKEN_IDENTIFIER",
22 "TOKEN_CHARACTER_CONST",
23 "TOKEN_STRING_CONST",
24 "TOKEN_NUMBER_CONST",
25 "TOKEN_SYMBOL_CONST",
26 "TOKEN_COMMENT",
27 "TOKEN_BINARY_SELECTOR",
28 "TOKEN_KEYWORD_SELECTOR",
29 "TOKEN_EOF",
32 static void
33 print_token (st_lexer *lexer, st_token *token)
35 st_token_type type;
37 char *string;
39 type = st_token_get_type (token);
41 switch (type) {
44 // tokens string values
45 case ST_TOKEN_COMMENT:
46 case ST_TOKEN_IDENTIFIER:
47 case ST_TOKEN_STRING_CONST:
48 case ST_TOKEN_SYMBOL_CONST:
49 case ST_TOKEN_NUMBER_CONST:
50 case ST_TOKEN_KEYWORD_SELECTOR:
51 case ST_TOKEN_BINARY_SELECTOR:
52 case ST_TOKEN_CHARACTER_CONST:
54 string = st_token_get_text (token);
56 printf ("%s (%i:%i: \"%s\")\n", token_names[type],
57 st_token_get_line (token), st_token_get_column (token), string);
58 break;
60 // Invalid Token;
61 case ST_TOKEN_INVALID:
63 printf ("%s\n", st_lexer_error_message (lexer));
64 break;
67 default:
68 printf ("%s (%i:%i)\n", token_names[type],
69 st_token_get_line (token), st_token_get_column (token));
70 break;
75 #define BUF_SIZE 10000
77 int
78 main (int argc, char *argv[])
80 st_lexer *lexer;
81 st_token *token;
83 printf ("Enter or pipe some Smalltalk code on stdin:\n\n");
85 /* read input from stdin */
86 char buffer[BUF_SIZE];
87 char c;
88 int i = 0;
89 while ((c = getchar ()) != EOF && i < (BUF_SIZE - 1))
90 buffer[i++] = c;
91 buffer[i] = '\0';
93 lexer = st_lexer_new (buffer);
95 st_token_type type;
97 do {
99 token = st_lexer_next_token (lexer);
100 type = st_token_get_type (token);
102 print_token (lexer, token);
104 } while (type != ST_TOKEN_EOF);
106 return 0;