Nested Classes — thoughts and workarounds

22 views
Skip to first unread message

Igbanam Ogbuluijah

unread,
Mar 21, 2024, 9:03:24 PMMar 21
to vim...@googlegroups.com
Hello community,

I've been playing with some idea which has brought me to needing Nested Classes in Vim. I am curious what you think about this? Would this be too much for Vimscript? If not, what current workarounds would you suggest?

Context: I'm taking a stab at parsing protobuf definitions into Vim 9 classes. Nested messages in the protobuf demands nested structs/classes. 

Constraints:
  • (strong) All code-gen must be in vim9script; else how would the language gain popularity?
  • (weak) All definitions should be in one file
Current workarounds
  1. Regress into old VimL and use functions as classes. Then define internal "structures" as internal functions.
  2. Create a directory where each file is a top-level message class
  3. (current winner) Flatten the nesting, but only export top-level messages
What do you think? How can I approach this better?

Best,
Igbanam

Lifepillar

unread,
Mar 23, 2024, 9:41:33 AMMar 23
to vim...@googlegroups.com
On 2024-03-22, Igbanam Ogbuluijah <xigb...@gmail.com> wrote:
> Hello community,
>
> I've been playing with some idea which has brought me to needing Nested
> Classes in Vim. I am curious what you think about this? Would this be too
> much for Vimscript? If not, what current workarounds would you suggest?

I don't think Vim 9 script needs or would gain anything for such
extensions.

> Context: I'm taking a stab at parsing protobuf definitions into Vim 9
> classes. Nested messages in the protobuf demands
><https://arc.net/l/quote/qfcssecm> nested structs/classes.

Something like:

message SearchResponse {
message Result {
string url = 1;
string title = 2;
repeated string snippets = 3;
}
repeated Result results = 1;
}

becomes two classes/structs `SearchResponse` and `SearchResponse_Result`
in the target languages I've looked at. In Vim, you could do the same:

interface Message
endinterface

class SearchResponseResult implements Message
var url: string
var title: string
var snippets: list<string>
endclass

class SearchResponse implements Message
var results: list<SearchResponseResult>
endclass

That seems the most straightforward approach to me.

If you don't care about typing (but wouldn't that defeat the purpose of
protobuf?), you may simply use dictionaries, which can be arbitrarily
nested.

If your problem is parsing, libparser might help:

https://github.com/lifepillar/vim-devel

Protobuf's grammar seems simple enough. You might even go fancy and
generate parsers from messages, which would be able to parse the
corresponding messages.

I don't see any reason to keep using Vim legacy script for new Vim
scripts.

Hope this helps,
Life.

Igbanam Ogbuluijah

unread,
Mar 23, 2024, 11:42:47 AMMar 23
to vim...@googlegroups.com
Ooph! Thank you!!

I wish I knew about your libparser before I started down the regex jungle trying to parse the protobuf structure. I will finish this as a v1 with caveman regex then port to libparser for a v2.

SearchResponseResult is a good idea as well.

Thank you.


- Igbanam

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/utmm5o%2412c1%241%40ciao.gmane.io.
Reply all
Reply to author
Forward
0 new messages