hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/sfinae.l
blob: 8453699349dd28c7fd02d0e5125016a9095b4164 (plain)
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
%option noyywrap nounput noinput yylineno

%{
#include "lexer.h"

#include <stdbool.h>
#include <stdio.h>

static bool failed = false;
static char character = '\0';

static char unescape(char escaped)
{
    switch (escaped)
    {
        case 'n': return '\n';
        case 't': return '\t';
        case '0': return '\0';
        default: return escaped;
    }
}

// complains, remembers, and hands back TOK_END so the scan stops where it went wrong
static int fail(const char* message)
{
    (void)fprintf(stderr, "line %d: %s\n", yylineno, message);
    failed = true;
    return TOK_END;
}
%}

%%

"::"                    return TOK_COLONCOLON;
"emit"                  return TOK_EMIT;
"commit"                return TOK_COMMIT;
"decltype"              return TOK_DECLTYPE;
"difference"            return TOK_DIFFERENCE;
[<>(),;]                return yytext[0];
'\\.'                   character = unescape(yytext[2]); return TOK_CHAR;
'.'                     character = yytext[1]; return TOK_CHAR;
"//"[^\n]*              /* a comment */
[ \t\r\n]+              /* blanks */
[0-9]+                  return fail("numeric literals are for the weak, subtract two characters");
[A-Za-z_][A-Za-z0-9_]*  return fail("that is not a notion this language recognises");
.                       return fail("what is this character even");

%%

bool lexer_failed(void)
{
    return failed;
}

char lexer_character(void)
{
    return character;
}