Class: Ibex::Runtime::SyntaxSession

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.rb

Overview

Generated-language syntax boundary backed by the existing Red/Green CST incremental engine. It does not execute parser production actions.

Constant Summary collapse

TRUSTED_PROFILE =

Signature:

  • Symbol

:trusted_application_code

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser_class, source, execution_profile:, resource_limits: nil, limits: nil, cancellation: nil, blender: true) ⇒ SyntaxSession

Returns a new instance of SyntaxSession.

RBS:

  • (Class parser_class, String | CST::SourceText source, execution_profile: Symbol?, ?resource_limits: ResourceLimits?, ?limits: SyntaxSessionLimits?, ?cancellation: CancellationToken?, ?blender: bool) -> void



4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
# File 'lib/json5/generated_parser.rb', line 4139

def initialize(
  parser_class,
  source,
  execution_profile:,
  resource_limits: nil,
  limits: nil,
  cancellation: nil,
  blender: true
)
  @execution_profile = validate_execution_profile(parser_class, execution_profile)
  @limits = limits || SyntaxSessionLimits.new #: SyntaxSessionLimits
  unless @limits.is_a?(SyntaxSessionLimits)
    raise ArgumentError, "limits must be an Ibex::Runtime::SyntaxSessionLimits"
  end
  unless cancellation.nil? || cancellation.is_a?(CancellationToken)
    raise ArgumentError, "cancellation must be an Ibex::Runtime::CancellationToken or nil"
  end

  @cancellation = cancellation #: CancellationToken?
  @parser_class = parser_class #: Class
  @resource_limits = resource_limits || ResourceLimits.new #: ResourceLimits
  @mutex = Mutex.new #: Mutex
  @revision = 0 #: Integer
  @operation_expected_tokens = [] #: Array[String]
  @operation_fallback_reasons = [] #: Array[Symbol]

  source_text = normalize_source(source)
  enforce_limit!(:source_bytes, @limits.max_source_bytes, source_text.bytesize)
  cancellation_checkpoint!
  event_observer = ->(event, parser) { handle_runtime_event(event, parser) } #: Proc
  @incremental = CST::IncrementalParseSession.new(
    parser_class,
    source_text,
    resource_limits: @resource_limits,
    blender: blender,
    event_observer: event_observer
  ) #: CST::IncrementalParseSession
  @result = snapshot(@incremental.result) #: SyntaxSessionResult
rescue ResourceLimitError => e
  raise SyntaxSessionResourceLimitError.new(resource: e.resource, limit: e.limit, observed: e.observed), cause: e
end

Instance Attribute Details

#execution_profileObject (readonly)

Signature:

  • Symbol



4133
4134
4135
# File 'lib/json5/generated_parser.rb', line 4133

def execution_profile
  @execution_profile
end

#limitsObject (readonly)

Signature:

  • SyntaxSessionLimits



4134
4135
4136
# File 'lib/json5/generated_parser.rb', line 4134

def limits
  @limits
end

Instance Method Details

#apply_edits(edits) ⇒ Object

Apply byte-oriented edits against the current source.

RBS:

  • (Array[CST::TextEdit] edits) -> SyntaxSessionResult



4193
4194
4195
# File 'lib/json5/generated_parser.rb', line 4193

def apply_edits(edits)
  @mutex.synchronize { apply_edits_locked(edits) }
end

#repair(policy: RepairPolicy.new, token_text: {}) ⇒ Object

Propose byte edits through the existing bounded repair search, then validate them with a fresh syntax-only parse. The session is unchanged.

RBS:

  • (?policy: RepairPolicy, ?token_text: Hash[String, String]) -> SyntaxRepairResult



4200
4201
4202
4203
4204
4205
4206
4207
4208
# File 'lib/json5/generated_parser.rb', line 4200

def repair(policy: RepairPolicy.new, token_text: {})
  @mutex.synchronize do
    SyntaxRepairer.new(
      @parser_class, @incremental.source_text, @result,
      execution_profile: @execution_profile, resource_limits: @resource_limits,
      limits: @limits, cancellation: @cancellation, policy: policy, token_text: token_text
    ).call
  end
end

#resultObject

RBS:

  • () -> SyntaxSessionResult



4187
4188
4189
# File 'lib/json5/generated_parser.rb', line 4187

def result
  @mutex.synchronize { @result }
end

#source_textObject

RBS:

  • () -> CST::SourceText



4182
4183
4184
# File 'lib/json5/generated_parser.rb', line 4182

def source_text
  @mutex.synchronize { @incremental.source_text }
end