Skip to content

Support rational and imaginary #461

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

Merged
merged 3 commits into from
Jul 18, 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
18 changes: 18 additions & 0 deletions lib/rdoc/ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,7 @@ def identify_number(op = "")
type = TkINTEGER
allow_point = true
allow_e = true
allow_ri = true
non_digit = false
while ch = getc
num << ch
Expand Down Expand Up @@ -1154,8 +1155,25 @@ def identify_number(op = "")
num << getc
end
allow_e = false
allow_ri = false
allow_point = false
non_digit = ch
when allow_ri && "r"
if non_digit
raise Error, "trailing `#{non_digit}' in number"
end
type = TkRATIONAL
if peek(0) == 'i'
type = TkIMAGINARY
num << getc
end
break
when allow_ri && "i"
if non_digit && non_digit != "r"
raise Error, "trailing `#{non_digit}' in number"
end
type = TkIMAGINARY
break
else
if non_digit
raise Error, "trailing `#{non_digit}' in number"
Expand Down
2 changes: 2 additions & 0 deletions lib/rdoc/ruby_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ def Token(token, value = nil)

[:TkINTEGER, TkVal],
[:TkFLOAT, TkVal],
[:TkRATIONAL, TkVal],
[:TkIMAGINARY, TkVal],
[:TkSTRING, TkVal],
[:TkHEREDOC, TkVal],
[:TkXSTRING, TkVal],
Expand Down
19 changes: 19 additions & 0 deletions test/test_rdoc_ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,5 +418,24 @@ def test_unary_minus
assert_equal("-0.1", ruby_lex.token.value)
end

def test_rational_imaginary_tokenize
tokens = RDoc::RubyLex.tokenize '1.11r + 2.34i + 5.55ri', nil

expected = [
@TK::TkRATIONAL .new( 0, 1, 0, '1.11r'),
@TK::TkSPACE .new( 5, 1, 5, ' '),
@TK::TkPLUS .new( 6, 1, 6, '+'),
@TK::TkSPACE .new( 7, 1, 7, ' '),
@TK::TkIMAGINARY.new( 8, 1, 8, '2.34i'),
@TK::TkSPACE .new(13, 1, 13, ' '),
@TK::TkPLUS .new(14, 1, 14, '+'),
@TK::TkSPACE .new(15, 1, 15, ' '),
@TK::TkIMAGINARY.new(16, 1, 16, '5.55ri'),
@TK::TkNL .new(22, 1, 22, "\n"),
]

assert_equal expected, tokens
end

end