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



63
64
65
66
# File 'lib/ansisys.rb', line 63

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.



59
60
61
# File 'lib/ansisys.rb', line 59

def buffer
  @buffer
end

Instance Method Details

#lex!Object

returns array of tokens while deleting the tokenized part from buffer



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ansisys.rb', line 74

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



69
70
71
# File 'lib/ansisys.rb', line 69

def push(string)
	@buffer += string
end