Class: PredictionContextCache

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr4/PredictionContext.rb

Overview

Used to cache PredictionContext objects. Its used for the shared

context cash associated with contexts in DFA states. This cache
can be used for both lexers and parsers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePredictionContextCache

Returns a new instance of PredictionContextCache.



71
72
73
# File 'lib/antlr4/PredictionContext.rb', line 71

def initialize 
    @cache = Hash.new
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



70
71
72
# File 'lib/antlr4/PredictionContext.rb', line 70

def cache
  @cache
end

Instance Method Details

#add(ctx) ⇒ Object

Add a context to the cache and return it. If the context already exists,

return that one instead and do not add a new context to the cache.
Protect shared cache from unsafe thread access.


79
80
81
82
83
84
85
86
87
# File 'lib/antlr4/PredictionContext.rb', line 79

def add(ctx)
    if ctx.equal? PredictionContext.EMPTY then
        return PredictionContext.EMPTY
    end
    existing = self.cache[ctx]
    return existing if existing 
    self.cache[ctx] = ctx
    return ctx
end

#get(ctx) ⇒ Object



88
89
90
# File 'lib/antlr4/PredictionContext.rb', line 88

def get(ctx)
    return self.cache[ctx]
end

#lengthObject



91
92
93
# File 'lib/antlr4/PredictionContext.rb', line 91

def length
    return self.cache.length
end