Skip to content

Commit

Permalink
Merge pull request #498 from aycabta/support-quoted-symbol-for-json-s…
Browse files Browse the repository at this point in the history
…tyle

Support quoted symbol for JSON-style hash literal
  • Loading branch information
hsbt authored Aug 23, 2017
2 parents 8566973 + 00734c1 commit 131d947
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/rdoc/ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def initialize(content, options)
@indent_stack = []
@lex_state = :EXPR_BEG
@space_seen = false
@after_question = false

@continue = false
@line = ""
Expand Down Expand Up @@ -383,6 +384,8 @@ def token
set_token_position tk.seek, tk.line_no, tk.char_no
tk = Token(tk1.class, tk.text + tk1.text)
end
@after_question = false if @after_question and !(TkQUESTION === tk)

# Tracer.off
tk
end
Expand Down Expand Up @@ -600,6 +603,7 @@ def lex_init()
|op, io|
if @lex_state == :EXPR_END
@lex_state = :EXPR_BEG
@after_question = true
Token(TkQUESTION)
else
ch = getc
Expand Down Expand Up @@ -1368,7 +1372,10 @@ def identify_string(ltype, quoted = ltype, type = nil)
end
end

if subtype
if peek(0) == ':' and !peek_match?(/^::/) and :EXPR_BEG == @lex_state and !@after_question
str.concat getc
return Token(TkSYMBOL, str)
elsif subtype
Token(DLtype2Token[ltype], str)
else
Token(Ltype2Token[ltype], str)
Expand Down
30 changes: 30 additions & 0 deletions test/test_rdoc_ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,36 @@ def test_class_tokenize_particular_kind_of_symbols
assert_equal expected, tokens
end

def test_class_tokenize_symbol_with_quote
tokens = RDoc::RubyLex.tokenize <<RUBY, nil
a.include?()?"a":"b"
{"t":1,'t2':2}
RUBY

expected = [
@TK::TkIDENTIFIER.new( 0, 1, 0, "a"),
@TK::TkDOT .new( 1, 1, 1, "."),
@TK::TkFID .new( 2, 1, 2, "include?"),
@TK::TkLPAREN .new(10, 1, 10, "("),
@TK::TkRPAREN .new(11, 1, 11, ")"),
@TK::TkQUESTION .new(12, 1, 12, "?"),
@TK::TkSTRING .new(13, 1, 13, "\"a\""),
@TK::TkCOLON .new(16, 1, 16, ":"),
@TK::TkSTRING .new(17, 1, 17, "\"b\""),
@TK::TkNL .new(20, 1, 20, "\n"),
@TK::TkLBRACE .new(21, 2, 0, "{"),
@TK::TkSYMBOL .new(22, 2, 1, "\"t\":"),
@TK::TkINTEGER .new(26, 2, 5, "1"),
@TK::TkCOMMA .new(27, 2, 6, ","),
@TK::TkSYMBOL .new(28, 2, 7, "'t2':"),
@TK::TkINTEGER .new(33, 2, 12, "2"),
@TK::TkRBRACE .new(34, 2, 13, "}"),
@TK::TkNL .new(35, 2, 21, "\n"),
]

assert_equal expected, tokens
end

def test_unary_minus
ruby_lex = RDoc::RubyLex.new("-1", nil)
assert_equal("-1", ruby_lex.token.value)
Expand Down

0 comments on commit 131d947

Please sign in to comment.