Class: JJTreeTask

Inherits:
CLApp
  • Object
show all
Includes:
FileUtils
Defined in:
lib/rakeutils/jjtreetask.rb

Overview

Implements programmatic control of the JJTree application.

Instance Method Summary collapse

Methods inherited from CLApp

#execute, #normalize_dir_path, #quote_all_values, #quote_value, #rubyize_path, #windowize_path

Constructor Details

#initializeJJTreeTask

Constructor



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rakeutils/jjtreetask.rb', line 27

def initialize()
  super( find_app )   # Call parent constructor.

  app_path = find_app
  if app_path.nil? or app_path.empty? or !File.exist?(app_path)
    if Ktutils::OS.windows?
      msg = "JAVACC_HOME environment variable is not configured correctly "
      msg += "or JavaCC is not installed."
      msg += "\njjtree.bat not found"
      raise msg
    else
      raise "jjtree not found"
    end
  end

  @static = ""
  @output_file = ""
  @output_dir = ""
end

Instance Method Details

#find_appObject

initialize



47
48
49
50
51
52
53
54
55
56
# File 'lib/rakeutils/jjtreetask.rb', line 47

def find_app
  if Ktutils::OS.windows?
    app_home = ENV["JAVACC_HOME"]
    unless app_home.nil? or app_home.empty?
      app_path = File.join(app_home, "bin", "jjtree.bat")
    end
  else
    app_path = `which jjtree`.chomp
  end
end

#generate_from(grammar) ⇒ Object

Generate javacc grammar file based on JJTree grammar description file.

grammar

grammar description file (.jjt)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rakeutils/jjtreetask.rb', line 82

def generate_from(grammar)
  puts "Generating JavaCC grammar from: #{grammar}"

  # Note: jjtree help states that args can be supplied using either of
  #       2 forms:
  #         -OPTION=value
  #         -OPTION:value
  #
  #       So far, I get errors (and jjtree doesn't recognize) options
  #       passed with '='.
  #
  #       Use form -OPTION: instead.
  options = []
  options << "-STATIC:#{@static}" unless @static.empty?
  options << "-OUTPUT_FILE:#{@output_file}" unless @output_file.empty?
  options << "-OUTPUT_DIRECTORY:#{@output_dir}" unless @output_dir.empty?

  cmd_line = options.join(' ') + " #{grammar}"

  begin
      execute( cmd_line, false )
  rescue Exception => e
      puts "!!! Errors occured during parsing of JJTree grammar."
      puts e.message
      exit
  end
end

#output_dir(pathname) ⇒ Object

Set the output directory.

pathname

string path of output directory. Default = current directory.



76
77
78
# File 'lib/rakeutils/jjtreetask.rb', line 76

def output_dir(pathname)
  @output_dir = pathname
end

#output_file(filename) ⇒ Object

Set the output filename.

filename

string name of output file. Default = input filename with .jj suffix.



70
71
72
# File 'lib/rakeutils/jjtreetask.rb', line 70

def output_file(filename)
  @output_file = filename
end

#static(true_or_false) ⇒ Object

Set the static class generation flag.

true_or_false

string value (true or false). Default = true.



60
61
62
63
64
65
66
# File 'lib/rakeutils/jjtreetask.rb', line 60

def static(true_or_false)
  if (true_or_false != 'true' && true_or_false != 'false')
    puts "JJTreeTask Error: static must be string value ('true' or 'false')"
    return
  end
  @static = true_or_false.to_s
end