Class: MiniHTML::TokenStream

Inherits:
Object
  • Object
show all
Defined in:
ext/minihtml_token_stream/minihtml_token_stream.c

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 47

static VALUE stream_initialize(const VALUE self, VALUE tokens) {
    Check_Type(tokens, T_ARRAY);
    stream_t *s;
    TypedData_Get_Struct(self, stream_t, &stream_type, s);
    s->tokens = tokens;
    s->tokens_idx = 0;
    s->marks_idx = 0;
    s->tokens_len = RARRAY_LEN(tokens);

    // prime lookahead
    s->look[0] = s->tokens_len >= 1 ? rb_ary_entry(tokens, 0) : Qnil;
    s->look[1] = s->tokens_len >= 2 ? rb_ary_entry(tokens, 1) : Qnil;
    return self;
}

Instance Method Details

#consumeObject



86
87
88
89
90
91
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 86

static VALUE stream_consume(const VALUE self) {
    UNWRAP_STREAM;
    const VALUE peek = s->look[0];
    stream_consume_c(s);
    return peek;
}

#discardObject



93
94
95
96
97
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 93

static VALUE stream_discard(const VALUE self) {
    UNWRAP_STREAM;
    stream_consume_c(s);
    return Qnil;
}

#empty?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 133

static VALUE stream_is_empty(const VALUE self) {
    UNWRAP_STREAM;
    if (s->tokens_idx >= s->tokens_len || is_eof(s->look[0]))
        return Qtrue;
    return Qfalse;
}

#markObject



99
100
101
102
103
104
105
106
107
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 99

static VALUE stream_create_mark(const VALUE self) {
    UNWRAP_STREAM;
    if (s->marks_idx >= 127) {
        rb_raise(rb_eRuntimeError, "Too many marks in stream");
    }

    s->marks[s->marks_idx++] = s->tokens_idx;
    return Qnil;
}

#peekObject



69
70
71
72
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 69

static VALUE stream_peek(const VALUE self) {
    UNWRAP_STREAM;
    return s->look[0];
}

#peek1Object



74
75
76
77
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 74

static VALUE stream_peek1(const VALUE self) {
    UNWRAP_STREAM;
    return s->look[1];
}

#peek_kindObject



140
141
142
143
144
145
146
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 140

static VALUE stream_peek_kind(const VALUE self) {
    UNWRAP_STREAM;
    if (s->look[0] == Qnil) return Qnil;
    const VALUE k = rb_hash_aref(s->look[0], sym_kind);
    Check_Type(k, T_SYMBOL);
    return k;
}

#peek_kind1Object



148
149
150
151
152
153
154
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 148

static VALUE stream_peek_kind1(const VALUE self) {
    UNWRAP_STREAM;
    if (s->look[1] == Qnil) return Qnil;
    const VALUE k = rb_hash_aref(s->look[1], sym_kind);
    Check_Type(k, T_SYMBOL);
    return k;
}

#popObject



122
123
124
125
126
127
128
129
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 122

static VALUE stream_mark_pop(const VALUE self) {
    UNWRAP_STREAM;
    if (s->marks_idx == 0) {
        rb_raise(rb_eRuntimeError, "BUG: No mark to pop");
    }
    s->marks_idx--;
    return Qnil;
}

#restoreObject



109
110
111
112
113
114
115
116
117
118
119
120
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 109

static VALUE stream_mark_restore(const VALUE self) {
    UNWRAP_STREAM;
    if (s->marks_idx == 0) {
        rb_raise(rb_eRuntimeError, "BUG: No mark to restore");
    }
    const int mark = s->marks[s->marks_idx - 1];
    s->tokens_idx = mark;
    s->look[0] = s->tokens_len > mark ? rb_ary_entry(s->tokens, mark) : Qnil;
    s->look[1] = s->tokens_len > mark + 1 ? rb_ary_entry(s->tokens, mark + 1) : Qnil;
    s->marks_idx--;
    return Qnil;
}

#statusObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'ext/minihtml_token_stream/minihtml_token_stream.c', line 156

static VALUE stream_status(const VALUE self) {
    UNWRAP_STREAM;
    const VALUE h = rb_hash_new();
    rb_hash_aset(h, ID2SYM(rb_intern("eof")), Qnil);
    rb_hash_aset(h, ID2SYM(rb_intern("tokens")), s->tokens);
    rb_hash_aset(h, ID2SYM(rb_intern("tokens_idx")), INT2NUM(s->tokens_idx));
    rb_hash_aset(h, ID2SYM(rb_intern("tokens_len")), INT2NUM((int)s->tokens_len));
    rb_hash_aset(h, ID2SYM(rb_intern("look0")), s->look[0]);
    rb_hash_aset(h, ID2SYM(rb_intern("look1")), s->look[1]);
    rb_hash_aset(h, ID2SYM(rb_intern("marks_idx")), INT2NUM(s->marks_idx));
    return h;
}