• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1%{
2
3#include <string>
4#include <map>
5#include <iostream>
6
7#include "declarations.h"
8#include "language_y.h"
9
10#ifndef YYSTYPE
11#define YYSTYPE yy::parser::semantic_type
12#endif
13
14#ifndef YYLTYPE
15#define YYLTYPE yy::parser::location_type
16#endif
17
18using token = yy::parser::token;
19
20#define YY_USER_ACTION yylloc->step(); yylloc->columns(yyleng);
21
22%}
23
24%option debug
25
26%option yylineno
27%option noyywrap
28%option nounput
29%option noinput
30%option reentrant
31%option bison-bridge
32%option bison-locations
33
34identifier [a-zA-Z][_a-zA-Z0-9]*
35size_modifier [+*/-][ +*/\-0-9]*
36intvalue (0|[1-9][0-9]*)
37hexvalue 0[x|X][0-9a-fA-F]+
38string_literal \".*\"
39
40%x COMMENT_STATE
41
42%%
43  /* NOTE:
44   * Rule ordering is important in order to establist priority. Some
45   * rules are a superset of other rules and will cause the sub rules to
46   * never match. Ex. Keywords must always go before identifiers, otherwise
47   * all keywords will be treated as an identifier.
48   */
49
50  /* Block Comment */
51"/*"                    { BEGIN(COMMENT_STATE); }
52<COMMENT_STATE>"*/"     { BEGIN(INITIAL); }
53<COMMENT_STATE>[\n]+    { yylloc->lines(yyleng); }
54<COMMENT_STATE>.        { /* do nothing */ }
55
56  /* Line Comment */
57"//"[^\r\n]*            { /* do nothing */ }
58
59  /* Begin reserved keyword definitions */
60  /* Fields */
61"_body_"                { return(token::BODY); }
62"_payload_"             { return(token::PAYLOAD); }
63"_size_"                { return(token::SIZE); }
64"_count_"               { return(token::COUNT); }
65"_fixed_"               { return(token::FIXED); }
66"_reserved_"            { return(token::RESERVED); }
67"_checksum_start_"      { return(token::CHECKSUM_START); }
68"_padding_"             { return(token::PADDING); }
69  /* Types */
70"checksum"              { return(token::CHECKSUM); }
71"custom_field"          { return(token::CUSTOM_FIELD); }
72"enum"                  { return(token::ENUM); }
73"group"                 { return(token::GROUP); }
74"packet"                { return(token::PACKET); }
75"test"                  { return(token::TEST); }
76"struct"                { return(token::STRUCT); }
77"little_endian_packets" {
78                          yylval->integer = 1;
79                          return token::IS_LITTLE_ENDIAN;
80                        }
81"big_endian_packets"    {
82                          yylval->integer = 0;
83                          return token::IS_LITTLE_ENDIAN;
84                        }
85
86  /* Begin identifier definitions */
87{string_literal}        {
88                          std::string with_quotes = std::string(yytext);
89                          yylval->string = new std::string(with_quotes.begin() + 1, with_quotes.end() - 1);
90                          return token::STRING;
91                        }
92
93{size_modifier}         {
94                          yylval->string = new std::string(yytext);
95                          return token::SIZE_MODIFIER;
96                        }
97
98{identifier}            {
99                          yylval->string = new std::string(yytext);
100                          return token::IDENTIFIER;
101                        }
102
103{intvalue}              {
104                          yylval->integer = std::stoull(std::string(yytext), nullptr, 10);
105                          return token::INTEGER;
106                        }
107
108{hexvalue}              {
109                          yylval->integer = std::stoull(std::string(yytext), nullptr, 16);
110                          return token::INTEGER;
111                        }
112
113  /* Begin token definitions */
114":"            { return(':'); }
115"{"            { return('{'); }
116"}"            { return('}'); }
117"["            { return('['); }
118"]"            { return(']'); }
119"("            { return('('); }
120")"            { return(')'); }
121"<"            { return('<'); }
122">"            { return('>'); }
123"="            { return('='); }
124","            { return(','); }
125
126(\n|\r\n)+     { yylloc->lines(yyleng); }
127[ \t\f\v]+     { /* Ignore all other whitespace */ }
128
129%%
130
131