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.



40
41
42
43
# File 'lib/utopia/trenni.rb', line 40

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

Instance Method Details

#parseObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/utopia/trenni.rb', line 45

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



64
65
66
67
68
69
70
71
72
73
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
100
101
102
103
104
105
# File 'lib/utopia/trenni.rb', line 64

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



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

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