/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  example39.spl: Some more advanced regex tests
 */

// SKIP_IF_NO_REGEX

function test1()
{
	var x = "foolish bigfoot";

	var r = x =~ /(?P<word>(?P<firstchar>\S)\S*)\s*/APLg;

	foreach i (r) {
		var $$;
		r[i].word =~ s/foo(.*)/bar$1/;
		debug "Match #$i: [${r[i].firstchar}] ${r[i].word} ($0)";
	}

	var text1 = "Ever seen a ${r[0].word} $1?";
	var text2 = text1 =~ s/seen/beeing eaten by/R;

	debug text1;
	debug text2;
}

function test2()
{
	var x ="foobar";

	x =~ /(?P<foo>f.+)/;

	debug "$1 == $<1> == $<foo>";

	var a = $1;
	var b = $<1>;
	var c = $<foo>;

	debug "$a == $b == $c";
}

function test3()
{
	var x = "axbxxax";
	x =~ s/(?<!x)(?P<foobar>x)/y/g;
	debug x;
}

function test4()
{
	var text = "Hello World";

	if (text =~ /(?P<foo>\S+)\s+(?P<bar>\S+)/) {
		import $$;
		debug "$foo $bar";
	}
	if (declared foo)
		panic "This is never reached";

	if (text =~ /(?P<foo>\S+)\s+(?P<bar>\S+)/I) {
		debug "$foo $bar";
	}
	if (declared foo)
		debug "Foo is now defined here too.";
}

function test5()
{
	function x() {
		"faxbar" =~ /f../;
		debug $0;
	}

	"foobar" =~ /f../;
	x();
	debug $0;
}

test1();
test2();
test3();
test4();
test5();

