Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Last active September 13, 2018 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinaisan/bc504d47cdcbda2601901d4571c0853e to your computer and use it in GitHub Desktop.
Save shinaisan/bc504d47cdcbda2601901d4571c0853e to your computer and use it in GitHub Desktop.
Example of pegjs
import peg from 'pegjs';
(() => {
// Event handler
const button = document.getElementById('button-parse');
button.addEventListener('click', (evt) => {
const div = document.getElementById('div-output');
try {
// Syntax
const syntaxInput = document.getElementById('textarea-syntax');
const syntax = syntaxInput.value;
// Input
const sourceInput = document.getElementById('textarea-source');
const source = sourceInput.value;
// Parser
const parser = peg.generate(syntax);
const result = parser.parse(source);
// DOM
div.innerHTML = '<pre>' + JSON.stringify(result, null, 2) + '</pre>';
} catch (err) {
div.innerText = err.message;
}
});
})();
#!/bin/bash
test -d src || mkdir src
cp -v index.js template.html src
{
"name": "pegjs-example-2",
"version": "1.0.0",
"description": "Example of pegjs",
"main": "index.js",
"scripts": {
"webpack": "webpack",
"start:dev": "webpack-serve ./webpack.config.js --no-reload",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "shinaisan@users.noreply.github.com",
"license": "ISC",
"dependencies": {
"pegjs": "^0.10.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"babel-loader": "^8.0.2",
"babel-preset-env": "^1.7.0",
"css-loader": "^1.0.0",
"html-webpack-plugin": "^3.2.0",
"raw-loader": "^0.5.1",
"style-loader": "^0.23.0",
"webpack": "^4.18.0",
"webpack-cli": "^3.1.0",
"webpack-serve": "^2.0.2"
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example of pegjs (PEG.js)</title>
</head>
<body>
<div>
<table style="width: 100%">
<tr><th>Syntax</th><th>Source</th></tr>
<tr>
<td style="width: 50%">
<textarea
id="textarea-syntax" rows="20"
style="width: 100%"></textarea>
</td>
<td style="width: 50%">
<textarea
id="textarea-source" rows="20"
style="width: 100%"></textarea>
</td>
</tr>
</table>
<button id="button-parse">Parse</button>
</div>
<div id="div-output">
</div>
</body>
</html>
const HtmlWebpackPlugin = require('html-webpack-plugin'); // Installed via npm
const webpack = require('webpack'); // To access built-in plugins
const path = require('path');
const paths = {
src: path.join(__dirname, 'src'),
dist: path.join(__dirname, 'dist'),
public: path.join(__dirname, 'public')
};
const HOST = process.env.HOST;
module.exports = {
mode: process.env.WEBPACK_SERVE ? 'development' : 'production',
// Or pass it as a CLI argument: webpack --mode=production
target: 'web', // Default
entry: {
main: path.join(paths.src, 'index.js')
},
output: {
path: path.resolve(paths.dist),
filename: '[name].js',
pathinfo: true
},
module: {
rules: [
{
test: /\.(txt|md|pegjs)$/,
use: 'raw-loader'
},
{ // webpack --module-bind 'css=style-loader!css-loader'
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: true // Enable CSS Modules
}
}
]
},
{
test: /\.(js|jsx)$/,
include: paths.src,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
presets: ["env"],
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: false
},
}
]
},
performance: {
hints: "warning",
maxEntrypointSize: 1000000,
maxAssetSize: 1000000
},
devtool: "source-map",
serve: {
host: HOST,
port: 3000,
content: paths.dist,
logLevel: 'debug'
},
stats: "verbose",
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.join(paths.src, 'template.html')
})
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment