/* 
 * Name:   Standard tokenizers/highlighters.
 * Author: Jonas Höglund (FireFly, firefly.nu).
 * Date:   2010-01-04.
 */

	// Aliases.
var consts = FF.TokenizerConstants;

var container = consts.createContainerPrototype;
var text = consts.createTextPrototype;
var regex = consts.createRegexPrototype;

var textAll = function(key, arr) {
	var out = [];
	
	for (var i=0; i<arr.length; i++)
		out.push(text(key, arr[i]));
	
	return out;
}

function fixPrototypes(hler) {
	var newProtos = [];
	
	for (var i=0; i<hler.prototypes.length; i++) {
		if (typeof(hler.prototypes[i][0]) != 'undefined') {
			for (var j=0; j<hler.prototypes[i].length; j++)
				newProtos.push(hler.prototypes[i][j]);
		} else {
			newProtos.push(hler.prototypes[i]);
		}
	}
	hler.prototypes = newProtos;
}

	// Create FireLang syntax highlighter.
var fireLang = {
	prototypes: [
		container('STRING', '"', '"', false),
		container('STRING', "'", "'", false),
		
			// Spaces, tabs and newlines.
		text('WHITESPACE', " "),
		text('WHITESPACE', "\t"),
		text('WHITESPACE', "\n"),
		
		text('SEPARATOR', ";"),
		
			// Keywords.
		text('KEYWORD', "function"),
		text('KEYWORD', "var"),
		
		container('ANON_FUNCTION', "{", "}", true),
		container('COMMENT', "/*", "*/", false),
		
		regex('NUMBER', /^\d+/),
		regex('OPERATOR_PART', /^[\[\]\-+*/\\()<>&%#$@!?|'¤"^:]+?/),
		text('OPERATOR_PART', "="),
		regex('IDENTIFIER', /^[\w$]+/)
	],
	format: {
		STRING: {color: '#C00'},
		COMMENT: {color: '#666'},
		
		KEYWORD: {color: '#00C', weight: 'bold'},
		IDENTIFIER: {color: '#000'},
	//	NUMBER: {color: '#006', style: 'italic'},
		NUMBER: {color: '#960', style: 'italic'},
		
		SEPARATOR: {weight: 'bold'},
		ANON_FUNCTION: {color: '#600'},
		OPERATOR_PART: {color: '#090'},
	}
};

	// Java syntax highlighter.
var java = {
	prototypes: [
			// Keywords.
		textAll('KEYWORD', ["public", "protected", "private",
				"static", "final", "class", "volatile",
				"transient", "synchronized", "interface",
				"new", "import", "package", "null", "for",
				"if", "else", "while", "goto", "do", "break",
				"continue", "select", "case", "default", "this"]),
		
			// Natives.
		textAll('NATIVE', ["void", "int", "long", "short",
				"byte", "float", "char", "double"]),
		
			// Constant values.
		container('STRING', '"', '"', false),
		regex('NUMBER', /^\d+/),
		
			// Whitespace.
		textAll('WHITESPACE', [" ", "\t", "\n"]),
		
			// Comments.
		regex('COMMENT', /^\/\/[^\n]*/),
		container('COMMENT', "/*", "*/", false),
		
			// Identifiers, others.
		regex('IDENTIFIER', /^[A-Za-z][\w_$]*/),
		
			// Operators and stuff.
		textAll('OPERATOR', [".", "=", ",", "-", "+", "*", "~",
				"/", "^", "|", "&", "{", "}", "(", ")", "[",
				"]", "!", "<", ">"]),
		
			// Delimiter.
		text('DELIMITER', ";"),
	],
	format: {
		KEYWORD: {color: '#909', weight: 'bold'},
		NATIVE: {color: '#00C'},
		IDENTIFIER: {color: '#000'},
		STRING: {color: '#C00'},
		'*STRING': {color: '#A00'},
		COMMENT: {color: '#666'},
		OPERATOR: {color: '#090'},
		DELIMITER: {weight: 'bold'},
	//	NUMBER: {color: '#006', style: 'italic'},
		NUMBER: {color: '#960', style: 'italic'},
	}
}
fixPrototypes(java);

var brainfuck = {
	prototypes: [
		textAll('CHANGES', ["+", "-"]),
		textAll('LOOP', ["[", "]"]),
		textAll('IO', [".", ","]),
		textAll('MOVE', ["<", ">"]),
		regex('COMMENT', /[^+-\[\],.<>]+/)
	],
	format: {
		CHANGES: {color: '#00F'},
		LOOP: {color: '#960'},
		IO: {color: '#0C0', weight: 'bold'},
		MOVE: {color: '#900'},
		COMMENT: {color: '#666'},
	}
}
fixPrototypes(brainfuck);

var migol = {
	prototypes: [
		textAll('WHITESPACE', [" ", "\t", "\n"]),
		regex('COMMENT', /^\/\/[^\n]*/),
		
		regex('CHARACTER', /^'./),
		
		textAll('OPERATOR', ["<", "$", ">", "+", "-", "*", "/",
				"^", "~", "="]),
		text('CONDITIONAL', "?"),
		text('SEPARATOR', ","),
		textAll('DEREFER', ["[", "]"]),
		
		regex('NUMBER', /^\d+/),
		text('POSITION', "#"),
		text('INPUT', "@"),
	],
	format: {
		COMMENT: {color: '#666'},
		NUMBER: {color: '#960'},
		CHARACTER: {color: '#C00'},
		POSITION: {color: '#090', weight: 'bold'},
		INPUT: {color: '#090', weight: 'bold'},
		OPERATOR: {color: '#000'},
		CONDITIONAL: {color: '#900', weight: 'bold'},
		SEPARATOR: {color: '#0C0'},
		DEREFER: {color: '#03C'},
	}
}
fixPrototypes(migol);

FF.Tokenizers = {
	firelang: fireLang,
	java: java,
	brainfuck: brainfuck,
	migol: migol,
};