View mode: basic / threaded / horizontal-split · Log in · Help
August 03, 2012
NotNull Parser
In context with my post here:
http://forum.dlang.org/thread/gajrorlwnrriljxnxfmt@forum.dlang.org#post-egvyqwkcqjglhrvujkar:40forum.dlang.org

I wrote the last days a NotNull Parser.
What is this?
The NotNull Parser generate for parameter statements with a
following "?" valid preconditions.

Example:
[code]
void foo(Object? o) {
	// do something with o
}
[/code]
		
will convert into

[code]
void foo(Object o, string filename = __FILE__, uint line =
__LINE__) in {
	if (o is null) throw new Exception("Object is null.", filename,
line);
} body {
	// do something with o
}
[/code]
		
Thereby you can write:

  - Object? o
or
  - Object ?o
or
  - Object ? o

all of these will be found and converted.
As you see the main focus lies on reference types and pointer.

It also works with Templates like
  - void foo(T)(T? obj) {
  - void foo(T)(vec2!(T)? obj) {
  - void foo(T)(vec2!T? obj) {
  - void foo()(vec2f? obj) {
		
If you have already the additional parameters __FILE__ and
__LINE__ they don't append double and your names for these
parameters will pass to the Exception.
If you have only one of them, an Exception will thrown. So if you
like to declare then on your own, write both. Otherwise use line
and filename.

Optional parameter (#t):
If you pass the optional parameter #t to the program, a second
file will be created (which is named "org_FILENAME.d") which
contains the original invalid D code.
So you have both and can give the valid D code to other
programmers if you need.

If you use "#t" you will see, that a number is subtract from
"line".
That's because the generated valid D code has more lines as the
original code, because of the insertions of precondition and
assert statements. These new lines are subtracts from the
__LINE__ parameter to validate the line number. Because of that
you can look into the shorter, readable but invalid D code and
find the line where the error occures. If you don't pass #t no
second file is created and you get the error line in the new
generated code.

NotNullParser Code: http://dpaste.dzfl.pl/aee2ec4f

I would be glad for suggestions and cristicm, maybe one of you
will try it out and found bugs which i can fix. :)
I will use this little parser in future projects with my friends.
Therefore i wrote a little precompiler (with std.process) which
takes all compiler flags and files, converts the files with my
not_null_parser and send then the request to the dmd compiler.
After that and if "#t" is passes, the valid D code files will be
deleted and the original invalid files will be renamed. Because
of that the user don't realize what's going on and work only with
the invalid but more readable and shorter code.

P.S.:
If you compile the code in debug mode and try to convert some
code, the converted code will shown in the console and no files
will be changed or created, but you see the number of changed
lines and the init- and replacement time.

And sorry for my bad english.
August 03, 2012
Re: NotNull Parser
Yay Preprocessors <3
August 03, 2012
Re: NotNull Parser
On Friday, 3 August 2012 at 10:42:48 UTC, David wrote:
> Yay Preprocessors <3

irony? :P
August 03, 2012
Re: NotNull Parser
On 08/03/2012 12:38 PM, Namespace wrote:
> In context with my post here:
> http://forum.dlang.org/thread/gajrorlwnrriljxnxfmt@forum.dlang.org#post-egvyqwkcqjglhrvujkar:40forum.dlang.org
>
>
> I wrote the last days a NotNull Parser.
> What is this?
> The NotNull Parser generate for parameter statements with a
> following "?" valid preconditions.
>
> Example:
> [code]
> void foo(Object? o) {
>     // do something with o
> }
> [/code]
>
> will convert into

The notation 'T?' is normally used for a nullable type.
August 03, 2012
Re: NotNull Parser
On Friday, 3 August 2012 at 10:53:02 UTC, Timon Gehr wrote:
> On 08/03/2012 12:38 PM, Namespace wrote:
>> In context with my post here:
>> http://forum.dlang.org/thread/gajrorlwnrriljxnxfmt@forum.dlang.org#post-egvyqwkcqjglhrvujkar:40forum.dlang.org
>>
>>
>> I wrote the last days a NotNull Parser.
>> What is this?
>> The NotNull Parser generate for parameter statements with a
>> following "?" valid preconditions.
>>
>> Example:
>> [code]
>> void foo(Object? o) {
>>    // do something with o
>> }
>> [/code]
>>
>> will convert into
>
> The notation 'T?' is normally used for a nullable type.

I can change the operator easily. Any better suggestions?
August 03, 2012
Re: NotNull Parser
Timon Gehr:

> The notation 'T?' is normally used for a nullable type.

Right. that's why I have suggested a trailing "@":
http://d.puremagic.com/issues/show_bug.cgi?id=4571

------------------------

Namespace:

> void foo(Object? o) {
> 	// do something with o
> }
> [/code]
> 		
> will convert into
>
> [code]
> void foo(Object o, string filename = __FILE__, uint line =
> __LINE__) in {
> 	if (o is null) throw new Exception("Object is null.", filename,
> line);
> } body {
> 	// do something with o
> }
> [/code]

Object@ is a type, so if you write:

void foo(Object@ o) {...

It means the compiler statically refuses you to give a nullable 
type to foo. So inside foo there is no need for a run time test.

Good work :-)

Bye,
bearophile
August 03, 2012
Re: NotNull Parser
> Object@ is a type, so if you write:
>
> void foo(Object@ o) {...
>
> It means the compiler statically refuses you to give a nullable 
> type to foo. So inside foo there is no need for a run time test.
>
> Good work :-)
>
> Bye,
> bearophile

Change line 14 from
OpToken = r"\?",
into
OpToken = r"\@",

And it works as you like. ;) I will change it too.
August 03, 2012
Re: NotNull Parser
No further suggestions or criticism?
August 06, 2012
Re: NotNull Parser
I'd like to share my files with you.

Here is the not_null_parser: http://dpaste.dzfl.pl/c522bd9f
re_name: http://dpaste.dzfl.pl/96928cd6
And the precompiler (which uses the above two): 
http://dpaste.dzfl.pl/59de30f4

If you want to use it, compile all three and move them into 
dmd(2) > windows > bin.
Then if you like to compile a file with the precompiler, write 
"precompiler" instead of dmd. Nothing more.
If you only like to _convert_ (not compile) files from invalid 
code with NotNull tokens (@) into valid D code with precondition 
and assertions write
"not_null_parser YOUR_FILE.d [#t]". The optional parameter "#t" 
only if you like to have a copy with NotNull tokens (see my first 
post for more informations).
You must have a copy if you want to use "re_name".

I also wrote a short website with a online converter (in php) to 
offer such conversion online without download anything, but i'm a 
windows user and my webspace use a linux server, so i cannot 
offer anything. :/
Top | Discussion index | About this forum | D home