2017-09-23 13:05:21 +00:00
|
|
|
%top {
|
|
|
|
/* Include this before everything else, for various large-file definitions */
|
|
|
|
#include "config.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We want a reentrant scanner.
|
|
|
|
*/
|
|
|
|
%option reentrant
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't read interactively from the terminal.
|
|
|
|
*/
|
|
|
|
%option never-interactive
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to stop processing when we get to the end of the input.
|
|
|
|
*/
|
|
|
|
%option noyywrap
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The type for the state we keep for the scanner (and parser).
|
|
|
|
*/
|
|
|
|
%option extra-type="protobuf_lang_state_t *"
|
|
|
|
|
|
|
|
/*
|
2020-11-17 12:48:00 +00:00
|
|
|
* Prefix scanner routines with "protobuf_lang_" rather than "yy", so this scanner
|
2017-09-23 13:05:21 +00:00
|
|
|
* can coexist with other scanners.
|
|
|
|
*/
|
2020-11-17 12:48:00 +00:00
|
|
|
%option prefix="protobuf_lang_"
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to override the memory allocators so that we don't get
|
|
|
|
* "unused argument" warnings from the yyscanner argument (which
|
|
|
|
* we don't use, as we have a global memory allocator).
|
|
|
|
*
|
|
|
|
* We provide, as macros, our own versions of the routines generated by Flex,
|
|
|
|
* which just call malloc()/realloc()/free() (as the Flex versions do),
|
|
|
|
* discarding the extra argument.
|
|
|
|
*/
|
|
|
|
%option noyyalloc
|
|
|
|
%option noyyrealloc
|
|
|
|
%option noyyfree
|
|
|
|
%option yylineno
|
|
|
|
%option noinput
|
|
|
|
%option nounput
|
|
|
|
|
|
|
|
%{
|
|
|
|
/* protobuf_lang_scanner.l
|
|
|
|
*
|
|
|
|
* C Protocol Buffers Language Lexer (for *.proto files)
|
2020-11-17 12:48:00 +00:00
|
|
|
* Copyright 2020, Huang Qiangxiong <qiangxiong.huang@qq.com>
|
2017-09-23 13:05:21 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include "protobuf_lang_tree.h"
|
2020-11-17 12:48:00 +00:00
|
|
|
#include "protobuf_lang_parser.h"
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable diagnostics in the code generated by Flex.
|
|
|
|
*/
|
|
|
|
DIAG_OFF_FLEX
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sleazy hack to suppress compiler warnings in yy_fatal_error().
|
|
|
|
*/
|
|
|
|
#define YY_EXIT_FAILURE ((void)yyscanner, 2)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Macros for the allocators, to discard the extra argument.
|
|
|
|
*/
|
2020-11-17 12:48:00 +00:00
|
|
|
#define protobuf_lang_alloc(size, yyscanner) (void *)malloc(size)
|
|
|
|
#define protobuf_lang_realloc(ptr, size, yyscanner) (void *)realloc((char *)(ptr), (size))
|
|
|
|
#define protobuf_lang_free(ptr, yyscanner) free((char *)ptr)
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
int old_status;
|
|
|
|
|
2020-11-17 12:48:00 +00:00
|
|
|
/* Extended error handling function defined in protobuf_lang_grammar.lemon */
|
|
|
|
void pbl_parser_error(protobuf_lang_state_t *state, const char *fmt, ...);
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
/* duplicate the text and keep the pointer in parser state for freeing later automatically */
|
|
|
|
static gchar*
|
|
|
|
strdup_and_store(void* yyscanner, const char* text);
|
|
|
|
|
2020-11-17 12:48:00 +00:00
|
|
|
#define PROTOBUF_LANG_PARSE(token_type) \
|
|
|
|
protobuf_lang_get_extra(yyscanner)->tmp_token = g_new0(protobuf_lang_token_t, 1); \
|
2021-03-27 16:34:06 +00:00
|
|
|
pbl_store_struct_token(protobuf_lang_get_extra(yyscanner), protobuf_lang_get_extra(yyscanner)->tmp_token); \
|
2020-11-17 12:48:00 +00:00
|
|
|
protobuf_lang_get_extra(yyscanner)->tmp_token->v = strdup_and_store(yyscanner, yytext); \
|
|
|
|
protobuf_lang_get_extra(yyscanner)->tmp_token->ln = protobuf_lang_get_lineno(yyscanner); \
|
|
|
|
return (token_type);
|
|
|
|
|
2017-09-23 13:05:21 +00:00
|
|
|
%}
|
|
|
|
|
|
|
|
%x COMMENT
|
|
|
|
|
|
|
|
%%
|
|
|
|
/* operations or symbols (PT_ means PBL Token) */
|
2020-11-17 12:48:00 +00:00
|
|
|
"(" PROTOBUF_LANG_PARSE(PT_LPAREN);
|
|
|
|
")" PROTOBUF_LANG_PARSE(PT_RPAREN);
|
|
|
|
"[" PROTOBUF_LANG_PARSE(PT_LBRACKET);
|
|
|
|
"]" PROTOBUF_LANG_PARSE(PT_RBRACKET);
|
|
|
|
"{" PROTOBUF_LANG_PARSE(PT_LCURLY);
|
|
|
|
"}" PROTOBUF_LANG_PARSE(PT_RCURLY);
|
|
|
|
"==" PROTOBUF_LANG_PARSE(PT_EQUAL);
|
|
|
|
"!=" PROTOBUF_LANG_PARSE(PT_NOTEQUAL);
|
|
|
|
"<>" PROTOBUF_LANG_PARSE(PT_NOTEQUAL2);
|
|
|
|
">=" PROTOBUF_LANG_PARSE(PT_GEQUAL);
|
|
|
|
"<=" PROTOBUF_LANG_PARSE(PT_LEQUAL);
|
|
|
|
"+=" PROTOBUF_LANG_PARSE(PT_ASSIGN_PLUS);
|
|
|
|
"=" PROTOBUF_LANG_PARSE(PT_ASSIGN);
|
|
|
|
"+" PROTOBUF_LANG_PARSE(PT_PLUS);
|
|
|
|
"-" PROTOBUF_LANG_PARSE(PT_MINUS);
|
|
|
|
"*" PROTOBUF_LANG_PARSE(PT_MULTIPLY);
|
|
|
|
"/" PROTOBUF_LANG_PARSE(PT_DIV);
|
|
|
|
"||" PROTOBUF_LANG_PARSE(PT_LOGIC_OR);
|
|
|
|
"|" PROTOBUF_LANG_PARSE(PT_OR);
|
|
|
|
"&&" PROTOBUF_LANG_PARSE(PT_LOGIC_AND);
|
|
|
|
"&" PROTOBUF_LANG_PARSE(PT_AND);
|
|
|
|
"!" PROTOBUF_LANG_PARSE(PT_NOT);
|
|
|
|
"~" PROTOBUF_LANG_PARSE(PT_NEG);
|
|
|
|
"^" PROTOBUF_LANG_PARSE(PT_XOR);
|
|
|
|
"<<" PROTOBUF_LANG_PARSE(PT_SHL);
|
|
|
|
">>" PROTOBUF_LANG_PARSE(PT_SHR);
|
|
|
|
"%" PROTOBUF_LANG_PARSE(PT_PERCENT);
|
|
|
|
"$" PROTOBUF_LANG_PARSE(PT_DOLLAR);
|
|
|
|
"?" PROTOBUF_LANG_PARSE(PT_COND);
|
|
|
|
";" PROTOBUF_LANG_PARSE(PT_SEMICOLON);
|
|
|
|
"." PROTOBUF_LANG_PARSE(PT_DOT);
|
|
|
|
"," PROTOBUF_LANG_PARSE(PT_COMMA);
|
|
|
|
":" PROTOBUF_LANG_PARSE(PT_COLON);
|
|
|
|
"<" PROTOBUF_LANG_PARSE(PT_LESS);
|
|
|
|
">" PROTOBUF_LANG_PARSE(PT_GREATER);
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
/* key words */
|
2020-11-17 12:48:00 +00:00
|
|
|
syntax PROTOBUF_LANG_PARSE(PT_SYNTAX);
|
|
|
|
import PROTOBUF_LANG_PARSE(PT_IMPORT);
|
|
|
|
weak PROTOBUF_LANG_PARSE(PT_WEAK);
|
|
|
|
public PROTOBUF_LANG_PARSE(PT_PUBLIC);
|
|
|
|
package PROTOBUF_LANG_PARSE(PT_PACKAGE);
|
|
|
|
option PROTOBUF_LANG_PARSE(PT_OPTION);
|
|
|
|
required PROTOBUF_LANG_PARSE(PT_REQUIRED);
|
|
|
|
optional PROTOBUF_LANG_PARSE(PT_OPTIONAL);
|
|
|
|
repeated PROTOBUF_LANG_PARSE(PT_REPEATED);
|
|
|
|
oneof PROTOBUF_LANG_PARSE(PT_ONEOF);
|
|
|
|
map PROTOBUF_LANG_PARSE(PT_MAP);
|
|
|
|
reserved PROTOBUF_LANG_PARSE(PT_RESERVED);
|
|
|
|
enum PROTOBUF_LANG_PARSE(PT_ENUM);
|
|
|
|
group PROTOBUF_LANG_PARSE(PT_GROUP);
|
|
|
|
extend PROTOBUF_LANG_PARSE(PT_EXTEND);
|
|
|
|
extensions PROTOBUF_LANG_PARSE(PT_EXTENSIONS);
|
|
|
|
message PROTOBUF_LANG_PARSE(PT_MESSAGE);
|
|
|
|
service PROTOBUF_LANG_PARSE(PT_SERVICE);
|
|
|
|
rpc PROTOBUF_LANG_PARSE(PT_RPC);
|
|
|
|
stream PROTOBUF_LANG_PARSE(PT_STREAM);
|
|
|
|
returns PROTOBUF_LANG_PARSE(PT_RETURNS);
|
|
|
|
to PROTOBUF_LANG_PARSE(PT_TO);
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
/* intLit values */
|
2020-11-17 12:48:00 +00:00
|
|
|
0|[1-9][0-9]* PROTOBUF_LANG_PARSE(PT_DECIMALLIT);
|
|
|
|
0[0-7]* PROTOBUF_LANG_PARSE(PT_OCTALLIT);
|
|
|
|
0[xX][0-9a-fA-F]+ PROTOBUF_LANG_PARSE(PT_HEXLIT);
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
/* Using extended identifier because we care only about position */
|
2020-11-17 12:48:00 +00:00
|
|
|
[a-zA-Z0-9_.][a-zA-Z0-9_.+-]* PROTOBUF_LANG_PARSE(PT_IDENT);
|
|
|
|
\"(\\.|\"\"|[^"\n"])*\" PROTOBUF_LANG_PARSE(PT_STRLIT);
|
|
|
|
\'(\\.|\'\'|[^"\n"])*\' PROTOBUF_LANG_PARSE(PT_STRLIT);
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
/* comments */
|
2019-10-08 15:35:34 +00:00
|
|
|
"//"[^\r\n]*
|
2017-09-23 13:05:21 +00:00
|
|
|
"/*" { old_status = YY_START; BEGIN COMMENT; }
|
|
|
|
<COMMENT>"*/" { BEGIN old_status; }
|
|
|
|
<COMMENT>([^*]|\n)+|.
|
|
|
|
|
|
|
|
/* space & tab */
|
2019-10-08 15:35:34 +00:00
|
|
|
[ \t\r\n]
|
2017-09-23 13:05:21 +00:00
|
|
|
/* prevent flex jam */
|
2020-11-17 12:48:00 +00:00
|
|
|
. { pbl_parser_error(protobuf_lang_get_extra(yyscanner), "unexpected token in proto file!\n"); }
|
2017-09-23 13:05:21 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
static gchar*
|
|
|
|
strdup_and_store(void* yyscanner, const char* text) {
|
2020-11-17 12:48:00 +00:00
|
|
|
return pbl_store_string_token(protobuf_lang_get_extra(yyscanner), g_strdup(text));
|
2017-09-23 13:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn diagnostics back on, so we check the code that we've written.
|
|
|
|
*/
|
2020-11-04 12:38:23 +00:00
|
|
|
DIAG_ON_FLEX
|