File: generalized_eof.re

package info (click to toggle)
re2c 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,512 kB
  • sloc: cpp: 34,160; ml: 8,494; sh: 5,311; makefile: 1,014; haskell: 611; python: 431; ansic: 234; javascript: 113
file content (52 lines) | stat: -rw-r--r-- 1,108 bytes parent folder | download
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
# re2py $INPUT -o $OUTPUT

%{rules:x
    $           { return 0 }
    *           { return 1 }
    [a] $       { return 2 }
    [a]         { return 3 }
    [b] $ | [b] { return 4 }
    [c]+ $      { return 5 }
%}

def lex_simple(yyinput):
    yycursor = 0
    yymarker = 0
    yylimit = len(yyinput) - 1
    %{use:x
        re2c:yyfill:enable = 0;
    %}

def lex_eof(yyinput):
    yycursor = 0
    yymarker = 0
    yylimit = len(yyinput) - 1
    %{use:x
        re2c:YYFILL = "False"; // always fails
        re2c:YYEND = "yycursor == yylimit";
        re2c:eof = 0;
    %}

%{max %}

def lex_scc(str):
    yyinput = str + (b"\0" * YYMAXFILL)
    yycursor = 0
    yylimit = len(yyinput)
    end = len(str) - 1
    %{use:x
        re2c:YYFILL = "return -1"; // always fails
        re2c:YYEND = "yycursor == end";
    %}

def test(str, ret):
    if lex_simple(str) != ret: raise "error simple"
    if lex_eof(str) != ret: raise "error eof" + lex_eof(str) + ret
    if lex_scc(str) != ret: raise "error scc"

test(b"\0", 0)
test(b"a\0", 2)
test(b"ax\0", 3)
test(b"b\0", 4)
test(b"bx\0", 4)
test(b"ccc\0", 5)