Class: RDoc::RubyLex

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

Overview

Ruby lexer adapted from irb.

The internals are not documented because they are scary.

Defined Under Namespace

Classes: Error

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" => "]",
  "W" => "]",
  "s" => ":"
}
PERCENT_PAREN =
{
  "{" => "}",
  "[" => "]",
  "<" => ">",
  "(" => ")"
}
PERCENT_PAREN_REV =
PERCENT_PAREN.invert
Ltype2Token =
{
  "\'" => TkSTRING,
  "\"" => TkSTRING,
  "\`" => TkXSTRING,
  "/" => TkREGEXP,
  "]" => TkDSTRING,
  ":" => TkSYMBOL
}
DLtype2Token =
{
  "\"" => TkDSTRING,
  "\`" => TkDXSTRING,
  "/" => TkDREGEXP,
}
IDENT_RE =
if defined? Encoding then
  eval '/[\w\u{0080}-\u{FFFFF}]/u' # 1.8 can't parse \u{}
else
  /[\w\x80-\xFF]/
end

Constants included from RubyToken

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

Class Attribute Summary collapse

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

Creates a new lexer for content. options is an RDoc::Options, only +tab_width is used.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rdoc/ruby_lex.rb', line 82

def initialize(content, options)
  lex_init

  if /\t/ =~ content then
    tab_width = options.tab_width
    content = content.split(/\n/).map do |line|
      1 while line.gsub!(/\t+/) {
        ' ' * (tab_width*$&.length - $`.length % tab_width)
      }  && $~
      line
    end.join("\n")
  end

  content << "\n" unless content[-1, 1] == "\n"

  set_input StringIO.new content

  @base_char_no = 0
  @char_no = 0
  @exp_line_no = @line_no = 1
  @here_readed = []
  @readed = []
  @rests = []
  @seek = 0

  @here_header = false
  @indent = 0
  @indent_stack = []
  @lex_state = EXPR_BEG
  @space_seen = false

  @continue = false
  @line = ""

  @skip_space = false
  @readed_auto_clean_up = false
  @exception_on_syntax_error = true

  @prompt = nil
  @prev_seek = nil
  @ltype = nil
end

Class Attribute Details

.debug_levelObject

Returns the value of attribute debug_level



50
51
52
# File 'lib/rdoc/ruby_lex.rb', line 50

def debug_level
  @debug_level
end

Instance Attribute Details

#char_noObject (readonly)

Returns the value of attribute char_no



139
140
141
# File 'lib/rdoc/ruby_lex.rb', line 139

def char_no
  @char_no
end

#continueObject (readonly)

Returns the value of attribute continue



45
46
47
# File 'lib/rdoc/ruby_lex.rb', line 45

def continue
  @continue
end

#exception_on_syntax_errorObject

Returns the value of attribute exception_on_syntax_error



136
137
138
# File 'lib/rdoc/ruby_lex.rb', line 136

def exception_on_syntax_error
  @exception_on_syntax_error
end

#indentObject (readonly)

Returns the value of attribute indent



141
142
143
# File 'lib/rdoc/ruby_lex.rb', line 141

def indent
  @indent
end

#lex_stateObject (readonly)

Returns the value of attribute lex_state



46
47
48
# File 'lib/rdoc/ruby_lex.rb', line 46

def lex_state
  @lex_state
end

#line_noObject (readonly)

Returns the value of attribute line_no



140
141
142
# File 'lib/rdoc/ruby_lex.rb', line 140

def line_no
  @line_no
end

#readed_auto_clean_upObject

Returns the value of attribute readed_auto_clean_up



135
136
137
# File 'lib/rdoc/ruby_lex.rb', line 135

def readed_auto_clean_up
  @readed_auto_clean_up
end

#readerObject (readonly)

Returns the value of attribute reader



47
48
49
# File 'lib/rdoc/ruby_lex.rb', line 47

def reader
  @reader
end

#seekObject (readonly)

Returns the value of attribute seek



138
139
140
# File 'lib/rdoc/ruby_lex.rb', line 138

def seek
  @seek
end

#skip_spaceObject

Returns the value of attribute skip_space



134
135
136
# File 'lib/rdoc/ruby_lex.rb', line 134

def skip_space
  @skip_space
end

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rdoc/ruby_lex.rb', line 53

def self.debug?
  @debug_level > 0
end

.tokenize(ruby, options) ⇒ Object

Returns an Array of ruby tokens. See ::new for a description of options.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rdoc/ruby_lex.rb', line 65

def self.tokenize ruby, options
  tokens = []

  scanner = RDoc::RubyLex.new ruby, options
  scanner.exception_on_syntax_error = true

  while token = scanner.token do
    tokens << token
  end

  tokens
end

Instance Method Details

#each_top_level_statementObject



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/rdoc/ruby_lex.rb', line 294

def each_top_level_statement
  initialize_input
  catch(:TERM_INPUT) do
    loop do
      begin
        @continue = false
        prompt
        unless l = lex
          throw :TERM_INPUT if @line == ''
        else
          #p l
          @line.concat l
          if @ltype or @continue or @indent > 0
            next
          end
        end
        if @line != "\n"
          yield @line, @exp_line_no
        end
        break unless l
        @line = ''
        @exp_line_no = @line_no

        @indent = 0
        @indent_stack = []
        prompt
      rescue TerminateLineInput
        initialize_input
        prompt
        get_readed
      end
    end
  end
end

#eof?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/rdoc/ruby_lex.rb', line 199

def eof?
  @io.eof?
end

#get_readedObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rdoc/ruby_lex.rb', line 155

def get_readed
  if idx = @readed.rindex("\n")
    @base_char_no = @readed.size - (idx + 1)
  else
    @base_char_no += @readed.size
  end

  readed = @readed.join("")
  @readed = []
  readed
end

#getcObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rdoc/ruby_lex.rb', line 167

def getc
  while @rests.empty?
    #      return nil unless buf_input
    @rests.push nil unless buf_input
  end
  c = @rests.shift
  if @here_header
    @here_readed.push c
  else
    @readed.push c
  end
  @seek += 1
  if c == "\n"
    @line_no += 1
    @char_no = 0
  else
    @char_no += 1
  end

  c
end

#getc_of_restsObject



203
204
205
206
207
208
209
# File 'lib/rdoc/ruby_lex.rb', line 203

def getc_of_rests
  if @rests.empty?
    nil
  else
    getc
  end
end

#getsObject



189
190
191
192
193
194
195
196
197
# File 'lib/rdoc/ruby_lex.rb', line 189

def gets
  l = ""
  while c = getc
    l.concat(c)
    break if c == "\n"
  end
  return nil if l == "" and c.nil?
  l
end

#identify_commentObject



1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
# File 'lib/rdoc/ruby_lex.rb', line 1262

def identify_comment
  @ltype = "#"

  comment = '#'

  while ch = getc
    # if ch == "\\" #"
    #   read_escape
    # end
    if ch == "\n"
      @ltype = nil
      ungetc
      break
    end

    comment << ch
  end

  return Token(TkCOMMENT, comment)
end

#identify_gvarObject



834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'lib/rdoc/ruby_lex.rb', line 834

def identify_gvar
  @lex_state = EXPR_END

  case ch = getc
  when /[~_*$?!@\/\\;,=:<>".]/   #"
    Token(TkGVAR, "$" + ch)
  when "-"
    Token(TkGVAR, "$-" + getc)
  when "&", "`", "'", "+"
    Token(TkBACK_REF, "$"+ch)
  when /[1-9]/
    ref = ch
    while (ch = getc) =~ /[0-9]/ do ref << ch end
    ungetc
    Token(TkNTH_REF, "$#{ref}")
  when /\w/
    ungetc
    ungetc
    identify_identifier
  else
    ungetc
    Token("$")
  end
end

#identify_here_documentObject



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
1002
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
# File 'lib/rdoc/ruby_lex.rb', line 977

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

  ltback, @ltype = @ltype, lt
  reserve = []
  while ch = getc
    reserve.push ch
    if ch == "\\"
      reserve.push ch = getc
    elsif ch == "\n"
      break
    end
  end

  output_heredoc = reserve.join =~ /\A\r?\n\z/

  if output_heredoc then
    doc = '<<'
    doc << '-' if indent
    doc << "#{user_quote}#{quoted}#{user_quote}\n"
  else
    doc = '"'
  end

  @here_header = false
  while l = gets
    l = l.sub(/(:?\r)?\n\z/, "\n")
    if (indent ? l.strip : l.chomp) == quoted
      break
    end
    doc << l
  end

  if output_heredoc then
    doc << l.chomp
  else
    doc << '"'
  end

  @here_header = true
  @here_readed.concat reserve
  while ch = reserve.pop
    ungetc ch
  end

  token_class = output_heredoc ? RDoc::RubyLex::TkHEREDOC : Ltype2Token[lt]
  @ltype = ltback
  @lex_state = EXPR_END
  Token(token_class, doc)
end

#identify_identifierObject



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/rdoc/ruby_lex.rb', line 865

def identify_identifier
  token = ""
  if peek(0) =~ /[$@]/
    token.concat(c = getc)
    if c == "@" and peek(0) == "@"
      token.concat getc
    end
  end

  while (ch = getc) =~ IDENT_RE do
    print " :#{ch}: " if RDoc::RubyLex.debug?
    token.concat ch
  end

  ungetc

  if (ch == "!" || ch == "?") && token[0,1] =~ /\w/ && peek(0) != "="
    token.concat getc
  end

  # almost fix token

  case token
  when /^\$/
    return Token(TkGVAR, token)
  when /^\@\@/
    @lex_state = EXPR_END
    # p Token(TkCVAR, token)
    return Token(TkCVAR, token)
  when /^\@/
    @lex_state = EXPR_END
    return Token(TkIVAR, token)
  end

  if @lex_state != EXPR_DOT
    print token, "\n" if RDoc::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)
            # check for ``class = val'' etc.
            valid = true
            case token
            when "class"
              valid = false unless peek_match?(/^\s*(<<|\w|::)/)
            when "def"
              valid = false if peek_match?(/^\s*(([+-\/*&\|^]|<<|>>|\|\||\&\&)=|\&\&|\|\|)/)
            when "do"
              valid = false if peek_match?(/^\s*([+-\/*]?=|\*|<|>|\&)/)
            when *ENINDENT_CLAUSE
              valid = false if peek_match?(/^\s*([+-\/*]?=|\*|<|>|\&|\|)/)
            else
              # no nothing
            end
            if valid
              if token == "do"
                if ![TkFOR, TkWHILE, TkUNTIL].include?(@indent_stack.last)
                  @indent += 1
                  @indent_stack.push token_c
                end
              else
                @indent += 1
                @indent_stack.push token_c
              end
            else
              token_c = TkIDENTIFIER
            end

          elsif DEINDENT_CLAUSE.include?(token)
            @indent -= 1
            @indent_stack.pop
          end
          @lex_state = trans[0]
        else
          @lex_state = EXPR_END
        end
      end
      return Token(token_c, 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
    @lex_state = EXPR_ARG
  else
    @lex_state = EXPR_END
  end

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

#identify_number(op = "") ⇒ Object



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
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
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
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
# File 'lib/rdoc/ruby_lex.rb', line 1067

def identify_number(op = "")
  @lex_state = EXPR_END

  num = op

  if peek(0) == "0" && peek(1) !~ /[.eE]/
    num << getc

    case peek(0)
    when /[xX]/
      ch = getc
      match = /[0-9a-fA-F_]/
    when /[bB]/
      ch = getc
      match = /[01_]/
    when /[oO]/
      ch = getc
      match = /[0-7_]/
    when /[dD]/
      ch = getc
      match = /[0-9_]/
    when /[0-7]/
      match = /[0-7_]/
    when /[89]/
      raise Error, "Illegal octal digit"
    else
      return Token(TkINTEGER, num)
    end

    num << ch if ch

    len0 = true
    non_digit = false
    while ch = getc
      num << ch
      if match =~ ch
        if ch == "_"
          if non_digit
            raise Error, "trailing `#{ch}' in number"
          else
            non_digit = ch
          end
        else
          non_digit = false
          len0 = false
        end
      else
        ungetc
        num[-1, 1] = ''
        if len0
          raise Error, "numeric literal without digits"
        end
        if non_digit
          raise Error, "trailing `#{non_digit}' in number"
        end
        break
      end
    end
    return Token(TkINTEGER, num)
  end

  type = TkINTEGER
  allow_point = true
  allow_e = true
  non_digit = false
  while ch = getc
    num << ch
    case ch
    when /[0-9]/
      non_digit = false
    when "_"
      non_digit = ch
    when allow_point && "."
      if non_digit
        raise Error, "trailing `#{non_digit}' in number"
      end
      type = TkFLOAT
      if peek(0) !~ /[0-9]/
        type = TkINTEGER
        ungetc
        num[-1, 1] = ''
        break
      end
      allow_point = false
    when allow_e && "e", allow_e && "E"
      if non_digit
        raise Error, "trailing `#{non_digit}' in number"
      end
      type = TkFLOAT
      if peek(0) =~ /[+-]/
        num << getc
      end
      allow_e = false
      allow_point = false
      non_digit = ch
    else
      if non_digit
        raise Error, "trailing `#{non_digit}' in number"
      end
      ungetc
      num[-1, 1] = ''
      break
    end
  end

  Token(type, num)
end

#identify_quotationObject



1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
# File 'lib/rdoc/ruby_lex.rb', line 1048

def identify_quotation
  type = ch = getc
  if lt = PERCENT_LTYPE[type]
    ch = getc
  elsif type =~ /\W/
    type = nil
    lt = "\""
  else
    return Token(TkMOD, '%')
  end
  #     if ch !~ /\W/
  #       ungetc
  #       next
  #     end
  #@ltype = lt
  @quoted = ch unless @quoted = PERCENT_PAREN[ch]
  identify_string(lt, @quoted, type)
end

#identify_string(ltype, quoted = ltype, type = nil) ⇒ Object



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
1207
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
# File 'lib/rdoc/ruby_lex.rb', line 1175

def identify_string(ltype, quoted = ltype, type = nil)
  close = PERCENT_PAREN.values.include?(quoted)
  @ltype = ltype
  @quoted = quoted

  str = if ltype == quoted and %w[" ' /].include? ltype then
          ltype.dup
        elsif RUBY_VERSION > '1.9' then
          "%#{type or PERCENT_LTYPE.key ltype}#{PERCENT_PAREN_REV[quoted]}"
        else
          "%#{type or PERCENT_LTYPE.index ltype}#{PERCENT_PAREN_REV[quoted]}"
        end

  subtype = nil
  begin
    nest = 0

    while ch = getc
      str << ch

      if @quoted == ch and nest <= 0
        break
      elsif @ltype != "'" && @ltype != "]" && @ltype != ":" and ch == "#"
        ch = getc
        subtype = true
        if ch == "{" then
          str << ch << skip_inner_expression
          next
        else
          ungetc
        end
      elsif ch == '\\'
        if %w[' /].include? @ltype then
          case ch = getc
          when "\\", "\n", "'"
          when @ltype
            str << ch
          else
            ungetc
          end
        else
          str << read_escape
        end
      end

      if close then
        if PERCENT_PAREN[ch] == @quoted
          nest += 1
        elsif ch == @quoted
          nest -= 1
        end
      end
    end

    if @ltype == "/"
      if peek(0) =~ /i|m|x|o|e|s|u|n/
        getc
      end
    end

    if subtype
      Token(DLtype2Token[ltype], str)
    else
      Token(Ltype2Token[ltype], str)
    end
  ensure
    @ltype = nil
    @quoted = nil
    @lex_state = EXPR_END
  end
end

#initialize_inputObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rdoc/ruby_lex.rb', line 278

def initialize_input
  @ltype = nil
  @quoted = nil
  @indent = 0
  @indent_stack = []
  @lex_state = EXPR_BEG
  @space_seen = false
  @here_header = false

  @continue = false
  prompt

  @line = ""
  @exp_line_no = @line_no
end

#inspectObject

:stopdoc:



127
128
129
130
131
132
# File 'lib/rdoc/ruby_lex.rb', line 127

def inspect # :nodoc:
  "#<%s:0x%x pos %d lex_state %p space_seen %p>" % [
    self.class, object_id,
    @io.pos, @lex_state, @space_seen,
  ]
end

#lexObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/rdoc/ruby_lex.rb', line 329

def lex
  until (((tk = token).kind_of?(TkNL) || tk.kind_of?(TkEND_OF_SCRIPT)) &&
         !@continue or
    tk.nil?)
    #p tk
    #p @lex_state
    #p self
  end
  line = get_readed
  #      print self.inspect
  if line == "" and tk.kind_of?(TkEND_OF_SCRIPT) || tk.nil?
    nil
  else
    line
  end
end

#lex_initObject



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
# File 'lib/rdoc/ruby_lex.rb', line 412

def lex_init()
  @OP = IRB::SLex.new
  @OP.def_rules("\0", "\004", "\032") do |op, io|
    Token(TkEND_OF_SCRIPT, '')
  end

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

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

  @OP.def_rule("=begin",
               proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
    |op, io|
    @ltype = "="
    res = ''
    nil until (ch = getc) == "\n"

    until ( peek_equal?("=end") && peek(4) =~ /\s/ ) do
      (ch = getc)
      res << ch
    end

    gets # consume =end

    @ltype = nil
    Token(TkRD_COMMENT, res)
  end

  @OP.def_rule("\n") do |op, io|
    print "\\n\n" if RDoc::RubyLex.debug?
    case @lex_state
    when EXPR_BEG, EXPR_FNAME, EXPR_DOT
      @continue = true
    else
      @continue = false
      @lex_state = EXPR_BEG
      until (@indent_stack.empty? ||
             [TkLPAREN, TkLBRACK, TkLBRACE,
               TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
        @indent_stack.pop
      end
    end
    @here_header = false
    @here_readed = []
    Token(TkNL)
  end

  @OP.def_rules("*", "**",
                "=", "==", "===",
                "=~", "<=>",
                "<", "<=",
                ">", ">=", ">>") do
    |op, io|
    case @lex_state
    when EXPR_FNAME, EXPR_DOT
      @lex_state = EXPR_ARG
    else
      @lex_state = EXPR_BEG
    end
    Token(op)
  end

  @OP.def_rules("!", "!=", "!~") do
    |op, io|
    @lex_state = EXPR_BEG
    Token(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 /\S/ =~ c && (/["'`]/ =~ c || /\w/ =~ c || c == "-")
        tk = identify_here_document
      end
    end
    unless tk
      tk = Token(op)
      case @lex_state
      when EXPR_FNAME, EXPR_DOT
        @lex_state = EXPR_ARG
      else
        @lex_state = EXPR_BEG
      end
    end
    tk
  end

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

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

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

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

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

  @OP.def_rule("+@", proc{|op, io| @lex_state == EXPR_FNAME}) do
    |op, io|
    @lex_state = EXPR_ARG
    Token(op)
  end

  @OP.def_rule("-@", proc{|op, io| @lex_state == EXPR_FNAME}) do
    |op, io|
    @lex_state = EXPR_ARG
    Token(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)
    end
  end

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

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

  lex_int2
end

#lex_int2Object



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
# File 'lib/rdoc/ruby_lex.rb', line 611

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

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

  @OP.def_rule("::") do
    |op, io|
    #      p @lex_state.id2name, @space_seen
    if @lex_state == EXPR_BEG or @lex_state == EXPR_ARG && @space_seen
      @lex_state = EXPR_BEG
      Token(TkCOLON3)
    else
      @lex_state = EXPR_DOT
      Token(TkCOLON2)
    end
  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, "/") #/)
    elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
      identify_string(op)
    else
      @lex_state = EXPR_BEG
      Token("/") #/)
    end
  end

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

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

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

  @OP.def_rules(";") do
    |op, io|
    @lex_state = EXPR_BEG
    until (@indent_stack.empty? ||
           [TkLPAREN, TkLBRACK, TkLBRACE,
             TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
      @indent_stack.pop
    end
    Token(op)
  end

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

  @OP.def_rule("~@", proc{|op, io| @lex_state == EXPR_FNAME}) do
    |op, io|
    @lex_state = EXPR_BEG
    Token("~")
  end

  @OP.def_rule("(") do
    |op, io|
    @indent += 1
    if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
      @lex_state = EXPR_BEG
      tk_c = TkfLPAREN
    else
      @lex_state = EXPR_BEG
      tk_c = TkLPAREN
    end
    @indent_stack.push tk_c
    Token tk_c
  end

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

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

  @OP.def_rule("[") do
    |op, io|
    @indent += 1
    if @lex_state == EXPR_FNAME
      tk_c = TkfLBRACK
    else
      if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
        tk_c = TkLBRACK
      elsif @lex_state == EXPR_ARG && @space_seen
        tk_c = TkLBRACK
      else
        tk_c = TkfLBRACK
      end
      @lex_state = EXPR_BEG
    end
    @indent_stack.push tk_c
    Token(tk_c)
  end

  @OP.def_rule("{") do
    |op, io|
    @indent += 1
    if @lex_state != EXPR_END && @lex_state != EXPR_ARG
      tk_c = TkLBRACE
    else
      tk_c = TkfLBRACE
    end
    @lex_state = EXPR_BEG
    @indent_stack.push tk_c
    Token(tk_c)
  end

  @OP.def_rule('\\') do
    |op, io|
    if getc == "\n"
      @space_seen = true
      @continue = true
      Token(TkSPACE)
    else
      ungetc
      Token("\\")
    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, :%)
    elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
      identify_quotation
    else
      @lex_state = EXPR_BEG
      Token("%") #))
    end
  end

  @OP.def_rule('$') do
    |op, io|
    identify_gvar
  end

  @OP.def_rule('@') do
    |op, io|
    if peek(0) =~ /[\w@]/
      ungetc
      identify_identifier
    else
      Token("@")
    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("_") do
    if peek_match?(/_END__/) and @lex_state == EXPR_BEG then
      6.times { getc }
      Token(TkEND_OF_SCRIPT, '__END__')
    else
      ungetc
      identify_identifier
    end
  end

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

  p @OP if RDoc::RubyLex.debug?
end

#peek(i = 0) ⇒ Object



247
248
249
250
251
252
# File 'lib/rdoc/ruby_lex.rb', line 247

def peek(i = 0)
  while @rests.size <= i
    return nil unless buf_input
  end
  @rests[i]
end

#peek_equal?(str) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
235
236
237
238
# File 'lib/rdoc/ruby_lex.rb', line 232

def peek_equal?(str)
  chrs = str.split(//)
  until @rests.size >= chrs.size
    return false unless buf_input
  end
  @rests[0, chrs.size] == chrs
end

#peek_match?(regexp) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
243
244
245
# File 'lib/rdoc/ruby_lex.rb', line 240

def peek_match?(regexp)
  while @rests.empty?
    return false unless buf_input
  end
  regexp =~ @rests.join("")
end

#promptObject



272
273
274
275
276
# File 'lib/rdoc/ruby_lex.rb', line 272

def prompt
  if @prompt
    @prompt.call(@ltype, @indent, @continue, @line_no)
  end
end

#read_escapeObject



1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
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
# File 'lib/rdoc/ruby_lex.rb', line 1283

def read_escape
  escape = ''
  ch = getc
  escape << ch

  case ch
  when "\n", "\r", "\f"
  when "\\", "n", "t", "r", "f", "v", "a", "e", "b", "s" #"
  when /[0-7]/
    ungetc ch
    3.times do
      ch = getc
      escape << ch
      case ch
      when /[0-7]/
      when nil
        break
      else
        ungetc
        break
      end
    end

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

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

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

  escape
end

#set_input(io, p = nil, &block) ⇒ Object

io functions



144
145
146
147
148
149
150
151
152
153
# File 'lib/rdoc/ruby_lex.rb', line 144

def set_input(io, p = nil, &block)
  @io = io
  if p.respond_to?(:call)
    @input = p
  elsif block_given?
    @input = block
  else
    @input = Proc.new{@io.gets}
  end
end

#set_prompt(p = nil, &block) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/rdoc/ruby_lex.rb', line 263

def set_prompt(p = nil, &block)
  p = block if block_given?
  if p.respond_to?(:call)
    @prompt = p
  else
    @prompt = Proc.new{print p}
  end
end

#skip_inner_expressionObject



1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
# File 'lib/rdoc/ruby_lex.rb', line 1247

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



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/rdoc/ruby_lex.rb', line 346

def token
  #      require "tracer"
  #      Tracer.on
  @prev_seek = @seek
  @prev_line_no = @line_no
  @prev_char_no = @char_no
  begin
    begin
      tk = @OP.match(self)
      @space_seen = tk.kind_of?(TkSPACE)
    rescue SyntaxError => e
      raise Error, "syntax error: #{e.message}" if
        @exception_on_syntax_error

      tk = TkError.new(@seek, @line_no, @char_no)
    end
  end while @skip_space and tk.kind_of?(TkSPACE)

  if @readed_auto_clean_up
    get_readed
  end
  #      Tracer.off
  tk
end

#ungetc(c = nil) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rdoc/ruby_lex.rb', line 211

def ungetc(c = nil)
  if @here_readed.empty?
    c2 = @readed.pop
  else
    c2 = @here_readed.pop
  end
  c = c2 unless c
  @rests.unshift c #c =
  @seek -= 1
  if c == "\n"
    @line_no -= 1
    if idx = @readed.rindex("\n")
      @char_no = idx + 1
    else
      @char_no = @base_char_no + @readed.size
    end
  else
    @char_no -= 1
  end
end