class AdLint::Cpp::PreprocessedSource

Attributes

root_fpath[R]

Public Class Methods

new(root_fpath) click to toggle source
# File lib/adlint/cpp/source.rb, line 42
def initialize(root_fpath)
  @root_fpath = root_fpath
  @tokens = []
end

Public Instance Methods

add_token(tok) click to toggle source
# File lib/adlint/cpp/source.rb, line 54
def add_token(tok)
  @tokens.push(tok)
end
pp_tokens() click to toggle source
# File lib/adlint/cpp/source.rb, line 58
def pp_tokens
  @tokens.select { |tok| tok.type == :PP_TOKEN }
end
substitute_code_blocks(traits) click to toggle source
# File lib/adlint/cpp/source.rb, line 62
def substitute_code_blocks(traits)
  traits.of_compiler.extension_substitutions.each do |ptn, repl|
    @tokens = create_extension_substitution(ptn, repl).execute(@tokens)
  end

  traits.of_compiler.arbitrary_substitutions.each do |ptn, repl|
    @tokens = create_arbitrary_substitution(ptn, repl).execute(@tokens)
  end

  create_inline_assembly_substitutions(self).each do |sub|
    @tokens = sub.execute(@tokens)
  end

  self
end
to_s() click to toggle source
# File lib/adlint/cpp/source.rb, line 78
def to_s
  @lst_fpath     = nil
  @lst_line_no   = 0
  @lst_column_no = 1
  @lst_token     = nil
  @io = StringIO.new
  @io.set_encoding(Encoding.default_external)
  @tokens.each { |tok| print(tok) }
  @io.string
end

Private Instance Methods

create_arbitrary_substitution(ptn, repl) click to toggle source
# File lib/adlint/cpp/source.rb, line 98
def create_arbitrary_substitution(ptn, repl)
  CodeSubstitution.new(ptn, repl)
end
create_extension_substitution(ptn, repl) click to toggle source
# File lib/adlint/cpp/source.rb, line 90
def create_extension_substitution(ptn, repl)
  CodeSubstitution.new(ptn, repl).tap do |sub|
    sub.on_substitution += lambda { |matched_toks|
      on_language_extension.invoke(matched_toks)
    }
  end
end
end_with_punctuator?(str) click to toggle source
# File lib/adlint/cpp/source.rb, line 149
def end_with_punctuator?(str)
  str !~ /[a-z_0-9]\z/i
end
insert_line_marker(tok) click to toggle source
# File lib/adlint/cpp/source.rb, line 157
def insert_line_marker(tok)
  if @lst_column_no > 1
    @io.puts
  end
  line_marker = "# #{tok.location.line_no.to_s.to_default_external} " +
                "\"#{tok.location.fpath.to_s.to_default_external}\""
  @io.puts(line_marker.to_default_external)
  @lst_fpath     = tok.location.fpath
  @lst_line_no   = tok.location.line_no
  @lst_column_no = 1
end
keyword_or_identifier?(str) click to toggle source
# File lib/adlint/cpp/source.rb, line 153
def keyword_or_identifier?(str)
  str =~ /\A[a-z_][a-z_0-9]*\z/i
end
need_hspace?(tok) click to toggle source
# File lib/adlint/cpp/source.rb, line 135
def need_hspace?(tok)
  return false unless @lst_token
  if keyword_or_identifier?(@lst_token.value)
    !start_with_punctuator?(tok.value)
  else
    !(end_with_punctuator?(@lst_token.value) ||
      start_with_punctuator?(tok.value))
  end
end
print(tok) click to toggle source
start_with_punctuator?(str) click to toggle source
# File lib/adlint/cpp/source.rb, line 145
def start_with_punctuator?(str)
  str !~ /\A[a-z_0-9]/i
end