From 554745f159666ca2f38194abc29cf1d1531acd49 Mon Sep 17 00:00:00 2001 From: Starcea Date: Wed, 7 Jun 2023 23:58:47 +0900 Subject: [PATCH] feat: parsers with stdin --- .gitignore | 2 -- package.json | 4 +--- scripts/paramParser.ts | 49 --------------------------------------- scripts/parse_param.py | 44 +++++++++++++++++++++++++++++++++++ scripts/parse_response.py | 30 ++++++++++++++++++++++++ scripts/responseParser.ts | 33 -------------------------- 6 files changed, 75 insertions(+), 87 deletions(-) delete mode 100644 scripts/paramParser.ts create mode 100644 scripts/parse_param.py create mode 100644 scripts/parse_response.py delete mode 100644 scripts/responseParser.ts diff --git a/.gitignore b/.gitignore index 7ce5e24..82ee25a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ node_modules dist .idea -params.txt -responses.txt diff --git a/package.json b/package.json index c9c8404..345bb1c 100644 --- a/package.json +++ b/package.json @@ -45,9 +45,7 @@ "test": "jest", "build": "rimraf dist && tsc", "release": "semantic-release", - "prepack": "pnpm build", - "params": "ts-node scripts/paramParser.ts", - "responses": "ts-node scripts/responseParser.ts" + "prepack": "pnpm build" }, "packageManager": "pnpm@8.5.1", "release": { diff --git a/scripts/paramParser.ts b/scripts/paramParser.ts deleted file mode 100644 index 3ad7fff..0000000 --- a/scripts/paramParser.ts +++ /dev/null @@ -1,49 +0,0 @@ -import fs from 'fs' - -const str = fs.readFileSync('./params.txt', 'utf-8') - -const items = str.split('\n').map((x) => - x - .trim() - .split(' ') - .filter((x) => x) - .join(' ') - .replace(/\t/g, '') -) - -console.log(items) - -const typeRegex = /^(.*) (.*)\((필수|선택)\) (.*)$/ - -const result: string[] = [] - -const typeNameFromString = (name: string) => { - switch (name) { - case 'STRING': - return 'string' - case 'INTEGER': - return 'number' - default: - throw new Error('not implemented') - } -} - -for (const data of items) { - const matches = typeRegex.exec(data) - - if (!matches) { - console.log('not matched:', data) - continue - } - - const [, name, typeName, required, description] = matches - - result.push( - `/** ${description} */`, - `readonly ${name}${required === '필수' ? '' : '?'}: ${typeNameFromString( - typeName - )}` - ) -} - -console.log(result.join('\n')) diff --git a/scripts/parse_param.py b/scripts/parse_param.py new file mode 100644 index 0000000..5dc76b1 --- /dev/null +++ b/scripts/parse_param.py @@ -0,0 +1,44 @@ +from re import match, sub + +raw = "" +while i := input(): + raw += i + raw += "\n" + +items = [ + sub(r" +", " ", " ".join(x).replace("\t", "")) + for x in (x.strip().split(" ") for x in raw.split("\n") if x.strip()) +] + +print(items) + +regex = r"^(.*) (.*)\((필수|선택)\) (.*)$" +result = [] + + +def string_to_type(name: str) -> str: + if name == "STRING": + return "string" + elif name == "INTEGER": + return "number" + else: + raise NotImplementedError("not implemented") + + +for data in items: + matches = match(regex, data) + + if not matches: + print("not matched:", data) + continue + + name, typeName, required, description = matches.groups() + + result.extend( + [ + f"/** {description} */", + f'readonly {name}{"" if required == "필수" else "?"}: {string_to_type(typeName)}', + ] + ) + +print("\n".join(result)) diff --git a/scripts/parse_response.py b/scripts/parse_response.py new file mode 100644 index 0000000..6de2052 --- /dev/null +++ b/scripts/parse_response.py @@ -0,0 +1,30 @@ +from re import match, sub + +raw = "" +while i := input(): + raw += i + raw += "\n" + +items = [ + sub(r" +", " ", " ".join(x).replace("\t", "")) + for x in (x.strip().split(" ") for x in raw.split("\n") if x.strip()) +] + +print(items) + +regex = r"^(\d*) (.*) (.*)$" +result = [] + +for data in items: + matches = match(regex, data) + + if not matches: + print("not matched:", data) + continue + + name, description = matches.groups()[1:3] + + result.append(f"/** {description} */") + result.append(f"readonly {name}: str") + +print("\n".join(result)) diff --git a/scripts/responseParser.ts b/scripts/responseParser.ts deleted file mode 100644 index 9737ba5..0000000 --- a/scripts/responseParser.ts +++ /dev/null @@ -1,33 +0,0 @@ -import fs from 'fs' - -const str = fs.readFileSync('./responses.txt', 'utf-8') - -const items = str.split('\n').map((x) => - x - .trim() - .split(' ') - .filter((x) => x) - .join(' ') - .replace(/\t/g, '') -) - -console.log(items) - -const regex = /^(\d*) (.*) (.*)$/ - -const result: string[] = [] - -for (const data of items) { - const matches = regex.exec(data) - - if (!matches) { - console.log('not matched:', data) - continue - } - - const [, name, description] = matches.slice(1) - - result.push(`/** ${description} */`, `readonly ${name}: string`) -} - -console.log(result.join('\n'))