diff --git a/package.json b/package.json index f0bd6a8..de5d969 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "scripts": { "build": "npm run tsc && npm run peg && npm run webpack", "build-dev": "npm run tsc && npm run peg-dev && npm run webpack-dev", - "peg": "node ./built/build", - "peg-dev": "node ./built/build trace", + "peg": "mkdirp ./built/parser && pegjs -o built/parser/core-parser.js --allowed-start-rules root,plain src/parser/core-parser.pegjs", + "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", "webpack": "webpack --mode=production", "webpack-dev": "webpack --mode=development", diff --git a/src/build.ts b/src/build.ts deleted file mode 100644 index 0986692..0000000 --- a/src/build.ts +++ /dev/null @@ -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); -}); diff --git a/src/misc/generate-peg.ts b/src/misc/generate-peg.ts deleted file mode 100644 index 0eb77c4..0000000 --- a/src/misc/generate-peg.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { promises as fs } from 'fs'; -import peg from 'pegjs'; - -export async function generateParser(srcPath: string, trace?: boolean): Promise -{ - // 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 -{ - // 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; -} diff --git a/src/parser/core-parser.pegjs b/src/parser/core-parser.pegjs index fb84ae5..f1779a8 100644 --- a/src/parser/core-parser.pegjs +++ b/src/parser/core-parser.pegjs @@ -4,23 +4,17 @@ mergeText } = require('./parser-utils'); - function applyParser(input, rule) { + function applyParser(input, startRule) { let parseFunc = peg$parse; - return parseFunc(input, rule ? { startRule : rule } : { }); + return parseFunc(input, startRule ? { startRule } : { }); } } root - = ts:all* -{ - return mergeText(ts); -} + = ts:(block / inline)* { return mergeText(ts); } -all - = block / inline - -// plain -// = +plain + = ts:(text /*/ emoji*/)* { return mergeText(ts); } block = title @@ -30,8 +24,10 @@ block inline = big - / c:. { return createTree('text', { text: c }); } + / text +text + = c:. { return createTree('text', { text: c }); } // block: title