mirror of
https://activitypub.software/TransFem-org/sfm-js
synced 2024-11-25 07:25:13 +00:00
revert way to build peg
This commit is contained in:
parent
7cf838abda
commit
6eb76f1e80
4 changed files with 10 additions and 71 deletions
|
@ -7,8 +7,8 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run tsc && npm run peg && npm run webpack",
|
"build": "npm run tsc && npm run peg && npm run webpack",
|
||||||
"build-dev": "npm run tsc && npm run peg-dev && npm run webpack-dev",
|
"build-dev": "npm run tsc && npm run peg-dev && npm run webpack-dev",
|
||||||
"peg": "node ./built/build",
|
"peg": "mkdirp ./built/parser && pegjs -o built/parser/core-parser.js --allowed-start-rules root,plain src/parser/core-parser.pegjs",
|
||||||
"peg-dev": "node ./built/build trace",
|
"peg-dev": "mkdirp ./built/parser && pegjs -o built/parser/core-parser.js --allowed-start-rules root,plain --trace src/parser/core-parser.pegjs",
|
||||||
"tsc": "tsc",
|
"tsc": "tsc",
|
||||||
"webpack": "webpack --mode=production",
|
"webpack": "webpack --mode=production",
|
||||||
"webpack-dev": "webpack --mode=development",
|
"webpack-dev": "webpack --mode=development",
|
||||||
|
|
25
src/build.ts
25
src/build.ts
|
@ -1,25 +0,0 @@
|
||||||
import { promises as fs } from 'fs';
|
|
||||||
import path from 'path';
|
|
||||||
import { generateCode } from './misc/generate-peg';
|
|
||||||
|
|
||||||
async function entryPoint() {
|
|
||||||
// get arguments
|
|
||||||
let trace = false;
|
|
||||||
if (process.argv.some(i => i == 'trace')) {
|
|
||||||
trace = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const srcPath = path.join(__dirname, '../src/parser/core-parser.pegjs');
|
|
||||||
const destPath = path.join(__dirname, '../built/parser/core-parser.js');
|
|
||||||
|
|
||||||
// generate a code from PEG
|
|
||||||
const generatedCode = await generateCode(srcPath, trace);
|
|
||||||
|
|
||||||
// write the generated code
|
|
||||||
await fs.writeFile(destPath, generatedCode, { encoding: 'utf8' });
|
|
||||||
}
|
|
||||||
entryPoint()
|
|
||||||
.catch(err => {
|
|
||||||
console.log(err);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
|
@ -1,32 +0,0 @@
|
||||||
import { promises as fs } from 'fs';
|
|
||||||
import peg from 'pegjs';
|
|
||||||
|
|
||||||
export async function generateParser(srcPath: string, trace?: boolean): Promise<peg.Parser>
|
|
||||||
{
|
|
||||||
// read the parser source
|
|
||||||
const source = await fs.readFile(srcPath, 'utf8');
|
|
||||||
|
|
||||||
// generate a parser code
|
|
||||||
const generatedCode = peg.generate(source, {
|
|
||||||
allowedStartRules: ['root', 'all', 'inline'],
|
|
||||||
trace: trace
|
|
||||||
});
|
|
||||||
|
|
||||||
return generatedCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function generateCode(srcPath: string, trace?: boolean): Promise<string>
|
|
||||||
{
|
|
||||||
// read the parser source
|
|
||||||
const source = await fs.readFile(srcPath, 'utf8');
|
|
||||||
|
|
||||||
// generate a parser code
|
|
||||||
const generatedCode = peg.generate(source, {
|
|
||||||
allowedStartRules: ['root', 'all', 'inline'],
|
|
||||||
output: 'source',
|
|
||||||
format: 'commonjs',
|
|
||||||
trace: trace
|
|
||||||
});
|
|
||||||
|
|
||||||
return generatedCode;
|
|
||||||
}
|
|
|
@ -4,23 +4,17 @@
|
||||||
mergeText
|
mergeText
|
||||||
} = require('./parser-utils');
|
} = require('./parser-utils');
|
||||||
|
|
||||||
function applyParser(input, rule) {
|
function applyParser(input, startRule) {
|
||||||
let parseFunc = peg$parse;
|
let parseFunc = peg$parse;
|
||||||
return parseFunc(input, rule ? { startRule : rule } : { });
|
return parseFunc(input, startRule ? { startRule } : { });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
root
|
root
|
||||||
= ts:all*
|
= ts:(block / inline)* { return mergeText(ts); }
|
||||||
{
|
|
||||||
return mergeText(ts);
|
|
||||||
}
|
|
||||||
|
|
||||||
all
|
plain
|
||||||
= block / inline
|
= ts:(text /*/ emoji*/)* { return mergeText(ts); }
|
||||||
|
|
||||||
// plain
|
|
||||||
// =
|
|
||||||
|
|
||||||
block
|
block
|
||||||
= title
|
= title
|
||||||
|
@ -30,8 +24,10 @@ block
|
||||||
|
|
||||||
inline
|
inline
|
||||||
= big
|
= big
|
||||||
/ c:. { return createTree('text', { text: c }); }
|
/ text
|
||||||
|
|
||||||
|
text
|
||||||
|
= c:. { return createTree('text', { text: c }); }
|
||||||
|
|
||||||
// block: title
|
// block: title
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue