Module: Stupidedi::Builder::Navigation
- Included in:
- StateMachine
- Defined in:
- lib/stupidedi/builder/navigation.rb
Querying the Current Position collapse
-
#deterministic? ⇒ Boolean
Is there exactly one valid parse tree in the current state?.
-
#distance(other) ⇒ Either<Integer>
Returns the number of segments apart the current state is from the given ‘StateMachine`’s state.
- #empty? ⇒ Boolean
-
#first? ⇒ Boolean
Is this the first segment?.
-
#last? ⇒ Boolean
Is this the last segment?.
- #successors ⇒ Array<InstructionTable>
Accessing the Current Node collapse
-
#element(m, n = nil, o = nil) ⇒ Either<Zipper::AbstractCursor<Values::AbstractElementVal>>
Extracts the mth element from the current segment, if the current state is deterministic.
-
#segment ⇒ Either<Zipper::AbstractCursor<Values::SegmentVal>>
Extracts the segment from the current state, if the current state is deterministic and positioned on a segment.
-
#zipper ⇒ Either<Zipper::AbstractCursor<Values::AbstractVal>>
Returns the current position within the parse tree, if the current state is deterministic.
Navigating the Tree collapse
- #count(id, *elements) ⇒ Integer
- #count!(id, *elements) ⇒ Integer
-
#find(id, *elements) ⇒ Either<StateMachine>
Returns a ‘StateMachine` positioned on the next matching segment, excluding InvalidSegmentVals, that satisfies the given element constraints.
-
#find!(id, *elements) ⇒ Either<StateMachine>
Returns a ‘StateMachine` positioned on the next matching segment, including InvalidSegmentVals, that satisfies the given element constraints.
-
#first ⇒ Either<StateMachine>
Returns a new ‘StateMachine` positioned on the first segment in the parse tree, if there are any segments in the parse tree.
- #iterate(id, *elements) ⇒ Object
-
#last ⇒ Either<StateMachine>
Returns a new ‘StateMachine` positioned on the last segment in the parse tree, if there are any segments in the parse tree.
-
#next(count = 1) ⇒ StateMachine
Returns a new ‘StateMachine` positioned on the next segment, if there is a next segment.
-
#parent ⇒ Either<StateMachine>
Returns a new ‘StateMachine` positioned on the first segment of the parent structure.
-
#prev(count = 1) ⇒ Either<StateMachine>
Returns a new ‘StateMachine` positioned on the previous segment, if there is a previous segment.
Instance Method Details
#count(id, *elements) ⇒ Integer
414 415 416 |
# File 'lib/stupidedi/builder/navigation.rb', line 414 def count(id, *elements) __count(false, id, elements) end |
#count!(id, *elements) ⇒ Integer
419 420 421 |
# File 'lib/stupidedi/builder/navigation.rb', line 419 def count!(id, *elements) __count(true, id, elements) end |
#deterministic? ⇒ Boolean
Is there exactly one valid parse tree in the current state?
15 16 17 |
# File 'lib/stupidedi/builder/navigation.rb', line 15 def deterministic? @active.length == 1 end |
#distance(other) ⇒ Either<Integer>
This method uses AbstractCursor#between, which assumes the two cursors point to the same tree. If that is not the case, the results are undefined.
Returns the number of segments apart the current state is from the given ‘StateMachine`’s state. Note the direction is not indicated by the return value, so ‘a.distance(b) == b.distance(a)` for all states `a` and `b`.
67 68 69 70 71 72 73 |
# File 'lib/stupidedi/builder/navigation.rb', line 67 def distance(other) zipper.flatmap do |a| other.zipper.map do |b| a.between(b).count(&:segment?) - 1 end end end |
#element(m, n = nil, o = nil) ⇒ Either<Zipper::AbstractCursor<Values::AbstractElementVal>>
Extracts the mth element from the current segment, if the current state is deterministic. Accepts optional arguments to extract a specific occurrence of a repeated element and/or a specific component from a composite element.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/stupidedi/builder/navigation.rb', line 110 def element(m, n = nil, o = nil) segment.flatmap do |s| unless m >= 1 raise ArgumentError, "argument must be positive" end if s.node.invalid? # InvalidSegmentVal doesn't have child AbstractElementVals, its # children are SimpleElementTok, CompositeElementTok, etc, which # are not parsed values. return Either.failure("invalid segment") end designator = s.node.id.to_s definition = s.node.definition length = definition.element_uses.length unless m <= length raise ArgumentError, "#{designator} segment has only #{length} elements" end designator << "%02d" % m value = s.child(m - 1) if n.nil? return Either.success(value) elsif value.node.repeated? unless n >= 1 raise ArgumentError, "argument must be positive" end limit = value.node.definition.repeat_count unless limit.include?(n) raise ArgumentError, "#{designator} can only occur #{limit.max} times" end unless value.node.children.defined_at?(n - 1) return Either.failure("#{designator} occurs only #{value.node.children.length} times") end value = value.child(n - 1) n, o = o, nil return Either.success(value) if n.nil? end unless value.node.composite? raise ArgumentError, "#{designator} is a simple element" end unless o.nil? raise ArgumentError, "#{designator} is a non-repeatable composite element" end unless n >= 1 raise ArgumentError, "argument must be positive" end length = definition.element_uses.at(m - 1).definition.component_uses.length unless n <= length raise ArgumentError, "#{designator} has only #{length} components" end if value.node.empty? Either.failure("#{designator} is empty") else Either.success(value.child(n - 1)) end end end |
#empty? ⇒ Boolean
19 20 21 22 23 24 25 26 27 |
# File 'lib/stupidedi/builder/navigation.rb', line 19 def empty? value = @active.head.node.zipper until value.root? value = value.up end value.node.children.empty? end |
#find(id, *elements) ⇒ Either<StateMachine>
Returns a ‘StateMachine` positioned on the next matching segment, excluding InvalidSegmentVals, that satisfies the given element constraints. The search space is limited to certain related elements described in [Navigating.md]
396 397 398 |
# File 'lib/stupidedi/builder/navigation.rb', line 396 def find(id, *elements) __find(false, id, elements) end |
#find!(id, *elements) ⇒ Either<StateMachine>
Returns a ‘StateMachine` positioned on the next matching segment, including InvalidSegmentVals, that satisfies the given element constraints. The search space is limited to certain related elements described in [Navigating.md]
409 410 411 |
# File 'lib/stupidedi/builder/navigation.rb', line 409 def find!(id, *elements) __find(true, id, elements) end |
#first ⇒ Either<StateMachine>
Returns a new ‘StateMachine` positioned on the first segment in the parse tree, if there are any segments in the parse tree.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/stupidedi/builder/navigation.rb', line 196 def first active = roots.map do |zipper| state = zipper value = zipper.node.zipper until value.node.segment? or value.leaf? value = value.down state = state.down end unless value.node.segment? return Either.failure("no segments") end # Synchronize the two parallel state and value nodes unless value.eql?(state.node.zipper) state = state.replace(state.node.copy(:zipper => value)) end state end Either.success(StateMachine.new(@config, active)) end |
#first? ⇒ Boolean
Is this the first segment?
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/stupidedi/builder/navigation.rb', line 30 def first? value = @active.head.node.zipper until value.root? return false unless value.first? value = value.up end return true end |
#iterate(id, *elements) ⇒ Object
423 424 425 426 427 428 429 430 431 |
# File 'lib/stupidedi/builder/navigation.rb', line 423 def iterate(id, *elements) m = find(id, *elements) while m.defined? m = m.flatmap do |n| yield(n) n.find(id, *elements) end end end |
#last ⇒ Either<StateMachine>
Returns a new ‘StateMachine` positioned on the last segment in the parse tree, if there are any segments in the parse tree.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/stupidedi/builder/navigation.rb', line 225 def last active = roots.map do |zipper| state = zipper value = zipper.node.zipper until value.node.segment? or value.leaf? value = value.down.last state = state.down.last end unless value.node.segment? return Either.failure("no segments") end # Synchronize the two parallel state and value nodes unless value.eql?(state.node.zipper) state = state.replace(state.node.copy(:zipper => value)) end state end Either.success(StateMachine.new(@config, active)) end |
#last? ⇒ Boolean
Is this the last segment?
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/stupidedi/builder/navigation.rb', line 42 def last? value = @active.head.node.zipper until value.root? return false unless value.last? value = value.up end return true end |
#next(count = 1) ⇒ StateMachine
Returns a new ‘StateMachine` positioned on the next segment, if there is a next segment. Optionally, a `count` argument may be provided that indicates how many segments to advance.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/stupidedi/builder/navigation.rb', line 302 def next(count = 1) unless count > 0 raise ArgumentError, "count must be positive" end active = @active.map do |zipper| state = zipper value = zipper.node.zipper count.times do while not value.root? and value.last? value = value.up state = state.up end if value.root? return Either.failure("cannot move to next after last segment") end value = value.next state = state.next until value.node.segment? value = value.down state = state.down end end # Synchronize the two parallel state and value nodes unless value.eql?(state.node.zipper) state = state.replace(state.node.copy(:zipper => value)) end state end Either.success(StateMachine.new(@config, active)) end |
#parent ⇒ Either<StateMachine>
Returns a new ‘StateMachine` positioned on the first segment of the parent structure. For example, when the current segment belongs to a loop but it’s not the first segment in that loop, this method will rewind to the first segment in the loop. If the current position is the first segment of a loop, this method will rewind to the first segment in the loop’s parent structure.
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/stupidedi/builder/navigation.rb', line 258 def parent active = [] @active.each do |zipper| state = zipper value = zipper.node.zipper while value.first? and not value.root? value = value.up state = state.up end if value.root? break end value = value.first state = state.first until value.node.segment? value = value.down state = state.down end # Synchronize the two parallel state and value nodes unless value.eql?(state.node.zipper) state = state.replace(state.node.copy(:zipper => value)) end active << state end if active.empty? Either.failure("no parent segment") else Either.success(StateMachine.new(@config, active)) end end |
#prev(count = 1) ⇒ Either<StateMachine>
Returns a new ‘StateMachine` positioned on the previous segment, if there is a previous segment. Optionally, a `count` argument may be provided that indicates how many segments to rewind.
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/stupidedi/builder/navigation.rb', line 347 def prev(count = 1) unless count > 0 raise ArgumentError, "count must be positive" end active = @active.map do |zipper| state = zipper value = zipper.node.zipper count.times do while not value.root? and value.first? value = value.up state = state.up end if value.root? return Either.failure("cannot move to prev before first segment") end state = state.prev value = value.prev until value.node.segment? value = value.down.last state = state.down.last end end # Synchronize the two parallel state and value nodes unless value.eql?(state.node.zipper) state = state.replace(state.node.copy(:zipper => value)) end state end Either.success(StateMachine.new(@config, active)) end |
#segment ⇒ Either<Zipper::AbstractCursor<Values::SegmentVal>>
Extracts the segment from the current state, if the current state is deterministic and positioned on a segment.
94 95 96 97 98 99 100 101 102 |
# File 'lib/stupidedi/builder/navigation.rb', line 94 def segment zipper.flatmap do |z| if z.node.segment? Either.success(z) else Either.failure("not a segment") end end end |
#successors ⇒ Array<InstructionTable>
10 11 12 |
# File 'lib/stupidedi/builder/navigation.rb', line 10 def successors @active.map{|a| a.node.instructions } end |
#zipper ⇒ Either<Zipper::AbstractCursor<Values::AbstractVal>>
Returns the current position within the parse tree, if the current state is deterministic.
82 83 84 85 86 87 88 |
# File 'lib/stupidedi/builder/navigation.rb', line 82 def zipper if deterministic? Either.success(@active.head.node.zipper) else Either.failure("non-deterministic state") end end |