Skip to content

Commit 131d947

Browse files
authored
Merge pull request #498 from aycabta/support-quoted-symbol-for-json-style
Support quoted symbol for JSON-style hash literal
2 parents 8566973 + 00734c1 commit 131d947

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def initialize(content, options)
112112
@indent_stack = []
113113
@lex_state = :EXPR_BEG
114114
@space_seen = false
115+
@after_question = false
115116

116117
@continue = false
117118
@line = ""
@@ -383,6 +384,8 @@ def token
383384
set_token_position tk.seek, tk.line_no, tk.char_no
384385
tk = Token(tk1.class, tk.text + tk1.text)
385386
end
387+
@after_question = false if @after_question and !(TkQUESTION === tk)
388+
386389
# Tracer.off
387390
tk
388391
end
@@ -600,6 +603,7 @@ def lex_init()
600603
|op, io|
601604
if @lex_state == :EXPR_END
602605
@lex_state = :EXPR_BEG
606+
@after_question = true
603607
Token(TkQUESTION)
604608
else
605609
ch = getc
@@ -1368,7 +1372,10 @@ def identify_string(ltype, quoted = ltype, type = nil)
13681372
end
13691373
end
13701374

1371-
if subtype
1375+
if peek(0) == ':' and !peek_match?(/^::/) and :EXPR_BEG == @lex_state and !@after_question
1376+
str.concat getc
1377+
return Token(TkSYMBOL, str)
1378+
elsif subtype
13721379
Token(DLtype2Token[ltype], str)
13731380
else
13741381
Token(Ltype2Token[ltype], str)

test/test_rdoc_ruby_lex.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,36 @@ def test_class_tokenize_particular_kind_of_symbols
838838
assert_equal expected, tokens
839839
end
840840

841+
def test_class_tokenize_symbol_with_quote
842+
tokens = RDoc::RubyLex.tokenize <<RUBY, nil
843+
a.include?()?"a":"b"
844+
{"t":1,'t2':2}
845+
RUBY
846+
847+
expected = [
848+
@TK::TkIDENTIFIER.new( 0, 1, 0, "a"),
849+
@TK::TkDOT .new( 1, 1, 1, "."),
850+
@TK::TkFID .new( 2, 1, 2, "include?"),
851+
@TK::TkLPAREN .new(10, 1, 10, "("),
852+
@TK::TkRPAREN .new(11, 1, 11, ")"),
853+
@TK::TkQUESTION .new(12, 1, 12, "?"),
854+
@TK::TkSTRING .new(13, 1, 13, "\"a\""),
855+
@TK::TkCOLON .new(16, 1, 16, ":"),
856+
@TK::TkSTRING .new(17, 1, 17, "\"b\""),
857+
@TK::TkNL .new(20, 1, 20, "\n"),
858+
@TK::TkLBRACE .new(21, 2, 0, "{"),
859+
@TK::TkSYMBOL .new(22, 2, 1, "\"t\":"),
860+
@TK::TkINTEGER .new(26, 2, 5, "1"),
861+
@TK::TkCOMMA .new(27, 2, 6, ","),
862+
@TK::TkSYMBOL .new(28, 2, 7, "'t2':"),
863+
@TK::TkINTEGER .new(33, 2, 12, "2"),
864+
@TK::TkRBRACE .new(34, 2, 13, "}"),
865+
@TK::TkNL .new(35, 2, 21, "\n"),
866+
]
867+
868+
assert_equal expected, tokens
869+
end
870+
841871
def test_unary_minus
842872
ruby_lex = RDoc::RubyLex.new("-1", nil)
843873
assert_equal("-1", ruby_lex.token.value)

0 commit comments

Comments
 (0)