src/ast/tokens.lex

    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
    
    /**
    # Lexer for Basilisk C
    
    Closely based on the [C99 lexer](c.lex). */
    
    %option bison-bridge noyywrap yylineno nounput
    
    %e  1019
    %p  2807
    %n  371
    %k  284
    %a  1213
    %o  1117
    
    O   [0-7]
    D   [0-9]
    NZ  [1-9]
    L   [a-zA-Z_]
    A   [a-zA-Z_0-9]
    H   [a-fA-F0-9]
    HP  (0[xX])
    E   ([Ee][+-]?{D}+)
    P   ([Pp][+-]?{D}+)
    FS  (f|F|l|L)
    IS  (((u|U)(l|L|ll|LL)?)|((l|L|ll|LL)(u|U)?))
    CP  (u|U|L)
    SP  (u8|u|U|L)
    ES  (\\(['"\?\\abfnrtv]|[0-7]{1,3}|x[a-fA-F0-9]+))
    WS  [ \t\v\n\f]
    STRING \"([^"\\\n]|{ES})*\"
    
    %{
    #include <stdio.h>
    #include <assert.h>
    #include "parser.h"
    #include "basilisk.h"
    
    #define YYSTYPE Ast *
    #define YY_DECL int yylex (YYSTYPE * yylval_param, AstRoot * parse)
    
    static void comment (void);
    static void preproc (void);
    static void bpreproc (void);
    static void ompreproc (void);
    static void file_line (AstRoot * parse, const char * text);
    static int  check_type (AstRoot * parse);
    
    static Ast * new_ast (AstRoot * parse,
    		      int token, int line, char * start, char * end)
    {
      Allocator * alloc = parse->alloc;
      Ast * n = allocate (alloc, sizeof(AstTerminal));
      memset (n, 0, sizeof(AstTerminal));
      n->sym = token_symbol (token);
      AstTerminal * t = ast_terminal (n);
      t->start = start;
      t->after = end;
      t->file = parse->file;
      while (start != end) {
        if (*start == '\n')
          line--;
        start++;
      }
      t->line = line;
      return n;
    }
    
    #define CAST()								\
      *yylval = new_ast (parse, yytext[0], yylineno, yytext, yytext);	\
      return yytext[0];
    
    #define SAST(t) 							\
      *yylval = new_ast (parse, t, yylineno, yytext, yytext + strlen(yytext) - 1); \
      return t;
    
    %}
    	 
    %%
    
    "/*"                                    { comment(); }
    "//".*                                  { /* consume //-comment */ }
    ^[ \t]*#[ \t]+[0-9]+[ \t]+{STRING}.*    { file_line (parse, yytext); }
    ^[ \t]*#	                        { preproc(); }
    ^[ \t]*@[ \t]*def[ \t].*                { bpreproc(); }
    ^[ \t]*@.*
    ^[ \t]*OMP[ \t]*\(	                { ompreproc(); }
    	 
    "auto"					{ SAST(AUTO); }
    "break"					{ SAST(BREAK); }
    "case"					{ SAST(CASE); }
    "char"					{ SAST(CHAR); }
    "const"					{ SAST(CONST); }
    "continue"				{ SAST(CONTINUE); }
    "default"				{ SAST(DEFAULT); }
    "do"					{ SAST(DO); }
    "double"				{ SAST(DOUBLE); }
    "else"					{ SAST(ELSE); }
    "enum"					{ SAST(ENUM); }
    "extern"				{ SAST(EXTERN); }
    "float"					{ SAST(FLOAT); }
    "for"					{ SAST(FOR); }
    "goto"					{ SAST(GOTO); }
    "if"					{ SAST(IF); }
    "inline"				{ SAST(INLINE); }
    "int"					{ SAST(INT); }
    "long"					{ SAST(LONG); }
    "register"				{ SAST(REGISTER); }
    "restrict"				{ SAST(RESTRICT); }
    "return"				{ SAST(RETURN); }
    "short"					{ SAST(SHORT); }
    "signed"				{ SAST(SIGNED); }
    "sizeof"				{ SAST(SIZEOF); }
    "static"				{ SAST(STATIC); }
    "struct"				{ SAST(STRUCT); }
    "switch"				{ SAST(SWITCH); }
    "typedef"				{ SAST(TYPEDEF); }
    "union"					{ SAST(UNION); }
    "unsigned"				{ SAST(UNSIGNED); }
    "void"					{ SAST(VOID); }
    "volatile"				{ SAST(VOLATILE); }
    "while"					{ SAST(WHILE); }
    "_Alignas"                              { SAST(ALIGNAS); }
    "_Alignof"                              { SAST(ALIGNOF); }
    "_Atomic"                               { SAST(ATOMIC); }
    "_Bool"                                 { SAST(BOOL); }
    "_Complex"                              { SAST(COMPLEX); }
    "complex"                               { SAST(COMPLEX); }
    "_Generic"                              { SAST(GENERIC); }
    "_Imaginary"                            { SAST(IMAGINARY); }
    "_Noreturn"                             { SAST(NORETURN); }
    "_Static_assert"                        { SAST(STATIC_ASSERT); }
    "_Thread_local"                         { SAST(THREAD_LOCAL); }
    "__func__"                              { SAST(FUNC_NAME); }
    
                        /* Basilisk C tokens */
    
    "new"{WS}+("vertex"{WS}+)?"scalar"      { SAST(NEW_FIELD); }
    "new"{WS}+("face"{WS}+)?"vector"        { SAST(NEW_FIELD); }
    "new"{WS}+("symmetric"{WS}+)?"tensor"   { SAST(NEW_FIELD); }
    "vertex"{WS}+"scalar"                   { SAST(TYPEDEF_NAME); }
    "face"{WS}+"vector"                     { SAST(TYPEDEF_NAME); }
    "symmetric"{WS}+"tensor"                { SAST(TYPEDEF_NAME); }
    "(const)"                               { SAST(MAYBECONST); }
    "trace"			                { SAST(TRACE); }
    "reduction"			        { SAST(REDUCTION); }
    
    "foreach_blockf" |
    "foreach_block" |
    "foreach_child" |
    "foreach_neighbor"                      { SAST(FOREACH_INNER); }
    
    "foreach_dimension"			{ SAST(FOREACH_DIMENSION); }
    
    "foreach" |
    "foreach_"{L}{A}*                       { SAST(FOREACH); }
    
                        /* GCC extensions */
    	   
    "__attribute__"{WS}*\(                  { ompreproc(); }
    	   
                        /* End of GCC extensions */
    
    {L}{A}*					{ SAST(check_type (parse)); }
    
    {HP}{H}+{IS}?				{ SAST(I_CONSTANT); }
    {NZ}{D}*{IS}?				{ SAST(I_CONSTANT); }
    "0"{O}*{IS}?				{ SAST(I_CONSTANT); }
    {CP}?"'"([^'\\\n]|{ES})+"'"		{ SAST(I_CONSTANT); }
    
    {D}+{E}{FS}?				{ SAST(F_CONSTANT); }
    {D}*"."{D}+{E}?{FS}?			{ SAST(F_CONSTANT); }
    {D}+"."{E}?{FS}?			{ SAST(F_CONSTANT); }
    {HP}{H}+{P}{FS}?			{ SAST(F_CONSTANT); }
    {HP}{H}*"."{H}+{P}{FS}?			{ SAST(F_CONSTANT); }
    {HP}{H}+"."{P}{FS}?			{ SAST(F_CONSTANT); }
    
    ({SP}?\"([^"\\\n]|{ES})*\"{WS}*)+	{ SAST(STRING_LITERAL); }
    
    "..."					{ SAST(ELLIPSIS); }
    ">>="					{ SAST(RIGHT_ASSIGN); }
    "<<="					{ SAST(LEFT_ASSIGN); }
    "+="					{ SAST(ADD_ASSIGN); }
    "-="					{ SAST(SUB_ASSIGN); }
    "*="					{ SAST(MUL_ASSIGN); }
    "/="					{ SAST(DIV_ASSIGN); }
    "%="					{ SAST(MOD_ASSIGN); }
    "&="					{ SAST(AND_ASSIGN); }
    "^="					{ SAST(XOR_ASSIGN); }
    "|="					{ SAST(OR_ASSIGN); }
    ">>"					{ SAST(RIGHT_OP); }
    "<<"					{ SAST(LEFT_OP); }
    "++"					{ SAST(INC_OP); }
    "--"					{ SAST(DEC_OP); }
    "->"					{ SAST(PTR_OP); }
    "&&"					{ SAST(AND_OP); }
    "||"					{ SAST(OR_OP); }
    "<="					{ SAST(LE_OP); }
    ">="					{ SAST(GE_OP); }
    "=="					{ SAST(EQ_OP); }
    "!="					{ SAST(NE_OP); }
    ";"					{ CAST(); }
    ("{"|"<%")				{ CAST(); }
    ("}"|"%>")				{ CAST(); }
    ","					{ CAST(); }
    ":"					{ CAST(); }
    "="					{ CAST(); }
    "("					{ CAST(); }
    ")"					{ CAST(); }
    ("["|"<:")				{ CAST(); }
    ("]"|":>")				{ CAST(); }
    "."					{ CAST(); }
    "&"					{ CAST(); }
    "!"					{ CAST(); }
    "~"					{ CAST(); }
    "-"					{ CAST(); }
    "+"					{ CAST(); }
    "*"					{ CAST(); }
    "/"					{ CAST(); }
    "%"					{ CAST(); }
    "<"					{ CAST(); }
    ">"					{ CAST(); }
    "^"					{ CAST(); }
    "|"					{ CAST(); }
    "?"					{ CAST(); }
    
    {WS}					{ /* whitespace separates tokens */ }
    .					{ /* discard bad characters */ }
    	 
    %%
    
    static void comment (void)
    {
      int c;  
      while ((c = input()) != 0)
        if (c == '*') {
          while ((c = input()) == '*')
    	;
    
          if (c == '/')
    	return;
    
          if (c == 0)
    	break;
        }
      //  yyerror ("unterminated comment");
    }
    
    static void preproc (void)
    {
      int c, c1 = 0;
      while ((c = input()) != 0) {
        if (c == '\n' && c1 != '\\')      
          return;
        c1 = c;
      }
      //  yyerror ("unterminated preprocessor directive");
    }
    
    static void bpreproc (void)
    {
      int c;
      while ((c = input()) != 0)
        if (c == '@')
          return;
      //  yyerror ("unterminated @def");
    }
    
    static void ompreproc (void)
    {
      int c, scope = 1;
      while ((c = input()) != 0) {
        if (c == '(')
          scope++;
        else if (c == ')') {
          scope--;
          if (scope == 0)
    	return;
        }
      }
      //  yyerror ("unterminated OMP");
    }
    
    static void file_line (AstRoot * parse, const char * text)
    {
      char * s = strchr (text, '#') + 1;
      yylineno = atoi(s) - 1;
      s = strchr (s, '"') + 1;
      char * end = strchr (s, '"');
      parse->file = allocate (parse->alloc, end - s + 1);
      strncpy ((char *) parse->file, s, end - s);
      //  fprintf (stderr, "%s: \"%s\" %d\n", text, file, yylineno);
    }
    
    static int check_type (AstRoot * parse)
    {
      if (parse->type_already_specified)
        return IDENTIFIER;
    
      Ast * declaration = ast_identifier_declaration (parse->stack, yytext);
      if (declaration) {
        if (ast_is_typedef (declaration))
          return TYPEDEF_NAME;
        return IDENTIFIER;
      }
    
      return IDENTIFIER;
    }
    
    void lexer_setup (char * buffer, size_t len)
    {
      yylineno = 1;
      yy_scan_buffer (buffer, len);
    }