From e59f3ba3384649cef46790f3b186be3d845006d6 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 25 Jul 2010 17:54:09 +0200 Subject: [PATCH] Split the source code into several files, introduce build system The source code is now in the src directory. The library needs to be built using "rake", which creates the lib/peg.js file by combining the source files. --- .gitignore | 2 +- README.md | 9 + Rakefile | 29 +- benchmark/index.html | 3 +- bin/pegjs-main.js | 3 +- src/checks.js | 127 +++++++ src/compiler.js | 31 ++ lib/compiler.js => src/emitter.js | 369 ------------------- lib/metagrammar.js => src/parser.js | 0 lib/metagrammar.pegjs => src/parser.pegjs | 0 src/passes.js | 83 +++++ src/peg.js | 43 +++ src/utils.js | 94 +++++ test/index.html | 5 +- test/{metagrammar-test.js => parser-test.js} | 0 15 files changed, 420 insertions(+), 378 deletions(-) create mode 100644 src/checks.js create mode 100644 src/compiler.js rename lib/compiler.js => src/emitter.js (71%) rename lib/metagrammar.js => src/parser.js (100%) rename lib/metagrammar.pegjs => src/parser.pegjs (100%) create mode 100644 src/passes.js create mode 100644 src/peg.js create mode 100644 src/utils.js rename test/{metagrammar-test.js => parser-test.js} (100%) diff --git a/.gitignore b/.gitignore index f207758..cdecab1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -lib/*.min.js +lib/* diff --git a/README.md b/README.md index 6577fbe..f18e9ac 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,15 @@ Features * Handles wide class of grammars (superset of LL(*k*) and LR(*k*)) * Precise and human-friendly error reporting +Building +-------- + +To build PEG.js, simply run the `rake` command: + + $ rake + +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. + Usage ----- diff --git a/Rakefile b/Rakefile index 5a786fd..a769c20 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,31 @@ +SRC_DIR = "src" +LIB_DIR = "lib" +BIN_DIR = "bin" + +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 + desc "Generate the grammar parser" task :metaparser do - system "bin/pegjs PEG.parser lib/metagrammar.pegjs" + system "#{BIN_DIR}/pegjs PEG.parser #{SRC_DIR}/parser.pegjs" +end + +desc "Build the peg.js file" +task :build do + File.open("#{LIB_DIR}/peg.js", "w") do |f| + f.write(preprocess(File.read("#{SRC_DIR}/peg.js"), SRC_DIR)) + end end + +task :default => :build diff --git a/benchmark/index.html b/benchmark/index.html index 3bc29a5..ac61e97 100644 --- a/benchmark/index.html +++ b/benchmark/index.html @@ -60,8 +60,7 @@ - - + - - + - +

PEG.js Test Suite

diff --git a/test/metagrammar-test.js b/test/parser-test.js similarity index 100% rename from test/metagrammar-test.js rename to test/parser-test.js