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 (69 lines) | stat: -rw-r--r-- 1,727 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// re2java $INPUT -o $OUTPUT

/*!rules:re2c:x
    re2c:YYCTYPE = int;
    re2c:YYPEEK = "Byte.toUnsignedInt(yyinput[yycursor])";

    $           { return 0; }
    *           { return 1; }
    [a] $       { return 2; }
    [a]         { return 3; }
    [b] $ | [b] { return 4; }
    [c]+ $      { return 5; }
*/

class Main {
    static int lex_simple(byte[] yyinput) {
        int yycursor = 0;
        int yymarker = 0;
        int yylimit = yyinput.length - 1;

        /*!use:re2c:x
            re2c:yyfill:enable = 0;
        */
    }

    static int lex_eof(byte[] yyinput) {
        int yycursor = 0;
        int yymarker = 0;
        int yylimit = yyinput.length - 1;

        /*!use:re2c:x
            re2c:eof = 0;
            re2c:YYFILL = "false"; // always fails
            re2c:YYEND = "yycursor == yylimit";
        */
    }

    /*!max:re2c*/

    static int lex_scc(byte[] str) {
        int yycursor = 0;
        int yymarker = 0;
        int len = str.length - 1; // skip terminating null
        int yylimit = len + YYMAXFILL;

        byte[] yyinput = new byte[yylimit];
        System.arraycopy(str, 0, yyinput, 0, len);

        /*!use:re2c:x
            re2c:YYFILL = "return -1;"; // always fails
            re2c:YYEND = "yycursor == len";
        */
    }

    static void test(byte[] str, int retval) {
        assert lex_simple(str) == retval;
        assert lex_eof(str) == retval;
        assert lex_scc(str) == retval;
    }

    public static void main(String []args) {
        test("\0".getBytes(), 0);
        test("a\0".getBytes(), 2);
        test("ax\0".getBytes(), 3);
        test("b\0".getBytes(), 4);
        test("bx\0".getBytes(), 4);
        test("ccc\0".getBytes(), 5);
    }
};