Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support quoted symbol for JSON-style hash literal #498

Merged
merged 3 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -597,6 +600,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 @@ -1365,7 +1369,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 @@ -805,6 +805,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