#!/usr/bin/env ascript
# -*- coding: shift_jis -*-
import(re)

sys.stdout.setencoding("shift_jis")

print_func(func:function) = printf('%s = %s\n', func, func.expr())
tester(test:expr):map:dynamic_scope = printf("%s .. %s\n", test, eval(test)):nomap

lambda {
	str = "あいうえお かきくけこ さしすせそ たちつてと"
	tester(`str)
	tester(`re.scan(r"\w+", str)::group(0))
	tester(`re.scan(r"\w+", str, 10)::group(0))
	tester(`re.scan(r"\w+", str, 0, 10)::group(0))
}()
lambda {
	str = "あいうえお かきくけこ さしすせそ たちつてと"
	tester(`str)
	tester(`str.scan(r"\w+")::group(0))
	tester(`str.scan(r"\w+", 10)::group(0))
	tester(`str.scan(r"\w+", 0, 10)::group(0))
}()
lambda {
	str = "あいうえお かきくけこ さしすせそ たちつてと"
	tester(`str)
	tester(`str.match(r"\w+", 0).group(0))
	tester(`str.match(r"\w+", 6).group(0))
	tester(`str.match(r"\w+", 12).group(0))
	tester(`str.match(r"\w+", 12, 13).group(0))
}()
lambda {
	m = '3.14'.match(r"(\d+)\.(\d*)")
	tester(`m.string)
	tester(`m.group(1))
	tester(`m.group(2))
	tester(`m.groups())
}()
lambda {
	tester(`re.scan("(ba)(.)", "foobarbazfoobarbaz"):list {|m| [m.group(0), m.group(1, 2)] })
}()
lambda {
	pattern = re.pattern(r"^http://([^/]+)")
	lines = """\
http://sourceforge.jp/projects/
foo
http://slashdot.jp/
http://www.google.com/
hoge
""".eachline()
	println(pattern.match(lines).skipnil():*group(1))
}()
lambda {
	ms = re.scan(r"\[([^]]+)\]", "jkfdjf[hoge]kdsaa[foo]kfjdkjfkd[bar]saajkjkjfdsfdaaa");
	println(ms::group(1)):nomap
	ms = re.pattern(r"\[([^]]+)\]").scan("jkfdjf[hoge]kdsaa[foo]kfjdkjfkd[bar]saajkjkjfdsfdaaa");
	println(ms::group(1)):nomap
	text = "fjdkjf[0]jfkdjk[100]fdfdsfjdsfjdkshe[300]llofkdjsk[999]fjdsfjdshello[9999]"
	println(re.scan(r"\[(\d+)\]", text):*group(1))
}()
lambda {
	fields = [re.split("a+", "aaaaaaabbbbbbaaacccccDDa")];
	println(fields):nomap
	fields = [re.pattern("a+").split("aaaaaaabbbbbbaaacccccDDa")];
	println(fields):nomap
}()
lambda {
	m = re.match("q[\\c\\\\]", "q\034");
	println(m):nomap
}()
lambda {
	m = re.match(r"(\b[a-z]+) \1", "cat in the the hat")
	println(m):nomap
	tester(`re.match(r"A(a*)A", "AA").group(1))
}()
lambda {
	m = re.match(r"(\d+)\.(\d*)", '3.14')
	tester(`m.string)
	tester(`m.group(1))
	tester(`m.group(2))
	tester(`m.groups())
}()
lambda {
	m = re.match(r"(?<first>\d+)\.(?<second>\d*)", '3.14')
	tester(`m.string)
	tester(`m.group("first"))
	tester(`m.group("second"))
}()
lambda {
	m = re.match(r"(?<first>\d+)\.(\d*)", '3.14')
	tester(`m.string)
	tester(`m.group("first"))
	tester(`m.group(2))
}()
lambda {
	m = re.match(r"(\d+)\.(?<second>\d*)", '3.14')
	tester(`m.string)
	tester(`m.group(1))
	tester(`m.group("second"))
}()
lambda {
	tester(`chooseif(re.match(r"hello", 'HELLO WORLD'), "match", "not match"))
	tester(`chooseif(re.match(re.pattern(r"hello"):icase, 'HELLO WORLD'), "match", "not match"))
}()
// match with pattern object
lambda {
	m = re.pattern(r"(\d+)\.(\d*)").match('3.14')
	tester(`m.string)
	tester(`m.group(1))
	tester(`m.group(2))
	tester(`m.groups())
}()
lambda {
	m = re.pattern(r"(?<first>\d+)\.(?<second>\d*)").match('3.14')
	tester(`m.string)
	tester(`m.group("first"))
	tester(`m.group("second"))
}()
lambda {
	m = re.pattern(r"(?<first>\d+)\.(\d*)").match('3.14')
	tester(`m.string)
	tester(`m.group("first"))
	tester(`m.group(2))
}()
lambda {
	m = re.pattern(r"(\d+)\.(?<second>\d*)").match('3.14')
	tester(`m.string)
	tester(`m.group(1))
	tester(`m.group("second"))
}()
lambda {
	tester(`chooseif(re.pattern(r"hello").match('HELLO WORLD'), "match", "not match"))
	tester(`chooseif((re.pattern(r"hello"):icase).match('HELLO WORLD'), "match", "not match"))
}()
lambda {
	tester(`re.sub(r",\s*", ":", "1234, 567,    890,   123"))
	tester(`re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
		r'static PyObject*\npy_\1(void)\n{',
		'def myfunc():'))
	dashrepl(matchobj) = {
		if (matchobj.group(0) == '-') { return(' ') } else { return('-') }
	}
	print_func(dashrepl)
	tester(`re.sub('-{1,2}', dashrepl, 'pro----gram-files'))
}()
lambda {
	tester(`re.pattern(r",\s*").sub(":", "1234, 567,    890,   123"))
	tester(`re.pattern(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):').sub(
		r'static PyObject*\npy_\1(void)\n{',
		'def myfunc():'))
	dashrepl(matchobj) = {
		if (matchobj.group(0) == '-') { return(' ') } else { return('-') }
	}
	print_func(dashrepl)
	tester(`re.pattern('-{1,2}').sub(dashrepl, 'pro----gram-files'))
}()
lambda {
	lines = """\
http://sourceforge.jp/projects/
foo
http://slashdot.jp/
http://www.google.com/
hoge
""".eachline()
	for (line in lines) {
		if (m = re.match(r"^http://([^/]+)", line)) {
			print(" ", m.group(1))
		}
	}
	println()
}()
lambda {
	lines = """\
http://sourceforge.jp/projects/
foo
http://slashdot.jp/
http://www.google.com/
hoge
""".eachline()
	print(" ", (re.match(r"^http://([^/]+)", lines):xlist)::group(1))
	println()
}()
