Class: MiniCSS::CSS::TokenStream

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

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 49

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);
    s->eof = rb_const_get(cTokenClass, rb_intern("EOF"));

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

Instance Method Details

#consumeObject



84
85
86
87
88
89
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 84

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

#discardObject



91
92
93
94
95
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 91

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

#discard_whitespaceObject



97
98
99
100
101
102
103
104
105
106
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 97

static VALUE stream_discard_whitespace(const VALUE self) {
    UNWRAP_STREAM;
    for (;;) {
        const VALUE k = rb_funcall(s->look[0], rb_intern("kind"), 0);
        Check_Type(k, T_SYMBOL);
        if (rb_sym2id(k) != id_type_whitespace) break;
        stream_consume_c(s);
    }
    return Qnil;
}

#empty?Boolean

Returns:



146
147
148
149
150
151
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 146

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



108
109
110
111
112
113
114
115
116
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 108

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



72
73
74
75
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 72

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

#peek_kindObject



153
154
155
156
157
158
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 153

static VALUE stream_peek_kind(const VALUE self) {
    UNWRAP_STREAM;
    const VALUE k = rb_funcall(s->look[0], rb_intern("kind"), 0);
    Check_Type(k, T_SYMBOL);
    return k;
}

#popObject



131
132
133
134
135
136
137
138
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 131

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



118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 118

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) : s->eof;
    s->look[1] = s->tokens_len > mark + 1 ? rb_ary_entry(s->tokens, mark + 1) : s->eof;
    s->marks_idx--;
    return Qnil;
}

#statusObject



160
161
162
163
164
165
166
167
168
169
170
171
# File 'ext/minicss_token_stream/minicss_token_stream.c', line 160

static VALUE stream_status(const VALUE self) {
    UNWRAP_STREAM;
    const VALUE h = rb_hash_new();
    rb_hash_aset(h, ID2SYM(rb_intern("eof")), s->eof);
    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;
}