Skip to content

Commit bfff7c2

Browse files
committed
initial commit
0 parents  commit bfff7c2

12 files changed

+739
-0
lines changed

LICENSE.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2014 Ryusei Yamaguchi
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# HxRE
2+
3+
Yet another Haxe library for regular expression engine.

haxelib.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "hxre",
3+
"url": "https://github.com/mandel59/hxre",
4+
"license": "MIT",
5+
"tags": ["cross", "regular expression", "string"],
6+
"description": "Regular Expression",
7+
"version": "0.1.0",
8+
"releasenote": "initial commit",
9+
"contributors": ["mandel59"],
10+
"dependencies": {}
11+
}

hxre/Ast.hx

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package hxre;
2+
3+
import haxe.ds.Option;
4+
import hxre.Types;
5+
6+
enum Ast {
7+
Dot;
8+
Literal(c : Char);
9+
AstClass(ranges : Array<Range<Char>>);
10+
11+
Begin;
12+
End;
13+
14+
Cap(ast : Ast, name : Option<String>);
15+
16+
Cat(asts : Array<Ast>);
17+
Alt(ast1 : Ast, ast2 : Ast);
18+
Rep(ast : Ast, repeater : Repeater, greed : Greed);
19+
}
20+
21+
enum Repeater {
22+
ZeroOne;
23+
ZeroMore;
24+
OneMore;
25+
NRange(n : Int, m : Int);
26+
NMore(n : Int);
27+
}
28+
29+
enum Greed {
30+
Greedy;
31+
Ungreedy;
32+
}

hxre/Compiler.hx

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package hxre;
2+
3+
import hxre.Types;
4+
5+
class Compiler implements Program {
6+
public var ncap (default, null) : Index<Index<Char>>;
7+
public var nturnstile (default, null) : Index<Bool>;
8+
public var insts (default, null) : Array<Inst>;
9+
public var names (default, null) : Map<String, Index<Index<Char>>>;
10+
11+
public static function compile(ast : Ast) : Program {
12+
var c = new Compiler(ast);
13+
c.construct(ast);
14+
return c;
15+
}
16+
17+
function new(ast : Ast) {
18+
ncap = 1;
19+
nturnstile = 0;
20+
insts = [];
21+
names = new Map();
22+
}
23+
24+
function countCaps(ast : Ast) {
25+
switch (ast) {
26+
case Cap(ast, name):
27+
var c = ncap++;
28+
switch (name) {
29+
case Some(n): names.set(n, c);
30+
default:
31+
}
32+
case Cat(asts):
33+
for (ast in asts) {
34+
countCaps(ast);
35+
}
36+
case Alt(ast1, ast2):
37+
countCaps(ast1);
38+
countCaps(ast2);
39+
case Rep(ast, _, _):
40+
countCaps(ast);
41+
default:
42+
}
43+
}
44+
45+
function construct(ast : Ast) {
46+
switch (ast) {
47+
case Dot:
48+
insts.push(AddThread);
49+
case Literal(c):
50+
insts.push(OneChar(c));
51+
insts.push(AddThread);
52+
case AstClass(ranges):
53+
insts.push(CharClass(ranges));
54+
insts.push(AddThread);
55+
case Begin:
56+
insts.push(Begin);
57+
case End:
58+
insts.push(End);
59+
case Cap(ast, name):
60+
var c = ncap++;
61+
switch (name) {
62+
case Some(n): names.set(n, c);
63+
default:
64+
}
65+
insts.push(SaveBegin(c));
66+
construct(ast);
67+
insts.push(SaveEnd(c));
68+
case Cat(asts):
69+
for (ast in asts) {
70+
construct(ast);
71+
}
72+
case Alt(ast1, ast2):
73+
var split = insts.length;
74+
insts.push(null);
75+
var j1 = insts.length;
76+
construct(ast1);
77+
var jump = insts.length;
78+
insts.push(null);
79+
var j2 = insts.length;
80+
construct(ast2);
81+
var j3 = insts.length;
82+
insts.push(PassTurnstile(nturnstile++));
83+
84+
insts[split] = Split(j1, j2);
85+
insts[jump] = Jump(j3);
86+
case Rep(ast, ZeroOne, g):
87+
var split = insts.length;
88+
insts.push(null);
89+
var j1 = insts.length;
90+
construct(ast);
91+
var j2 = insts.length;
92+
insts.push(PassTurnstile(nturnstile++));
93+
94+
switch (g) {
95+
case Greedy: insts[split] = Split(j1, j2);
96+
case Ungreedy: insts[split] = Split(j2, j1);
97+
}
98+
case Rep(ast, ZeroMore, g):
99+
var j1 = insts.length;
100+
insts.push(PassTurnstile(nturnstile++));
101+
var split = insts.length;
102+
insts.push(null);
103+
var j2 = insts.length;
104+
construct(ast);
105+
var jump = insts.length;
106+
insts.push(null);
107+
var j3 = insts.length;
108+
109+
switch (g) {
110+
case Greedy: insts[split] = Split(j2, j3);
111+
case Ungreedy: insts[split] = Split(j3, j2);
112+
}
113+
insts[jump] = Jump(j1);
114+
case Rep(ast, OneMore, g):
115+
var j1 = insts.length;
116+
insts.push(PassTurnstile(nturnstile++));
117+
construct(ast);
118+
var split = insts.length;
119+
insts.push(null);
120+
var j2 = insts.length;
121+
122+
switch (g) {
123+
case Greedy: insts[split] = Split(j1, j2);
124+
case Ungreedy: insts[split] = Split(j2, j1);
125+
}
126+
case Rep(ast, NRange(n, m), g):
127+
if (m > 0) {
128+
var c = ncap;
129+
130+
for (i in 0 ... n) {
131+
ncap = c;
132+
construct(ast);
133+
}
134+
135+
var splits = [];
136+
for (i in n ... m) {
137+
ncap = c;
138+
splits.push(insts.length);
139+
insts.push(null);
140+
construct(ast);
141+
}
142+
143+
var j1 = insts.length;
144+
insts.push(PassTurnstile(nturnstile++));
145+
146+
switch (g) {
147+
case Greedy:
148+
for (split in splits) {
149+
insts[split] = Split(split + 1, j1);
150+
}
151+
case Ungreedy:
152+
for (split in splits) {
153+
insts[split] = Split(j1, split + 1);
154+
}
155+
}
156+
} else {
157+
countCaps(ast);
158+
}
159+
case Rep(ast, NMore(n), g):
160+
var c = ncap;
161+
162+
for (i in 0 ... n) {
163+
ncap = c;
164+
construct(ast);
165+
}
166+
167+
ncap = c;
168+
construct(Rep(ast, ZeroMore, g));
169+
}
170+
}
171+
}

hxre/Inst.hx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package hxre;
2+
3+
import hxre.Types;
4+
5+
enum Inst {
6+
AddThread;
7+
Jump(x : Index<Inst>);
8+
Split(x : Index<Inst>, y : Index<Inst>);
9+
PassTurnstile(i : Index<Bool>);
10+
SaveBegin(i : Index<Index<Char>>);
11+
SaveEnd(i : Index<Index<Char>>);
12+
13+
OneChar(c : Char);
14+
CharClass(ranges : Array<Range<Char>>);
15+
Begin;
16+
End;
17+
}

0 commit comments

Comments
 (0)