Module: GraphQL::Language::Lexer

Defined in:
lib/graphql/language/lexer.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

._graphql_lexer_actionsObject

Returns the value of attribute _graphql_lexer_actions.



28
29
30
# File 'lib/graphql/language/lexer.rb', line 28

def _graphql_lexer_actions
  @_graphql_lexer_actions
end

._graphql_lexer_eof_transObject

Returns the value of attribute _graphql_lexer_eof_trans.



458
459
460
# File 'lib/graphql/language/lexer.rb', line 458

def _graphql_lexer_eof_trans
  @_graphql_lexer_eof_trans
end

._graphql_lexer_from_state_actionsObject

Returns the value of attribute _graphql_lexer_from_state_actions.



438
439
440
# File 'lib/graphql/language/lexer.rb', line 438

def _graphql_lexer_from_state_actions
  @_graphql_lexer_from_state_actions
end

._graphql_lexer_index_offsetsObject

Returns the value of attribute _graphql_lexer_index_offsets.



212
213
214
# File 'lib/graphql/language/lexer.rb', line 212

def _graphql_lexer_index_offsets
  @_graphql_lexer_index_offsets
end

._graphql_lexer_key_offsetsObject

Returns the value of attribute _graphql_lexer_key_offsets.



51
52
53
# File 'lib/graphql/language/lexer.rb', line 51

def _graphql_lexer_key_offsets
  @_graphql_lexer_key_offsets
end

._graphql_lexer_range_lengthsObject

Returns the value of attribute _graphql_lexer_range_lengths.



192
193
194
# File 'lib/graphql/language/lexer.rb', line 192

def _graphql_lexer_range_lengths
  @_graphql_lexer_range_lengths
end

._graphql_lexer_single_lengthsObject

Returns the value of attribute _graphql_lexer_single_lengths.



172
173
174
# File 'lib/graphql/language/lexer.rb', line 172

def _graphql_lexer_single_lengths
  @_graphql_lexer_single_lengths
end

._graphql_lexer_to_state_actionsObject

Returns the value of attribute _graphql_lexer_to_state_actions.



418
419
420
# File 'lib/graphql/language/lexer.rb', line 418

def _graphql_lexer_to_state_actions
  @_graphql_lexer_to_state_actions
end

._graphql_lexer_trans_actionsObject

Returns the value of attribute _graphql_lexer_trans_actions.



325
326
327
# File 'lib/graphql/language/lexer.rb', line 325

def _graphql_lexer_trans_actions
  @_graphql_lexer_trans_actions
end

._graphql_lexer_trans_keysObject

Returns the value of attribute _graphql_lexer_trans_keys.



71
72
73
# File 'lib/graphql/language/lexer.rb', line 71

def _graphql_lexer_trans_keys
  @_graphql_lexer_trans_keys
end

._graphql_lexer_trans_targsObject

Returns the value of attribute _graphql_lexer_trans_targs.



232
233
234
# File 'lib/graphql/language/lexer.rb', line 232

def _graphql_lexer_trans_targs
  @_graphql_lexer_trans_targs
end

.graphql_lexer_en_mainObject

Returns the value of attribute graphql_lexer_en_main.



491
492
493
# File 'lib/graphql/language/lexer.rb', line 491

def graphql_lexer_en_main
  @graphql_lexer_en_main
end

.graphql_lexer_errorObject

Returns the value of attribute graphql_lexer_error.



486
487
488
# File 'lib/graphql/language/lexer.rb', line 486

def graphql_lexer_error
  @graphql_lexer_error
end

.graphql_lexer_first_finalObject

Returns the value of attribute graphql_lexer_first_final.



482
483
484
# File 'lib/graphql/language/lexer.rb', line 482

def graphql_lexer_first_final
  @graphql_lexer_first_final
end

.graphql_lexer_startObject

Returns the value of attribute graphql_lexer_start.



478
479
480
# File 'lib/graphql/language/lexer.rb', line 478

def graphql_lexer_start
  @graphql_lexer_start
end

Class Method Details

.emit(token_name, ts, te, meta) ⇒ Object



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
# File 'lib/graphql/language/lexer.rb', line 1004

def self.emit(token_name, ts, te, meta)
  meta[:tokens] << token = GraphQL::Language::Token.new(
    name: token_name,
    value: meta[:data][ts...te].pack(PACK_DIRECTIVE).force_encoding(UTF_8_ENCODING),
    line: meta[:line],
    col: meta[:col],
    prev_token: meta[:previous_token],
  )
  meta[:previous_token] = token
  # Bump the column counter for the next token
  meta[:col] += te - ts
end

.emit_string(ts, te, meta) ⇒ Object



1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
# File 'lib/graphql/language/lexer.rb', line 1037

def self.emit_string(ts, te, meta)
  value = meta[:data][ts...te].pack(PACK_DIRECTIVE).force_encoding(UTF_8_ENCODING)
  if value !~ VALID_STRING
    meta[:tokens] << token = GraphQL::Language::Token.new(
      name: :BAD_UNICODE_ESCAPE,
      value: value,
      line: meta[:line],
      col: meta[:col],
      prev_token: meta[:previous_token],
    )
  else
    replace_escaped_characters_in_place(value)

    meta[:tokens] << token = GraphQL::Language::Token.new(
      name: :STRING,
      value: value,
      line: meta[:line],
      col: meta[:col],
      prev_token: meta[:previous_token],
    )
  end

  meta[:previous_token] = token
  meta[:col] += te - ts
end

.record_comment(ts, te, meta) ⇒ Object



990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/graphql/language/lexer.rb', line 990

def self.record_comment(ts, te, meta)
  token = GraphQL::Language::Token.new(
    name: :COMMENT,
    value: meta[:data][ts...te].pack(PACK_DIRECTIVE).force_encoding(UTF_8_ENCODING),
    line: meta[:line],
    col: meta[:col],
    prev_token: meta[:previous_token],
  )

  meta[:previous_token] = token

  meta[:col] += te - ts
end

.replace_escaped_characters_in_place(raw_string) ⇒ Object

Replace any escaped unicode or whitespace with the actual characters To avoid allocating more strings, this modifies the string passed into it



17
18
19
20
21
# File 'lib/graphql/language/lexer.rb', line 17

def self.replace_escaped_characters_in_place(raw_string)
  raw_string.gsub!(ESCAPES, ESCAPES_REPLACE)
  raw_string.gsub!(UTF_8, &UTF_8_REPLACE)
  nil
end

.run_lexer(query_string) ⇒ Object

line 119 "lib/graphql/language/lexer.rl"



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
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/graphql/language/lexer.rb', line 498

def self.run_lexer(query_string)
  data = query_string.unpack("c*")
  eof = data.length

  # Since `Lexer` is a module, store all lexer state
  # in this local variable:
  meta = {
    line: 1,
    col: 1,
    data: data,
    tokens: [],
    previous_token: nil,
  }

  
# line 514 "lib/graphql/language/lexer.rb"
begin
	p ||= 0
	pe ||= data.length
	cs = graphql_lexer_start
	ts = nil
	te = nil
	act = 0
end

# line 135 "lib/graphql/language/lexer.rl"

  emit_token = ->(name) {
    emit(name, ts, te, meta)
  }

  
# line 531 "lib/graphql/language/lexer.rb"
begin
	_klen, _trans, _keys, _acts, _nacts = nil
	_goto_level = 0
	_resume = 10
	_eof_trans = 15
	_again = 20
	_test_eof = 30
	_out = 40
	while true
	_trigger_goto = false
	if _goto_level <= 0
	if p == pe
		_goto_level = _test_eof
		next
	end
	end
	if _goto_level <= _resume
	_acts = _graphql_lexer_from_state_actions[cs]
	_nacts = _graphql_lexer_actions[_acts]
	_acts += 1
	while _nacts > 0
		_nacts -= 1
		_acts += 1
		case _graphql_lexer_actions[_acts - 1]
			when 1 then
# line 1 "NONE"
		begin
ts = p
		end
# line 561 "lib/graphql/language/lexer.rb"
		end # from state action switch
	end
	if _trigger_goto
		next
	end
	_keys = _graphql_lexer_key_offsets[cs]
	_trans = _graphql_lexer_index_offsets[cs]
	_klen = _graphql_lexer_single_lengths[cs]
	_break_match = false
	
	begin
	  if _klen > 0
_lower = _keys
_upper = _keys + _klen - 1

loop do
   break if _upper < _lower
   _mid = _lower + ( (_upper - _lower) >> 1 )

   if data[p].ord < _graphql_lexer_trans_keys[_mid]
      _upper = _mid - 1
   elsif data[p].ord > _graphql_lexer_trans_keys[_mid]
      _lower = _mid + 1
   else
      _trans += (_mid - _keys)
      _break_match = true
      break
   end
end # loop
break if _break_match
_keys += _klen
_trans += _klen
	  end
	  _klen = _graphql_lexer_range_lengths[cs]
	  if _klen > 0
_lower = _keys
_upper = _keys + (_klen << 1) - 2
loop do
   break if _upper < _lower
   _mid = _lower + (((_upper-_lower) >> 1) & ~1)
   if data[p].ord < _graphql_lexer_trans_keys[_mid]
     _upper = _mid - 2
   elsif data[p].ord > _graphql_lexer_trans_keys[_mid+1]
     _lower = _mid + 2
   else
     _trans += ((_mid - _keys) >> 1)
     _break_match = true
     break
   end
end # loop
break if _break_match
_trans += _klen
	  end
	end while false
	end
	if _goto_level <= _eof_trans
	cs = _graphql_lexer_trans_targs[_trans]
	if _graphql_lexer_trans_actions[_trans] != 0
		_acts = _graphql_lexer_trans_actions[_trans]
		_nacts = _graphql_lexer_actions[_acts]
		_acts += 1
		while _nacts > 0
			_nacts -= 1
			_acts += 1
			case _graphql_lexer_actions[_acts - 1]
when 2 then
# line 1 "NONE"
		begin
te = p+1
		end
when 3 then
# line 52 "lib/graphql/language/lexer.rl"
		begin
act = 1;		end
when 4 then
# line 53 "lib/graphql/language/lexer.rl"
		begin
act = 2;		end
when 5 then
# line 54 "lib/graphql/language/lexer.rl"
		begin
act = 3;		end
when 6 then
# line 55 "lib/graphql/language/lexer.rl"
		begin
act = 4;		end
when 7 then
# line 56 "lib/graphql/language/lexer.rl"
		begin
act = 5;		end
when 8 then
# line 57 "lib/graphql/language/lexer.rl"
		begin
act = 6;		end
when 9 then
# line 58 "lib/graphql/language/lexer.rl"
		begin
act = 7;		end
when 10 then
# line 59 "lib/graphql/language/lexer.rl"
		begin
act = 8;		end
when 11 then
# line 60 "lib/graphql/language/lexer.rl"
		begin
act = 9;		end
when 12 then
# line 61 "lib/graphql/language/lexer.rl"
		begin
act = 10;		end
when 13 then
# line 62 "lib/graphql/language/lexer.rl"
		begin
act = 11;		end
when 14 then
# line 63 "lib/graphql/language/lexer.rl"
		begin
act = 12;		end
when 15 then
# line 64 "lib/graphql/language/lexer.rl"
		begin
act = 13;		end
when 16 then
# line 65 "lib/graphql/language/lexer.rl"
		begin
act = 14;		end
when 17 then
# line 66 "lib/graphql/language/lexer.rl"
		begin
act = 15;		end
when 18 then
# line 67 "lib/graphql/language/lexer.rl"
		begin
act = 16;		end
when 19 then
# line 68 "lib/graphql/language/lexer.rl"
		begin
act = 17;		end
when 20 then
# line 69 "lib/graphql/language/lexer.rl"
		begin
act = 18;		end
when 21 then
# line 70 "lib/graphql/language/lexer.rl"
		begin
act = 19;		end
when 22 then
# line 78 "lib/graphql/language/lexer.rl"
		begin
act = 27;		end
when 23 then
# line 85 "lib/graphql/language/lexer.rl"
		begin
act = 34;		end
when 24 then
# line 95 "lib/graphql/language/lexer.rl"
		begin
act = 38;		end
when 25 then
# line 71 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:RCURLY)  end
		end
when 26 then
# line 72 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:LCURLY)  end
		end
when 27 then
# line 73 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:RPAREN)  end
		end
when 28 then
# line 74 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:LPAREN)  end
		end
when 29 then
# line 75 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:RBRACKET)  end
		end
when 30 then
# line 76 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:LBRACKET)  end
		end
when 31 then
# line 77 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:COLON)  end
		end
when 32 then
# line 78 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_string(ts + 1, te - 1, meta)  end
		end
when 33 then
# line 79 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:VAR_SIGN)  end
		end
when 34 then
# line 80 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:DIR_SIGN)  end
		end
when 35 then
# line 81 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:ELLIPSIS)  end
		end
when 36 then
# line 82 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:EQUALS)  end
		end
when 37 then
# line 83 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:BANG)  end
		end
when 38 then
# line 84 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:PIPE)  end
		end
when 39 then
# line 88 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin 
meta[:line] += 1
meta[:col] = 1
     end
		end
when 40 then
# line 95 "lib/graphql/language/lexer.rl"
		begin
te = p+1
 begin  emit_token.call(:UNKNOWN_CHAR)  end
		end
when 41 then
# line 52 "lib/graphql/language/lexer.rl"
		begin
te = p
p = p - 1; begin  emit_token.call(:INT)  end
		end
when 42 then
# line 53 "lib/graphql/language/lexer.rl"
		begin
te = p
p = p - 1; begin  emit_token.call(:FLOAT)  end
		end
when 43 then
# line 85 "lib/graphql/language/lexer.rl"
		begin
te = p
p = p - 1; begin  emit_token.call(:IDENTIFIER)  end
		end
when 44 then
# line 86 "lib/graphql/language/lexer.rl"
		begin
te = p
p = p - 1; begin  record_comment(ts, te, meta)  end
		end
when 45 then
# line 93 "lib/graphql/language/lexer.rl"
		begin
te = p
p = p - 1; begin  meta[:col] += te - ts  end
		end
when 46 then
# line 95 "lib/graphql/language/lexer.rl"
		begin
te = p
p = p - 1; begin  emit_token.call(:UNKNOWN_CHAR)  end
		end
when 47 then
# line 52 "lib/graphql/language/lexer.rl"
		begin
 begin p = ((te))-1; end
 begin  emit_token.call(:INT)  end
		end
when 48 then
# line 95 "lib/graphql/language/lexer.rl"
		begin
 begin p = ((te))-1; end
 begin  emit_token.call(:UNKNOWN_CHAR)  end
		end
when 49 then
# line 1 "NONE"
		begin
	case act
	when 1 then
	begin begin p = ((te))-1; end
 emit_token.call(:INT) end
	when 2 then
	begin begin p = ((te))-1; end
 emit_token.call(:FLOAT) end
	when 3 then
	begin begin p = ((te))-1; end
 emit_token.call(:ON) end
	when 4 then
	begin begin p = ((te))-1; end
 emit_token.call(:FRAGMENT) end
	when 5 then
	begin begin p = ((te))-1; end
 emit_token.call(:TRUE) end
	when 6 then
	begin begin p = ((te))-1; end
 emit_token.call(:FALSE) end
	when 7 then
	begin begin p = ((te))-1; end
 emit_token.call(:NULL) end
	when 8 then
	begin begin p = ((te))-1; end
 emit_token.call(:QUERY) end
	when 9 then
	begin begin p = ((te))-1; end
 emit_token.call(:MUTATION) end
	when 10 then
	begin begin p = ((te))-1; end
 emit_token.call(:SUBSCRIPTION) end
	when 11 then
	begin begin p = ((te))-1; end
 emit_token.call(:SCHEMA) end
	when 12 then
	begin begin p = ((te))-1; end
 emit_token.call(:SCALAR) end
	when 13 then
	begin begin p = ((te))-1; end
 emit_token.call(:TYPE) end
	when 14 then
	begin begin p = ((te))-1; end
 emit_token.call(:IMPLEMENTS) end
	when 15 then
	begin begin p = ((te))-1; end
 emit_token.call(:INTERFACE) end
	when 16 then
	begin begin p = ((te))-1; end
 emit_token.call(:UNION) end
	when 17 then
	begin begin p = ((te))-1; end
 emit_token.call(:ENUM) end
	when 18 then
	begin begin p = ((te))-1; end
 emit_token.call(:INPUT) end
	when 19 then
	begin begin p = ((te))-1; end
 emit_token.call(:DIRECTIVE) end
	when 27 then
	begin begin p = ((te))-1; end
 emit_string(ts + 1, te - 1, meta) end
	when 34 then
	begin begin p = ((te))-1; end
 emit_token.call(:IDENTIFIER) end
	when 38 then
	begin begin p = ((te))-1; end
 emit_token.call(:UNKNOWN_CHAR) end
end 
			end
# line 939 "lib/graphql/language/lexer.rb"
			end # action switch
		end
	end
	if _trigger_goto
		next
	end
	end
	if _goto_level <= _again
	_acts = _graphql_lexer_to_state_actions[cs]
	_nacts = _graphql_lexer_actions[_acts]
	_acts += 1
	while _nacts > 0
		_nacts -= 1
		_acts += 1
		case _graphql_lexer_actions[_acts - 1]
when 0 then
# line 1 "NONE"
		begin
ts = nil;		end
# line 959 "lib/graphql/language/lexer.rb"
		end # to state action switch
	end
	if _trigger_goto
		next
	end
	p += 1
	if p != pe
		_goto_level = _resume
		next
	end
	end
	if _goto_level <= _test_eof
	if p == eof
	if _graphql_lexer_eof_trans[cs] > 0
		_trans = _graphql_lexer_eof_trans[cs] - 1;
		_goto_level = _eof_trans
		next;
	end
end
	end
	if _goto_level <= _out
		break
	end
	end
	end

# line 141 "lib/graphql/language/lexer.rl"

  meta[:tokens]
end

.tokenize(query_string) ⇒ Object



11
12
13
# File 'lib/graphql/language/lexer.rb', line 11

def self.tokenize(query_string)
  run_lexer(query_string)
end