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