Class: JavaCCTask

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

Overview

Implements programmatic control of the JavaCC application.

Instance Method Summary collapse

Methods inherited from CLApp

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

Constructor Details

#initializeJavaCCTask

Constructor



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

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 += "\njavacc.bat not found"
      raise msg
    else
      raise "javacc not found"
    end
  end

  @look_ahead = ""
  @static = ""
  @output_dir = ""
end

Instance Method Details

#find_appObject

initialize



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

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", "javacc.bat")
    end
  else
    app_path = `which javacc`.chomp
  end
end

#generate_from(grammar) ⇒ Object

Generate java classes based on JavaCC grammar description file.

grammar

grammar description file (.jj)



81
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
# File 'lib/rakeutils/javacctask.rb', line 81

def generate_from(grammar)
  puts "Generating Java classes based on grammar file: #{grammar}"

  # Note: javacc help states that args can be supplied using either of
  #       2 forms:
  #         -OPTION=value
  #         -OPTION:value
  #
  #       So far, I get errors (and javacc doesn't recognize) options
  #       passed with '='.
  #
  #       Use form -OPTION: instead.
  options = []
  options << "-STATIC:#{@static}" unless @static.empty?
  options << "-LOOKAHEAD:#{@look_ahead}" unless @look_ahead.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 JavaCC grammar."
      puts e.message
      #exit
  end
end

#output_dir(pathname) ⇒ Object

Set the output directory.

pathname

string; path of output directory. Default = current directory.



75
76
77
# File 'lib/rakeutils/javacctask.rb', line 75

def output_dir(pathname)
  @output_dir = pathname
end

#output_file(look_ahead) ⇒ Object

Set the lookahead depth.

look_ahead

string; depth of lookahead. Default = 1.



69
70
71
# File 'lib/rakeutils/javacctask.rb', line 69

def output_file(look_ahead)
  @look_ahead = look_ahead.to_s
end

#static(true_or_false) ⇒ Object

Set the static class generation flag.

true_or_false

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



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

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
end