forked from pegjs/pegjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
45 lines (35 loc) · 1.06 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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"
def preprocess(input, base_dir)
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)
else
line
end
end.join("\n")
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))
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