Regex
I did a quick test with making a simpler API for EReg, and it resulted in this simple class, which takes an EReg as a constructor parameter:
class Regex {
var exp : EReg;
var str : String;
public function new( r : EReg ) {
exp = r;
}
public function match( s : String ) {
str = s;
return this;
}
public function matchedLeft() {
return exp.matchedLeft();
}
public function matchedRight() {
return exp.matchedRight();
}
public function hasNext() {
return exp.match( str );
}
public function next() {
var match = new Array<String>();
var i = 0, m = exp.matched( i );
while( m != null ) {
match[i] = m;
m = exp.matched( ++i );
}
str = exp.matchedRight();
return match;
}
}
It might be used like this:
var r = new Regex( ~/<%(.*?)%>/sm );
for( m in r.match( str ) )
trace( "Inside tags: " + m[1] + ", Entire match: " + m[0] );
This is just a quick test which seems to work, unfortunately neko seems to like to throw errors around so most of the next() method would have to be wrapped in try{}catch(){} to work in neko I think. And off course looping through the matches like that is not an optimal solution, it should rather be built into EReg and return the targets own implementation arrays if possible (like the match data arrays in JS and AS).
If you think something is missing - fork it and fix it! http://gist.github.com/127421
EReg
First obstacle, I couldn’t figure out how to loop through the results of a regular expression by looking on the haXe website, or googling for it, so I thought I’d post it here (found it in the must-have haXe & neko book):
var r = ~/<%(.*?)%>/sm;
var s = file;
while( r.match( s ) ) {
trace( r.matched( 1 ) );
s = r.matchedRight();
}
It’s an example from the ehx project. It extracts all parts within tags. The plan is to compile those parts using hscript.
I can’t help to think that EReg could’ve extended Iterable or something like that so I could do something like this instead:
var r = ~/<%(.*?)%>/sm;
for( m in r.match( file ) )
trace( m.group( 1 ) );
Which, with the new “using” feature in haXe 2.04 could be (assuming there’s some kind of ERegTools.match( str , ereg );):
for( m in file.match( ~/<%(.*?)%>/sm ) )
trace( m.group( 1 ) );
But I guess that since this would have to return some kind of Match object every time it would probably be slower than keeping the references inside the EReg instance. Or it might be an issue with some targets. Maybe it’s something I might add to the “current projects” list.
Why another blog?
The reason I’m writing this blog is to write about the things I learn while developing for software and web. Currently I’m learning haXe as it’s a very interesting concept (developing for multiple targets) and not a very big step from developing actionscript (among other things) for the last 5 years.
Current projects is:
Travis (the traverser) An experiment of a jquery-like XML parser. The idea is to have an XML parser that doesn’t really care about the validity of the XML, but instead just based on an E4X-like selector tries to find the nodes and attributes from it. Status right now is I’m trying to figure out the syntax and API I’m after and writing some unit tests for those. Next step will be to figure out how to actually parse the XML.
ehx After using Ruby for a couple of years I’ve grown to like the template system called erb, so this is mostly just an experiment to see if it’s even the slightest chance to make a similar template system for haXe that isn’t too slow. I’m gonna use hscript as a basis I think.
hxformat It’s an already established library on haxelib but I’m adding some more formats, those done so far is PNG (read/write) and BMP (read/write) image formats. Next one I’ll be chasing will most likely be JPEG. It’s a great way to learn the haXe syntax, how to read file format specifications and the difference of writing code in a cross-target-friendly way.
macports I’d really like to have haxe and neko installed using my favorite software packaging system. So to start I’ve had to get neko to compile on Mac OS X. And to understand how it currently installs I’ve rewritten the haxe OCAML installer to a bash-script. Next step is to figure out how those Portfiles are written.
sitting in a pleasant sofa is fun.
combine for double the fun.