Class: Jade::Parsing::Combinators::State

Inherits:
Data
  • Object
show all
Defined in:
lib/jade/parsing/combinators.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens:, entry:, position: 0, context_stack: [], tolerant: false, diagnostics: Diagnostics::List.empty, source: nil) ⇒ State

Returns a new instance of State.



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/jade/parsing/combinators.rb', line 179

def initialize(
  tokens:,
  entry:,
  position: 0,
  context_stack: [],
  tolerant: false,
  diagnostics: Diagnostics::List.empty,
  source: nil
)
  super
end

Instance Attribute Details

#context_stackObject (readonly)

Returns the value of attribute context_stack

Returns:

  • (Object)

    the current value of context_stack



175
176
177
# File 'lib/jade/parsing/combinators.rb', line 175

def context_stack
  @context_stack
end

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics

Returns:

  • (Object)

    the current value of diagnostics



175
176
177
# File 'lib/jade/parsing/combinators.rb', line 175

def diagnostics
  @diagnostics
end

#entryObject (readonly)

Returns the value of attribute entry

Returns:

  • (Object)

    the current value of entry



175
176
177
# File 'lib/jade/parsing/combinators.rb', line 175

def entry
  @entry
end

#positionObject (readonly)

Returns the value of attribute position

Returns:

  • (Object)

    the current value of position



175
176
177
# File 'lib/jade/parsing/combinators.rb', line 175

def position
  @position
end

#sourceObject (readonly)

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



175
176
177
# File 'lib/jade/parsing/combinators.rb', line 175

def source
  @source
end

#tokensObject (readonly)

Returns the value of attribute tokens

Returns:

  • (Object)

    the current value of tokens



175
176
177
# File 'lib/jade/parsing/combinators.rb', line 175

def tokens
  @tokens
end

#tolerantObject (readonly)

Returns the value of attribute tolerant

Returns:

  • (Object)

    the current value of tolerant



175
176
177
# File 'lib/jade/parsing/combinators.rb', line 175

def tolerant
  @tolerant
end

Instance Method Details

#add_diagnostic(diagnostic) ⇒ Object



215
216
217
# File 'lib/jade/parsing/combinators.rb', line 215

def add_diagnostic(diagnostic)
  with(diagnostics: diagnostics.add(diagnostic))
end

#advance(n = 1) ⇒ Object



195
196
197
# File 'lib/jade/parsing/combinators.rb', line 195

def advance(n = 1)
  with(position: position + n)
end

#currentObject



191
192
193
# File 'lib/jade/parsing/combinators.rb', line 191

def current
  tokens[position]
end

#eof?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/jade/parsing/combinators.rb', line 199

def eof?
  position >= tokens.length
end

#same_line?(pos_a, pos_b) ⇒ Boolean

Two positions on the same source line. Uses the source’s ‘line_starts` index; returns true when source is unavailable so callers degrade to “accept whatever parses” rather than spurious errors.

Returns:

  • (Boolean)


207
208
209
210
211
212
213
# File 'lib/jade/parsing/combinators.rb', line 207

def same_line?(pos_a, pos_b)
  return true unless source

  starts = source.line_starts
  starts.bsearch_index { |ls| ls > pos_a } ==
    starts.bsearch_index { |ls| ls > pos_b }
end

#skip_until(sync_types) ⇒ Object



219
220
221
222
223
# File 'lib/jade/parsing/combinators.rb', line 219

def skip_until(sync_types)
  (position...tokens.length)
    .find { sync_types.include?(tokens[it].type) }
    .then { with(position: it || tokens.length) }
end