summaryrefslogtreecommitdiffabout
path: root/src/belex.l
blob: f35f114288aa50dbb985d4ba7d3621964d1cb725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
%option nounput
%option noinput

%{
#include "backend.h"
	
enum {
	T_BOGUS = 256,
	T_IDENT,
	T_NUMBER,
	T_STRING,
	T_BACKEND,
	T_HOST,
	T_PORT
};

static char const *input_string;
static size_t input_len;
static size_t input_pos;
static char const *string_start;
static size_t current_pos;

#define YY_INPUT(buf,result,max_size) \
	do {						\
		size_t n = input_len - input_pos;	\
		if (n > max_size)			\
			n = max_size;			\
		memcpy(buf, input_string, n);		\
		input_pos += n;				\
		result = n;				\
	} while (0)

#define YY_USER_ACTION				\
	current_pos += yyleng;

#define YY_DECL static int yylex(void)
static int yywrap(void);

static char const *
input_point(void)
{
    return input_string + current_pos;
}

#define YYSTYPE be_string_t
#define YYSTYPE_INITIALIZER { NULL, 0 }

static YYSTYPE yylval;
%}

%x COMMENT STR

%%
         /* C-style comments */
"/*"                    BEGIN(COMMENT);
<COMMENT>[^*]*          /* eat anything that's not a '*' */
<COMMENT>"*"+[^*/]*     /* eat up '*'s not followed by '/'s */
<COMMENT>"*"+"/"        BEGIN(INITIAL);

         /* Single-line comments */
"//".*\n                ;
#.*\n                   ;

         /* Multi-line strings */
"{\""                   { BEGIN(STR); string_start = input_point(); }
<STR>[^\"]*             /* eat anything that's not a '"' */
<STR>"\""+[^"}]*        /* eat up '"'s not followed by '}'s */
<STR>"\""+"}"           { BEGIN(INITIAL);
                          yylval.start = string_start;
                          yylval.len = input_point() - yylval.start - 2;
                          return T_STRING;
                        }
         /* Single-line strings */
\".*\"                  {
                            yylval.start = input_point() - yyleng + 1;
                            yylval.len = yyleng - 2;
                            return T_STRING;
                        }
backend                 return T_BACKEND;
".host"                 return T_HOST;
".port"                 return T_PORT;
"{"|"}"|"="|";"         return yytext[0];

[a-zA-Z_][a-zA-Z0-9_.]+ {
                            yylval.start = input_point() - yyleng;
                            yylval.len = yyleng;
                            return T_IDENT;
                        }
[0-9]+                  return T_NUMBER;

[ \t\n]+                ;

.                       return T_BOGUS;

%%
int
yywrap(void)
{
    return 1;
}

static void
set_input(char const *str, size_t len)
{
    input_string = str;
    input_len = len;
    input_pos = 0;
    current_pos = 0;
}

static int brace_nesting;

static void
read_backend(regfun_t regfun, void *d)
{
    int c;
    int in_statement;
    YYSTYPE label, host = YYSTYPE_INITIALIZER, port = YYSTYPE_INITIALIZER;

    if ((c = yylex()) != T_IDENT && c != T_STRING)
        return;
    label = yylval;
    if ((c = yylex()) != '{')
        return;
    brace_nesting++;
    in_statement = 0;
    while (brace_nesting == 1) {
       c = yylex();
       if (c == 0)
           break;
       else if (c == '{')
           brace_nesting++;
       else if (c == '}')
           brace_nesting++;
       else if (in_statement) {
           if (c == ';')
               in_statement = 0;
       } else {
           in_statement = 1;
           if (c == T_HOST || c == T_PORT) {
               YYSTYPE *stk = c == T_HOST ? &host : &port;
               in_statement = 1;
               if ((c = yylex()) == '=') {
                  c = yylex();
                  if (c == T_STRING || c == T_IDENT) {
                      *stk = yylval;
                  }
               }
           }
       }
    }
    regfun(&label, &host, &port, d);
}

void
backend_parser(const char *str, size_t len, regfun_t regfun, void *d)
{
    int c;

    set_input(str, len);

    brace_nesting = 0;
    while ((c = yylex())) {
        if (brace_nesting == 0) {
            if (c == T_BACKEND) {
                 read_backend(regfun, d); 
            }
        }
        switch (c) {
        case '{':
            brace_nesting++;
            break;
        case '}':
            brace_nesting--;
            break;
        }
    }
}

Return to:

Send suggestions and report system problems to the System administrator.