Lexer actually lexes filenames now, and odats are made out of mapvariants
[henge/webcc.git] / src / apc / lexer_lex.rl
1 /* Ragel State Machine for tokenizing text */
2 #include <stdio.h>
3 #include <string.h>
4 #include <apc/parser.tab.h>
5
6
7 extern void lexer_pushtok(int, YYSTYPE);
8 //extern char* scanner_current_file();
9 extern int lexer_ismapfile(const char*, int);
10 extern int lexer_tokfile(const char*, int);
11
12
13 int lexer_lex(const char*);
14 int ipow(int, int);
15 int ttov(const uint8_t* , int);
16 uint64_t ttor(const uint8_t* , int);
17 char* ttos(const uint8_t* , int);
18
19
20 #define MAX_TOK_LEN 64
21 #define MAX_TOKENS 16
22 #define MAX_STR_SIZE (MAX_TOK_LEN * MAX_TOKENS)
23
24 /* Scan filename and push the its tokens
25 onto the stack */
26 int lexer_lex (const char* str)
27 {
28 int ntok, len;
29
30
31 len = strlen(str);
32 printf("Calling lexer_lex on %s\n", str);
33
34
35 return ntok;
36 }
37
38 int ipow(int base, int exp)
39 {
40 int result = 1;
41 while (exp)
42 {
43 if (exp & 1)
44 result = result * base;
45 exp = exp >> 1;
46 base *= base;
47 }
48
49 return result;
50 }
51
52 /* Token to Value */
53 int ttov(const uint8_t* str, int len)
54 {
55 int i, val = 0;
56
57 for (i = 0; i < len; i++)
58 {
59 val += ((str[len - (i + 1)] - '0') * ipow(10,i));
60 }
61
62 return val;
63 }
64
65 uint64_t ttor(const uint8_t* str, int len)
66 {
67 int i;
68 uint64_t num = 0;
69
70 for (i = 0; i < len; i++)
71 {
72 num += ((str[len - (i + 1)] - '0') * ipow(10,i));
73 }
74
75 return num;
76 }
77
78 char* ttos(const uint8_t* str, int len)
79 {
80 int i;
81 char token_buf[MAX_TOK_LEN];
82
83 memmove(token_buf, str, len);
84 token_buf[len+1] = '\0';
85
86 return strdup(token_buf);
87 }