class AdLint::Cpp::CodeSubstitution

Public Class Methods

new(ptn_str, repl_str) click to toggle source
# File lib/adlint/cpp/subst.rb, line 40
def initialize(ptn_str, repl_str)
  @pattern     = StringToPPTokensLexer.new(ptn_str).execute.to_a
  @replacement = StringToPPTokensLexer.new(repl_str).execute.to_a
end

Public Instance Methods

execute(toks) click to toggle source
# File lib/adlint/cpp/subst.rb, line 49
def execute(toks)
  rslt_toks = []
  idx = 0
  while first_tok = toks[idx]
    matcher = Matcher.new(@pattern)
    matched_len = matcher.match(toks, idx)
    if matcher.accepted? || idx + matched_len == toks.size
      notify_substitution(toks, idx, matched_len)
      rslt_toks.concat(@replacement.map { |tok|
        Token.new(tok.type, tok.value, first_tok.location, tok.type_hint)
      })
      idx += matched_len
    else
      rslt_toks.push(first_tok)
      idx += 1
    end
  end
  rslt_toks
end

Private Instance Methods

notify_substitution(toks, idx, len) click to toggle source
# File lib/adlint/cpp/subst.rb, line 70
def notify_substitution(toks, idx, len)
  matched_toks = toks[idx, len]
  on_substitution.invoke(matched_toks) unless matched_toks.empty?
end