Class: AnsiSys::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/ansisys.rb

Constant Summary collapse

PARAMETER_AND_LETTER =

Control Sequence Introducer and following code

/\A([\d;]*)([[:alpha:]])/o
CODE_EQUIVALENT =
{
	"\r" => ['B'],
	"\n" => ['E'],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csis = ["\x1b["]) ⇒ Lexer

csis is an Array of Code Sequence Introducers which can be e[, x9B, or both



46
47
48
49
# File 'lib/ansisys.rb', line 46

def initialize(csis = ["\x1b["])	# CSI can also be "\x9B"
	@code_start_re = Regexp.union(*(CODE_EQUIVALENT.keys + csis))
	@buffer = ''
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



42
43
44
# File 'lib/ansisys.rb', line 42

def buffer
  @buffer
end

Instance Method Details

#lex!Object

returns array of tokens while deleting the tokenized part from buffer



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ansisys.rb', line 57

def lex!
	r = Array.new
	@buffer.gsub!(/(?:\r\n|\n\r)/, "\n")
	while @code_start_re =~ @buffer
		r << [:string, $`] unless $`.empty?
		if CODE_EQUIVALENT.has_key?($&)
			CODE_EQUIVALENT[$&].each do |c|
				r << [:code, c]
			end
			@buffer = $'
		else
			csi = $&
			residual = $'
			if PARAMETER_AND_LETTER =~ residual
				r << [:code, $&]
				@buffer = $'
			else
				@buffer = csi + residual
				return r
			end
		end
	end
	r << [:string, @buffer] unless @buffer.empty?
	@buffer = ''
	return r
end

#push(string) ⇒ Object

add the String (clear text with some or no escape sequences) to buffer



52
53
54
# File 'lib/ansisys.rb', line 52

def push(string)
	@buffer += string
end