diff --git a/examples/SyntaxHighlighter/include/highlighter.h b/examples/SyntaxHighlighter/include/highlighter.h index 0e3bd39..ffbc4ca 100644 --- a/examples/SyntaxHighlighter/include/highlighter.h +++ b/examples/SyntaxHighlighter/include/highlighter.h @@ -11,16 +11,33 @@ class Highlighter { public: + /** + * Create a new Highlighter with the content of the xml file + */ Highlighter(std::istream &file); virtual ~Highlighter(); + + /** + * Do the actual highlighting and write it to the provided ostream + * This is a pure virtual function and has to be implemented when creating a new kind of highlighter + */ virtual void highlight(std::ostream &os)=0; protected: - + + /** + * Do the actual work with the lexer + */ + void process(); + /** + * Local Tokentype used to identify what part of the xml structure is inside the token + * This is necessary because we use two different lexers with different TokenTypes + */ + enum TokenType { COMMENT, TAG, @@ -32,20 +49,38 @@ class Highlighter { BRACKET, nonmatching }; + + /** + * Represent all the different colors + */ enum Color { Red, Green, Blue, Orange, Yellow, Cyan, Grey, Black, White, Magenta, Pink, Brown, Indigo, Violet, Undefined}; // All the colors, not all of them are used, but it's easy to change now + + /** + * A structure used to hold the tokens with their content, TokenType and color + */ struct Token { std::string content = ""; Color color; TokenType type; }; + + // Used to determine the color connected to the TokenType std::map colormap; + // Hold the tokens std::vector m_tokens; + // Hold the first lexer XMLLexer *m_lexer; }; class ConsoleHighlighter: public Highlighter { + + /** + * A SyntaxHighlighter specifically for console output + * Look at the base class for more information + */ + public: ConsoleHighlighter(std::istream &file); void highlight(std::ostream &os); diff --git a/examples/SyntaxHighlighter/src/attributelexer.lxs b/examples/SyntaxHighlighter/src/attributelexer.lxs index 3bdc00f..18cc9a0 100644 --- a/examples/SyntaxHighlighter/src/attributelexer.lxs +++ b/examples/SyntaxHighlighter/src/attributelexer.lxs @@ -1,6 +1,6 @@ ELEMENT = "]*" ATTRIBUTE_CONTENT_SINGLE_QUOTES = '[^<>']*' BRACKET = [] diff --git a/examples/SyntaxHighlighter/src/highlighter.cpp b/examples/SyntaxHighlighter/src/highlighter.cpp index cac778a..407291d 100644 --- a/examples/SyntaxHighlighter/src/highlighter.cpp +++ b/examples/SyntaxHighlighter/src/highlighter.cpp @@ -5,7 +5,7 @@ Highlighter::Highlighter(std::istream &file) { m_lexer = new XMLLexer(file); - colormap[CONTENT] = Undefined; + colormap[CONTENT] = Undefined; // The abstract base class shouldn't have default colors colormap[ELEMENT] = Undefined; colormap[ATTRIBUTE] = Undefined; colormap[ATTRIBURE_CONTENT] = Undefined; @@ -19,12 +19,12 @@ Highlighter::~Highlighter() { } void Highlighter::process() { - while (true) { + while (true) { // Wait until the NoMoreTokens exception is thrown try { - XMLLexer::Token token = m_lexer->nextToken(); + XMLLexer::Token token = m_lexer->nextToken(); // Get the next token Token newtoken; newtoken.content = token.content; - switch(token.type) { + switch(token.type) { // setup local tokentype case XMLLexer::TokenType::CONTENT: newtoken.type = CONTENT; break; @@ -38,11 +38,11 @@ void Highlighter::process() { newtoken.type = nonmatching; break; } - newtoken.color = colormap.find(newtoken.type)->second; + newtoken.color = colormap.find(newtoken.type)->second; // get the appropriate color m_tokens.push_back(newtoken); } catch (XMLLexer::NoMoreTokens &err) { - break; - } catch (XMLLexer::NoMatch& err) { + break; // We reached the end of the file + } catch (XMLLexer::NoMatch& err) { // No match was found, setup a new token with 1 character and tokentype nonmatching + skip this character Token newtoken; newtoken.content = m_lexer->peek(); m_lexer->skip(1); @@ -53,7 +53,7 @@ void Highlighter::process() { auto tokens = std::move(m_tokens); m_tokens.clear(); for(auto &tagtoken: tokens) { - if(tagtoken.type == TAG && !tagtoken.content.empty()) { + if(tagtoken.type == TAG && !tagtoken.content.empty()) { // If the token was a tag, use a second lexer to find attributes etc. std::istringstream content(tagtoken.content); AttributeLexer attributelexer(content); while (true) { @@ -100,8 +100,8 @@ void Highlighter::process() { } ConsoleHighlighter::ConsoleHighlighter(std::istream &file): Highlighter(file) { - colormap[CONTENT] = White; - colormap[ELEMENT] = Blue; + colormap[CONTENT] = White; // Fill in the colors we want to use for each tokentype + colormap[ELEMENT] = Blue; // Change these for different colors colormap[TAG] = Magenta; colormap[ATTRIBUTE] = Yellow; colormap[ATTRIBURE_CONTENT] = Green; @@ -112,7 +112,7 @@ ConsoleHighlighter::ConsoleHighlighter(std::istream &file): Highlighter(file) { } void ConsoleHighlighter::highlight(std::ostream &os) { - for(auto &token: m_tokens) { + for(auto &token: m_tokens) { // Go over all tokens and print them out with the right color switch(token.color) { case Yellow: os << "\033[1;33m" << token.content << "\033[0m";