summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCpp
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2021-05-21 14:39:42 +0300
committerAndreas Kling <kling@serenityos.org>2021-05-22 15:52:11 +0200
commit0c9db38e8f99af4ac005c1817cd3877601de83be (patch)
tree477c04ce0049f9c2a931b407bab59380a83af281 /Userland/Libraries/LibCpp
parentcdea9f5339721f5a78ac19227a979c2e522ac141 (diff)
downloadserenity-0c9db38e8f99af4ac005c1817cd3877601de83be.zip
LibCpp: Modify Token::to_string() to include more information
Token::to_string() now includes not only the token's type, but also its text and span in the document.
Diffstat (limited to 'Userland/Libraries/LibCpp')
-rw-r--r--Userland/Libraries/LibCpp/Parser.cpp9
-rw-r--r--Userland/Libraries/LibCpp/SyntaxHighlighter.cpp2
-rw-r--r--Userland/Libraries/LibCpp/Token.cpp12
-rw-r--r--Userland/Libraries/LibCpp/Token.h7
4 files changed, 18 insertions, 12 deletions
diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp
index da3ef9f594..d93b977dca 100644
--- a/Userland/Libraries/LibCpp/Parser.cpp
+++ b/Userland/Libraries/LibCpp/Parser.cpp
@@ -21,12 +21,7 @@ Parser::Parser(const StringView& program, const String& filename, Preprocessor::
if constexpr (CPP_DEBUG) {
dbgln("Tokens:");
for (auto& token : m_tokens) {
- StringView text;
- if (token.start().line != token.end().line || token.start().column > token.end().column)
- text = {};
- else
- text = text_of_token(token);
- dbgln("{} {}:{}-{}:{} ({})", token.to_string(), token.start().line, token.start().column, token.end().line, token.end().column, text);
+ dbgln("{}", token.to_string());
}
}
}
@@ -930,7 +925,7 @@ Optional<size_t> Parser::index_of_token_at(Position pos) const
void Parser::print_tokens() const
{
for (auto& token : m_tokens) {
- dbgln("{}", token.to_string());
+ outln("{}", token.to_string());
}
}
diff --git a/Userland/Libraries/LibCpp/SyntaxHighlighter.cpp b/Userland/Libraries/LibCpp/SyntaxHighlighter.cpp
index 79363df170..4b7d716b51 100644
--- a/Userland/Libraries/LibCpp/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibCpp/SyntaxHighlighter.cpp
@@ -63,7 +63,7 @@ void SyntaxHighlighter::rehighlight(const Palette& palette)
Vector<GUI::TextDocumentSpan> spans;
for (auto& token : tokens) {
- dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "{} @ {}:{} - {}:{}", token.to_string(), token.start().line, token.start().column, token.end().line, token.end().column);
+ dbgln_if(SYNTAX_HIGHLIGHTING_DEBUG, "{} @ {}:{} - {}:{}", token.type_as_string(), token.start().line, token.start().column, token.end().line, token.end().column);
GUI::TextDocumentSpan span;
span.range.set_start({ token.start().line, token.start().column });
span.range.set_end({ token.end().line, token.end().column });
diff --git a/Userland/Libraries/LibCpp/Token.cpp b/Userland/Libraries/LibCpp/Token.cpp
index 06c21fcffe..481ca0e42e 100644
--- a/Userland/Libraries/LibCpp/Token.cpp
+++ b/Userland/Libraries/LibCpp/Token.cpp
@@ -5,6 +5,7 @@
*/
#include "Token.h"
+#include <AK/String.h>
namespace Cpp {
@@ -24,4 +25,15 @@ bool Position::operator<=(const Position& other) const
{
return !(*this > other);
}
+
+String Token::to_string() const
+{
+ return String::formatted("{} {}:{}-{}:{} ({})", type_to_string(m_type), start().line, start().column, end().line, end().column, text());
+}
+
+String Token::type_as_string() const
+{
+ return type_to_string(m_type);
+}
+
}
diff --git a/Userland/Libraries/LibCpp/Token.h b/Userland/Libraries/LibCpp/Token.h
index 3f9253b06a..7a2fc919ed 100644
--- a/Userland/Libraries/LibCpp/Token.h
+++ b/Userland/Libraries/LibCpp/Token.h
@@ -116,10 +116,9 @@ struct Token {
VERIFY_NOT_REACHED();
}
- const char* to_string() const
- {
- return type_to_string(m_type);
- }
+ String to_string() const;
+ String type_as_string() const;
+
const Position& start() const { return m_start; }
const Position& end() const { return m_end; }