Class: Oga::CSS::Lexer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/oga/css/lexer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Lexer for turning CSS expressions into a sequence of tokens. Tokens are returned as arrays with every array having two values:

  1. The token type as a Symbol
  2. The token value, or nil if there is no value.

Thread Safety

Similar to the XPath lexer this lexer keeps track of an internal state. As a result it’s not safe to share the same instance of this lexer between multiple threads. However, no global state is used so you can use separate instances in threads just fine.

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Lexer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Lexer

Parameters:

  • data (String)

    The data to lex.



376
377
378
# File 'lib/oga/css/lexer.rb', line 376

def initialize(data)
  @data = data.strip
end

Class Attribute Details

._css_lexer_eof_transObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



332
333
334
# File 'lib/oga/css/lexer.rb', line 332

def _css_lexer_eof_trans
  @_css_lexer_eof_trans
end

._css_lexer_from_state_actionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



319
320
321
# File 'lib/oga/css/lexer.rb', line 319

def _css_lexer_from_state_actions
  @_css_lexer_from_state_actions
end

._css_lexer_index_offsetsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
# File 'lib/oga/css/lexer.rb', line 61

def _css_lexer_index_offsets
  @_css_lexer_index_offsets
end

._css_lexer_indiciesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/oga/css/lexer.rb', line 74

def _css_lexer_indicies
  @_css_lexer_indicies
end

._css_lexer_key_spansObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



48
49
50
# File 'lib/oga/css/lexer.rb', line 48

def _css_lexer_key_spans
  @_css_lexer_key_spans
end

._css_lexer_to_state_actionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



306
307
308
# File 'lib/oga/css/lexer.rb', line 306

def _css_lexer_to_state_actions
  @_css_lexer_to_state_actions
end

._css_lexer_trans_actionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



288
289
290
# File 'lib/oga/css/lexer.rb', line 288

def _css_lexer_trans_actions
  @_css_lexer_trans_actions
end

._css_lexer_trans_keysObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/oga/css/lexer.rb', line 25

def _css_lexer_trans_keys
  @_css_lexer_trans_keys
end

._css_lexer_trans_targsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



270
271
272
# File 'lib/oga/css/lexer.rb', line 270

def _css_lexer_trans_targs
  @_css_lexer_trans_targs
end

.css_lexer_en_mainObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



366
367
368
# File 'lib/oga/css/lexer.rb', line 366

def css_lexer_en_main
  @css_lexer_en_main
end

.css_lexer_en_predicateObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



362
363
364
# File 'lib/oga/css/lexer.rb', line 362

def css_lexer_en_predicate
  @css_lexer_en_predicate
end

.css_lexer_en_pseudo_argsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



358
359
360
# File 'lib/oga/css/lexer.rb', line 358

def css_lexer_en_pseudo_args
  @css_lexer_en_pseudo_args
end

.css_lexer_errorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



353
354
355
# File 'lib/oga/css/lexer.rb', line 353

def css_lexer_error
  @css_lexer_error
end

.css_lexer_first_finalObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



349
350
351
# File 'lib/oga/css/lexer.rb', line 349

def css_lexer_first_final
  @css_lexer_first_final
end

.css_lexer_startObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



345
346
347
# File 'lib/oga/css/lexer.rb', line 345

def css_lexer_start
  @css_lexer_start
end

Instance Method Details

#advance(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Advances through the input and generates the corresponding tokens. Each token is yielded to the supplied block.

This method stores the supplied block in @block and resets it after the lexer loop has finished.

See Also:

  • Oga::CSS::Lexer.[[#add_token]


401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File 'lib/oga/css/lexer.rb', line 401

def advance(&block)
  @block   = block
  @escaped = false

  data  = @data # saves ivar lookups while lexing.
  ts    = nil
  te    = nil
  stack = []
  top   = 0
  cs    = self.class.css_lexer_start
  act   = 0
  eof   = @data.bytesize
  p     = 0
  pe    = eof

  _css_lexer_eof_trans          = self.class.send(:_css_lexer_eof_trans)
  _css_lexer_from_state_actions = self.class.send(:_css_lexer_from_state_actions)
  _css_lexer_index_offsets      = self.class.send(:_css_lexer_index_offsets)
  _css_lexer_indicies           = self.class.send(:_css_lexer_indicies)
  _css_lexer_key_spans          = self.class.send(:_css_lexer_key_spans)
  _css_lexer_to_state_actions   = self.class.send(:_css_lexer_to_state_actions)
  _css_lexer_trans_actions      = self.class.send(:_css_lexer_trans_actions)
  _css_lexer_trans_keys         = self.class.send(:_css_lexer_trans_keys)
  _css_lexer_trans_targs        = self.class.send(:_css_lexer_trans_targs)

  
# line 428 "lib/oga/css/lexer.rb"
begin
	testEof = false
	_slen, _trans, _keys, _inds, _acts, _nacts = nil
	_goto_level = 0
	_resume = 10
	_eof_trans = 15
	_again = 20
	_test_eof = 30
	_out = 40
	while true
	if _goto_level <= 0
	if p == pe
		_goto_level = _test_eof
		next
	end
	if cs == 0
		_goto_level = _out
		next
	end
	end
	if _goto_level <= _resume
	case _css_lexer_from_state_actions[cs] 
	when 14 then
# line 1 "NONE"
		begin
ts = p
		end
# line 456 "lib/oga/css/lexer.rb"
	end
	_keys = cs << 1
	_inds = _css_lexer_index_offsets[cs]
	_slen = _css_lexer_key_spans[cs]
	_wide = ( (data.getbyte(p) || 0))
	_trans = if (   _slen > 0 && 
			_css_lexer_trans_keys[_keys] <= _wide && 
			_wide <= _css_lexer_trans_keys[_keys + 1] 
) then
			_css_lexer_indicies[ _inds + _wide - _css_lexer_trans_keys[_keys] ] 
		 else 
			_css_lexer_indicies[ _inds + _slen ]
		 end
	end
	if _goto_level <= _eof_trans
	cs = _css_lexer_trans_targs[_trans]
	if _css_lexer_trans_actions[_trans] != 0
	case _css_lexer_trans_actions[_trans]
	when 23 then
# line 151 "lib/oga/css/lexer.rl"
		begin
 @escaped = true 		end
	when 2 then
# line 1 "NONE"
		begin
te = p+1
		end
	when 36 then
# line 253 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_NTH)  end
		end
	when 6 then
# line 255 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_ODD)  end
		end
	when 5 then
# line 256 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_EVEN)  end
		end
	when 33 then
# line 235 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin 
    add_token(:T_RPAREN)

    cs = 16;
   end
		end
	when 34 then
# line 158 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 39 then
# line 246 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 42 then
# line 254 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin  add_token(:T_MINUS)  end
		end
	when 41 then
# line 196 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    value = slice_input(ts, te).to_i

    add_token(:T_INT, value)
   end
		end
	when 37 then
# line 158 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 3 then
# line 158 "lib/oga/css/lexer.rl"
		begin
 begin p = ((te))-1; end
 begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 4 then
# line 1 "NONE"
		begin
	case act
	when 0 then
	begin	begin
		cs = 0
		_goto_level = _again
		next
	end
end
	when 4 then
	begin begin p = ((te))-1; end
 add_token(:T_MINUS) end
end 
			end
	when 45 then
# line 294 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_EQ)  end
		end
	when 12 then
# line 295 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_SPACE_IN)  end
		end
	when 10 then
# line 296 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_STARTS_WITH)  end
		end
	when 9 then
# line 297 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_ENDS_WITH)  end
		end
	when 50 then
# line 298 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_IN)  end
		end
	when 11 then
# line 299 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin  add_token(:T_HYPHEN_IN)  end
		end
	when 46 then
# line 276 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin 
    add_token(:T_RBRACK)

    cs = 16;
   end
		end
	when 8 then
# line 214 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin 
    emit(:T_STRING, ts + 1, te - 1)
   end
		end
	when 49 then
# line 284 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 47 then
# line 158 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 7 then
# line 158 "lib/oga/css/lexer.rl"
		begin
 begin p = ((te))-1; end
 begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 18 then
# line 270 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin 
    add_token(:T_LBRACK)

    cs = 36;
   end
		end
	when 19 then
# line 134 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin 
    add_token(:T_PIPE)
   end
		end
	when 16 then
# line 229 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin 
    add_token(:T_LPAREN)

    cs = 27;
   end
		end
	when 17 then
# line 158 "lib/oga/css/lexer.rl"
		begin
te = p+1
 begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 15 then
# line 319 "lib/oga/css/lexer.rl"
		begin
te = p+1
		end
	when 27 then
# line 308 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin  add_token(:T_GREATER)  end
		end
	when 25 then
# line 309 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin  add_token(:T_PLUS)  end
		end
	when 28 then
# line 310 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin  add_token(:T_TILDE)  end
		end
	when 26 then
# line 138 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    add_token(:T_COMMA)
   end
		end
	when 24 then
# line 124 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    add_token(:T_SPACE)
   end
		end
	when 20 then
# line 158 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 1 then
# line 158 "lib/oga/css/lexer.rl"
		begin
 begin p = ((te))-1; end
 begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 40 then
# line 128 "lib/oga/css/lexer.rl"
		begin
 add_token(:T_HASH) 		end
# line 248 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 29 then
# line 128 "lib/oga/css/lexer.rl"
		begin
 add_token(:T_HASH) 		end
# line 306 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 43 then
# line 129 "lib/oga/css/lexer.rl"
		begin
 add_token(:T_DOT) 		end
# line 248 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 30 then
# line 129 "lib/oga/css/lexer.rl"
		begin
 add_token(:T_DOT) 		end
# line 306 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 44 then
# line 130 "lib/oga/css/lexer.rl"
		begin
 add_token(:T_COLON) 		end
# line 248 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 31 then
# line 130 "lib/oga/css/lexer.rl"
		begin
 add_token(:T_COLON) 		end
# line 306 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1;		end
	when 38 then
# line 151 "lib/oga/css/lexer.rl"
		begin
 @escaped = true 		end
# line 158 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 48 then
# line 151 "lib/oga/css/lexer.rl"
		begin
 @escaped = true 		end
# line 158 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 21 then
# line 151 "lib/oga/css/lexer.rl"
		begin
 @escaped = true 		end
# line 158 "lib/oga/css/lexer.rl"
		begin
te = p
p = p - 1; begin 
    value = slice_input(ts, te)

    # Translates "foo\.bar" into "foo.bar"
    if @escaped
      value    = value.gsub('\.', '.')
      @escaped = false
    end

    add_token(:T_IDENT, value)
   end
		end
	when 22 then
# line 1 "NONE"
		begin
te = p+1
		end
# line 151 "lib/oga/css/lexer.rl"
		begin
 @escaped = true 		end
	when 35 then
# line 1 "NONE"
		begin
te = p+1
		end
# line 254 "lib/oga/css/lexer.rl"
		begin
act = 4;		end
# line 924 "lib/oga/css/lexer.rb"
	end
	end
	end
	if _goto_level <= _again
	case _css_lexer_to_state_actions[cs] 
	when 13 then
# line 1 "NONE"
		begin
ts = nil;		end
	when 32 then
# line 1 "NONE"
		begin
ts = nil;		end
# line 1 "NONE"
		begin
act = 0
		end
# line 942 "lib/oga/css/lexer.rb"
	end

	if cs == 0
		_goto_level = _out
		next
	end
	p += 1
	if p != pe
		_goto_level = _resume
		next
	end
	end
	if _goto_level <= _test_eof
	if p == eof
	if _css_lexer_eof_trans[cs] > 0
		_trans = _css_lexer_eof_trans[cs] - 1;
		_goto_level = _eof_trans
		next;
	end
	end

	end
	if _goto_level <= _out
		break
	end
end
	end

# line 76 "lib/oga/css/lexer.rl"

  # % fix highlight
ensure
  @block = nil
end

#lexArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gathers all the tokens for the input and returns them as an Array.

Returns:

  • (Array)

See Also:

  • Oga::CSS::Lexer.[[#advance]


384
385
386
387
388
389
390
391
392
# File 'lib/oga/css/lexer.rb', line 384

def lex
  tokens = []

  advance do |type, value|
    tokens << [type, value]
  end

  return tokens
end