You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1021 B

#include "highlighter.h"
HighLighter::HighLighter(QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
HighlightRule num_rule;
num_rule.pattern.setPattern("[0-9]{1,1}");
num_rule.format.setForeground(QColor(144,238,144));
highlightrules.append(num_rule);
HighlightRule key_rule;
key_rule.pattern.setPattern("\\b(char|string|int|float|double|bool|void|true|flase)\\b");
key_rule.format.setForeground(QColor(30,144,255));
highlightrules.append(key_rule);
HighlightRule code_rule;
code_rule.pattern.setPattern("\\b(if|while|for|else|break|return|switch|case)\\b");
code_rule.format.setForeground(QColor(186,85,211));
highlightrules.append(code_rule);
}
void HighLighter::highlightBlock(const QString &text)
{
for(const HighlightRule &rule : highlightrules){
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while(index>=0){
int length = expression.matchedLength();
setFormat(index,length,rule.format);
index = expression.indexIn(text,index+length);}
}
}