Class: PPRB::PPRB

Inherits:
Object
  • Object
show all
Defined in:
lib/pprb.rb

Constant Summary collapse

PROFILES =
{
  :default => {
    :block => /^%(.*)/,
    :line  => /^>(.*)/,
    :end   => /^-$/,
    :ticks => /`(.*?)`/
  },

  :c_parsable => {
    :block => %r[^//\s*%(.*)],
    :line  => %r[^//\s*>(.*)],
    :end   => %r[^//\s*-$],
    :ticks => /\brb_(\w+?)_\b/
  },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, start_state = :pp) ⇒ PPRB

source is either a File object or a string start_state is either :pp or :rb



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pprb.rb', line 21

def initialize source, start_state = :pp
  @source = source
  @path = File.expand_path(source.path) if source.class == File

  @BLOCK = @LINE = @END = @TICKS = ''
  profile :default

  @ruby_output = ''
  @target_output = ''

  @input_state = [start_state]
  @last_emitted = :code

  load_rules
  source.lines.each { |l| process_line l }
  @ruby_output << ']' if @last_emitted == :text
end

Instance Attribute Details

#ruby_outputObject (readonly)

Returns the value of attribute ruby_output.



44
45
46
# File 'lib/pprb.rb', line 44

def ruby_output
  @ruby_output
end

#target_outputObject (readonly)

Returns the value of attribute target_output.



44
45
46
# File 'lib/pprb.rb', line 44

def target_output
  @target_output
end

Instance Method Details

#config(options) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/pprb.rb', line 85

def config options
  for key, value in options
    var = "@#{key.to_s.upcase}"
    raise "Unknown option: #{key}" if instance_variable_get(var) == nil
    instance_variable_set var, value
  end
end

#profile(filter = nil, prof = nil, overlay = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pprb.rb', line 62

def profile filter = nil, prof = nil, overlay = nil
  # sort-out the passed arguments
  overlay = filter  if filter.class == Hash
  overlay = prof    if prof.class == Hash
  prof    = filter  if filter.class == Symbol
  filter  = nil     if filter != nil and filter.class != String
  prof    = nil     if prof != nil and prof.class != Symbol

  raise "No profile or overlay specified" if overlay == nil and prof == nil

  unless filter
    if prof
      profile = PROFILES[prof]
      raise "Unknown profile: #{p}" if profile == nil
      config profile
    end

    config overlay if overlay
  else
    profile nil, prof, overlay if File.fnmatch(filter, @path)
  end
end

#runObject



39
40
41
42
# File 'lib/pprb.rb', line 39

def run
  run_and_translate_errors @ruby_output
  @target_output
end