xaizek / zograscope (License: AGPLv3 only) (since 2018-12-07)
Mainly a syntax-aware diff that also provides a number of additional tools.
<root> / src / c / c11-lexer.flex (936bafefe4f07fe46ccb9d18d910e3e8beff6a14) (16KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
/* Copyright (C) 2017 xaizek <xaizek@posteo.net>
 *
 * This file is part of zograscope.
 *
 * zograscope is free software: you can redistribute it and/or modify
 * it under the terms of version 3 of the GNU Affero General Public License as
 * published by the Free Software Foundation.
 *
 * zograscope is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with zograscope.  If not, see <http://www.gnu.org/licenses/>.
 */

%option bison-bridge
%option bison-locations
%option reentrant
%option noyywrap
%option extra-type="struct C11LexerData *"
%option prefix="c11_"

%{

#include <iostream>
#include <locale>
#include <string>

#include "c/C11LexerData.hpp"
#include "c/C11SType.hpp"
#include "c/c11-parser.gen.hpp"
#include "TreeBuilder.hpp"

#define YYSTYPE C11_STYPE
#define YYLTYPE C11_LTYPE

#define YY_INPUT(buf, result, maxSize) \
    do { (result) = yyextra->readInput((buf), (maxSize)); } while (false)

#define YY_USER_ACTION \
    yylval->text = { }; \
    yylval->text.from = yyextra->offset; \
    yylval->text.len = yyleng; \
    yylloc->first_line = yyextra->line; \
    yylloc->first_column = yyextra->col; \
    yylloc->last_line = yyextra->line; \
    yylloc->last_column = yyextra->col + yyleng; \
    yyextra->offset += yyleng; \
    yyextra->col += yyleng;

#define TOKEN(t) \
    yyextra->tb->markWithPostponed(yylval->text); \
    return (yylval->text.token = (t))

#define KW(t) \
    BEGIN(INITIAL); \
    TOKEN(t)

#define ADVANCE_LINE() \
    ++yyextra->line; \
    yyextra->col = 1U; \
    yyextra->lineoffset = yyextra->offset;

using namespace c11stypes;

static void reportError(C11_LTYPE *loc, const char text[], std::size_t len,
                        C11LexerData *data);

%}

%X directive dirmlcomment beforeparen slcomment mlcomment slit

 /* (6.4.3) hex-quad:
  *     hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit
  */
HEXQUAD                 [[:xdigit:]]{4}
 /* (6.4.3) universal-character-name:
  *     \u hex-quad
  *     \U hex-quad hex-quad
  */
UCN                     \\u{HEXQUAD}|\\U{HEXQUAD}{2}
 /* (6.4.2.1) nondigit: one of
  * _ a b c d e f g h i j k l m
  *   n o p q r s t u v w x y z
  *   A B C D E F G H I J K L M
  *   N O P Q R S T U V W X Y Z
  */
NONDIGIT                [_a-zA-Z]
 /* (6.4.2.1) identifier-nondigit:
  *     nondigit
  *     universal-character-name
  *     other implementation-defined characters
  */
ID_NONDIGIT             {NONDIGIT}|{UCN}
 /* (6.4.2.1) identifier:
  *     identifier-nondigit
  *     identifier identifier-nondigit
  *     identifier digit
  */
ID                      {ID_NONDIGIT}({ID_NONDIGIT}|[[:digit:]])*
 /* (6.4.4.1) octal-digit: one of */
 /*     0 1 2 3 4 5 6 7 */
ODIGIT                  [0-7]
 /* (6.4.4.4) octal-escape-sequence: */
 /*     \ octal-digit */
 /*     \ octal-digit octal-digit */
 /*     \ octal-digit octal-digit octal-digit */
OESC                    \\{ODIGIT}{1,3}
 /* (6.4.4.4) hexadecimal-escape-sequence: */
 /*     \x hexadecimal-digit */
 /*     hexadecimal-escape-sequence hexadecimal-digit */
HESC                    \\x[[:xdigit:]]+
 /* (6.4.4.4) simple-escape-sequence: one of */
 /*     \' \" \? \\ */
 /*     \a \b \f \n \r \t \v */
SESC                    \\['"?\\abfnrtv]
 /* (6.4.4.4) escape-sequence: */
 /*     simple-escape-sequence */
 /*     octal-escape-sequence */
 /*     hexadecimal-escape-sequence */
 /*     universal-character-name */
ESEQ                    {SESC}|{OESC}|{HESC}|{UCN}
 /* (6.4.4.4) c-char: */
 /*     any member of the source character set except */
 /*         the single-quote ', backslash \, or new-line character */
 /*     escape-sequence */
CCHAR                   [^'\\\n]|{ESEQ}
 /* (6.4.4.4) c-char-sequence: */
 /*     c-char */
 /*     c-char-sequence c-char */
CCHARSEQ                {CCHAR}+
 /* (6.4.4.2) floating-suffix: one of */
 /*     f l F L */
FSUFFIX                 [flFL]
 /* (6.4.4.2) hexadecimal-digit-sequence: */
 /*     hexadecimal-digit */
 /*     hexadecimal-digit-sequence hexadecimal-digit */
HSEQ                    [[:xdigit:]]+
 /* (6.4.4.2) sign: one of */
 /*     + - */
SIGN                    [-+]
 /* (6.4.4.2) digit-sequence: */
 /*     digit */
 /*     digit-sequence digit */
DSEQ                    [[:digit:]]+
 /* (6.4.4.2) binary-exponent-part: */
 /*     p signopt digit-sequence */
 /*     P signopt digit-sequence */
BEXP                    [pP]{SIGN}?{DSEQ}
 /* (6.4.4.2) hexadecimal-fractional-constant: */
 /*     hexadecimal-digit-sequenceopt . */
 /*     hexadecimal-digit-sequence */
 /*     hexadecimal-digit-sequence . */
HFRAC                   {HSEQ}?\.|{HSEQ}\.?
 /* (6.4.4.2) exponent-part: */
 /*     e signopt digit-sequence */
 /*     E signopt digit-sequence */
EPART                   [eE]{SIGN}?{DSEQ}
 /* (6.4.4.2) fractional-constant: */
 /*     digit-sequenceopt . digit-sequence */
 /*     digit-sequence . */
FRACCONST               {DSEQ}?\.{DSEQ}|{DSEQ}\.
 /* (6.4.4.1) hexadecimal-prefix: one of */
 /*     0x 0X */
HPREFIX                 0[xX]
 /* (6.4.4.2) hexadecimal-floating-constant: */
 /*     hexadecimal-prefix hexadecimal-fractional-constant binary-exponent-part floating-suffixopt */
 /*     hexadecimal-prefix hexadecimal-digit-sequence binary-exponent-part floating-suffixopt */
HFCONST                 {HPREFIX}{HFRAC}{BEXP}{FSUFFIX}?|{HPREFIX}{HSEQ}{BEXP}{FSUFFIX}?
 /* (6.4.4.1) unsigned-suffix: one of */
 /*     u U */
USUFFIX                 [uU]
 /* (6.4.4.1) long-suffix: one of */
 /*     l L */
LSUFFIX                 [lL]
 /* (6.4.4.1) long-long-suffix: one of */
 /*     ll LL */
LLSUFFIX                ll|LL
 /* (6.4.4.1) nonzero-digit: one of */
 /*     1 2 3 4 5 6 7 8 9 */
NZDIGIT                 [1-9]
 /* (6.4.4.1) decimal-constant: */
 /*     nonzero-digit */
 /*     decimal-constant digit */
DCONST                  {NZDIGIT}[[:digit:]]*
 /* (6.4.4.1) octal-constant: */
 /*     0 */
 /*     octal-constant octal-digit */
OCONST                  0{ODIGIT}*
 /* (6.4.4.1) integer-suffix: */
 /*     unsigned-suffix long-suffixopt */
 /*     unsigned-suffix long-long-suffix */
 /*     long-suffix unsigned-suffixopt */
 /*     long-long-suffix unsigned-suffixopt */
ISUFFIX                 {USUFFIX}{LSUFFIX}?|{USUFFIX}{LLSUFFIX}|{LSUFFIX}{USUFFIX}?|{LLSUFFIX}{USUFFIX}?
 /* (6.4.4.2) decimal-floating-constant: */
 /*     fractional-constant exponent-partopt floating-suffixopt */
 /*     digit-sequence exponent-part floating-suffixopt */
DFCONST                 {FRACCONST}{EPART}?{FSUFFIX}?|{DSEQ}{EPART}{FSUFFIX}?
 /* (6.4.4.1) hexadecimal-constant: */
 /*     hexadecimal-prefix hexadecimal-digit */
 /*     hexadecimal-constant hexadecimal-digit */
HCONST                  {HPREFIX}[[:xdigit:]]+
 /* (6.4.5) encoding-prefix: */
 /*     u8 */
 /*     u */
 /*     U */
 /*     L */
EPREFIX                 u8|u|U|L
 /* (6.4.5) s-char: */
 /*     any member of the source character set except */
 /*         the double-quote ", backslash \, or new-line character */
 /*     escape-sequence */
SCHAR                   [^"\\\n]|{ESEQ}
 /* (6.4.7) header-name: */
 /*     < h-char-sequence > */
 /*     " q-char-sequence " */
HEADERNAME              <{HCHARSEQ}>|"{QCHARSEQ}"
 /* (6.4.7) h-char-sequence: */
 /*     h-char */
 /*     h-char-sequence h-char */
HCHARSEQ                {HCHAR}+
 /* (6.4.7) h-char: */
 /*     any member of the source character set except */
 /*         the new-line character and > */
HCHAR                   [^>\n]
 /* (6.4.7) q-char-sequence: */
 /*     q-char */
 /*     q-char-sequence q-char */
QCHARSEQ                {QCHAR}+
 /* (6.4.7) q-char: */
 /*     any member of the source character set except */
 /*         the new-line character and " */
QCHAR                   [^"\n]

NL                      \n|\r|\r\n

%%

[ ]                     ;
\t {
    if (yyextra->tabWidth > 1) {
        yyextra->col += yyextra->tabWidth;
        yyextra->col -= (yyextra->col - 1)%yyextra->tabWidth;
    }
}
{NL}                    { ADVANCE_LINE(); }
\\{NL} {
    yylval->text.len = 1;
    yylloc->last_column = yylloc->first_column + 1;
    yyextra->tb->addPostponed(yylval->text, *yylloc, +C11SType::LineGlue);
    ADVANCE_LINE();
}
<INITIAL,beforeparen>"case"                  { KW(CASE); }
<INITIAL,beforeparen>"default"               { KW(DEFAULT); }
<INITIAL,beforeparen>"sizeof"                { KW(SIZEOF); }
<INITIAL,beforeparen>"return"                { KW(RETURN); }
<INITIAL,beforeparen>"_Alignof"              { KW(_ALIGNOF); }
<INITIAL,beforeparen>"_Generic"              { KW(_GENERIC); }
<INITIAL,beforeparen>"typedef"               { KW(TYPEDEF); }
<INITIAL,beforeparen>"extern"                { KW(EXTERN); }
<INITIAL,beforeparen>"static"                { KW(STATIC); }
<INITIAL,beforeparen>"_Thread_local"         { KW(_THREAD_LOCAL); }
<INITIAL,beforeparen>"auto"                  { KW(AUTO); }
<INITIAL,beforeparen>"register"              { KW(REGISTER); }
<INITIAL,beforeparen>"void"                  { KW(VOID); }
<INITIAL,beforeparen>"char"                  { KW(CHAR); }
<INITIAL,beforeparen>"short"                 { KW(SHORT); }
<INITIAL,beforeparen>"int"                   { KW(INT); }
<INITIAL,beforeparen>"long"                  { KW(LONG); }
<INITIAL,beforeparen>"float"                 { KW(FLOAT); }
<INITIAL,beforeparen>"double"                { KW(DOUBLE); }
<INITIAL,beforeparen>"signed"                { KW(SIGNED); }
<INITIAL,beforeparen>"unsigned"              { KW(UNSIGNED); }
<INITIAL,beforeparen>"_Bool"                 { KW(_BOOL); }
<INITIAL,beforeparen>"_Complex"              { KW(_COMPLEX); }
<INITIAL,beforeparen>"struct"                { KW(STRUCT); }
<INITIAL,beforeparen>"union"                 { KW(UNION); }
<INITIAL,beforeparen>"enum"                  { KW(ENUM); }
<INITIAL,beforeparen>"_Atomic"               { KW(_ATOMIC); }
<INITIAL,beforeparen>"const"                 { KW(CONST); }
<INITIAL,beforeparen>"restrict"              { KW(RESTRICT); }
<INITIAL,beforeparen>"volatile"              { KW(VOLATILE); }
<INITIAL,beforeparen>"inline"                { KW(INLINE); }
<INITIAL,beforeparen>"_Noreturn"             { KW(_NORETURN); }
<INITIAL,beforeparen>"_Alignas"              { KW(_ALIGNAS); }
<INITIAL,beforeparen>"_Static_assert"        { KW(_STATIC_ASSERT); }
<INITIAL,beforeparen>"if"                    { KW(IF); }
<INITIAL,beforeparen>"else"                  { KW(ELSE); }
<INITIAL,beforeparen>"switch"                { KW(SWITCH); }
<INITIAL,beforeparen>"while"                 { KW(WHILE); }
<INITIAL,beforeparen>"do"                    { KW(DO); }
<INITIAL,beforeparen>"for"                   { KW(FOR); }
<INITIAL,beforeparen>"break"                 { KW(BREAK); }
<INITIAL,beforeparen>"continue"              { KW(CONTINUE); }
<INITIAL,beforeparen>"goto"                  { KW(GOTO); }
<INITIAL,beforeparen>"asm"                   { KW(ASM); }
<INITIAL,beforeparen>"__asm__"               { KW(ASM); }
<INITIAL,beforeparen>"__volatile__"          { KW(VOLATILE); }
<INITIAL,beforeparen>"__attribute__"         { KW(ATTRIBUTE); }
<INITIAL>{ID}                                { TOKEN(ID); }
<beforeparen>{ID} {
    BEGIN(INITIAL);
    yyextra->tb->markWithPostponed(yylval->text);
    yylval->text.token = FUNCTION;
    return ID;
}

{ID}[[:space:]]*"(" {
    BEGIN(beforeparen);
    yyextra->offset -= yyleng;
    yyextra->col -= yyleng;
    yyless(0);
}


 /* A.1.5 Constants */

 /* (6.4.4.1) integer-constant: */
 /*     decimal-constant integer-suffixopt */
 /*     octal-constant integer-suffixopt */
 /*     hexadecimal-constant integer-suffixopt */
{DCONST}{ISUFFIX}?|{OCONST}{ISUFFIX}?|{HCONST}{ISUFFIX}? { TOKEN(ICONST); }

 /* (6.4.4.2) floating-constant: */
 /*     decimal-floating-constant */
 /*     hexadecimal-floating-constant */
{DFCONST}|{HFCONST}     { TOKEN(FCONST); }

 /* (6.4.4.4) character-constant: */
 /*     ' c-char-sequence ' */
 /*     L' c-char-sequence ' */
 /*     u' c-char-sequence ' */
 /*     U' c-char-sequence ' */
[LuU]?'{CCHARSEQ}'      { TOKEN(CHCONST); }

 /* A.1.6 String literals */

 /* (6.4.5) string-literal: */
 /*     encoding-prefixopt " s-char-sequenceopt " */
 /* (6.4.5) s-char-sequence: */
 /*     s-char */
 /*     s-char-sequence s-char */
{EPREFIX}?\" {
    yyextra->startTok = *yylval;
    yyextra->startTok.text.token = SLIT;
    yyextra->startLoc = *yylloc;
    BEGIN(slit);
}

<slit>{SCHAR} ;

<slit>\" {
    yyextra->startTok.text.len = yyextra->offset - yyextra->startTok.text.from;
    yyextra->tb->markWithPostponed(yyextra->startTok.text);

    *yylval = yyextra->startTok;
    *yylloc = yyextra->startLoc;

    BEGIN(INITIAL);
    return SLIT;
}
<slit>\\?{NL}           { ADVANCE_LINE(); }
<slit>.                 { reportError(yylloc, yytext, yyleng, yyextra); }

"->"                    { TOKEN(ARR_OP); }
"++"                    { TOKEN(INC_OP); }
"--"                    { TOKEN(DEC_OP); }
"<<"                    { TOKEN(LSH_OP); }
">>"                    { TOKEN(RSH_OP); }
"<="                    { TOKEN(LTE_OP); }
">="                    { TOKEN(GTE_OP); }
"=="                    { TOKEN(EQ_OP); }
"!="                    { TOKEN(NE_OP); }
"&&"                    { TOKEN(AND_OP); }
"||"                    { TOKEN(OR_OP); }
"*="                    { TOKEN(TIMESEQ_OP); }
"/="                    { TOKEN(DIVEQ_OP); }
"%="                    { TOKEN(MODEQ_OP); }
"+="                    { TOKEN(PLUSEQ_OP); }
"-="                    { TOKEN(MINUSEQ_OP); }
"<<="                   { TOKEN(LSHIFTEQ_OP); }
">>="                   { TOKEN(RSHIFTEQ_OP); }
"&="                    { TOKEN(ANDEQ_OP); }
"^="                    { TOKEN(XOREQ_OP); }
"|="                    { TOKEN(OREQ_OP); }

^[[:space:]]{-}[\n\r]*# {
    yyextra->startTok = *yylval;
    yyextra->startTok.text.token = DIRECTIVE;
    yyextra->startLoc = *yylloc;
    BEGIN(directive);
}
<directive>\\{NL} {
    ADVANCE_LINE();
}
<directive>{NL} {
    yyextra->startTok.text.len = yyextra->offset
                               - yyextra->startTok.text.from - 1;
    yyextra->startLoc.last_line = yylloc->last_line;
    yyextra->startLoc.last_column = yylloc->last_column;
    yyextra->tb->addPostponed(yyextra->startTok.text, yyextra->startLoc,
                              +C11SType::Directive);

    ADVANCE_LINE();
    BEGIN(INITIAL);
}
<directive>"/*"         BEGIN(dirmlcomment);
<dirmlcomment>"*/"      BEGIN(directive);
<dirmlcomment>{NL}      ADVANCE_LINE();
<dirmlcomment>.         ;
<directive>HEADERNAME   ;
<directive>.            ;

"//" {
    yyextra->startTok = *yylval;
    yyextra->startTok.text.token = SLCOMMENT;
    yyextra->startLoc = *yylloc;
    BEGIN(slcomment);
}
<slcomment>{NL} {
    yyextra->startTok.text.len = yyextra->offset
                               - yyextra->startTok.text.from - 1;
    yyextra->startLoc.last_line = yylloc->last_line;
    yyextra->startLoc.last_column = yylloc->last_column;
    yyextra->tb->addPostponed(yyextra->startTok.text, yyextra->startLoc,
                              +C11SType::Comment);

    ADVANCE_LINE();
    BEGIN(INITIAL);
}
<slcomment>.            ;

"/*" {
    yyextra->startTok = *yylval;
    yyextra->startTok.text.token = MLCOMMENT;
    yyextra->startLoc = *yylloc;
    BEGIN(mlcomment);
}
<mlcomment>"*/" {
    yyextra->startTok.text.len = yyextra->offset - yyextra->startTok.text.from;
    yyextra->startLoc.last_line = yylloc->last_line;
    yyextra->startLoc.last_column = yylloc->last_column;
    yyextra->tb->addPostponed(yyextra->startTok.text, yyextra->startLoc,
                              +C11SType::Comment);

    BEGIN(INITIAL);
}
<mlcomment>{NL}         { ADVANCE_LINE(); }
<mlcomment>.            ;

"..."                   { TOKEN(DOTS); }

"("|")"|";"|"{"|"}"|"["|"]"|"."|","|"?"|":"|"&"|"|"|"^"|"*"|"/"|"%"|"+"|"-"|"~"|"!"|"<"|">"|"=" {
    TOKEN(yytext[0]);
}

. { reportError(yylloc, yytext, yyleng, yyextra); }

%%

static void
reportError(C11_LTYPE *loc, const char text[], std::size_t len,
            C11LexerData *data)
{
    std::string error;

    if (len > 1U) {
        error = std::string("Unknown token: ") + text;
    } else if (std::isprint(text[0], std::locale())) {
        error = std::string("Unknown token: ") + text[0];
    } else {
        error = std::string("Unknown token: <") + std::to_string(text[0]) + '>';
    }

    C11_LTYPE changedLoc = *loc;
    changedLoc.first_column = data->offset - data->lineoffset;
    c11_error(&changedLoc, nullptr, data->tb, data->pd, error.c_str());
}

void
fakeYYunputUseC11()
{
    // This is needed to prevent compilation error on -Werror=unused.
    static_cast<void>(&yyunput);
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/zograscope

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/zograscope

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master