Class: MathML::LaTeX::Scanner

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/math_ml/latex.rb

Instance Method Summary collapse

Instance Method Details

#_checkObject



55
# File 'lib/math_ml/latex.rb', line 55

alias :_check :check

#_eos?Object



54
# File 'lib/math_ml/latex.rb', line 54

alias :_eos? :eos?

#_scanObject



56
# File 'lib/math_ml/latex.rb', line 56

alias :_scan :scan

#check(re) ⇒ Object



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

def check(re)
	skip_space_and(true){_check(re)}
end

#check_any(remain_space = false) ⇒ Object



110
111
112
# File 'lib/math_ml/latex.rb', line 110

def check_any(remain_space=false)
	skip_space_and(true){scan_any(remain_space)}
end

#check_blockObject



83
84
85
# File 'lib/math_ml/latex.rb', line 83

def check_block
	skip_space_and(true){scan_block}
end

#check_commandObject



71
72
73
# File 'lib/math_ml/latex.rb', line 71

def check_command
	check(RE::COMMANDS)
end

#check_optionObject



147
148
149
# File 'lib/math_ml/latex.rb', line 147

def check_option
	skip_space_and(true){scan_option}
end

#doneObject



37
38
39
# File 'lib/math_ml/latex.rb', line 37

def done
	self.string[0, pos]
end

#eos?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/math_ml/latex.rb', line 67

def eos?
	_eos? || _check(/#{RE::SPACE}+\z/)
end

#peek_commandObject



79
80
81
# File 'lib/math_ml/latex.rb', line 79

def peek_command
	check_command ? self[1] : nil
end

#scan(re) ⇒ Object



63
64
65
# File 'lib/math_ml/latex.rb', line 63

def scan(re)
	skip_space_and(false){_scan(re)}
end

#scan_any(remain_space = false) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/math_ml/latex.rb', line 114

def scan_any(remain_space=false)
	p = pos
	scan_space
	r = remain_space ? matched.to_s : ""
	case
	when s = scan_block
	when s = scan_command
	else
		unless _scan(/./) || remain_space
			self.pos = p
			return nil
		end
		s = matched.to_s
	end
	r << s
end

#scan_blockObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/math_ml/latex.rb', line 87

def scan_block
	return nil unless scan(/\{/)
	block = "{"
	bpos = pos-1
	nest = 1
	while _scan(/(#{MBEC}*?)([\{\}])/)
		block << matched
		case self[2]
		when "{"
			nest+=1
		when "}"
			nest-=1
			break if nest==0
		end
	end
	if nest>0
		self.pos = bpos
		raise BlockNotClosed
	end
	self.pos = bpos
	_scan(/\A\{(#{Regexp.escape(block[RE::BLOCK, 1].to_s)})\}/)
end

#scan_commandObject



75
76
77
# File 'lib/math_ml/latex.rb', line 75

def scan_command
	scan(RE::COMMANDS)
end

#scan_optionObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/math_ml/latex.rb', line 131

def scan_option
	return nil unless scan(/\[/)
	opt = "["
	p = pos-1
	until (s=scan_any(true)) =~ /\A#{RE::SPACE}*\]\z/
		opt << s
		if eos?
			self.pos = p
			raise OptionNotClosed
		end
	end
	opt << s
	self.pos = p
	_scan(/\A\[(#{Regexp.escape(opt[RE::OPTION, 1].to_s)})\]/)
end

#scan_spaceObject



41
42
43
# File 'lib/math_ml/latex.rb', line 41

def scan_space
	_scan(/#{RE::SPACE}+/)
end

#skip_space_and(check_mode) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/math_ml/latex.rb', line 45

def skip_space_and(check_mode)
	opos = pos
	scan_space
	r = yield
	self.pos = opos if check_mode || !r
	r
end