docs: todo: update name assignments
[netsniff-ng.git] / src / bpf_hla_lexer.l
blobb895692dc8356c6ae97cfa661ec20d1e57264489
1 /*
2  * netsniff-ng - the packet sniffing beast
3  * By Daniel Borkmann <daniel@netsniff-ng.org>
4  * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5  * Swiss federal institute of technology (ETH Zurich)
6  * Subject to the GPL, version 2.
7  */
9 %{
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
16 #include "bpf_hla_parser.tab.h"
17 #include "bpf_symtab.h"
18 #include "built_in.h"
20 void bpf_hla_lex_init(void);
22 struct res_keywords {
23         const char *key;
24         int token;
27 #define KEYPAIR(k,t)    { .key = (k), .token = (t)}
28 static const struct res_keywords keytab[] = {
29         KEYPAIR("def",  K_DEF),
30         KEYPAIR("pkt",  K_PKT),
31         KEYPAIR("ret",  K_RET),
32         KEYPAIR("if",   K_IF),
33         KEYPAIR("elif", K_ELIF),
34         KEYPAIR("else", K_ELSE),
37 void bpf_hla_lex_init(void)
39         int i;
40         for (i = 0; i < array_size(keytab); ++i)
41                 bpf_symtab_insert(keytab[i].key, keytab[i].token);
44 #define YY_USER_INIT    bpf_hla_lex_init();
46 extern void zzerror(const char *);
50 %option align
51 %option nounput
52 %option noyywrap
53 %option noreject
54 %option 8bit
55 %option caseless
56 %option noinput
57 %option nodefault
59 alpha           [A-Za-z]
60 alphanum        [A-Za-z0-9]
62 digit           [0-9]
63 digit_s         [1-9]
64 digit_n         [0]
65 bindigit        [0-1]
66 bindigit_b      [b]
67 hex             [a-fA-F0-9]
68 hex_x           [x]
70 number_dec      {digit_n}|{digit_s}{digit}*
71 number_oct      {digit_n}{digit}+
72 number_hex      {digit_n}{hex_x}{hex}+
73 number_bin      {digit_n}{bindigit_b}{bindigit}+
77 {alpha}{alphanum}* {
78                 zzlval.idx = bpf_symtab_find(zztext);
79                 if (zzlval.idx < 0) {
80                         zzlval.idx = bpf_symtab_insert(zztext, K_NAME);
81                         return K_NAME;
82                   } else {
83                         return bpf_symtab_type(zzlval.idx);
84                 }}
86 "("             { return '('; }
87 ")"             { return ')'; }
88 "{"             { return '{'; }
89 "}"             { return '}'; }
90 "="             { return '='; }
91 ";"             { return ';'; }
92 ","             { return ','; }
93 "+"             { return '+'; }
94 "-"             { return '-'; }
95 "&"             { return '&'; }
96 "|"             { return '|'; }
97 "^"             { return '^'; }
98 "!"             { return '!'; }
99 "<"             { return '<'; }
100 ">"             { return '>'; }
101 "*"             { return '*'; }
102 "/"             { return '/'; }
103 "%"             { return '%'; }
105 {number_hex}    { zzlval.number = strtoul(zztext, NULL, 16);
106                   return number_hex; }
108 {number_dec}    { zzlval.number = strtol(zztext, NULL, 10);
109                   return number_dec; }
111 {number_oct}    { zzlval.number = strtol(zztext, NULL, 8);
112                   return number_oct; }
114 {number_bin}    { zzlval.number = strtol(zztext + 2, NULL, 2);
115                   return number_bin; }
117 "/*"([^\*]|\*[^/])*"*/" { /* NOP */ }
118 ";"[^\n]*       {/* NOP */}
119 "\n"            { zzlineno++; }
120 [ \t]+          {/* NOP */ }
121 .               { printf("Unknown character '%s'", zztext);
122                   zzerror("lex Unknown character"); }