Skip to content

Commit 53f146a

Browse files
authored
Merge pull request #467 from aycabta/handle-json-style-hask-key-as-symbol
Handle JSON-style hash key as symbol
2 parents cd63f48 + f291a7d commit 53f146a

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,12 @@ def identify_identifier
963963
@indent_stack.push token_c
964964
end
965965
else
966-
token_c = TkIDENTIFIER
966+
if peek(0) == ':' and !peek_match?(/^::/)
967+
token.concat getc
968+
token_c = TkSYMBOL
969+
else
970+
token_c = TkIDENTIFIER
971+
end
967972
end
968973

969974
elsif DEINDENT_CLAUSE.include?(token)
@@ -996,7 +1001,12 @@ def identify_identifier
9961001
elsif token[token.size - 1, 1] =~ /[!?]/
9971002
return Token(TkFID, token)
9981003
else
999-
return Token(TkIDENTIFIER, token)
1004+
if peek(0) == ':' and !peek_match?(/^::/)
1005+
token.concat getc
1006+
return Token(TkSYMBOL, token)
1007+
else
1008+
return Token(TkIDENTIFIER, token)
1009+
end
10001010
end
10011011
end
10021012

test/test_rdoc_ruby_lex.rb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,27 @@ def test_class_tokenize_hash_symbol
103103
tokens = RDoc::RubyLex.tokenize '{ class:"foo" }', nil
104104

105105
expected = [
106-
@TK::TkLBRACE .new( 0, 1, 0, '{'),
107-
@TK::TkSPACE .new( 1, 1, 1, ' '),
108-
@TK::TkIDENTIFIER.new( 2, 1, 2, 'class'),
109-
@TK::TkSYMBOL .new( 7, 1, 7, ':"foo"'),
110-
@TK::TkSPACE .new(13, 1, 13, ' '),
111-
@TK::TkRBRACE .new(14, 1, 14, '}'),
106+
@TK::TkLBRACE.new( 0, 1, 0, '{'),
107+
@TK::TkSPACE .new( 1, 1, 1, ' '),
108+
@TK::TkSYMBOL.new( 2, 1, 2, 'class:'),
109+
@TK::TkSTRING.new( 8, 1, 8, '"foo"'),
110+
@TK::TkSPACE .new(13, 1, 13, ' '),
111+
@TK::TkRBRACE.new(14, 1, 14, '}'),
112+
@TK::TkNL .new(15, 1, 15, "\n"),
113+
]
114+
115+
assert_equal expected, tokens
116+
end
117+
118+
def test_class_tokenize_double_colon_is_not_hash_symbol
119+
tokens = RDoc::RubyLex.tokenize 'self.class::Row', nil
120+
121+
expected = [
122+
@TK::TkSELF .new( 0, 1, 0, "self"),
123+
@TK::TkDOT .new( 4, 1, 4, "."),
124+
@TK::TkIDENTIFIER.new( 5, 1, 5, "class"),
125+
@TK::TkCOLON2 .new(10, 1, 10, "::"),
126+
@TK::TkCONSTANT .new(12, 1, 12, "Row"),
112127
@TK::TkNL .new(15, 1, 15, "\n"),
113128
]
114129

@@ -444,8 +459,7 @@ def test_class_tokenize_symbol
444459
expected = [
445460
@TK::TkIDENTIFIER.new( 0, 1, 0, 'scope'),
446461
@TK::TkSPACE .new( 5, 1, 5, ' '),
447-
@TK::TkIDENTIFIER.new( 6, 1, 6, 'module'),
448-
@TK::TkCOLON .new(12, 1, 12, ':'),
462+
@TK::TkSYMBOL .new( 6, 1, 6, 'module:'),
449463
@TK::TkSPACE .new(13, 1, 13, ' '),
450464
@TK::TkSYMBOL .new(14, 1, 14, ':v1'),
451465
@TK::TkNL .new(17, 1, 17, "\n"),

0 commit comments

Comments
 (0)