Skip to content
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
7 changes: 4 additions & 3 deletions lib/rouge/lexers/cpp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ def self.reserved
rule %r/(class|struct)\b/, Keyword, :classname
rule %r/template\b/, Keyword, :template
rule %r/#{dq}(\.#{dq})?(?:y|d|h|(?:min)|s|(?:ms)|(?:us)|(?:ns)|i|(?:if)|(?:il))\b/, Num::Other
rule %r((#{dq}[.]#{dq}?|[.]#{dq})(e[+-]?#{dq}[lu]*)?)i, Num::Float
rule %r(#{dq}e[+-]?#{dq}[lu]*)i, Num::Float
rule %r/0x\h('?\h)*[lu]*/i, Num::Hex
rule %r((#{dq}[.]#{dq}?|[.]#{dq})([ep][+-]?#{dq})?[luf]*)i, Num::Float
rule %r(#{dq}[ep][+-]?#{dq}[luf]*)i, Num::Float
rule %r/0x\h('?\h)*([ep][+-]?#{dq})?[lu]*/i, Num::Hex
rule %r/0x\h('?\h)*[.]\h+([ep][+-]?#{dq})[luf]*/i, Num::Hex
rule %r/0b[01]+('[01]+)*/, Num::Bin
rule %r/0[0-7]('?[0-7])*[lu]*/i, Num::Oct
rule %r/#{dq}[lu]*/i, Num::Integer
Expand Down
41 changes: 41 additions & 0 deletions spec/visual/samples/cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,44 @@ export struct Shape {
int m_x;
int m_y;
}

float test(8.874L);
float test(8.874l);
float test(8.874F);
float test(8.874f);
float test(8.874U);
float test(8.874u);

// via https://en.cppreference.com/w/cpp/language/floating_literal
int main()
{
std::cout
<< "Literal" "\t" "Printed value" << std::left
<< OUT( 58. ) // double
<< OUT( 4e2 ) // double
<< OUT( 123.456e-67 ) // double
<< OUT( 123.456e-67f ) // float, truncated to zero
<< OUT( .1E4f ) // float
<< OUT( 0x10.1p0 ) // double
<< OUT( 0x1p5 ) // double
<< OUT( 0x1e5 ) // integer literal, not floating-point
<< OUT( 3.14'15'92 ) // double, single quotes ignored (C++14)
<< OUT( 1.18e-4932l ) // long double
<< std::setprecision(39)
<< OUT( 3.4028234e38f ) // float
<< OUT( 3.4028234e38 ) // double
<< OUT( 3.4028234e38l ) // long double
<< '\n';

static_assert(3.4028234e38f == std::numeric_limits<float>::max());

static_assert(3.4028234e38f == // ends with 4
3.4028235e38f); // ends with 5

static_assert(3.4028234e38 != // ends with 4
3.4028235e38); // ends with 5

// Both floating-point constants below are 3.4028234e38
static_assert(3.4028234e38f != // a float (then promoted to double)
3.4028234e38); // a double
}