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

Improve consistency of lexing of class-like names in C++ lexer #1495

Merged
merged 4 commits into from Apr 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/rouge/lexers/cpp.rb
Expand Up @@ -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
Expand Down Expand Up @@ -57,7 +57,8 @@ def self.reserved
dq = /\d('?\d)*/

prepend :statements do
rule %r/class\b/, Keyword, :classname
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
Expand All @@ -77,6 +78,12 @@ def self.reserved
rule %r/[.]{3}/, Operator
mixin :whitespace
end

state :template do
rule %r/>/, Punctuation, :pop!
rule %r/typename\b/, Keyword, :classname
mixin :root
end
end
end
end
16 changes: 16 additions & 0 deletions spec/visual/samples/cpp
Expand Up @@ -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 T>
class Y // template definition
{
void mf() { }
};
}
template class N::Y<char*>;
template void N::Y<double>::mf();
template struct Z<double>;
template<typename T> concept C1 = sizeof(T) != sizeof(int);
template<C1 T> struct S1 { };
template<C1 T> using Ptr = T*;

// variadic template
template<class F, class... Args>
void forward_args(F f, Args... args) {
Expand Down