www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is there any preprocessor scripting language?

reply "MarisaLovesUsAll" <marisalovesusall gmail.com> writes:
I just want to write something like this:

#ololo script
     foreach(child; findAllChildrens())
     {
         child.insert(
             static void doABarrelRoll() { }
         );
     }
#end

Templates/mixins is not enough for me... maybe because of 
hands.dll error :)
Aug 22 2014
parent reply "Kagamin" <spam here.lot> writes:
You can try D parsing libraries: 
http://forum.dlang.org/thread/jqwvudiwgiuprqcuaaoy forum.dlang.org 
they're believed to enable writing refactoring scripts.
Aug 22 2014
next sibling parent "Kagamin" <spam here.lot> writes:
Example:

import std.lexer;
import std.d.lexer;
import std.array;
import std.stdio;

void main(string[] args)
{
	File input = File(args[1]);
	File output = args.length > 2 ? File(args[2]) : stdout;
	ubyte[] inputBytes = uninitializedArray!(ubyte[])(input.size);
	input.rawRead(inputBytes);
	StringCache cache = StringCache(StringCache.defaultBucketCount);
	LexerConfig config;
	config.fileName = args[1];
	config.stringBehavior = StringBehavior.source;
	auto tokens = byToken(inputBytes, config, &cache).array;
	foreach (i; 0 .. tokens.length)
	{
		switch (tokens[i].type)
		{
		case tok!"catch":
			if (i + 1 < tokens.length && tokens[i + 1].type != tok!"(")
			{
				output.write("catch (Throwable)");
				break;
			}
			else
				goto default;
		default:
			output.write(tokens[i].text is null
				? str(tokens[i].type)
				: tokens[i].text);
			break;
		}
	}
}
Aug 22 2014
prev sibling parent "MarisaLovesUsAll" <marisalovesusall gmail.com> writes:
On Friday, 22 August 2014 at 13:33:12 UTC, Kagamin wrote:
 You can try D parsing libraries: 
 http://forum.dlang.org/thread/jqwvudiwgiuprqcuaaoy forum.dlang.org 
 they're believed to enable writing refactoring scripts.
Thanks a lot! It will be useful.
Aug 22 2014