Class: TlaParserS::Context
- Inherits:
-
Object
- Object
- TlaParserS::Context
- Extended by:
- Forwardable
- Includes:
- Utils::MyLogger
- Defined in:
- lib/semantics/context.rb
Overview
******************************************************************
Manage context to parse TLA+ snippets
Builds context in three phases:
1) Initial context includes TLA+ standard definitions e.g. TRUE, FALSE
when context is created
2) Global context includes snippets defining macros, procedures,
operators, and variables. Using pushContext/addContext
3) During resolve phase push to context formal parameters, quantification
bind variables, variables in set constructor generator. Uses
pushContext/addContext methods internally from 'resolveDefine',
******************************************************************
Constant Summary collapse
- PROGNAME =
Logger
nil
Constants included from Utils::MyLogger
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#symbol_table ⇒ Object
Returns the value of attribute symbol_table.
Constructor & configs collapse
-
#initialize(options = {}) ⇒ Context
constructor
——————————————————————.
Services to initialize collapse
- #initContext ⇒ Object
-
#initEntries(entries) ⇒ Object
Create new context in symbol table && add entries.
Instance Method Summary collapse
-
#addContext(snippets, moduleName = nil) ⇒ Object
Add more symbols to current context levell.
-
#pushContext(snippets = nil, moduleName = nil) ⇒ Object
Add one level to context & and add snipets there.
-
#resolveDefine(defineNode) ⇒ Hash:Array
Resolve definitions with body (macro, procedure,operator definition = callables), and variable definitions.
-
#resolveExpression(expr) ⇒ Hash:Array
Resolve symbol table entries in ‘expression’.
-
#resolveSymbol(symbol) ⇒ Hash
Locate ‘symbol’ in context.
Methods included from Utils::MyLogger
Constructor Details
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
31 32 33 |
# File 'lib/semantics/context.rb', line 31 def @options end |
#symbol_table ⇒ Object
Returns the value of attribute symbol_table.
29 30 31 |
# File 'lib/semantics/context.rb', line 29 def symbol_table @symbol_table end |
Instance Method Details
#addContext(snippets, moduleName = nil) ⇒ Object
Add more symbols to current context levell
107 108 109 110 |
# File 'lib/semantics/context.rb', line 107 def addContext( snippets, moduleName=nil ) symbol_table.addContext( snippets, moduleName ) return self end |
#initContext ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/semantics/context.rb', line 69 def initContext @symbol_table = SymbolTable.new( ) # # create context for standar library && add entries # @symbol_table.pushContext( nil ) # GLOABLS.each do |global| # entry = { # :context_type => "Init", # :context => "Global", # :symbol_type => "StandardLib", # :symbol_name => global, # } # @symbol_table.addEntry( entry ) # end return self end |
#initEntries(entries) ⇒ Object
Create new context in symbol table && add entries
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/semantics/context.rb', line 88 def initEntries( entries ) return unless entries && entries.any? # create new context pushContext # add entries entries.each do |entry| symbol_table.addEntry( entry ) end end |
#pushContext(snippets = nil, moduleName = nil) ⇒ Object
Add one level to context & and add snipets there
117 118 119 120 121 |
# File 'lib/semantics/context.rb', line 117 def pushContext( snippets=nil, moduleName=nil ) warn "Method symbol_definitions not implemented on #{snippets}" if snippets && !snippets.respond_to?(:symbol_definitions) symbol_table.pushContext( snippets, moduleName ) return self end |
#resolveDefine(defineNode) ⇒ Hash:Array
Resolve definitions with body (macro, procedure,operator definition = callables), and variable definitions.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/semantics/context.rb', line 225 def resolveDefine( defineNode ) # @logger.debug "resolveDefine starting callable=#{defineNode.inspect}" if @logger.debug? ret = nil pushContext( defineNode ) # dumpContext if defineNode.respond_to?( :body_node ) then # macro, procedure, operatordef @logger.info( "#{__method__} start with resolveStatement for #{defineNode.body_node}" ) if defineNode.body_node.is_a?(SbuilderSexp::Expression) # operator ret = resolveExpression( defineNode.body_node ) else # macro, procedure ret = resolveStatement( defineNode.body_node ) end # resolve expressions initializing variable declarations if defineNode.respond_to?(:variable_declarations) sret = [] defineNode.variable_declarations.each do |variable_declaration| sret = sret.concat( resolveExpression( variable_declaration.init )) end # add resolved symbols from declaration init only if symbol # not already resolved sret.each do |s| ret << s unless ret.map { |r| r[:symbol] }.include?( s[:symbol]) end end # puts "sret found=#{sret}, ret=#{ret}" if sret.any? elsif defineNode.respond_to?( :init ) then @logger.info( "#{__method__} start with resolveExpression #{defineNode.init}" ) ret = resolveExpression( defineNode.init ) @logger.debug( "#{__method__} resolveExpression-->#{ret}" ) if @logger.debug? else msg = <<-EOS Unknown tree node object #{defineNode}. Should be 'Callabe' or 'VariableDef' EOS @logger.error( "#{__method__} #{msg}" ) raise ContextException.new msg end popContext() ret end |
#resolveExpression(expr) ⇒ Hash:Array
Resolve symbol table entries in ‘expression’
of symbol table lookup
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 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 341 342 343 344 345 346 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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/semantics/context.rb', line 280 def resolveExpression( expr ) ret = [] expr && expr.traverse do |m, node_type, enode, node_val | @logger.debug( "#{__method__} node_type=#{node_type}, node_val=#{node_val}" ) if @logger.debug? case node_type when "Identifier" ret << resolvedSymbol( node_val, symbol_table.resolveContext( node_val ) ) when "AdditiveExpression", "MultitiveExpression", "UnaryExpression", "ParenthesisExpression" when "PrimaryExpression" sret = [] # puts "PrimaryExpression: attribute_accessors=#{enode.attribute_accessors.join(',')}" enode.attribute_accessors && enode.attribute_accessors.each do |accessor| sret = sret.concat( resolveExpression( accessor )) end ret = ret.concat( sret ) when "FieldByValue" # ret << resolvedSymbol( node_val, symbol_table.resolveContext( node_val ) ) # expr = node.field_value_expression.traverse { |m, node_type, enode, node_val| m.concat( expression_value(node_type, enode, node_val )) } sret = resolveExpression( enode.field_value_expression ) ret = ret.concat( sret ) when "FieldByName" when "SequenceExpression" @logger.debug( "#{__method__} SequenceExpression: enode=#{enode.inspect}" ) if @logger.debug? sret = [] enode.tuples && enode.tuples.each do |tuple_expr| sret = sret.concat( resolveExpression( tuple_expr )) end ret = ret.concat( sret ) when "IfExpression" @logger.debug( "#{__method__} IfExpression: enode=#{enode.inspect}" ) if @logger.debug? sret = resolveExpression( enode.condition_node ) ret.concat( sret ) @logger.info( "#{__method__} IfExpression: condition-expr=#{sret}" ) sret = resolveExpression( enode.then_expression_node ) ret.concat( sret ) @logger.info( "#{__method__} IfExpression: then-expr=#{sret}" ) sret = [] sret = resolveExpression( enode.else_expression_node ) if enode.else_expression_node ret.concat( sret ) @logger.info( "#{__method__} IfExpression: els-expr=#{sret}" ) when "ChooseExpression" @logger.debug( "#{__method__} ChooseExpression: enode=#{enode.inspect}" ) if @logger.debug? @logger.debug( "#{__method__} ChooseExpression: enode.binds_node=#{enode.binds_node}" ) if @logger.debug? # bind node defines symbol table pushContext( enode ) choose_expression=enode.choose_expression @logger.debug( "#{__method__} ChooseExpression: choose_expression=#{choose_expression.inspect}" ) if @logger.debug? sret = resolveExpression( choose_expression ) @logger.info( "#{__method__} ChooseExpression: resolved sret=#{sret.map {|e| { e[:symbol] => e[:resolved] ? e[:resolved][:symbol_type]+':'+e[:resolved][:context] : nil} }}" ) ret = ret.concat( sret ) popContext() when "RecordDefinition" @logger.debug( "#{__method__} RecordDefinition: enode=#{enode.inspect}" ) if @logger.debug? sret = [] enode.record_fields.each do |record_field| sret = sret.concat( resolveExpression( record_field.element_expression )) end @logger.info( "#{__method__} RecordDefinition: resolved sret=#{sret.map {|e| { e[:symbol] => e[:resolved] ? e[:resolved][:symbol_type]+':'+e[:resolved][:context] : nil} }}" ) ret = ret.concat( sret ) when "RecordExcept" # resolve where base expression # id = enode.record_identifier # ret << resolvedSymbol( id, symbol_table.resolveContext( id ) ) bret = resolveExpression( enode.record_base ) ret = ret.concat( bret ) @logger.debug( "#{__method__} RecordExcept: record_base -->=#{bret}" ) if @logger.debug? sret = [] # resolve EXCEPT constructor field rvalue expression enode.record_field_definitions && enode.record_field_definitions.each do |field_definition| sret = sret.concat( resolveExpression( field_definition.rvalue_expression )) end ret = ret.concat( sret ) when "SetConstructor" sret = [] enode.set_elements && enode.set_elements.each do |set_element| sret = sret.concat( resolveExpression( set_element )) end ret = ret.concat( sret ) when "SetExpressionMap" @logger.debug( "#{__method__} SetExpressionMap: enode=#{enode.inspect}" ) if @logger.debug? # resolve set mapped sret = resolveExpression( enode.binds_node.bind_set ) ret = ret.concat( sret ) # resolve expression in set map pushContext( enode ) sret = resolveExpression( enode.set_expression ) @logger.debug( "#{__method__} SetExpressionMap: resolved sret=#{sret}" ) if @logger.debug? ret = ret.concat( sret ) when "FunctionExpression" @logger.debug( "#{__method__} FunctionExpression: enode=#{enode.inspect}" ) if @logger.debug? pushContext( enode ) sret = resolveExpression( enode.function_expression ) ret = ret.concat( sret ) popContext( ) when "SetExpression" @logger.debug( "#{__method__} SetExpression: enode=#{enode.inspect}" ) if @logger.debug? pushContext( enode ) sret = resolveExpression( enode.set_expression ) @logger.debug( "#{__method__} SetExpression: resolved sret=#{sret}" ) if @logger.debug? ret = ret.concat( sret ) popContext( ) when "OperatorExpression" @logger.debug( "#{__method__} OperatorExpression: enode=#{enode.inspect}" ) if @logger.debug? operator_name = enode.operator_name ret << resolvedSymbol( operator_name, symbol_table.resolveContext( operator_name ) ) sret = [] enode.arguments && enode.arguments.each do |argument_node| sret = sret.concat( resolveExpression( argument_node )) end @logger.info( "#{__method__} OperatorExpression: resolved sret=#{sret.map {|e| { e[:symbol] => e[:resolved] ? e[:resolved][:symbol_type]+':'+e[:resolved][:context] : nil} }}" ) ret = ret.concat( sret ) when "LetExpression" @logger.debug( "#{__method__} SetExpression: node.symbol_definitions=#{enode.symbol_definitions}" ) if @logger.debug? pushContext( enode ) # resolve let operator oret = resolveExpression( enode.operatorDef.body_node ) @logger.debug( "#{__method__} resolved oret=#{oret}" ) if @logger.debug? ret = ret.concat( oret ) # resolve let expression eret = resolveExpression( enode.letExpression ) @logger.debug( "#{__method__} resolved eret=#{eret}" ) if @logger.debug? ret = ret.concat( eret ) popContext( ) when "QuantifyExpression" @logger.debug( "#{__method__} SetExpression: node.symbol_definitions=#{enode.symbol_definitions}" ) if @logger.debug? pushContext( enode ) sret = resolveExpression( enode.binds_nodes.first.bind_set ) ret = ret.concat( sret ) @logger.debug( "#{__method__} resolved sret=#{sret}" ) if @logger.debug? @logger.debug( "#{__method__} SetExpression: #{enode.quantified_expression.inspect}" ) if @logger.debug? qret = resolveExpression( enode.quantified_expression ) @logger.debug( "#{__method__} resolved qret=#{qret}" ) if @logger.debug? ret = ret.concat( qret ) @logger.debug( "#{__method__} resolved ret=#{ret}" ) if @logger.debug? popContext( ) when "IntegerValue", "StringValue", "OriginalValue" else msg = "Unknwon expression node type #{node_type} with value #{node_val}" @logger.error( "#{__method__} #{msg}" ) raise ContextException.new msg end end ret end |
#resolveSymbol(symbol) ⇒ Hash
Locate ‘symbol’ in context
129 130 131 132 133 134 135 136 |
# File 'lib/semantics/context.rb', line 129 def resolveSymbol( symbol ) resolved = symbol_table.resolveContext( symbol ) return resolvedSymbol( symbol, resolved ); # return { # :symbol => symbol, # :resolved => resolved, # } end |