Closed vssukharev closed 1 week ago
Probably I've found the problem. parse_doc_start
function in src/compiler/lexer.c. To be more precise, it's line 1200, when we set may_have_contract
to false after seeing a *
. I don't know why it's done so try to figure out.
This is not a bug. <* *>
is not parsed as comments. It consists of a header section which is terminated by either on a new row by an @identifier
or *>
. When you write * @require
then this is interpreted as a the header.
From: https://c3-lang.org/language-fundamentals/comments/#doc-comments
Doc Comments
Documentation comments start with < and must be terminated using >. Note that any number of may follow < and any number of stars may preceed >. Any space and in the beginning of each line will be ignored.
Documentation comments start with <*
and must be terminated using *>
. Note that you may not include *
after <*
except to write *>
. Any space at the beginning of each line will be ignored.
I've updated it a bit now @joshring
@lerno, it is quiet confusing to have star it the beginning fed as header - it's so easy to misuse that. I've tested it and it doesn't really conflct with existing code as we merely skip the star before contract
Btw, std library also has several functions with:
<*
* ...
*>
(1) we don't want people to use a leading star, because that actually complicates parsing (I was pretty unhappy about that special code in the lexer before)
(2) The idea was that this new style would look more like a code block, so making it retain more comment-like affordances is probably not a great idea. There were some suggestions that maintained a left-hand side *
vertical line (or similar), but in those cases the intent was to have that mandatory. So we either have mandatory *
in the left hand column or we don't allow them, that's what I prefer.
The only occurrence I could found in the stdlib was a single comment that was accidentally turned into a contract. I've fixed it now (turning it into a comment again). Did you find something else?
You can comment one line in lexer.c and see the errors during tests to see other occurrences of such bug in stdlib. It reported me an error in mem_alloc or something like that. You can my commit in pull request which erases this line.
Regarding inclusion of a star, it's not really a problem to handle this what I've tried to do in pull request, but this will add one more stylistical benefit to the language.
Could you please give some examples of when this star might be required to write?
Ah, the interfaces has contracts indented.
An example when we don't want to trim the *:
<*
This code takes abc and calculates
*(abc + 3), and then puts it into bcd.
@return "the result of the computation"
*>
For now it doesn't cause any problems and is simply skipped. You mean it would be complicated to distinguish and generate contracts and comments in future?
I have a tentative plan to make the doc comments be accessible at compile time, so I don't want cleaning of *
that aren't intended. A similar problem arises with generating docs. It would have to add a heuristic to know if a *
should be stripped or not. Consider:
<*
*abc + foo will be calculated and returned.
*>
fn void* foo(void* abc, int foo) { ... }
vs
<*
* abc + foo will be calculated and returned.
*>
fn int foo(int abc, int foo) { ... }
When parsing the doc, this is done assuming text, so there is no way for anything to work out whether stripping the *
is correct or not. Presumably IDEs would like to be able to show these docs on hover etc.
Also having an optional *
prefix is reasonable when everything is comment based, less so when it's not.
As for the parsing, mostly it prevents a later extension like this:
<*
Foo
@require a > 0
"a must be at least 1"
*>
Which would eliminate some state from the lexer.
Oh, okay. Then it makes more sense to me, thanks!
Let we have such macro:
It is expected that when we try to call it as
add("1", 2)
, the compiler will error with message:Instead it goes into:
The funniest thing is that when I change mnemonic of a function to:
compiler falls into first message! I have deleted star before
@require
. It may be funny with macros but devastating with run-time checks in functions. So if I do something like this:Program will successfully run without any problem, but should I erase a star, it works as expected: