Lexesis/examples/SyntaxHighlighter/include/highlighter.h

91 lines
2.2 KiB
C
Raw Normal View History

2016-05-28 00:51:12 +02:00
#pragma once
#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H
#include "XMLLexer.h"
#include <string>
#include <vector>
#include <map>
#include <iostream>
class Highlighter {
public:
/**
* Create a new Highlighter with the content of the xml file
*/
2016-05-28 00:51:12 +02:00
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
*/
2016-05-28 00:51:12 +02:00
virtual void highlight(std::ostream &os)=0;
protected:
/**
* Do the actual work with the lexer
*/
2016-05-28 00:51:12 +02:00
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
*/
2016-05-28 00:51:12 +02:00
enum TokenType {
COMMENT,
2016-05-28 00:51:12 +02:00
TAG,
CONTENT,
ELEMENT,
ATTRIBUTE,
ATTRIBURE_CONTENT,
WHITESPACE,
BRACKET,
nonmatching
};
/**
* Represent all the different colors
*/
2016-05-28 00:51:12 +02:00
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
*/
2016-05-28 00:51:12 +02:00
struct Token {
std::string content = "";
Color color;
TokenType type;
};
// Used to determine the color connected to the TokenType
2016-05-28 00:51:12 +02:00
std::map<TokenType, Color> colormap;
// Hold the tokens
2016-05-28 00:51:12 +02:00
std::vector<Token> m_tokens;
// Hold the first lexer
2016-05-28 00:51:12 +02:00
XMLLexer *m_lexer;
};
class ConsoleHighlighter: public Highlighter {
/**
* A SyntaxHighlighter specifically for console output
* Look at the base class for more information
*/
2016-05-28 00:51:12 +02:00
public:
ConsoleHighlighter(std::istream &file);
void highlight(std::ostream &os);
};
#endif // HIGHLIGHTER_H