diff --git a/.idea/chevrotain.iml b/.idea/chevrotain.iml index ebe28e9b83..09232cd3d3 100644 --- a/.idea/chevrotain.iml +++ b/.idea/chevrotain.iml @@ -27,6 +27,7 @@ + diff --git a/examples/implementation_languages/impl_lang_spec.js b/examples/implementation_languages/impl_lang_spec.js index 0cd1485d07..7d48f19232 100644 --- a/examples/implementation_languages/impl_lang_spec.js +++ b/examples/implementation_languages/impl_lang_spec.js @@ -12,7 +12,10 @@ function createSanityTest(languageName, parseJson) { describe("The ability to use Chevrotain using different implementation languages", () => { createSanityTest("ECMAScript 5", require("./ecma5/ecma5_json")) - createSanityTest("ECMAScript 6/2015", require("./ecma6/ecma6_json")) + createSanityTest( + "ECMAScript 6/2015", + require("./modern_ecmascript/ecma6_json") + ) createSanityTest( "TypeScript", require("./typescript/typescript_json").parseJson diff --git a/examples/implementation_languages/impl_lang_spec.mjs b/examples/implementation_languages/impl_lang_spec.mjs new file mode 100644 index 0000000000..2344a678a9 --- /dev/null +++ b/examples/implementation_languages/impl_lang_spec.mjs @@ -0,0 +1,12 @@ +import { parseJson } from "./modern_ecmascript/modern_ecmascript_json.mjs" +import assert from "assert" + +describe("The ability to use Chevrotain using modern ECMAScript", () => { + it("works with ESM", () => { + const inputText = '{ "arr": [1,2,3], "obj": {"num":666}}' + const lexAndParseResult = parseJson(inputText) + + assert.equal(lexAndParseResult.lexErrors.length, 0) + assert.equal(lexAndParseResult.parseErrors.length, 0) + }) +}) diff --git a/examples/implementation_languages/ecma6/ecma6_json.js b/examples/implementation_languages/modern_ecmascript/modern_ecmascript_json.mjs similarity index 97% rename from examples/implementation_languages/ecma6/ecma6_json.js rename to examples/implementation_languages/modern_ecmascript/modern_ecmascript_json.mjs index 862c12f0fd..0010a66adb 100644 --- a/examples/implementation_languages/ecma6/ecma6_json.js +++ b/examples/implementation_languages/modern_ecmascript/modern_ecmascript_json.mjs @@ -1,5 +1,5 @@ "use strict" -const { createToken, Lexer, CstParser } = require("chevrotain") +import { createToken, Lexer, CstParser } from "chevrotain" // ----------------- lexer ----------------- const True = createToken({ name: "True", pattern: /true/ }) @@ -114,7 +114,7 @@ class JsonParserES6 extends CstParser { // reuse the same parser instance. const parser = new JsonParserES6() -module.exports = function (text) { +export function parseJson (text) { const lexResult = JsonLexer.tokenize(text) // setting a new input will RESET the parser instance's state. parser.input = lexResult.tokens diff --git a/examples/implementation_languages/package.json b/examples/implementation_languages/package.json index d2c35f82e7..6fae3d2806 100644 --- a/examples/implementation_languages/package.json +++ b/examples/implementation_languages/package.json @@ -5,7 +5,9 @@ "build": "npm-run-all build:ts build:coffee", "build:ts": "tsc ./typescript/typescript_json.ts --types \" \"", "build:coffee": "coffee -c ./coffeescript/coffeescript_json.coffee", - "test": "mocha \"*spec.js\"" + "test": "npm-run-all test:*", + "test:cjs": "mocha \"*spec.js\"", + "test:esm": "mocha \"*spec.mjs\"" }, "dependencies": { "chevrotain": "^7.1.2" diff --git a/packages/chevrotain/package.json b/packages/chevrotain/package.json index b1ffe69c16..8c82f17e04 100644 --- a/packages/chevrotain/package.json +++ b/packages/chevrotain/package.json @@ -23,8 +23,7 @@ "name": "Shahar Soel" }, "files": [ - "lib_esm/src/**/*.js", - "lib_esm/src/**/*.js.map", + "lib_esm/**/*.js", "lib/src/**/*.js", "lib/src/**/*.js.map", "lib/chevrotain.d.ts", @@ -36,8 +35,11 @@ "diagrams/**/*.*", "CHANGELOG.md" ], - "main": "lib/src/api.js", - "module": "lib_esm/src/api.js", + "main": "./lib/src/api.js", + "exports": { + "require": "./lib/src/api.js", + "import": "./lib_esm/api_esm.mjs" + }, "repository": { "type": "git", "url": "git://github.com/Chevrotain/chevrotain.git" @@ -45,10 +47,8 @@ "homepage": "https://chevrotain.io/docs/", "scripts": { "---------- CI FLOWS --------": "", - "build": "npm-run-all clean compile build:esm dts api-site:build bundle", - "build:esm": "npm-run-all clean:esm compile:esm", - "test": "npm-run-all test:esm compile:def coverage check-coverage", - "test:esm": "mocha \"./lib_esm/test/**/*spec.js\" --require esm", + "build": "npm-run-all clean compile dts api-site:build bundle", + "test": "npm-run-all compile:def coverage check-coverage", "version": "tsc ./src/version.ts --outDir lib/src && node ./scripts/version-update.js", "postversion": "npm-run-all website:build website:upload api-site:build api-site:upload", "---------- DEV FLOWS --------": "", @@ -56,16 +56,15 @@ "unit-tests": "mocha \"./lib/test/**/*spec.js\"", "quick-build": "tsc && yarn run bundle", "---------- BUILD STEPS --------": "", - "clean": "shx rm -rf coverage dev lib", - "clean:esm": "shx rm -rf lib_esm", - "compile": "tsc && node ./scripts/fix-coverage-report.js", - "compile:esm": "tsc --project tsconfig.esm.json", + "clean": "shx rm -rf coverage dev lib lib_esm", + "compile": "tsc && node ./scripts/fix-coverage-report.js && gen-esm-wrapper", "compile:watch": "tsc -w", "compile:def": "npm-run-all compile-def-api compile-def-modules compile-def-namespace", + "gen-esm-wrapper": "gen-esm-wrapper . ./lib_esm/api_esm.mjs", "dts": "node scripts/process-docs.js", "coverage": "nyc mocha \"./lib/test/**/*spec.js\"", "check-coverage": "nyc check-coverage --lines 100 --branches 100 --statements 100 --functions 100", - "bundle": "npm-run-all bundle:regular bundle:min", + "bundle": "npm-run-all bundle:*", "api-site:build": "typedoc api.d.ts --out dev/docs --excludeExternals --excludePrivate", "api-site:upload": "./scripts/api-site-upload.sh", "website:dev": "vuepress dev docs", @@ -77,8 +76,10 @@ "compile-def-modules": "tsc --noImplicitAny test_integration/definitions/es6_modules.ts --outDir dev/garbage --lib \"es2015\"", "compile-def-namespace": "tsc --noImplicitAny test_integration/definitions/namespaces.ts --module none --outDir dev/garbage --lib \"es2015\"", "---------- BUNDLING --------": "", - "bundle:regular": "webpack --config webpack_release.config.js", - "bundle:min": "webpack --config webpack_release_min.config.js" + "bundle:regular": "esbuild ./lib/src/api.js --bundle --sourcemap --outfile=lib/chevrotain.js", + "bundle:min": "esbuild ./lib/src/api.js --bundle --minify --sourcemap --outfile=lib/chevrotain.min.js", + "bundle:esm:regular": "esbuild ./lib/src/api.js --bundle --sourcemap --format=esm --outfile=lib/chevrotain.mjs", + "bundle:esm:min": "esbuild ./lib/src/api.js --bundle --minify --format=esm --sourcemap --outfile=lib/chevrotain.min.mjs" }, "dependencies": { "regexp-to-ast": "0.5.0" @@ -89,7 +90,6 @@ "@types/sinon-chai": "^3.2.0", "chai": "^4.1.2", "error-stack-parser": "^2.0.6", - "esm": "^3.2.25", "gitty": "^3.6.0", "jsdom": "16.4.0", "jsonfile": "^6.0.1", @@ -101,9 +101,9 @@ "typedoc": "^0.20.28", "typescript": "4.2.2", "vuepress": "^1.4.1", - "webpack": "5.24.2", - "webpack-cli": "^4.1.0", - "xregexp": "^5.0.1" + "xregexp": "^5.0.1", + "gen-esm-wrapper": "^1.0.4", + "esbuild": "^0.8.53" }, "typings": "lib/chevrotain.d.ts", "mocha": { diff --git a/packages/chevrotain/src/api.ts b/packages/chevrotain/src/api.ts index 0293cb5025..39efbc1032 100644 --- a/packages/chevrotain/src/api.ts +++ b/packages/chevrotain/src/api.ts @@ -1,8 +1,19 @@ /* istanbul ignore file - tricky to import some things from this module during testing */ +import * as apiDefs from "../api" // semantic version +import { VERSION } from "./version" export { VERSION } from "./version" +import { Lexer, LexerDefinitionErrorType } from "./scan/lexer_public" +export { Lexer, LexerDefinitionErrorType } from "./scan/lexer_public" + +import { + CstParser, + EmbeddedActionsParser, + ParserDefinitionErrorType, + EMPTY_ALT +} from "./parse/parser/parser" export { CstParser, EmbeddedActionsParser, @@ -10,10 +21,14 @@ export { EMPTY_ALT } from "./parse/parser/parser" -export { Lexer, LexerDefinitionErrorType } from "./scan/lexer_public" - -// Tokens utilities - +import { + createToken, + createTokenInstance, + EOF, + tokenLabel, + tokenMatcher, + tokenName +} from "./scan/tokens_public" export { createToken, createTokenInstance, @@ -23,14 +38,24 @@ export { tokenName } from "./scan/tokens_public" -// Other Utilities - +import { + defaultGrammarResolverErrorProvider, + defaultGrammarValidatorErrorProvider, + defaultParserErrorProvider +} from "./parse/errors_public" export { defaultGrammarResolverErrorProvider, defaultGrammarValidatorErrorProvider, defaultParserErrorProvider } from "./parse/errors_public" +import { + EarlyExitException, + isRecognitionException, + MismatchedTokenException, + NotAllInputParsedException, + NoViableAltException +} from "./parse/exceptions_public" export { EarlyExitException, isRecognitionException, @@ -39,10 +64,18 @@ export { NoViableAltException } from "./parse/exceptions_public" -export { defaultLexerErrorProvider } from "./scan/lexer_errors_public" - -// grammar reflection API - +import { + Alternation, + Alternative, + NonTerminal, + Option, + Repetition, + RepetitionMandatory, + RepetitionMandatoryWithSeparator, + RepetitionWithSeparator, + Rule, + Terminal +} from "./parse/grammar/gast/gast_public" export { Alternation, Alternative, @@ -56,21 +89,88 @@ export { Terminal } from "./parse/grammar/gast/gast_public" -// GAST Utilities - +import { + serializeGrammar, + serializeProduction +} from "./parse/grammar/gast/gast_public" export { serializeGrammar, serializeProduction } from "./parse/grammar/gast/gast_public" +import { GAstVisitor } from "./parse/grammar/gast/gast_visitor_public" export { GAstVisitor } from "./parse/grammar/gast/gast_visitor_public" +import { + assignOccurrenceIndices, + resolveGrammar, + validateGrammar +} from "./parse/grammar/gast/gast_resolver_public" export { assignOccurrenceIndices, resolveGrammar, validateGrammar } from "./parse/grammar/gast/gast_resolver_public" +import { createSyntaxDiagramsCode } from "./diagrams/render_public" +export { createSyntaxDiagramsCode } from "./diagrams/render_public" + +import { + generateParserFactory, + generateParserModule +} from "./generate/generate_public" +export { + generateParserFactory, + generateParserModule +} from "./generate/generate_public" + +const api: typeof apiDefs = { + VERSION, + Lexer, + LexerDefinitionErrorType, + // @ts-ignore + CstParser, + // @ts-ignore + EmbeddedActionsParser, + ParserDefinitionErrorType, + EMPTY_ALT, + createToken, + createTokenInstance, + EOF, + tokenLabel, + tokenMatcher, + tokenName, + defaultGrammarResolverErrorProvider, + defaultGrammarValidatorErrorProvider, + defaultParserErrorProvider, + EarlyExitException, + isRecognitionException, + MismatchedTokenException, + NotAllInputParsedException, + NoViableAltException, + Alternation, + Alternative, + NonTerminal, + Option, + Repetition, + RepetitionMandatory, + RepetitionMandatoryWithSeparator, + RepetitionWithSeparator, + Rule, + Terminal, + serializeGrammar, + serializeProduction, + GAstVisitor, + assignOccurrenceIndices, + resolveGrammar, + validateGrammar, + createSyntaxDiagramsCode, + generateParserFactory, + generateParserModule +} + +export default api + /* istanbul ignore next */ export function clearCache() { console.warn( @@ -80,13 +180,6 @@ export function clearCache() { ) } -export { createSyntaxDiagramsCode } from "./diagrams/render_public" - -export { - generateParserFactory, - generateParserModule -} from "./generate/generate_public" - export class Parser { constructor() { throw new Error( diff --git a/packages/chevrotain/tsconfig.esm.json b/packages/chevrotain/tsconfig.esm.json deleted file mode 100644 index 2050f256c8..0000000000 --- a/packages/chevrotain/tsconfig.esm.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "moduleResolution": "node", - "module": "esnext", - "target": "es5", - "outDir": "lib_esm" - } -} diff --git a/packages/chevrotain/tsconfig.json b/packages/chevrotain/tsconfig.json index 73633bf106..aa758e1de8 100644 --- a/packages/chevrotain/tsconfig.json +++ b/packages/chevrotain/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "module": "commonjs", + "module": "CommonJS", "removeComments": false, "target": "ES5", "outDir": "lib", diff --git a/packages/chevrotain/webpack_release.config.js b/packages/chevrotain/webpack_release.config.js deleted file mode 100644 index 5e707206ac..0000000000 --- a/packages/chevrotain/webpack_release.config.js +++ /dev/null @@ -1,29 +0,0 @@ -const path = require("path") -const webpack = require("webpack") -const jf = require("jsonfile") -const pkg = jf.readFileSync("./package.json") - -const banner = `/*! ${pkg.name} - v${pkg.version} */` - -module.exports = { - mode: "production", - stats: { - colors: true, - modules: true, - reasons: true - }, - entry: "./lib/src/api.js", - output: { - path: path.resolve(__dirname, "./lib/"), - filename: "chevrotain.js", - library: "chevrotain", - libraryTarget: "umd", - umdNamedDefine: true, - // https://github.com/webpack/webpack/issues/6784#issuecomment-375941431 - globalObject: "typeof self !== 'undefined' ? self : this" - }, - optimization: { - minimize: false - }, - plugins: [new webpack.BannerPlugin({ banner: banner, raw: true })] -} diff --git a/packages/chevrotain/webpack_release_min.config.js b/packages/chevrotain/webpack_release_min.config.js deleted file mode 100644 index e70b976d61..0000000000 --- a/packages/chevrotain/webpack_release_min.config.js +++ /dev/null @@ -1,29 +0,0 @@ -const path = require("path") -const webpack = require("webpack") -const jf = require("jsonfile") -const pkg = jf.readFileSync("./package.json") - -const banner = `/*! ${pkg.name} - v${pkg.version} */` - -module.exports = { - mode: "production", - stats: { - colors: true, - modules: true, - reasons: true - }, - entry: "./lib/src/api.js", - output: { - path: path.resolve(__dirname, "./lib/"), - filename: "chevrotain.min.js", - library: "chevrotain", - libraryTarget: "umd", - umdNamedDefine: true, - // https://github.com/webpack/webpack/issues/6784#issuecomment-375941431 - globalObject: "typeof self !== 'undefined' ? self : this" - }, - optimization: { - minimize: true - }, - plugins: [new webpack.BannerPlugin({ banner: banner, raw: true })] -} diff --git a/packages/chevrotain/webpack_specs.config.js b/packages/chevrotain/webpack_specs.config.js deleted file mode 100644 index 5d5ce9d9e0..0000000000 --- a/packages/chevrotain/webpack_specs.config.js +++ /dev/null @@ -1,29 +0,0 @@ -const path = require("path") -const webpack = require("webpack") -const jf = require("jsonfile") -const pkg = jf.readFileSync("./package.json") - -const banner = `/*! ${pkg.name} - v${pkg.version} */` - -module.exports = { - mode: "production", - stats: { - colors: true, - modules: true, - reasons: true - }, - entry: "./lib/test/all.js", - output: { - path: path.resolve(__dirname, "./lib/"), - filename: "chevrotainSpecs.js" - }, - optimization: { - minimize: false - }, - plugins: [new webpack.BannerPlugin({ banner: banner, raw: true })], - externals: { - jsdom: "jsdom", - "mock-require": "mock-require", - "require-from-string": "require-from-string" - } -} diff --git a/readme.md b/readme.md index d2c3196632..8f81a42565 100644 --- a/readme.md +++ b/readme.md @@ -32,9 +32,13 @@ as any other pure code without requiring any new tools or processes. - Latest: - `https://unpkg.com/chevrotain/lib/chevrotain.js` - `https://unpkg.com/chevrotain/lib/chevrotain.min.js` + - `https://unpkg.com/chevrotain/lib_esm/chevrotain.mjs` + - `https://unpkg.com/chevrotain/lib_esm/chevrotain.min.mjs` - Explicit version number: - `https://unpkg.com/chevrotain@7.1.2/lib/chevrotain.js` - `https://unpkg.com/chevrotain@7.1.2/lib/chevrotain.min.js` + - `https://unpkg.com/chevrotain@7.1.2/lib_esm/chevrotain.mjs` + - `https://unpkg.com/chevrotain@7.1.2/lib_esm/chevrotain.min.mjs` ## Documentation & Resources diff --git a/yarn.lock b/yarn.lock index e3dc7dfa8f..5499fa7461 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3013,7 +3013,7 @@ assert-plus@1.0.0, assert-plus@^1.0.0: resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525" integrity sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU= -assert@^1.1.1: +assert@^1.1.1, assert@^1.4.1: version "1.5.0" resolved "https://registry.yarnpkg.com/assert/-/assert-1.5.0.tgz#55c109aaf6e0aefdb3dc4b71240c70bf574b18eb" integrity sha512-EDsgawzwoun2CZkCgtxJbv392v4nbk9XDD06zI+kQYoBM/3RBWLlEyJARDOmhAAosBjWACEkKL6S+lIZtcAubA== @@ -5322,6 +5322,11 @@ es6-promisify@^5.0.0: dependencies: es6-promise "^4.0.3" +esbuild@^0.8.53: + version "0.8.53" + resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.8.53.tgz#b408bb0ca1b29dab13d8bbf7d59f59afe6776e86" + integrity sha512-GIaYGdMukH58hu+lf07XWAeESBYFAsz8fXnrylHDCbBXKOSNtFmoYA8PhSeSF+3/qzeJ0VjzV9AkLURo5yfu3g== + escalade@^3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40" @@ -5448,11 +5453,6 @@ eslint@7.18.0: text-table "^0.2.0" v8-compile-cache "^2.0.3" -esm@^3.2.25: - version "3.2.25" - resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10" - integrity sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA== - espree@^7.3.0, espree@^7.3.1: version "7.3.1" resolved "https://registry.yarnpkg.com/espree/-/espree-7.3.1.tgz#f2df330b752c6f55019f8bd89b7660039c1bbbb6" @@ -6095,6 +6095,13 @@ gauge@~2.7.3: strip-ansi "^3.0.1" wide-align "^1.1.0" +gen-esm-wrapper@^1.0.4: + version "1.1.1" + resolved "https://registry.yarnpkg.com/gen-esm-wrapper/-/gen-esm-wrapper-1.1.1.tgz#1e4d508d0229284490f101d22ac33e09435f80fb" + integrity sha512-/ZMuX3itPtnjjWuIArzBi6mDnkGVcsx8guLCeH3WLB+iLeFr8TOKhIrNaYmPuRtQcpeBKrlswCPAY9oLRKIxKw== + dependencies: + is-valid-identifier "^2.0.2" + genfun@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/genfun/-/genfun-5.0.0.tgz#9dd9710a06900a5c4a5bf57aca5da4e52fe76537" @@ -7449,6 +7456,13 @@ is-utf8@^0.2.0, is-utf8@^0.2.1: resolved "https://registry.yarnpkg.com/is-utf8/-/is-utf8-0.2.1.tgz#4b0da1442104d1b336340e80797e865cf39f7d72" integrity sha1-Sw2hRCEE0bM2NA6AeX6GXPOffXI= +is-valid-identifier@^2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/is-valid-identifier/-/is-valid-identifier-2.0.2.tgz#146d9dbf29821b8118580b039d2203aa4bd1da4b" + integrity sha512-mpS5EGqXOwzXtKAg6I44jIAqeBfntFLxpAth1rrKbxtKyI6LPktyDYpHBI+tHlduhhX/SF26mFXmxQu995QVqg== + dependencies: + assert "^1.4.1" + is-windows@^1.0.0, is-windows@^1.0.1, is-windows@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"