module Temple::StaticAnalyzer

Constants

DYNAMIC_TOKENS
STATIC_KEYWORDS
STATIC_OPERATORS
STATIC_TOKENS

Public Class Methods

available?() click to toggle source
# File lib/temple/static_analyzer.rb, line 33
def available?
  defined?(Ripper) && Ripper.respond_to?(:lex)
end
static?(code) click to toggle source
# File lib/temple/static_analyzer.rb, line 37
def static?(code)
  return false if code.nil? || code.strip.empty?
  return false if syntax_error?(code)

  Ripper.lex(code).each do |_, token, str|
    case token
    when *STATIC_TOKENS
      # noop
    when :on_kw
      return false unless STATIC_KEYWORDS.include?(str)
    when :on_op
      return false unless STATIC_OPERATORS.include?(str)
    when *DYNAMIC_TOKENS
      return false
    else
      return false
    end
  end
  true
end
syntax_error?(code) click to toggle source
# File lib/temple/static_analyzer.rb, line 58
def syntax_error?(code)
  SyntaxChecker.new(code).parse
  false
rescue SyntaxChecker::ParseError
  true
end