From f3845726f23bfbaa0f9ecef845a0cdd25cdd66d2 Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 24 Jan 2011 13:38:53 +0100 Subject: [PATCH] Rakefile -> Jakefile Or, swapped Ruby dependency for a Node dependency. The build script was also modified to always regenerate the parser (in case of the "parser" task) or rebuild the library (in case of the "build" task) even if the source files were not modified. Not doing this led to problems when the generating code changed but the files didn't (which happened often during development). --- Jakefile | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 6 ++--- Rakefile | 47 ------------------------------------ 3 files changed, 75 insertions(+), 50 deletions(-) create mode 100644 Jakefile delete mode 100644 Rakefile diff --git a/Jakefile b/Jakefile new file mode 100644 index 0000000..ea3205e --- /dev/null +++ b/Jakefile @@ -0,0 +1,72 @@ +var sys = require("sys"); +var fs = require("fs"); + +/* Relative paths are here because of use in |require|. */ +var SRC_DIR = "./src"; +var LIB_DIR = "./lib"; +var BIN_DIR = "./bin"; + +var PEGJS = BIN_DIR + "/pegjs"; + +var PEGJS_SRC_FILE = SRC_DIR + "/peg.js"; +var PEGJS_OUT_FILE = LIB_DIR + "/peg.js"; + +var PARSER_SRC_FILE = SRC_DIR + "/parser.pegjs"; +var PARSER_OUT_FILE = SRC_DIR + "/parser.js"; + +var PEGJS_VERSION = fs.readFileSync("VERSION", "utf8").trim(); + +var PEG = require(PEGJS_OUT_FILE); + +function exitFailure() { + process.exit(1); +} + +function abort(message) { + sys.error(message); + exitFailure(); +} + +desc("Generate the grammar parser"); +task("parser", [], function() { + var input = fs.readFileSync(PARSER_SRC_FILE, "utf8"); + + try { + var parser = PEG.buildParser(input); + } catch (e) { + if (e.line !== undefined && e.column !== undefined) { + abort(e.line + ":" + e.column + ": " + e.message); + } else { + abort(e.message); + } + } + + fs.writeFileSync(PARSER_OUT_FILE, "PEG.parser = " + parser.toSource() + ";\n"); +}); + +desc("Build the peg.js file"); +task("build", ["parser"], function() { + function preprocess(file) { + var input = fs.readFileSync(file, "utf8").trim(); + return input.split("\n").map(function(line) { + var matches = /^\s*\/\/\s*@include\s*"([^"]*)"\s*$/.exec(line); + if (matches !== null) { + var includedFile = SRC_DIR + "/" + matches[1]; + + try { + fs.statSync(includedFile); + } catch (e) { + abort("Included file \"" + includedFile + "\" does not exist."); + } + + return preprocess(includedFile); + } else { + return line; + } + }).join("\n").replace("@VERSION", PEGJS_VERSION); + } + + fs.writeFileSync(PEGJS_OUT_FILE, preprocess(PEGJS_SRC_FILE), "utf8"); +}); + +task("default", ["build"], function() {}); diff --git a/README.md b/README.md index 271d592..9383b78 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,11 @@ Features Building -------- -To build PEG.js, simply run the `rake` command: +To build PEG.js, you need to have [Node](http://nodejs.org/) installed together with the `jake` package. Then simply run the `jake` command: - $ rake + $ jake -Of course, you need to have [Rake](http://rake.rubyforge.org/) installed. The command creates PEG.js library in `lib/peg.js` by processing files in the `src` directory. +This command creates the PEG.js library in `lib/peg.js` by processing files in the `src` directory. Usage ----- diff --git a/Rakefile b/Rakefile deleted file mode 100644 index 90e5846..0000000 --- a/Rakefile +++ /dev/null @@ -1,47 +0,0 @@ -SRC_DIR = "src" -LIB_DIR = "lib" -BIN_DIR = "bin" - -PEGJS = "#{BIN_DIR}/pegjs" - -SRC_FILES = Dir["#{SRC_DIR}/**/*.js"] - -PEGJS_SRC_FILE = "#{SRC_DIR}/peg.js" -PEGJS_OUT_FILE = "#{LIB_DIR}/peg.js" - -PARSER_SRC_FILE = "#{SRC_DIR}/parser.pegjs" -PARSER_OUT_FILE = "#{SRC_DIR}/parser.js" - -PEGJS_VERSION = File.read("VERSION").strip - -def preprocess(input, base_dir, version) - input.split("\n").map do |line| - if line =~ /^\s*\/\/\s*@include\s*"([^"]*)"\s*$/ - included_file = "#{base_dir}/#$1" - if !File.exist?(included_file) - abort "Included file \"#{included_file}\" does not exist." - end - preprocess(File.read(included_file), base_dir, version) - else - line - end - end.join("\n").gsub("@VERSION", version) -end - -file PARSER_OUT_FILE => PARSER_SRC_FILE do - system "#{PEGJS} --export-var PEG.parser #{PARSER_SRC_FILE} #{PARSER_OUT_FILE}" -end - -file PEGJS_OUT_FILE => SRC_FILES do - File.open(PEGJS_OUT_FILE, "w") do |f| - f.write(preprocess(File.read(PEGJS_SRC_FILE), SRC_DIR, PEGJS_VERSION)) - end -end - -desc "Generate the grammar parser" -task :parser => PARSER_OUT_FILE - -desc "Build the peg.js file" -task :build => PEGJS_OUT_FILE - -task :default => :build