]> git.proxmox.com Git - ovs.git/blob - ovn/lib/lex.h
ovn: Support multiple addresses on a single logical router port.
[ovs.git] / ovn / lib / lex.h
1 /*
2 * Copyright (c) 2015, 2016 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef OVN_LEX_H
18 #define OVN_LEX_H 1
19
20 /* OVN lexical analyzer
21 * ====================
22 *
23 * This is a simple lexical analyzer (or tokenizer) for OVN match expressions
24 * and ACLs. */
25
26 #include "openvswitch/meta-flow.h"
27
28 struct ds;
29
30 /* Token type. */
31 enum lex_type {
32 LEX_T_END, /* end of input */
33
34 /* Tokens with auxiliary data. */
35 LEX_T_ID, /* foo */
36 LEX_T_STRING, /* "foo" */
37 LEX_T_INTEGER, /* 12345 or 1.2.3.4 or ::1 or 01:02:03:04:05 */
38 LEX_T_MASKED_INTEGER, /* 12345/10 or 1.2.0.0/16 or ::2/127 or... */
39 LEX_T_MACRO, /* $NAME */
40 LEX_T_ERROR, /* invalid input */
41
42 /* Bare tokens. */
43 LEX_T_LPAREN, /* ( */
44 LEX_T_RPAREN, /* ) */
45 LEX_T_LCURLY, /* { */
46 LEX_T_RCURLY, /* } */
47 LEX_T_LSQUARE, /* [ */
48 LEX_T_RSQUARE, /* ] */
49 LEX_T_EQ, /* == */
50 LEX_T_NE, /* != */
51 LEX_T_LT, /* < */
52 LEX_T_LE, /* <= */
53 LEX_T_GT, /* > */
54 LEX_T_GE, /* >= */
55 LEX_T_LOG_NOT, /* ! */
56 LEX_T_LOG_AND, /* && */
57 LEX_T_LOG_OR, /* || */
58 LEX_T_ELLIPSIS, /* .. */
59 LEX_T_COMMA, /* , */
60 LEX_T_SEMICOLON, /* ; */
61 LEX_T_EQUALS, /* = */
62 LEX_T_EXCHANGE, /* <-> */
63 LEX_T_DECREMENT, /* -- */
64 LEX_T_COLON, /* : */
65 };
66
67 /* Subtype for LEX_T_INTEGER and LEX_T_MASKED_INTEGER tokens.
68 *
69 * These do not change the semantics of a token; instead, they determine the
70 * format used when a token is serialized back to a text form. That's
71 * important because 3232268289 is meaningless to a human whereas 192.168.128.1
72 * has some actual significance. */
73 enum lex_format {
74 LEX_F_DECIMAL,
75 LEX_F_HEXADECIMAL,
76 LEX_F_IPV4,
77 LEX_F_IPV6,
78 LEX_F_ETHERNET,
79 };
80 const char *lex_format_to_string(enum lex_format);
81
82 /* A token. */
83 struct lex_token {
84 /* One of LEX_*. */
85 enum lex_type type;
86
87 /* Meaningful for LEX_T_ID, LEX_T_STRING, LEX_T_ERROR, LEX_T_MACRO only.
88 * For these token types, 's' may point to 'buffer'; otherwise, it points
89 * to malloc()ed memory owned by the token.
90 *
91 * Must be NULL for other token types.
92 *
93 * For LEX_T_MACRO, 's' does not include the leading $. */
94 char *s;
95
96 /* LEX_T_INTEGER, LEX_T_MASKED_INTEGER only. */
97 enum lex_format format;
98
99 union {
100 /* LEX_T_INTEGER, LEX_T_MASKED_INTEGER only. */
101 struct {
102 union mf_subvalue value; /* LEX_T_INTEGER, LEX_T_MASKED_INTEGER. */
103 union mf_subvalue mask; /* LEX_T_MASKED_INTEGER only. */
104 };
105
106 /* LEX_T_ID, LEX_T_STRING, LEX_T_ERROR, LEX_T_MACRO only. */
107 char buffer[256];
108 };
109 };
110
111 void lex_token_init(struct lex_token *);
112 void lex_token_destroy(struct lex_token *);
113 void lex_token_swap(struct lex_token *, struct lex_token *);
114 void lex_token_strcpy(struct lex_token *, const char *s, size_t length);
115 void lex_token_strset(struct lex_token *, char *s);
116 void lex_token_vsprintf(struct lex_token *, const char *format, va_list args);
117
118 void lex_token_format(const struct lex_token *, struct ds *);
119 const char *lex_token_parse(struct lex_token *, const char *input,
120 const char **startp);
121
122 /* A lexical analyzer. */
123 struct lexer {
124 const char *input; /* Remaining input (not owned by lexer). */
125 const char *start; /* Start of current token in 'input'. */
126 struct lex_token token; /* Current token (owned by lexer). */
127 };
128
129 void lexer_init(struct lexer *, const char *input);
130 void lexer_destroy(struct lexer *);
131
132 enum lex_type lexer_get(struct lexer *);
133 enum lex_type lexer_lookahead(const struct lexer *);
134 bool lexer_match(struct lexer *, enum lex_type);
135 bool lexer_match_id(struct lexer *, const char *id);
136 bool lexer_is_int(const struct lexer *);
137 bool lexer_get_int(struct lexer *, int *value);
138
139 #endif /* ovn/lex.h */