Class: Utopia::Trenni::Scanner

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/utopia/trenni.rb

Constant Summary collapse

TEXT =
/([^<#]|<(?!\?r)|#(?!\{)){1,1024}/m

Instance Method Summary collapse

Constructor Details

#initialize(callback, string) ⇒ Scanner

Returns a new instance of Scanner.



58
59
60
61
# File 'lib/utopia/trenni.rb', line 58

def initialize(callback, string)
	@callback = callback
	super(string)
end

Instance Method Details

#parseObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/utopia/trenni.rb', line 63

def parse
	until eos?
		pos = self.pos

		scan_text
		scan_expression

		if pos == self.pos
			raise StandardError.new "Could not scan current input #{self.pos} #{eos?}!"
		end
	end
end

#scan_expressionObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/utopia/trenni.rb', line 82

def scan_expression
	if scan(/\#\{/)
		level = 1
		code = ""

		until eos? || level == 0
			if scan(/[^"'\{\}]+/m)
				code << matched
			end

			if scan(/"(\\"|[^"])*"/m)
				code << matched
			end

			if scan(/'(\\'|[^'])*'/m)
				code << matched
			end

			if scan(/\{/)
				code << matched
				level += 1
			end

			if scan(/\}/)
				code << matched if level > 1
				level -= 1
			end
		end

		if level == 0
			@callback.output(code)
		else
			raise StandardError.new "Could not find end of expression #{self}!"
		end
	elsif scan(/<\?r/)
		if scan_until(/(.*?)\?>/m)
			@callback.expression(self[1])
		else
			raise StandardError.new "Could not find end of expression #{self}!"
		end
	end
end

#scan_textObject



76
77
78
79
80
# File 'lib/utopia/trenni.rb', line 76

def scan_text
	if scan(TEXT)
		@callback.text(matched)
	end
end