Class: Arrow::Template::Parser::State

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Patterns
Defined in:
lib/arrow/template/parser.rb

Overview

Parse state object class. Instance of this class represent a parser’s progress through a given template body.

Constant Summary

Constants included from Patterns

Patterns::ALTERNATION, Patterns::ARGDEFAULT, Patterns::ARGUMENT, Patterns::CAPTURE, Patterns::COMMA, Patterns::DBLQSTRING, Patterns::DOT, Patterns::EQUALS, Patterns::IDENTIFIER, Patterns::INFIX, Patterns::LBRACKET, Patterns::NUMBER, Patterns::PATHNAME, Patterns::QUOTEDSTRING, Patterns::RBRACKET, Patterns::REBINDOP, Patterns::REGEXP, Patterns::SLASHQSTRING, Patterns::SYMBOL, Patterns::TAGCLOSE, Patterns::TAGMIDDLE, Patterns::TAGOPEN, Patterns::TICKQSTRING, Patterns::VARIABLE, Patterns::WHITESPACE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Constructor Details

#initialize(text, template, initialData = {}) ⇒ State

Create and return a new parse state for the specified text. The initialData is used to propagate directive bookkeeping state through recursive parses.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/arrow/template/parser.rb', line 115

def initialize( text, template, initialData={} )
	@scanner = StringScanner.new( text )
	@template = template
	@tag_open = nil
	@tag_middle = nil
	@tag_close = nil
	@current_branch = []
	@current_branch_node = nil
	@current_node = nil
	@data = initialData

	#self.log.debug "From %s: Created a parse state for %p (%p). Data is: %p" %
	#	[ caller(1).first, text, template, initialData ]
end

Instance Attribute Details

#current_branchObject (readonly)

The current branch of the parse state. Branches are added and removed for re-entrances into the parse loop.



146
147
148
# File 'lib/arrow/template/parser.rb', line 146

def current_branch
  @current_branch
end

#current_branch_nodeObject (readonly)

The pointer into the syntax tree for the node which is the base of the current branch.



150
151
152
# File 'lib/arrow/template/parser.rb', line 150

def current_branch_node
  @current_branch_node
end

#current_nodeObject (readonly)

The pointer into the syntax tree for the current node



153
154
155
# File 'lib/arrow/template/parser.rb', line 153

def current_node
  @current_node
end

#dataObject (readonly)

A miscellaneous data hash to allow directives to keep their own state for a parse.



169
170
171
# File 'lib/arrow/template/parser.rb', line 169

def data
  @data
end

#scannerObject (readonly)

The StringScanner object which is scanning the text being parsed.



142
143
144
# File 'lib/arrow/template/parser.rb', line 142

def scanner
  @scanner
end

#tag_closeObject

The pattern that will match the current tag-closing characters during the parse of a directive.



165
166
167
# File 'lib/arrow/template/parser.rb', line 165

def tag_close
  @tag_close
end

#tag_middleObject

The pattern that will match the middle of the current tag during the parse of a directive.



161
162
163
# File 'lib/arrow/template/parser.rb', line 161

def tag_middle
  @tag_middle
end

#tag_openObject (readonly)

The string that contains the opening string of the current tag, if any.



157
158
159
# File 'lib/arrow/template/parser.rb', line 157

def tag_open
  @tag_open
end

#templateObject (readonly)

The template that corresponds to the parse state



139
140
141
# File 'lib/arrow/template/parser.rb', line 139

def template
  @template
end

Instance Method Details

#add_nodes(*nodes) ⇒ Object Also known as: <<

Add the given nodes to the state’s syntax tree.



189
190
191
192
193
# File 'lib/arrow/template/parser.rb', line 189

def add_nodes( *nodes )
	@current_branch.push( *nodes )
	@current_node = @current_branch.last
	return self
end

#branch(node) {|_self| ... } ⇒ Object

Add a branch belonging to the specified node to the parse state for the duration of the supplied block, removing and returning it when the block returns.

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (LocalJumpError)


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/arrow/template/parser.rb', line 200

def branch( node )
	raise LocalJumpError, "no block given" unless
		block_given?

	# Save and set the current branch node
	entryNode = @current_branch_node
	@current_branch_node = node

	# Push a new branch and make the current branch point to it
	# while saving the one we entered with so it can be restored
	# later.
	entryBranch = @current_branch
	entryBranch.push( @current_branch = [] )

	yield( self )

	# Restore the current branch and branch node to what they were
	# before
	@current_branch = entryBranch
	@current_branch_node = entryNode

	return @current_branch.pop
end

#lineObject

Return the line number of the line on which the parse’s pointer current rests.



174
175
176
# File 'lib/arrow/template/parser.rb', line 174

def line
	return @scanner.string[ 0, @scanner.pos ].count( $/ ) + 1
end

#set_tag_patterns(opening) ⇒ Object

Set the middle and closing tag patterns from the given matched opening string.



181
182
183
184
185
# File 'lib/arrow/template/parser.rb', line 181

def set_tag_patterns( opening )
	@tag_open = opening
	@tag_close = TAGCLOSE[ opening ]
	@tag_middle = TAGMIDDLE[ @tag_close ]
end