Class: RubyLex

Inherits:
Object
  • Object
show all
Extended by:
Exception2MessageMapper
Includes:
IRB, RubyToken
Defined in:
lib/rdoc-f95/parsers/parse_rb.rb

Overview

Lexical analyzer for Ruby source

Defined Under Namespace

Classes: BufferedReader

Constant Summary collapse

ENINDENT_CLAUSE =
[
  "case", "class", "def", "do", "for", "if",
  "module", "unless", "until", "while", "begin" #, "when"
]
DEINDENT_CLAUSE =

, “when”

["end" #, "when"
]
PERCENT_LTYPE =
{
  "q" => "\'",
  "Q" => "\"",
  "x" => "\`",
  "r" => "/",
  "w" => "]"
}
PERCENT_PAREN =
{
  "{" => "}",
  "[" => "]",
  "<" => ">",
  "(" => ")"
}
Ltype2Token =
{
  "\'" => TkSTRING,
  "\"" => TkSTRING,
  "\`" => TkXSTRING,
  "/" => TkREGEXP,
  "]" => TkDSTRING
}
DLtype2Token =
{
  "\"" => TkDSTRING,
  "\`" => TkDXSTRING,
  "/" => TkDREGEXP,
}

Constants included from RubyToken

RubyToken::EXPR_ARG, RubyToken::EXPR_BEG, RubyToken::EXPR_CLASS, RubyToken::EXPR_DOT, RubyToken::EXPR_END, RubyToken::EXPR_FNAME, RubyToken::EXPR_MID, RubyToken::NEWLINE_TOKEN, RubyToken::TkReading2Token, RubyToken::TkSymbol2Token, RubyToken::TokenDefinitions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RubyToken

#Token, def_token, #set_token_position

Constructor Details

#initialize(content, options) ⇒ RubyLex

Returns a new instance of RubyLex.


447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 447

def initialize(content, options)
  lex_init

  @options = options

  @reader = BufferedReader.new content, @options

  @exp_line_no = @line_no = 1
  @base_char_no = 0
  @indent = 0

  @ltype = nil
  @quoted = nil
  @lex_state = EXPR_BEG
  @space_seen = false

  @continue = false
  @line = ""

  @skip_space = false
  @read_auto_clean_up = false
  @exception_on_syntax_error = true
end

Instance Attribute Details

#continueObject (readonly)

Returns the value of attribute continue


440
441
442
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 440

def continue
  @continue
end

#exception_on_syntax_errorObject

Returns the value of attribute exception_on_syntax_error


473
474
475
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 473

def exception_on_syntax_error
  @exception_on_syntax_error
end

#indentObject (readonly)

Returns the value of attribute indent


474
475
476
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 474

def indent
  @indent
end

#lex_stateObject (readonly)

Returns the value of attribute lex_state


441
442
443
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 441

def lex_state
  @lex_state
end

#read_auto_clean_upObject

Returns the value of attribute read_auto_clean_up


472
473
474
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 472

def read_auto_clean_up
  @read_auto_clean_up
end

#skip_spaceObject

Returns the value of attribute skip_space


471
472
473
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 471

def skip_space
  @skip_space
end

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)

443
444
445
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 443

def RubyLex.debug?
  false
end

Instance Method Details

#char_noObject


481
482
483
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 481

def char_no
  @reader.column
end

#get_readObject


485
486
487
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 485

def get_read
  @reader.get_read
end

#getcObject


489
490
491
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 489

def getc
  @reader.getc
end

#getc_of_restsObject


493
494
495
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 493

def getc_of_rests
  @reader.getc_already_read
end

#getsObject


497
498
499
500
501
502
503
504
505
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 497

def gets
  c = getc or return
  l = ""
  begin
    l.concat c unless c == "\r"
    break if c == "\n"
  end while c = getc
  l
end

#identify_commentObject


1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 1273

def identify_comment
  @ltype = "#"
  comment = "#"
  while ch = getc
    if ch == "\\"
      ch = getc
      if ch == "\n"
        ch = " "
      else
        comment << "\\"
      end
    else
      if ch == "\n"
        @ltype = nil
        ungetc
        break
      end
    end
    comment << ch
  end
  return Token(TkCOMMENT).set_text(comment)
end

#identify_gvarObject


968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 968

def identify_gvar
  @lex_state = EXPR_END
  str = "$"

  tk = case ch = getc
       when /[~_*[email protected]\/\\;,=:<>".]/   #"
         str << ch
         Token(TkGVAR, str)

       when "-"
         str << "-" << getc
         Token(TkGVAR, str)

       when "&", "`", "'", "+"
         str << ch
         Token(TkBACK_REF, str)

       when /[1-9]/
         str << ch
         while (ch = getc) =~ /[0-9]/
           str << ch
         end
         ungetc
         Token(TkNTH_REF)
       when /\w/
         ungetc
         ungetc
         return identify_identifier
       else
         ungetc
         Token("$")
       end
  tk.set_text(str)
end

#identify_here_documentObject


1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 1078

def identify_here_document
  ch = getc
  if ch == "-"
    ch = getc
    indent = true
  end
  if /['"`]/ =~ ch            # '
    lt = ch
    quoted = ""
    while (c = getc) && c != lt
	quoted.concat c
    end
  else
    lt = '"'
    quoted = ch.dup
    while (c = getc) && c =~ /\w/
	quoted.concat c
    end
    ungetc
  end

  ltback, @ltype = @ltype, lt
  reserve = ""

  while ch = getc
    reserve << ch
    if ch == "\\"    #"
      ch = getc
	reserve << ch
    elsif ch == "\n"
	break
    end
  end

  str = ""
  while (l = gets)
    l.chomp!
    l.strip! if indent
    break if l == quoted
    str << l.chomp << "\n"
  end

  @reader.divert_read_from(reserve)

  @ltype = ltback
  @lex_state = EXPR_END
  Token(Ltype2Token[lt], str).set_text(str.dump)
end

#identify_identifierObject


1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
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
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 1003

def identify_identifier
  token = ""
  token.concat getc if peek(0) =~ /[[email protected]]/
  token.concat getc if peek(0) == "@"

  while (ch = getc) =~ /\w|_/
    print ":", ch, ":" if RubyLex.debug?
    token.concat ch
  end
  ungetc

  if ch == "!" or ch == "?"
    token.concat getc
  end
  # fix token

  # $stderr.puts "identifier - #{token}, state = #@lex_state"

  case token
  when /^\$/
    return Token(TkGVAR, token).set_text(token)
  when /^\@/
    @lex_state = EXPR_END
    return Token(TkIVAR, token).set_text(token)
  end

  if @lex_state != EXPR_DOT
    print token, "\n" if RubyLex.debug?

    token_c, *trans = TkReading2Token[token]
    if token_c
	# reserved word?

	if (@lex_state != EXPR_BEG &&
   @lex_state != EXPR_FNAME &&
   trans[1])
 # modifiers
 token_c = TkSymbol2Token[trans[1]]
 @lex_state = trans[0]
	else
 if @lex_state != EXPR_FNAME
   if ENINDENT_CLAUSE.include?(token)
     @indent += 1
   elsif DEINDENT_CLAUSE.include?(token)
     @indent -= 1
   end
   @lex_state = trans[0]
 else
   @lex_state = EXPR_END
 end
	end
	return Token(token_c, token).set_text(token)
    end
  end

  if @lex_state == EXPR_FNAME
    @lex_state = EXPR_END
    if peek(0) == '='
	token.concat getc
    end
  elsif @lex_state == EXPR_BEG || @lex_state == EXPR_DOT
    @lex_state = EXPR_ARG
  else
    @lex_state = EXPR_END
  end

  if token[0, 1] =~ /[A-Z]/
    return Token(TkCONSTANT, token).set_text(token)
  elsif token[token.size - 1, 1] =~ /[!?]/
    return Token(TkFID, token).set_text(token)
  else
    return Token(TkIDENTIFIER, token).set_text(token)
  end
end

#identify_number(start) ⇒ Object


1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 1146

def identify_number(start)
  str = start.dup

  if start == "+" or start == "-" or start == ""
    start = getc
    str << start
  end

  @lex_state = EXPR_END

  if start == "0"
    if peek(0) == "x"
      ch = getc
      str << ch
      match = /[0-9a-f_]/
    else
      match = /[0-7_]/
    end
    while ch = getc
      if ch !~ match
        ungetc
        break
      else
        str << ch
      end
    end
    return Token(TkINTEGER).set_text(str)
  end

  type = TkINTEGER
  allow_point = TRUE
  allow_e = TRUE
  while ch = getc
    case ch
    when /[0-9_]/
      str << ch

    when allow_point && "."
	type = TkFLOAT
	if peek(0) !~ /[0-9]/
 ungetc
 break
	end
      str << ch
	allow_point = false

    when allow_e && "e", allow_e && "E"
      str << ch
	type = TkFLOAT
	if peek(0) =~ /[+-]/
 str << getc
	end
	allow_e = false
	allow_point = false
    else
	ungetc
	break
    end
  end
  Token(type).set_text(str)
end

#identify_quotation(initial_char) ⇒ Object


1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 1127

def identify_quotation(initial_char)
  ch = getc
  if lt = PERCENT_LTYPE[ch]
    initial_char += ch
    ch = getc
  elsif ch =~ /\W/
    lt = "\""
  else
    fail SyntaxError, "unknown type of %string ('#{ch}')"
  end
#     if ch !~ /\W/
#       ungetc
#       next
#     end
  #@ltype = lt
  @quoted = ch unless @quoted = PERCENT_PAREN[ch]
  identify_string(lt, @quoted, ch, initial_char)
end

#identify_string(ltype, quoted = ltype, opener = nil, initial_char = nil) ⇒ Object


1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 1208

def identify_string(ltype, quoted = ltype, opener=nil, initial_char = nil)
  @ltype = ltype
  @quoted = quoted
  subtype = nil

  str = ""
  str << initial_char if initial_char
  str << (opener||quoted)

  nest = 0
  begin
    while ch = getc
	str << ch
	if @quoted == ch
        if nest == 0
          break
        else
          nest -= 1
        end
      elsif opener == ch
        nest += 1
	elsif @ltype != "'" && @ltype != "]" and ch == "#"
        ch = getc
        if ch == "{"
          subtype = true
          str << ch << skip_inner_expression
        else
          ungetc(ch)
        end
	elsif ch == '\\' #'
 str << read_escape
	end
    end
    if @ltype == "/"
	if peek(0) =~ /i|o|n|e|s/
 str << getc
	end
    end
    if subtype
	Token(DLtype2Token[ltype], str)
    else
	Token(Ltype2Token[ltype], str)
    end.set_text(str)
  ensure
    @ltype = nil
    @quoted = nil
    @lex_state = EXPR_END
  end
end

#lexObject


520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 520

def lex
  until (((tk = token).kind_of?(TkNL) || tk.kind_of?(TkEND_OF_SCRIPT)) &&
    !@continue or
    tk.nil?)
  end
  line = get_read

  if line == "" and tk.kind_of?(TkEND_OF_SCRIPT) || tk.nil?
    nil
  else
    line
  end
end

#lex_initObject


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
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 589

def lex_init()
  if RUBY_VERSION.to_f < 1.9
    @OP = SLex.new
  else
    @OP = IRB::SLex.new
  end
  @OP.def_rules("\0", "\004", "\032") do |chars, io|
    Token(TkEND_OF_SCRIPT).set_text(chars)
  end

  @OP.def_rules(" ", "\t", "\f", "\r", "\13") do |chars, io|
    @space_seen = TRUE
    while (ch = getc) =~ /[ \t\f\r\13]/
      chars << ch
    end
    ungetc
    Token(TkSPACE).set_text(chars)
  end

  @OP.def_rule("#") do
    |op, io|
    identify_comment
  end

  @OP.def_rule("=begin", proc{@prev_char_no == 0 && peek(0) =~ /\s/}) do
    |op, io|
    str = op
    @ltype = "="


    begin
      line = ""
      begin
        ch = getc
        line << ch
      end until ch == "\n"
      str << line
    end until line =~ /^=end/

    ungetc

    @ltype = nil

    if str =~ /\A=begin\s+rdoc/i
      str.sub!(/\A=begin.*\n/, '')
      str.sub!(/^=end.*/m, '')
      Token(TkCOMMENT).set_text(str)
    else
      Token(TkRD_COMMENT)#.set_text(str)
    end
  end

  @OP.def_rule("\n") do
    print "\\n\n" if RubyLex.debug?
    case @lex_state
    when EXPR_BEG, EXPR_FNAME, EXPR_DOT
	@continue = TRUE
    else
	@continue = FALSE
	@lex_state = EXPR_BEG
    end
    Token(TkNL).set_text("\n")
  end

  @OP.def_rules("*", "**",
  "!", "!=", "!~",
  "=", "==", "===",
  "=~", "<=>",
  "<", "<=",
  ">", ">=", ">>") do
    |op, io|
    @lex_state = EXPR_BEG
    Token(op).set_text(op)
  end

  @OP.def_rules("<<") do
    |op, io|
    tk = nil
    if @lex_state != EXPR_END && @lex_state != EXPR_CLASS &&
 (@lex_state != EXPR_ARG || @space_seen)
	c = peek(0)
	if /[-\w_\"\'\`]/ =~ c
 tk = identify_here_document
	end
    end
    if !tk
      @lex_state = EXPR_BEG
      tk = Token(op).set_text(op)
    end
    tk
  end

  @OP.def_rules("'", '"') do
    |op, io|
    identify_string(op)
  end

  @OP.def_rules("`") do
    |op, io|
    if @lex_state == EXPR_FNAME
	Token(op).set_text(op)
    else
	identify_string(op)
    end
  end

  @OP.def_rules('?') do
    |op, io|
    if @lex_state == EXPR_END
	@lex_state = EXPR_BEG
	Token(TkQUESTION).set_text(op)
    else
	ch = getc
	if @lex_state == EXPR_ARG && ch !~ /\s/
 ungetc
 @lex_state = EXPR_BEG;
 Token(TkQUESTION).set_text(op)
	else
        str = op
        str << ch
 if (ch == '\\') #'
   str << read_escape
 end
 @lex_state = EXPR_END
 Token(TkINTEGER).set_text(str)
	end
    end
  end

  @OP.def_rules("&", "&&", "|", "||") do
    |op, io|
    @lex_state = EXPR_BEG
    Token(op).set_text(op)
  end

  @OP.def_rules("+=", "-=", "*=", "**=",
  "&=", "|=", "^=", "<<=", ">>=", "||=", "&&=") do
    |op, io|
    @lex_state = EXPR_BEG
    op =~ /^(.*)=$/
    Token(TkOPASGN, $1).set_text(op)
  end

  @OP.def_rule("[email protected]", proc{@lex_state == EXPR_FNAME}) do |op, io|
    Token(TkUPLUS).set_text(op)
  end

  @OP.def_rule("[email protected]", proc{@lex_state == EXPR_FNAME}) do |op, io|
    Token(TkUMINUS).set_text(op)
  end

  @OP.def_rules("+", "-") do
    |op, io|
    catch(:RET) do
	if @lex_state == EXPR_ARG
 if @space_seen and peek(0) =~ /[0-9]/
   throw :RET, identify_number(op)
 else
   @lex_state = EXPR_BEG
 end
	elsif @lex_state != EXPR_END and peek(0) =~ /[0-9]/
 throw :RET, identify_number(op)
	else
 @lex_state = EXPR_BEG
	end
	Token(op).set_text(op)
    end
  end

  @OP.def_rule(".") do
    @lex_state = EXPR_BEG
    if peek(0) =~ /[0-9]/
	ungetc
	identify_number("")
    else
	# for obj.if
	@lex_state = EXPR_DOT
	Token(TkDOT).set_text(".")
    end
  end

  @OP.def_rules("..", "...") do
    |op, io|
    @lex_state = EXPR_BEG
    Token(op).set_text(op)
  end

  lex_int2
end

#lex_int2Object


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
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 779

def lex_int2
  @OP.def_rules("]", "}", ")") do
    |op, io|
    @lex_state = EXPR_END
    @indent -= 1
    Token(op).set_text(op)
  end

  @OP.def_rule(":") do
    if @lex_state == EXPR_END || peek(0) =~ /\s/
	@lex_state = EXPR_BEG
	tk = Token(TkCOLON)
    else
	@lex_state = EXPR_FNAME;
	tk = Token(TkSYMBEG)
    end
    tk.set_text(":")
  end

  @OP.def_rule("::") do
#      p @lex_state.id2name, @space_seen
    if @lex_state == EXPR_BEG or @lex_state == EXPR_ARG && @space_seen
	@lex_state = EXPR_BEG
	tk = Token(TkCOLON3)
    else
	@lex_state = EXPR_DOT
	tk = Token(TkCOLON2)
    end
    tk.set_text("::")
  end

  @OP.def_rule("/") do
    |op, io|
    if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
	identify_string(op)
    elsif peek(0) == '='
	getc
	@lex_state = EXPR_BEG
	Token(TkOPASGN, :/).set_text("/=") #")
    elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
	identify_string(op)
    else
	@lex_state = EXPR_BEG
      Token("/").set_text(op)
    end
  end

  @OP.def_rules("^") do
    @lex_state = EXPR_BEG
    Token("^").set_text("^")
  end

  #       @OP.def_rules("^=") do
  # 	@lex_state = EXPR_BEG
  # 	Token(TkOPASGN, :^)
  #       end

  @OP.def_rules(",", ";") do
    |op, io|
    @lex_state = EXPR_BEG
    Token(op).set_text(op)
  end

  @OP.def_rule("~") do
    @lex_state = EXPR_BEG
    Token("~").set_text("~")
  end

  @OP.def_rule("[email protected]", proc{@lex_state = EXPR_FNAME}) do
    @lex_state = EXPR_BEG
    Token("~").set_text("[email protected]")
  end

  @OP.def_rule("(") do
    @indent += 1
    if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
	@lex_state = EXPR_BEG
	tk = Token(TkfLPAREN)
    else
	@lex_state = EXPR_BEG
	tk = Token(TkLPAREN)
    end
    tk.set_text("(")
  end

  @OP.def_rule("[]", proc{@lex_state == EXPR_FNAME}) do
    Token("[]").set_text("[]")
  end

  @OP.def_rule("[]=", proc{@lex_state == EXPR_FNAME}) do
    Token("[]=").set_text("[]=")
  end

  @OP.def_rule("[") do
    @indent += 1
    if @lex_state == EXPR_FNAME
	t = Token(TkfLBRACK)
    else
	if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
 t = Token(TkLBRACK)
	elsif @lex_state == EXPR_ARG && @space_seen
 t = Token(TkLBRACK)
	else
 t = Token(TkfLBRACK)
	end
	@lex_state = EXPR_BEG
    end
    t.set_text("[")
  end

  @OP.def_rule("{") do
    @indent += 1
    if @lex_state != EXPR_END && @lex_state != EXPR_ARG
	t = Token(TkLBRACE)
    else
	t = Token(TkfLBRACE)
    end
    @lex_state = EXPR_BEG
    t.set_text("{")
  end

  @OP.def_rule('\\') do   #'
    if getc == "\n"
	@space_seen = true
	@continue = true
	Token(TkSPACE).set_text("\\\n")
    else
	ungetc
	Token("\\").set_text("\\")  #"
    end
  end

  @OP.def_rule('%') do
    |op, io|
    if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
	identify_quotation('%')
    elsif peek(0) == '='
	getc
	Token(TkOPASGN, "%").set_text("%=")
    elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
	identify_quotation('%')
    else
	@lex_state = EXPR_BEG
	Token("%").set_text("%")
    end
  end

  @OP.def_rule('$') do  #'
    identify_gvar
  end

  @OP.def_rule('@') do
    if peek(0) =~ /[@\w_]/
	ungetc
	identify_identifier
    else
	Token("@").set_text("@")
    end
  end

  #       @OP.def_rule("def", proc{|op, io| /\s/ =~ io.peek(0)}) do
  # 	|op, io|
  # 	@indent += 1
  # 	@lex_state = EXPR_FNAME
  # #	@lex_state = EXPR_END
  # #	until @rests[0] == "\n" or @rests[0] == ";"
  # #	  rests.shift
  # #	end
  #       end

  @OP.def_rule("__END__", proc{@prev_char_no == 0 && peek(0) =~ /[\r\n]/}) do
    throw :eof
  end

  @OP.def_rule("") do
    |op, io|
    printf "MATCH: start %s: %s\n", op, io.inspect if RubyLex.debug?
    if peek(0) =~ /[0-9]/
	t = identify_number("")
    elsif peek(0) =~ /[\w_]/
	t = identify_identifier
    end
    printf "MATCH: end %s: %s\n", op, io.inspect if RubyLex.debug?
    t
  end

  p @OP if RubyLex.debug?
end

#line_noObject

io functions


477
478
479
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 477

def line_no
  @reader.line_num
end

#peek(i = 0) ⇒ Object


516
517
518
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 516

def peek(i = 0)
  @reader.peek(i)
end

#peek_equal?(str) ⇒ Boolean

Returns:

  • (Boolean)

512
513
514
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 512

def peek_equal?(str)
  @reader.peek_equal(str)
end

#read_escapeObject


1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 1296

def read_escape
  res = ""
  case ch = getc
  when /[0-7]/
    ungetc ch
    3.times do
	case ch = getc
	when /[0-7]/
	when nil
 break
	else
 ungetc
 break
	end
      res << ch
    end

  when "x"
    res << ch
    2.times do
	case ch = getc
	when /[0-9a-fA-F]/
	when nil
 break
	else
 ungetc
 break
	end
      res << ch
    end

  when "M"
    res << ch
    if (ch = getc) != '-'
	ungetc
    else
      res << ch
	if (ch = getc) == "\\" #"
        res << ch
 res << read_escape
      else
        res << ch
	end
    end

  when "C", "c" #, "^"
    res << ch
    if ch == "C" and (ch = getc) != "-"
	ungetc
    else
      res << ch
      if (ch = getc) == "\\" #"
        res << ch
        res << read_escape
      else
        res << ch
      end
    end
  else
    res << ch
  end
  res
end

#skip_inner_expressionObject


1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 1258

def skip_inner_expression
  res = ""
  nest = 0
  while (ch = getc)
    res << ch
    if ch == '}'
      break if nest.zero?
      nest -= 1
    elsif ch == '{'
      nest += 1
    end
  end
  res
end

#tokenObject


534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 534

def token
  set_token_position(line_no, char_no)
  begin
    begin
	tk = @OP.match(self)
	@space_seen = tk.kind_of?(TkSPACE)
    rescue SyntaxError
	abort if @exception_on_syntax_error
	tk = TkError.new(line_no, char_no)
    end
  end while @skip_space and tk.kind_of?(TkSPACE)
  if @read_auto_clean_up
    get_read
  end
#   throw :eof unless tk
  tk
end

#ungetc(c = nil) ⇒ Object


508
509
510
# File 'lib/rdoc-f95/parsers/parse_rb.rb', line 508

def ungetc(c = nil)
  @reader.ungetc(c)
end