Class: AdLint::Cpp::CodeSubstitution

Inherits:
Object
  • Object
show all
Extended by:
Pluggable
Defined in:
lib/adlint/cpp/subst.rb

Instance Method Summary collapse

Methods included from Pluggable

def_plugin

Constructor Details

#initialize(ptn_str, repl_str) ⇒ CodeSubstitution

Returns a new instance of CodeSubstitution.



40
41
42
43
# 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

Instance Method Details

#execute(toks) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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