From 7f4854e26aa121b656e8a270d843b982e6d3be4d Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 29 Dec 2024 21:28:10 +0300 Subject: [PATCH 1/3] replace genrate_grammer.py by generate-grammar.vsh --- .gitignore | 1 + docs/faq.rst | 9 -------- scripts/generate-grammar.vsh | 40 ++++++++++++++++++++++++++++++++++++ scripts/generate_grammar.py | 26 ----------------------- 4 files changed, 41 insertions(+), 35 deletions(-) create mode 100755 scripts/generate-grammar.vsh delete mode 100644 scripts/generate_grammar.py diff --git a/.gitignore b/.gitignore index d0cf327..221db21 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ vsql-server 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/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()) From 515e3b5728ebd78be3db8a75c06f3d343b3669c1 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 29 Dec 2024 23:21:21 +0300 Subject: [PATCH 2/3] fix Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From f8be52863a3180888fc6c7dc6071fcf0a279b650 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 29 Dec 2024 23:25:06 +0300 Subject: [PATCH 3/3] upd .gitignore --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 221db21..511ce6e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,6 @@ vsql-server *.vsql .vscode .vlang_tmp_build/ -grammar.bnf -vsql/grammar.v scripts/generate-v-client-library-docs scripts/generate-grammar y.output