From 303ba2827a85fa36e6d82e434a148355eecde9b2 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sat, 1 Feb 2020 04:29:30 +0900 Subject: [PATCH] :v: --- .gitignore | 1 + README.md | 24 ++++++++++++- package.json | 21 ++++++----- src/client/mainEntry.ts | 13 +++++++ src/index.ts | 8 +++++ src/parser.ts | 1 - src/{ => parser}/coreParser.pegjs | 58 ++++++++++++++++++------------- src/parser/pegParser.d.ts | 6 ++++ tsconfig.client.json | 30 ++++++++++++++++ tsconfig.json | 5 +-- webpack.config.js | 26 ++++++++++++++ 11 files changed, 157 insertions(+), 36 deletions(-) create mode 100644 src/client/mainEntry.ts create mode 100644 src/index.ts delete mode 100644 src/parser.ts rename src/{ => parser}/coreParser.pegjs (68%) create mode 100644 src/parser/pegParser.d.ts create mode 100644 tsconfig.client.json create mode 100644 webpack.config.js diff --git a/.gitignore b/.gitignore index 2badb49..dfa7854 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules built package-lock.json +temp diff --git a/README.md b/README.md index 9e017e2..2b29d66 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,24 @@ # mfm-parser-pegjs -A trial of creating a MFM parser with peg.js +## Description +A trial of creating a MFM parser with peg.js + +## Installation +``` +npm i +``` + +## Build +For production: +``` +npm run build +``` + +For development: +``` +npm run build-dev +``` + +## Start +``` +npm start +``` diff --git a/package.json b/package.json index 05e9d36..5b11a7f 100644 --- a/package.json +++ b/package.json @@ -2,24 +2,29 @@ "private": true, "name": "mfm-parser-pegjs", "version": "0.1.0", + "main": "./built/index.js", "scripts": { - "build": "npm run peg && npm run tsc", - "build-debug": "npm run peg-debug && npm run tsc", - "peg": "mkdirp ./built && pegjs -o built/coreParser.js src/coreParser.pegjs", - "peg-debug": "mkdirp ./built && pegjs -o built/coreParser.js --trace src/coreParser.pegjs", - "tsc": "tsc" + "build": "npm run peg && npm run tsc && npm run webpack", + "build-dev": "npm run peg-dev && npm run tsc && npm run webpack-dev", + "peg": "mkdirp ./built/parser && pegjs -o built/parser/coreParser.js src/parser/coreParser.pegjs", + "peg-dev": "mkdirp ./built/parser && pegjs -o built/parser/coreParser.js --trace src/parser/coreParser.pegjs", + "tsc": "tsc", + "webpack": "webpack --mode=production", + "webpack-dev": "webpack --mode=development", + "start": "node ." }, "repository": { "type": "git", "url": "git+https://github.com/marihachi/mfm-parser-pegjs.git" }, "author": "Marihachi", - "dependencies": { - }, "devDependencies": { "@types/node": "^12.0.4", "mkdirp": "^0.5.1", "pegjs": "^0.10.0", - "typescript": "3.7.x" + "ts-loader": "6.2.x", + "typescript": "3.7.x", + "webpack": "4.40.x", + "webpack-cli": "3.3.x" } } diff --git a/src/client/mainEntry.ts b/src/client/mainEntry.ts new file mode 100644 index 0000000..1fb7479 --- /dev/null +++ b/src/client/mainEntry.ts @@ -0,0 +1,13 @@ +import { PegParser } from '../parser/pegParser'; + +async function entryPoint() { + const coreParser: PegParser = require('../../built/parser/coreParser.js'); + + const input = '[hoge]'; + console.log('parsing input:', input); + const result = coreParser.parse(input); + console.log('parsing result:'); + console.log(result); +} +entryPoint() +.catch(err => console.log(err)); diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..a2c8589 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,8 @@ +import { PegParser } from './parser/pegParser'; + +const coreParser: PegParser = require('./parser/coreParser'); +const input = '[hoge]'; +console.log('parsing input:', input); +const result = coreParser.parse(input); +console.log('parsing result:'); +console.log(result); diff --git a/src/parser.ts b/src/parser.ts deleted file mode 100644 index 2dda3ca..0000000 --- a/src/parser.ts +++ /dev/null @@ -1 +0,0 @@ -const coreParser = require('./coreParser.js'); diff --git a/src/coreParser.pegjs b/src/parser/coreParser.pegjs similarity index 68% rename from src/coreParser.pegjs rename to src/parser/coreParser.pegjs index 77fefbb..5f48929 100644 --- a/src/coreParser.pegjs +++ b/src/parser/coreParser.pegjs @@ -54,35 +54,45 @@ blockCode // parts -plain - = emoji - / text +// plain +// = emoji +// / text + +// block +// = title +// / quote +// / search +// / blockCode +// / mathBlock +// / center + +// inline +// = big +// / bold +// / small +// / italic +// / strike +// / motion +// / spin +// / jump +// / flip +// / inlineCode +// / mathInline +// / mention +// / hashtag +// / url +// / link +// / plain + +// root +// = block +// / inline block = title - / quote - / search / blockCode - / mathBlock - / center -inline - = big - / bold - / small - / italic - / strike - / motion - / spin - / jump - / flip - / inlineCode - / mathInline - / mention - / hashtag - / url - / link - / plain +inline = "inline" root = block diff --git a/src/parser/pegParser.d.ts b/src/parser/pegParser.d.ts new file mode 100644 index 0000000..8e316c8 --- /dev/null +++ b/src/parser/pegParser.d.ts @@ -0,0 +1,6 @@ +export interface PegParserOptions { + startRule?: string; +} +export interface PegParser { + parse(input: string, options?: PegParserOptions): any; +} diff --git a/tsconfig.client.json b/tsconfig.client.json new file mode 100644 index 0000000..ebfaf92 --- /dev/null +++ b/tsconfig.client.json @@ -0,0 +1,30 @@ +{ + "compilerOptions": { + /* Basic Options */ + "target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ + "module": "ESNext", + "moduleResolution": "node", + "outDir": "./built/client/", /* Redirect output structure to the directory. */ + "rootDir": "./src/client/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + "removeComments": false, + + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + "strictFunctionTypes": true, /* Enable strict checking of function types. */ + + /* Additional Checks */ + "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + + /* Module Resolution Options */ + "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + + "experimentalDecorators": true, + }, + "include": [ + "src/client/**/*", + ], + "exclude": [ + "node_modules", + "test/**/*.ts", + ] +} diff --git a/tsconfig.json b/tsconfig.json index 6f4f710..03be262 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ /* Basic Options */ "target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ - "declaration": true, /* Generates corresponding '.d.ts' file. */ + //"declaration": true, /* Generates corresponding '.d.ts' file. */ "outDir": "./built/", /* Redirect output structure to the directory. */ "rootDir": "./src/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ "removeComments": true, /* Do not emit comments to output. */ @@ -23,6 +23,7 @@ ], "exclude": [ "node_modules", - "test/**/*.ts", + "src/client/**/*", + "test/**/*", ] } diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..a2d36e3 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,26 @@ +module.exports = { + entry: './src/client/mainEntry.ts', + output: { + path: `${__dirname}/built/client`, + publicPath: '/', // base path of URL + filename: 'bundle.js', + chunkFilename: "bundle.[name].js", + }, + module: { + rules: [ + { + test: /\.ts$/, + exclude: /node_modules/, + use: [ + { + loader: 'ts-loader', + options: { configFile: 'tsconfig.client.json' }, + }, + ], + }, + ] + }, + resolve: { + extensions: ['.ts'], + }, +};