File: advanced.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 (261 lines) | stat: -rw-r--r-- 7,972 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
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
// re2v $INPUT -o $OUTPUT -cf --loop-switch -Wno-nondeterministic-tags

import arrays
import log
import os

/*!conditions:re2c*/

// Use a small buffer to cover the case when a lexeme doesn't fit.
// In real world use a larger buffer.
const bufsize = 100

const mtag_root = -1
const tag_none = -1

// An m-tag tree is a way to store histories with an O(1) copy operation.
// Histories naturally form a tree, as they have common start and fork at some
// point. The tree is stored as an array of pairs (tag value, link to parent).
// An m-tag is represented with a single link in the tree (array index).
struct MtagElem {
    elem int
    pred int
}
type MtagTrie = []MtagElem

// Append a single value to an m-tag history.
fn add_mtag(mut trie &MtagTrie, mtag int, value int) int {
    trie = arrays.concat(trie, MtagElem{value, mtag})
    return trie.len - 1
}

// Recursively unwind tag histories and collect version components.
fn unwind(trie MtagTrie, x int, y int, str []u8) []string {
    // Reached the root of the m-tag tree, stop recursion.
    if x == mtag_root && y == mtag_root {
        return []
    }

    // Unwind history further.
    mut result := unwind(trie, trie[x].pred, trie[y].pred, str)

    // Get tag values. Tag histories must have equal length.
    if x == mtag_root || y == mtag_root {
        panic("tag histories have different length")
    }
    ex := trie[x].elem
    ey := trie[y].elem

    if ex != tag_none && ey != tag_none {
        // Both tags are valid string indices, extract component.
        result = arrays.concat(result, str[ex..ey].str())
    } else if !(ex == tag_none && ey == tag_none) {
        panic("both tags should be tagNone")
    }
    return result
}

struct State {
mut:
    file   os.File
    buf    []u8
    cur    int
    mar    int
    tok    int
    lim    int
    cond   int
    state  int
    trie   MtagTrie
    /*!stags:re2c format = '\n\t@@ int'; */
    /*!mtags:re2c format = '\n\t@@ int'; */
    accept int
}

enum Status {
    lex_end
    lex_ready
    lex_waiting
    lex_bad_packet
    lex_big_packet
}

fn fill(mut st &State) Status {
    shift := st.tok
    used := st.lim - st.tok
    free := bufsize - used

    // Error: no space. In real life can reallocate a larger buffer.
    if free < 1 { return .lex_big_packet }

    // Shift buffer contents (discard already processed data).
    copy(mut &st.buf, st.buf[shift..shift+used])
    st.cur -= shift
    st.mar -= shift
    st.lim -= shift
    st.tok -= shift
    /*!stags:re2c format = '\n\tif st.@@ != tag_none { st.@@ -= shift };'; */

    // Fill free space at the end of buffer with new data.
    pos := st.file.tell() or { 0 }
    if n := st.file.read_bytes_into(u64(pos), mut st.buf[st.lim..bufsize]) {
        st.lim += n
    }
    st.buf[st.lim] = 0 // append sentinel symbol

    return .lex_ready
}

fn lex(mut st &State) Status {
    mut yych := u8(0)
    mut l1 := tag_none
    mut l2 := tag_none
    mut f1 := mtag_root
    mut f2 := mtag_root
    mut p1 := mtag_root
    mut p2 := mtag_root
    mut p3 := mtag_root
    mut p4 := mtag_root
loop:
    /*!re2c
        re2c:api = custom;
        re2c:eof = 0;
        re2c:tags = 1;
        re2c:tags:expression   = "st.@@";
        re2c:variable:yyaccept = "st.accept";
        re2c:define:YYPEEK     = "st.buf[st.cur]";
        re2c:define:YYSKIP     = "st.cur += 1";
        re2c:define:YYBACKUP   = "st.mar = st.cur";
        re2c:define:YYRESTORE  = "st.cur = st.mar";
        re2c:define:YYLESSTHAN = "st.lim <= st.cur";
        re2c:define:YYFILL     = "return .lex_waiting";
        re2c:define:YYGETSTATE = "st.state";
        re2c:define:YYSETSTATE = "st.state = @@{state}";
        re2c:define:YYGETCONDITION = "st.cond";
        re2c:define:YYSETCONDITION = "st.cond = @@";
        re2c:define:YYSTAGP    = "@@ = st.cur";
        re2c:define:YYSTAGN    = "@@ = tag_none";
        re2c:define:YYMTAGP    = "@@ = add_mtag(mut &st.trie, @@, st.cur)";
        re2c:define:YYMTAGN    = "@@ = add_mtag(mut &st.trie, @@, tag_none)";

        crlf  = '\r\n';
        sp    = ' ';
        htab  = '\t';
        ows   = (sp | htab)*;
        digit = [0-9];
        alpha = [a-zA-Z];
        vchar = [\x1f-\x7e];
        tchar = [-!#$%&'*+.^_`|~] | digit | alpha;

        obs_fold            = #f1 crlf (sp | htab)+ #f2;
        obs_text            = [\x80-\xff];
        field_name          = tchar+;
        field_vchar         = vchar | obs_text;
        field_content       = field_vchar ((sp | htab)+ field_vchar)?;
        field_value_folded  = (field_content* obs_fold field_content*)+;
        header_field_folded = field_value_folded ows;
        token               = tchar+;
        qdtext
            = htab
            | sp
            | [\x21-\x5B\x5D-\x7E] \ '"'
            | obs_text;
        quoted_pair         = '\\' ( htab | sp | vchar | obs_text );
        quoted_string       = '"' ( qdtext | quoted_pair )* '"';
        parameter           = #p1 token #p2 '=' #p3 ( token | quoted_string ) #p4;
        media_type          = @l1 token '/' token @l2 ( ows ';' ows parameter )*;

        <media_type> media_type ows crlf {
            mt := st.buf[l1..l2].str()
            log.debug("media type: $mt")

            pnames := unwind(st.trie, p1, p2, st.buf)
            log.debug("pnames: $pnames")

            pvals := unwind(st.trie, p3, p4, st.buf)
            log.debug("pvals: $pvals")

            st.tok = st.cur
            unsafe { goto loop }
        }

        <header> header_field_folded crlf {
            folds := unwind(st.trie, f1, f2, st.buf)
            log.debug("folds: $folds")

            st.tok = st.cur
            unsafe { goto loop }
        }

        <*> * { return .lex_bad_packet }
        <*> $ { unsafe { goto end } }
    */
end:
    return .lex_end
}

fn test(expect Status, packets []string) {
    // Create a "socket" (open the same file for reading and writing).
    fname := "pipe"
    mut fw := os.create(fname) or { panic("cannot create file") }
    mut fr := os.open(fname) or { panic("cannot open file") }

    // Initialize lexer state: `state` value is -1, all offsets are at the end
    // of buffer.
    mut st := &State{
        file:   fr,
        // Sentinel at `lim` offset is set to zero, which triggers YYFILL.
        buf:    []u8{len: bufsize + 1},
        cur:    bufsize,
        mar:    bufsize,
        tok:    bufsize,
        lim:    bufsize,
        cond:   yycmedia_type,
        state:  -1,
        trie:   []MtagElem{},
        /*!stags:re2c format = '\n\t\t@@: tag_none,'; */
        /*!mtags:re2c format = '\n\t\t@@: mtag_root,'; */
        accept: 0,
    }
    // buf is zero-initialized, no need to write sentinel

    // Main loop. The buffer contains incomplete data which appears packet by
    // packet. When the lexer needs more input it saves its internal state and
    // returns to the caller which should provide more input and resume lexing.
    mut status := Status.lex_ready
    mut send := 0
    for {
        status = lex(mut &st)
        if status == .lex_end {
            break
        } else if status == .lex_waiting {
            if send < packets.len {
                log.debug("sending packet $send")
                fw.write_string(packets[send]) or { panic("cannot write to file") }
                fw.flush()
                send += 1
            }
            status = fill(mut &st)
            log.debug("filled buffer $st.buf, status $status")
            if status != .lex_ready {
                break
            }
        } else if status == .lex_bad_packet {
            break
        }
    }

    // Check results.
    if status != expect {
        panic("expected $expect, got $status")
    }

    // Cleanup: remove input file.
    fr.close()
    fw.close()
    os.rm(fname) or { panic("cannot remove file") }
}

fn main() {
    //log.set_level(.debug)
    test(.lex_end, ["ap", "plication/j", "son;", " charset=\"", "utf\\\"-8\"\r", "\n", ""])
}