Module: Hoe::Racc

Defined in:
lib/hoe/racc.rb

Overview

Racc plugin for hoe.

Tasks Provided:

lexer

Generate lexers for all .rex files in your Manifest.txt.

parser

Generate parsers for all .y files in your Manifest.txt.

.y -> .rb rule

Generate a parser using racc.

.rex -> .rb rule

Generate a lexer using oedipus_lex.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) oedipus_options

Optional: Defines what flags to use for oedipus_lex. default: “–independent”



33
34
35
# File 'lib/hoe/racc.rb', line 33

def oedipus_options
  @oedipus_options
end

- (Object) racc_flags

Optional: Defines what flags to use for racc. default: “-v -l”



28
29
30
# File 'lib/hoe/racc.rb', line 28

def racc_flags
  @racc_flags
end

- (Object) racc_tasks

Optional: Defines what tasks need to generate parsers/lexers first.

Defaults to [:multi, :test, :check_manifest]

If you have extra tasks that require your parser/lexer to be built, add their names here in your hoe spec. eg:

racc_tasks << :debug


23
24
25
# File 'lib/hoe/racc.rb', line 23

def racc_tasks
  @racc_tasks
end

Instance Method Details

- (Object) activate_racc_deps

Activate the racc dependencies



52
53
54
# File 'lib/hoe/racc.rb', line 52

def activate_racc_deps
  dependency 'racc', '~> 1.4.6', :development
end

- (Object) define_racc_tasks

Define tasks for racc plugin



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hoe/racc.rb', line 59

def define_racc_tasks
  racc_files   = self.spec.files.find_all { |f| f =~ /\.y$/ }
  rex_files    = self.spec.files.find_all { |f| f =~ /\.rex$/ }

  parser_files = racc_files.map { |f| f.sub(/\.y$/, ".rb") }
  lexer_files  = rex_files.map  { |f| f.sub(/\.rex$/, ".rex.rb") }

  self.clean_globs += parser_files
  self.clean_globs += lexer_files

  rule ".rb" => ".y" do |t|
    begin
      sh "racc #{racc_flags} -o #{t.name} #{t.source}"
    rescue
      abort "need racc, sudo gem install racc"
    end
  end

  # HACK: taken from oedipus_lex's .rake file to bypass isolate bootstrap
  rule ".rex.rb" => proc {|path| path.sub(/\.rb$/, "") } do |t|
    require "oedipus_lex"
    warn "Generating #{t.name} from #{t.source}"
    oedipus = OedipusLex.new oedipus_options
    oedipus.parse_file t.source

    File.open t.name, "w" do |f|
      f.write oedipus.generate
    end
  end

  task :isolate # stub task

  desc "build the parser" unless parser_files.empty?
  task :parser => :isolate

  desc "build the lexer" unless lexer_files.empty?
  task :lexer  => :isolate

  task :parser => parser_files
  task :lexer  => lexer_files

  racc_tasks.each do |t|
    task t => [:parser, :lexer]
  end
end

- (Object) initialize_racc

Initialize variables for racc plugin.



38
39
40
41
42
43
44
45
46
47
# File 'lib/hoe/racc.rb', line 38

def initialize_racc
  self.racc_tasks = [:multi, :test, :check_manifest]

  # -v = verbose
  # -l = no-line-convert (they don't ever line up anyhow)
  self.racc_flags ||= "-v -l"
  self.oedipus_options ||= {
                            :do_parse => false
                           }
end