diff --git a/.gitignore b/.gitignore index 15e811d..e423b4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -lib/* -dist/* +browser/* diff --git a/Makefile b/Makefile index 32a18a0..41a6f5f 100644 --- a/Makefile +++ b/Makefile @@ -20,31 +20,21 @@ MODULES = utils \ # ===== Directories ===== SRC_DIR = src +LIB_DIR = lib BIN_DIR = bin +BROWSER_DIR = browser SPEC_DIR = spec BENCHMARK_DIR = benchmark -EXAMPLES_DIR = examples -LIB_DIR = lib -DIST_DIR = dist -DIST_WEB_DIR = $(DIST_DIR)/web -DIST_NODE_DIR = $(DIST_DIR)/node # ===== Files ===== PARSER_SRC_FILE = $(SRC_DIR)/parser.pegjs -PARSER_OUT_FILE = $(SRC_DIR)/parser.js +PARSER_OUT_FILE = $(LIB_DIR)/parser.js -PEGJS_SRC_FILE = $(SRC_DIR)/peg.js -PEGJS_LIB_FILE = $(LIB_DIR)/peg.js +BROWSER_FILE_DEV = $(BROWSER_DIR)/peg-$(PEGJS_VERSION).js +BROWSER_FILE_MIN = $(BROWSER_DIR)/peg-$(PEGJS_VERSION).min.js -PEGJS_DIST_FILE_DEV = $(DIST_WEB_DIR)/peg-$(PEGJS_VERSION).js -PEGJS_DIST_FILE_MIN = $(DIST_WEB_DIR)/peg-$(PEGJS_VERSION).min.js - -PACKAGE_JSON_FILE = package.json -CHANGELOG_FILE = CHANGELOG -LICENSE_FILE = LICENSE -README_FILE = README.md -VERSION_FILE = VERSION +VERSION_FILE = VERSION # ===== Executables ===== @@ -56,107 +46,86 @@ BENCHMARK_RUN = $(BENCHMARK_DIR)/run # ===== Targets ===== +# Default target +all: browser + # Generate the grammar parser parser: $(PEGJS) $(PARSER_SRC_FILE) $(PARSER_OUT_FILE) -# Build the PEG.js library -build: - mkdir -p $(LIB_DIR) - rm -f $(PEGJS_LIB_FILE) +# Build the browser version of the library +browser: + mkdir -p $(BROWSER_DIR) + + rm -f $(BROWSER_FILE_DEV) + rm -f $(BROWSER_FILE_MIN) # The following code is inspired by CoffeeScript's Cakefile. - echo '/*' >> $(PEGJS_LIB_FILE) - echo " * PEG.js $(PEGJS_VERSION)" >> $(PEGJS_LIB_FILE) - echo ' *' >> $(PEGJS_LIB_FILE) - echo ' * http://pegjs.majda.cz/' >> $(PEGJS_LIB_FILE) - echo ' *' >> $(PEGJS_LIB_FILE) - echo ' * Copyright (c) 2010-2012 David Majda' >> $(PEGJS_LIB_FILE) - echo ' * Licensed under the MIT license' >> $(PEGJS_LIB_FILE) - echo ' */' >> $(PEGJS_LIB_FILE) - echo 'var PEG = (function(undefined) {' >> $(PEGJS_LIB_FILE) - echo ' var modules = {' >> $(PEGJS_LIB_FILE) - echo ' define: function(name, factory) {' >> $(PEGJS_LIB_FILE) - echo ' var dir = name.replace(/(^|\/)[^/]+$$/, "$$1"),' >> $(PEGJS_LIB_FILE) - echo ' module = { exports: {} };' >> $(PEGJS_LIB_FILE) - echo '' >> $(PEGJS_LIB_FILE) - echo ' function require(path) {' >> $(PEGJS_LIB_FILE) - echo ' var name = dir + path,' >> $(PEGJS_LIB_FILE) - echo ' regexp = /[^\/]+\/\.\.\/|\.\//;' >> $(PEGJS_LIB_FILE) - echo '' >> $(PEGJS_LIB_FILE) - echo " /* Can't use /.../g because we can move backwards in the string. */" >> $(PEGJS_LIB_FILE) - echo ' while (regexp.test(name)) {' >> $(PEGJS_LIB_FILE) - echo ' name = name.replace(regexp, "");' >> $(PEGJS_LIB_FILE) - echo ' }' >> $(PEGJS_LIB_FILE) - echo '' >> $(PEGJS_LIB_FILE) - echo ' return modules[name];' >> $(PEGJS_LIB_FILE) - echo ' }' >> $(PEGJS_LIB_FILE) - echo '' >> $(PEGJS_LIB_FILE) - echo ' factory(module, require);' >> $(PEGJS_LIB_FILE) - echo ' this[name] = module.exports;' >> $(PEGJS_LIB_FILE) - echo ' }' >> $(PEGJS_LIB_FILE) - echo ' };' >> $(PEGJS_LIB_FILE) - echo '' >> $(PEGJS_LIB_FILE) - - for module in $(MODULES); do \ - echo " modules.define(\"$$module\", function(module, require) {" >> $(PEGJS_LIB_FILE); \ - sed -e 's/^/ /' src/$$module.js >> $(PEGJS_LIB_FILE); \ - echo ' });' >> $(PEGJS_LIB_FILE); \ - echo '' >> $(PEGJS_LIB_FILE); \ + echo '/*' >> $(BROWSER_FILE_DEV) + echo " * PEG.js $(PEGJS_VERSION)" >> $(BROWSER_FILE_DEV) + echo ' *' >> $(BROWSER_FILE_DEV) + echo ' * http://pegjs.majda.cz/' >> $(BROWSER_FILE_DEV) + echo ' *' >> $(BROWSER_FILE_DEV) + echo ' * Copyright (c) 2010-2012 David Majda' >> $(BROWSER_FILE_DEV) + echo ' * Licensed under the MIT license' >> $(BROWSER_FILE_DEV) + echo ' */' >> $(BROWSER_FILE_DEV) + echo 'var PEG = (function(undefined) {' >> $(BROWSER_FILE_DEV) + echo ' var modules = {' >> $(BROWSER_FILE_DEV) + echo ' define: function(name, factory) {' >> $(BROWSER_FILE_DEV) + echo ' var dir = name.replace(/(^|\/)[^/]+$$/, "$$1"),' >> $(BROWSER_FILE_DEV) + echo ' module = { exports: {} };' >> $(BROWSER_FILE_DEV) + echo '' >> $(BROWSER_FILE_DEV) + echo ' function require(path) {' >> $(BROWSER_FILE_DEV) + echo ' var name = dir + path,' >> $(BROWSER_FILE_DEV) + echo ' regexp = /[^\/]+\/\.\.\/|\.\//;' >> $(BROWSER_FILE_DEV) + echo '' >> $(BROWSER_FILE_DEV) + echo " /* Can't use /.../g because we can move backwards in the string. */" >> $(BROWSER_FILE_DEV) + echo ' while (regexp.test(name)) {' >> $(BROWSER_FILE_DEV) + echo ' name = name.replace(regexp, "");' >> $(BROWSER_FILE_DEV) + echo ' }' >> $(BROWSER_FILE_DEV) + echo '' >> $(BROWSER_FILE_DEV) + echo ' return modules[name];' >> $(BROWSER_FILE_DEV) + echo ' }' >> $(BROWSER_FILE_DEV) + echo '' >> $(BROWSER_FILE_DEV) + echo ' factory(module, require);' >> $(BROWSER_FILE_DEV) + echo ' this[name] = module.exports;' >> $(BROWSER_FILE_DEV) + echo ' }' >> $(BROWSER_FILE_DEV) + echo ' };' >> $(BROWSER_FILE_DEV) + echo '' >> $(BROWSER_FILE_DEV) + + for module in $(MODULES); do \ + echo " modules.define(\"$$module\", function(module, require) {" >> $(BROWSER_FILE_DEV); \ + sed -e 's/^/ /' lib/$$module.js >> $(BROWSER_FILE_DEV); \ + echo ' });' >> $(BROWSER_FILE_DEV); \ + echo '' >> $(BROWSER_FILE_DEV); \ done - echo ' return modules["peg"]' >> $(PEGJS_LIB_FILE) - echo '})();' >> $(PEGJS_LIB_FILE) - echo '' >> $(PEGJS_LIB_FILE) - echo 'if (typeof module !== "undefined") {' >> $(PEGJS_LIB_FILE) - echo ' module.exports = PEG;' >> $(PEGJS_LIB_FILE) - echo '}' >> $(PEGJS_LIB_FILE) - -# Remove built PEG.js library (created by "build") -clean: - rm -rf $(LIB_DIR) - -# Prepare dstribution files -dist: build - # Web - mkdir -p $(DIST_WEB_DIR) - cp $(PEGJS_LIB_FILE) $(PEGJS_DIST_FILE_DEV) - $(UGLIFYJS) --ascii -o $(PEGJS_DIST_FILE_MIN) $(PEGJS_LIB_FILE) - - # Node.js - mkdir -p $(DIST_NODE_DIR) - cp -r \ - $(LIB_DIR) \ - $(BIN_DIR) \ - $(EXAMPLES_DIR) \ - $(PACKAGE_JSON_FILE) \ - $(CHANGELOG_FILE) \ - $(LICENSE_FILE) \ - $(README_FILE) \ - $(VERSION_FILE) \ - $(DIST_NODE_DIR) - -# Remove distribution file (created by "dist") -distclean: - rm -rf $(DIST_DIR) + echo ' return modules["peg"]' >> $(BROWSER_FILE_DEV) + echo '})();' >> $(BROWSER_FILE_DEV) + + $(UGLIFYJS) --ascii -o $(BROWSER_FILE_MIN) $(BROWSER_FILE_DEV) + +# Remove browser version of the library (created by "browser") +browserclean: + rm -rf $(BROWSER_DIR) # Run the spec suite -spec: build +spec: $(JASMINE_NODE) --verbose $(SPEC_DIR) # Run the benchmark suite -benchmark: build +benchmark: $(BENCHMARK_RUN) # Run JSHint on the source -hint: build +hint: $(JSHINT) \ - `find $(SRC_DIR) -name '*.js'` \ + `find $(LIB_DIR) -name '*.js'` \ `find $(SPEC_DIR) -name '*.js' -and -not -path '$(SPEC_DIR)/vendor/*'` \ $(BENCHMARK_DIR)/*.js \ $(BENCHMARK_RUN) \ $(PEGJS) -.PHONY: spec benchmark hint parser build clean dist distclean -.SILENT: spec benchmark hint parser build clean dist distclean +.PHONY: all parser browser browserclean spec benchmark hint +.SILENT: all parser browser browserclean spec benchmark hint diff --git a/benchmark/README b/benchmark/README index ec7933b..25d391e 100644 --- a/benchmark/README +++ b/benchmark/README @@ -8,27 +8,36 @@ optimization. Running in a browser -------------------- - 1. Start a web server and make it serve the PEG.js root directory (one level - up from this one). + 1. Make sure you have Node.js and all the development dependencies specified + in package.json installed. - 2. Point your browser to an URL corresponding to the index.html file. + 2. Run the following command in the PEG.js root directory (one level up from + this one): - 3. Wait for the table to fill. + make browser + + 3. Start a web server and make it serve the PEG.js root directory. + + 4. Point your browser to an URL corresponding to the index.html file. + + 5. Click the "Run" button and wait for the table to fill. -If you have Python installed, you can just run the following command in the -PEG.js root directory +If you have Python installed, you can fulfill steps 3 and 4 by running the +following command in the PEG.js root directory python -m SimpleHTTPServer -and load http://localhost:8000/benchmark/ in your browser. +and load http://localhost:8000/benchmark/index.html in your browser. Running from a command-line --------------------------- - 1. Make sure you have Node.js installed. + 1. Make sure you have Node.js and all the development dependencies specified + in package.json installed. - 2. Run the following command: + 2. Run the following command in the PEG.js root directory (one level up from + this one): - ./run + make benchmark 3. Wait for the table to fill. diff --git a/benchmark/index.html b/benchmark/index.html index 5b201e9..a8633cc 100644 --- a/benchmark/index.html +++ b/benchmark/index.html @@ -30,7 +30,7 @@ - + diff --git a/src/compiler.js b/lib/compiler.js similarity index 100% rename from src/compiler.js rename to lib/compiler.js diff --git a/src/compiler/passes.js b/lib/compiler/passes.js similarity index 100% rename from src/compiler/passes.js rename to lib/compiler/passes.js diff --git a/src/compiler/passes/allocate-registers.js b/lib/compiler/passes/allocate-registers.js similarity index 100% rename from src/compiler/passes/allocate-registers.js rename to lib/compiler/passes/allocate-registers.js diff --git a/src/compiler/passes/generate-code.js b/lib/compiler/passes/generate-code.js similarity index 100% rename from src/compiler/passes/generate-code.js rename to lib/compiler/passes/generate-code.js diff --git a/src/compiler/passes/remove-proxy-rules.js b/lib/compiler/passes/remove-proxy-rules.js similarity index 100% rename from src/compiler/passes/remove-proxy-rules.js rename to lib/compiler/passes/remove-proxy-rules.js diff --git a/src/compiler/passes/report-left-recursion.js b/lib/compiler/passes/report-left-recursion.js similarity index 100% rename from src/compiler/passes/report-left-recursion.js rename to lib/compiler/passes/report-left-recursion.js diff --git a/src/compiler/passes/report-missing-rules.js b/lib/compiler/passes/report-missing-rules.js similarity index 100% rename from src/compiler/passes/report-missing-rules.js rename to lib/compiler/passes/report-missing-rules.js diff --git a/src/grammar-error.js b/lib/grammar-error.js similarity index 100% rename from src/grammar-error.js rename to lib/grammar-error.js diff --git a/src/parser.js b/lib/parser.js similarity index 100% rename from src/parser.js rename to lib/parser.js diff --git a/src/peg.js b/lib/peg.js similarity index 100% rename from src/peg.js rename to lib/peg.js diff --git a/src/utils.js b/lib/utils.js similarity index 100% rename from src/utils.js rename to lib/utils.js diff --git a/package.json b/package.json index a0309dd..aba318c 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,29 @@ "email": "david@majda.cz", "url": "http://majda.cz/" }, + "files": [ + "CHANGELOG", + "LICENSE", + "README.md", + "VERSION", + "bin/pegjs", + "examples/arithmetics.pegjs", + "examples/css.pegjs", + "examples/javascript.pegjs", + "examples/json.pegjs", + "lib/compiler.js", + "lib/compiler/passes.js", + "lib/compiler/passes/allocate-registers.js", + "lib/compiler/passes/generate-code.js", + "lib/compiler/passes/remove-proxy-rules.js", + "lib/compiler/passes/report-left-recursion.js", + "lib/compiler/passes/report-missing-rules.js", + "lib/grammar-error.js", + "lib/parser.js", + "lib/peg.js", + "lib/utils.js", + "package.json" + ], "main": "lib/peg", "bin": "bin/pegjs", "scripts": { diff --git a/spec/README b/spec/README index 1f1471b..787270d 100644 --- a/spec/README +++ b/spec/README @@ -7,27 +7,36 @@ should always pass on all supported platforms. Running in a browser -------------------- - 1. Start a web server and make it serve the PEG.js root directory (one level - up from this one). + 1. Make sure you have Node.js and all the development dependencies specified + in package.json installed. - 2. Point your browser to an URL corresponding to the index.html file. + 2. Run the following command in the PEG.js root directory (one level up from + this one): - 3. Watch the specs pass (or fail). + make browser + + 3. Start a web server and make it serve the PEG.js root directory. + + 4. Point your browser to an URL corresponding to the index.html file. + + 5. Watch the specs pass (or fail). -If you have Python installed, you can just run the following command in the -PEG.js root directory +If you have Python installed, you can fulfill steps 3 and 4 by running the +following command in the PEG.js root directory python -m SimpleHTTPServer -and load http://localhost:8000/spec/ in your browser. +and loading http://localhost:8000/spec/index.html in your browser. Running from a command-line --------------------------- - 1. Make sure you have Node.js and the "jasmine-node" npm pacakge installed. + 1. Make sure you have Node.js and all the development dependencies specified + in package.json installed. - 2. Run the following command: + 2. Run the following command in the PEG.js root directory (one level up from + this one): - jasmine-node --verbose . + make spec 3. Watch the specs pass (or fail). diff --git a/spec/index.html b/spec/index.html index 4a5db67..ac58f7b 100644 --- a/spec/index.html +++ b/spec/index.html @@ -5,7 +5,7 @@ - +