From 7043a2307ca42e35d43ae3ec17ef2a628e6524fd Mon Sep 17 00:00:00 2001 From: gechandesu <47027335+gechandesu@users.noreply.github.com> Date: Sun, 29 Dec 2024 23:40:43 +0300 Subject: [PATCH] chore: Replace generate_grammar.py with generate-grammar.vsh (#204) Co-authored-by: ge --- .gitignore | 3 +-- Makefile | 2 +- docs/faq.rst | 9 -------- scripts/generate-grammar.vsh | 40 ++++++++++++++++++++++++++++++++++++ scripts/generate_grammar.py | 26 ----------------------- 5 files changed, 42 insertions(+), 38 deletions(-) create mode 100755 scripts/generate-grammar.vsh delete mode 100644 scripts/generate_grammar.py diff --git a/.gitignore b/.gitignore index d0cf327..511ce6e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,8 @@ vsql-server *.vsql .vscode .vlang_tmp_build/ -grammar.bnf -vsql/grammar.v scripts/generate-v-client-library-docs +scripts/generate-grammar y.output vsql/y.v vsql/y.y diff --git a/Makefile b/Makefile index 29b1059..8266914 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,7 @@ clean-docs: # Grammar vsql/y.y: - python3 scripts/generate_grammar.py + ./scripts/generate-grammar.vsh vsql/y.v: vsql/y.y v run scripts/vyacc.v -o vsql/y.v vsql/y.y diff --git a/docs/faq.rst b/docs/faq.rst index 791f822..7070898 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -44,15 +44,6 @@ available and these will be close enough to use as a reference: 1. `SQL 2016 Foundation Grammar (BNF) `_ 2. `SQL 1999 `_ -Wait! You said this is pure V but I see a Python file? ------------------------------------------------------- - -The python file ``generate-grammar.py`` is used to convert the BNF grammar into -V code that makes up the parser. The result file ``grammar.v`` is committed so -there is no need to use python to compile and otherwise work on vsql unless you -need to change the grammar. However, if you would like to covert this script to -V, I'd be happy to have your help! - Why is there no `NOW()` function? --------------------------------- diff --git a/scripts/generate-grammar.vsh b/scripts/generate-grammar.vsh new file mode 100755 index 0000000..9d00cd7 --- /dev/null +++ b/scripts/generate-grammar.vsh @@ -0,0 +1,40 @@ +#!/usr/bin/env -S v + +path := abs_path('vsql') +output_filename := join_path_single(path, 'y.y') +files := glob(join_path_single(path, '*.y')) or { [] } + +mut top := '' +mut middle := '' +mut bottom := '' + +for file_path in files { + if is_dir(file_path) { + continue + } + content := read_file(file_path) or { + eprintln('error: unable to open file ${file_path}: ${err}') + exit(1) + } + parts := content.split('%%') + if parts.len != 3 { + eprintln('${file_path}: wrong number of parts (${parts.len})') + exit(1) + } + top += parts[0] + middle += parts[1] + bottom += parts[2] +} + +mut output := open_file(output_filename, 'w') or { + eprintln('error: unable to open file to write') + exit(1) +} + +output.writeln(top.trim_space())! +output.writeln('%%')! +output.writeln(middle.trim_space())! +output.writeln('%%')! +output.write_string(bottom.trim_space())! +output.flush() +output.close() diff --git a/scripts/generate_grammar.py b/scripts/generate_grammar.py deleted file mode 100644 index b4a8fab..0000000 --- a/scripts/generate_grammar.py +++ /dev/null @@ -1,26 +0,0 @@ -from os import listdir -from os.path import isfile, join -import sys - -path = 'vsql' -files = [path + '/' + f for f in listdir(path) if isfile(join(path, f)) and f.endswith('.y')] - -top = "" -middle = "" -bottom = "" - -for file_path in sorted(files): - with open(file_path) as f: - parts = f.read().split('%%') - if len(parts) != 3: - sys.exit(path + ': wrong number of parts (' + str(len(parts)) + ')') - top += parts[0] - middle += parts[1] - bottom += parts[2] - -with open(path + '/' + 'y.y', 'w') as f: - f.write(top.strip()) - f.write('\n%%\n') - f.write(middle.strip()) - f.write('\n%%\n') - f.write(bottom.strip())