NotNull Parser

Namespace rswhite4 at googlemail.com
Fri Aug 3 03:38:49 PDT 2012


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.


More information about the Digitalmars-d-learn mailing list