From f0345a6617e2458000210fb6edafccf5a04eca61 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 10 Apr 2015 11:30:55 +0200 Subject: Add function for retrieving the name of a token. --- src/core/common/Token.cpp | 26 ++++++++++++++++++++++++++ src/core/common/Token.hpp | 16 ++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'src/core') diff --git a/src/core/common/Token.cpp b/src/core/common/Token.cpp index 17ce03e..f9e60ce 100644 --- a/src/core/common/Token.cpp +++ b/src/core/common/Token.cpp @@ -20,5 +20,31 @@ namespace ousia { +std::string Token::name() const +{ + if (isSpecial()) { + return specialName(id); + } + return content; +} + +const char* Token::specialName(TokenId id) +{ + switch (id) { + case Tokens::Newline: + return "newline"; + case Tokens::Paragraph: + return "paragraph"; + case Tokens::Section: + return "section"; + case Tokens::Indent: + return "indent"; + case Tokens::Dedent: + return "dedent"; + } + return ""; +} + + } diff --git a/src/core/common/Token.hpp b/src/core/common/Token.hpp index ad36e14..ccbc84b 100644 --- a/src/core/common/Token.hpp +++ b/src/core/common/Token.hpp @@ -189,6 +189,22 @@ struct Token { */ static bool isSpecial(TokenId id) {return id > Tokens::MaxTokenId; } + /** + * Returns the name of the token -- which is either its content or the name + * of the special token (if it is one). + * + * @return the human readable name of this token instance. + */ + std::string name() const; + + /** + * Returns the name of the special token or an empty string if it is not a + * special token. + * + * @param id + */ + static const char* specialName(TokenId id); + /** * The getLocation function allows the tokens to be directly passed as * parameter to Logger or LoggableException instances. -- cgit v1.2.3