cog icon indicating copy to clipboard operation
cog copied to clipboard

Remove begin/end spec as part of the common prefix

Open kstrafe opened this issue 7 months ago • 2 comments

When we define our own --markers='x-begin x-middle x-end', and our code contains as a first character x, then that character will be considered part of the common prefix and be removed from the code. Here's an example.

// cog-begin
// cog.outl('hello world')
// cog-middle
// cog-end

The common prefix for this code is // cog, which makes the code generator fail because python will receive the code .outl('hello world'), which makes no sense.

This issue is especially noticable if you're only invoking a module, for instance:

// cog-begin
// codegen.generate_some_code()
// cog-middle
// cog-end

Here the python interpeter will receive degen.generate_some_code() and fail.

This patch removes the begin and end specs from the marker line to avoid this issue.

kstrafe avatar May 12 '25 15:05 kstrafe

Help me understand: why not choose markers that are more unique?

nedbat avatar May 13 '25 09:05 nedbat

Help me understand: why not choose markers that are more unique?

For aesthetics.

As a bonus, this will make cog support languages where no marker can satisfy the guarantee of uniqueness to prevent common prefix issues.

I also think it makes intuitive sense to start looking for the common prefix right before the begin/end spec substrings. These will never actually be part of a common prefix because you cannot have two begin specs in a row as cog returns an error.

kstrafe avatar May 13 '25 10:05 kstrafe