2 * Copyright (c) 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 /* OVN lexical analyzer
21 * ====================
23 * This is a simple lexical analyzer (or tokenizer) for OVN match expressions
26 #include "meta-flow.h"
32 LEX_T_END, /* end of input */
34 /* Tokens with auxiliary data. */
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_ERROR, /* invalid input */
46 LEX_T_LSQUARE, /* [ */
47 LEX_T_RSQUARE, /* ] */
54 LEX_T_LOG_NOT, /* ! */
55 LEX_T_LOG_AND, /* && */
56 LEX_T_LOG_OR, /* || */
57 LEX_T_ELLIPSIS, /* .. */
59 LEX_T_SEMICOLON, /* ; */
61 LEX_T_EXCHANGE, /* <-> */
62 LEX_T_DECREMENT, /* -- */
65 /* Subtype for LEX_T_INTEGER and LEX_T_MASKED_INTEGER tokens.
67 * These do not change the semantics of a token; instead, they determine the
68 * format used when a token is serialized back to a text form. That's
69 * important because 3232268289 is meaningless to a human whereas 192.168.128.1
70 * has some actual significance. */
78 const char *lex_format_to_string(enum lex_format);
82 * 's' may point to 'buffer'; otherwise, it points to malloc()ed memory owned
85 enum lex_type type; /* One of LEX_*. */
86 char *s; /* LEX_T_ID, LEX_T_STRING, LEX_T_ERROR only. */
87 enum lex_format format; /* LEX_T_INTEGER, LEX_T_MASKED_INTEGER only. */
90 union mf_subvalue value; /* LEX_T_INTEGER, LEX_T_MASKED_INTEGER. */
91 union mf_subvalue mask; /* LEX_T_MASKED_INTEGER only. */
93 char buffer[256]; /* Buffer for LEX_T_ID/LEX_T_STRING. */
97 void lex_token_init(struct lex_token *);
98 void lex_token_destroy(struct lex_token *);
99 void lex_token_swap(struct lex_token *, struct lex_token *);
100 void lex_token_strcpy(struct lex_token *, const char *s, size_t length);
101 void lex_token_strset(struct lex_token *, char *s);
102 void lex_token_vsprintf(struct lex_token *, const char *format, va_list args);
104 void lex_token_format(const struct lex_token *, struct ds *);
105 const char *lex_token_parse(struct lex_token *, const char *input,
106 const char **startp);
108 /* A lexical analyzer. */
110 const char *input; /* Remaining input (not owned by lexer). */
111 const char *start; /* Start of current token in 'input'. */
112 struct lex_token token; /* Current token (owned by lexer). */
115 void lexer_init(struct lexer *, const char *input);
116 void lexer_destroy(struct lexer *);
118 enum lex_type lexer_get(struct lexer *);
119 enum lex_type lexer_lookahead(const struct lexer *);
120 bool lexer_match(struct lexer *, enum lex_type);
121 bool lexer_match_id(struct lexer *, const char *id);
122 bool lexer_is_int(const struct lexer *);
123 bool lexer_get_int(struct lexer *, int *value);
125 #endif /* ovn/lex.h */