From 3aa615cc9d0c7d875fcdaecdd7394ce468e389f0 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 11:45:36 +0900 Subject: [PATCH 1/4] Treat struct names the same as class names --- lib/rouge/lexers/cpp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rouge/lexers/cpp.rb b/lib/rouge/lexers/cpp.rb index 3fd9d1b29a..3ae5500df2 100644 --- a/lib/rouge/lexers/cpp.rb +++ b/lib/rouge/lexers/cpp.rb @@ -57,7 +57,7 @@ def self.reserved dq = /\d('?\d)*/ prepend :statements do - rule %r/class\b/, Keyword, :classname + rule %r/(class|struct)\b/, Keyword, :classname rule %r/\d+(\.\d+)?(?:h|(?:min)|s|(?:ms)|(?:us)|(?:ns))/, Num::Other rule %r((#{dq}[.]#{dq}?|[.]#{dq})(e[+-]?#{dq}[lu]*)?)i, Num::Float rule %r(#{dq}e[+-]?#{dq}[lu]*)i, Num::Float From 9a8dcc0279bad662b6b41ac0ae7e255a3a0bc843 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 12:03:22 +0900 Subject: [PATCH 2/4] Add template examples to visual sample --- spec/visual/samples/cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/visual/samples/cpp b/spec/visual/samples/cpp index abd7f6e5be..d09a703fd9 100644 --- a/spec/visual/samples/cpp +++ b/spec/visual/samples/cpp @@ -78,6 +78,22 @@ double distance = [](double x, double y, double xx, double yy) -> double { return abs(x-xx) + abs(y-yy); }; +// templates +namespace N +{ + template + class Y // template definition + { + void mf() { } + }; +} +template class N::Y; +template void N::Y::mf(); +template struct Z; +template concept C1 = sizeof(T) != sizeof(int); +template struct S1 { }; +template using Ptr = T*; + // variadic template template void forward_args(F f, Args... args) { From 3a2feec918cade9b235c37e319788f06be230b93 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 12:09:11 +0900 Subject: [PATCH 3/4] Add state for template declarations --- lib/rouge/lexers/cpp.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/rouge/lexers/cpp.rb b/lib/rouge/lexers/cpp.rb index 3ae5500df2..a750e286a7 100644 --- a/lib/rouge/lexers/cpp.rb +++ b/lib/rouge/lexers/cpp.rb @@ -22,10 +22,10 @@ class Cpp < C def self.keywords @keywords ||= super + Set.new(%w( - asm auto catch const_cast delete dynamic_cast explicit export - friend mutable namespace new operator private protected public - reinterpret_cast restrict size_of static_cast template this throw - throws typeid typename using virtual final override + asm auto catch const_cast delete dynamic_cast explicit export friend + mutable namespace new operator private protected public + reinterpret_cast restrict size_of static_cast this throw throws + typeid typename using virtual final override alignas alignof constexpr decltype noexcept static_assert thread_local try @@ -58,6 +58,7 @@ def self.reserved prepend :statements do rule %r/(class|struct)\b/, Keyword, :classname + rule %r/template\b/, Keyword, :template rule %r/\d+(\.\d+)?(?:h|(?:min)|s|(?:ms)|(?:us)|(?:ns))/, Num::Other rule %r((#{dq}[.]#{dq}?|[.]#{dq})(e[+-]?#{dq}[lu]*)?)i, Num::Float rule %r(#{dq}e[+-]?#{dq}[lu]*)i, Num::Float @@ -77,6 +78,12 @@ def self.reserved rule %r/[.]{3}/, Operator mixin :whitespace end + + state :template do + rule %r/>/, Punctuation, :pop! + rule %r/(class|struct|typename)\b/, Keyword, :classname + mixin :root + end end end end From d78c5c6e1c896775d334ad8283800e931c246ee3 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 17:06:27 +0900 Subject: [PATCH 4/4] Simplify rule in :template state --- lib/rouge/lexers/cpp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rouge/lexers/cpp.rb b/lib/rouge/lexers/cpp.rb index a750e286a7..9a74e19299 100644 --- a/lib/rouge/lexers/cpp.rb +++ b/lib/rouge/lexers/cpp.rb @@ -81,7 +81,7 @@ def self.reserved state :template do rule %r/>/, Punctuation, :pop! - rule %r/(class|struct|typename)\b/, Keyword, :classname + rule %r/typename\b/, Keyword, :classname mixin :root end end