class Hamlit::StaticAnalyzer

Constants

DYNAMIC_TOKENS
STATIC_KEYWORDS
STATIC_OPERATORS
STATIC_TOKENS

Public Class Methods

static?(code) click to toggle source
# File lib/hamlit/static_analyzer.rb, line 28
def self.static?(code)
  return false if code.nil? || code.strip.empty?
  return false if RubyExpression.syntax_error?(code)

  Ripper.lex(code).each do |(_, col), 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

Public Instance Methods

on_dynamic(code) click to toggle source
# File lib/hamlit/static_analyzer.rb, line 49
def on_dynamic(code)
  if StaticAnalyzer.static?(code)
    [:static, eval(code).to_s]
  else
    [:dynamic, code]
  end
end