/*
*  Lexeme object and related functions for the LibreSignage
*  markup transpiler.
*/

module.exports.Lexeme = class Lexeme {
	/*
	*  Lexeme object implementation.
	*
	*  'type' is one of the the types defined in lexer_defs.js.
	*  'raw' is the raw string this lexeme matches.
	*  'ln_num' and 'ln_pos' are the line number and line position
	*  respectively.
	*/
	constructor(type, raw, ln_num, ln_pos) {
		this.type = type;
		this.raw = raw;
		this.ln_num = ln_num;
		this.ln_pos = ln_pos;
	}

	matches(lexeme) {
		// Compare lexeme types.
		return this.type == lexeme.type;
	}

	ch_type(new_type) {
		// Change lexeme type.
		this.type = new_type;
	}

	dump() {
		console.log(
			`[@${this.ln_num}:${this.ln_pos}]` +
			`(${this.type}, '${this.raw}')`
		);
	}

	clone() {
		// Clone this lexeme object and return the new one.
		return new module.exports.Lexeme(
			this.type,
			this.raw,
			this.ln_num,
			this.ln_pos
		);
	}
}

module.exports.dump_lexemes = function(lexemes) {
	/*
	*  Dump all lexemes from the 'lexemes' array.
	*/
	for (let l of lexemes) { l.dump(); }
}
