/*
Copyright (C) 2006 - 2009 Evan Teran
                   eteran@alum.rit.edu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "SyntaxHighlighter.h"

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
SyntaxHighlighter::SyntaxHighlighter(QObject *parent) : QSyntaxHighlighter(parent) {
	createRules();
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) {
	createRules();
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
SyntaxHighlighter::SyntaxHighlighter(QTextEdit *parent) : QSyntaxHighlighter(parent) {
	createRules();
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void SyntaxHighlighter::createRules() {
	HighlightingRule	rule;
	QTextCharFormat		format;
	
	// registers
	format = QTextCharFormat();
	format.setForeground(Qt::red);
	format.setFontWeight(QFont::Bold);
	rule.pattern	= QRegExp("\\b(((e|r)(ax|bx|cx|dx|bp|sp|si|di|ip))|([abcd](l|h))|([cdefgs]s)|(x?mm[0-7])|r(8|9|(1[0-5]))[dwb]?)\\b");
	rule.format		= format;
	rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
	rules.append(rule);

	// constants
	format = QTextCharFormat();
	format.setForeground(Qt::black);
	format.setFontWeight(QFont::Normal);
	rule.pattern	= QRegExp("\\b((0[0-7]*)|(0(x|X)[0-9a-fA-F]+)|([1-9][0-9]*))\\b");
	rule.format		= format;
	rules.append(rule);

	// pointer types
	format = QTextCharFormat();
	format.setForeground(Qt::darkGreen);
	format.setFontWeight(QFont::Normal);
	rule.pattern	= QRegExp("\\b(t?byte|(xmm|[qdf]?)word) ptr\\b");
	rule.format		= format;
	rules.append(rule);
	
	// prefix
	format = QTextCharFormat();
	format.setForeground(Qt::black);
	format.setFontWeight(QFont::Bold);
	rule.pattern	= QRegExp("\\b(lock|rep(ne)?)\\b");
	rule.format		= format;
	rules.append(rule);
	
	// flow control
	format = QTextCharFormat();
	format.setBackground(Qt::yellow);
	rule.pattern	= QRegExp("\\b(jmpf?|call|jn?[blopsz]|jn?[bl]e|jcez)\\b");
	rule.format		= format;
	rules.append(rule);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void SyntaxHighlighter::highlightBlock(const QString &text) {
	foreach (const HighlightingRule &rule, rules) {
		QRegExp expression(rule.pattern);
		int index = expression.indexIn(text);
		while (index >= 0) {
			const int length = expression.matchedLength();
			setFormat(index, length, rule.format);
			index = expression.indexIn(text, index + length);
		}
	}
}
