Class: IOSParser::CLexer

Inherits:
Object
  • Object
show all
Defined in:
ext/ios_parser/c_lexer/lexer.c

Instance Method Summary collapse

Constructor Details

#initialize(input_text) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/ios_parser/c_lexer/lexer.c', line 204

static VALUE initialize(VALUE self, VALUE input_text) {
    LexInfo *lex;
    Data_Get_Struct(self, LexInfo, lex);

    lex->text = NULL;
    lex->pos = 0;
    lex->line = 1;
    lex->start_of_line = 0;
    lex->token_line = 0;
    lex->token_start = 0;
    lex->token_length = 0;
    lex->token_state = LEX_STATE_ROOT;
    lex->tokens = rb_ary_new();

    lex->indent = 0;
    lex->indent_pos = 0;
    lex->indents[0] = 0;

    return self;
}

Instance Method Details

#call(input_text) ⇒ Object



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'ext/ios_parser/c_lexer/lexer.c', line 530

static VALUE call(VALUE self, VALUE input_text) {
    LexInfo *lex;
    size_t input_len;

    if (TYPE(input_text) != T_STRING) {
        rb_raise(rb_eTypeError, "The argument to CLexer#call must be a String.");
        return Qnil;
    }

    Data_Get_Struct(self, LexInfo, lex);

    StringValue(input_text);
    lex->text = RSTRING_PTR(input_text);
    input_len = RSTRING_LEN(input_text);

    for (lex->pos = 0; lex->pos < input_len; lex->pos++) {
        switch(lex->token_state) {
        case (LEX_STATE_ROOT):
            process_root(lex);
            break;

        case (LEX_STATE_INDENT):
            process_start_of_line(lex);
            break;

        case (LEX_STATE_INTEGER):
            process_integer(lex);
            break;

        case (LEX_STATE_DECIMAL):
            process_decimal(lex);
            break;

        case (LEX_STATE_QUOTED_STRING):
            process_quoted_string(lex);
            break;

        case (LEX_STATE_WORD):
            process_word(lex);
            break;

        case (LEX_STATE_COMMENT):
            process_comment(lex);
            break;

        case (LEX_STATE_BANNER):
            process_banner(lex);
            break;

        case (LEX_STATE_CERTIFICATE):
            process_certificate(lex);
            break;
        }
    }

    if (lex->token_state == LEX_STATE_QUOTED_STRING) {
        rb_raise(rb_eLexError,
                 "Unterminated quoted string starting at %d: %.*s",
                 (int)lex->token_start,
                 (int)lex->token_length, &lex->text[lex->token_start]);
    }

    delimit(lex);
    lex->token_start = lex->pos - 1;
    lex->line = lex->line - 1;

    for (; lex->indent_pos > 0; lex->indent_pos--) {
        ADD_TOKEN(lex, ID2SYM(rb_intern("DEDENT")));
    }

    return lex->tokens;
}