-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlloc.cs
82 lines (68 loc) · 2.01 KB
/
Alloc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
namespace Lex
{
/*
* Class: Alloc
*/
public class Alloc
{
/*
* Function: NewCDfa
*/
public static Dfa NewDfa(Spec spec)
{
Dfa dfa;
dfa = new Dfa(spec.dfa_states.Count);
spec.dfa_states.Add(dfa);
return dfa;
}
/*
* Function: NewNfaPair
*/
public static NfaPair NewNfaPair()
{
NfaPair pair = new NfaPair();
return pair;
}
/*
* Function: NewNfa
*/
public static Nfa NewNfa(Spec spec)
{
Nfa p;
/* UNDONE: Buffer this? */
p = new Nfa();
/*p.label = spec.nfa_states.size();*/
spec.nfa_states.Add(p);
p.SetEdge(Nfa.EPSILON);
return p;
}
/**
* NewNLPair
* return a new NfaPair that matches a new line
* (\r\n?|\n)
*/
public static NfaPair NewNLPair(Spec spec)
{
NfaPair pair = NewNfaPair();
pair.end = NewNfa(spec); // newline accepting state
pair.start = NewNfa(spec); // new state with two epsilon edges
Nfa pstart = pair.start;
pstart.SetNext(NewNfa(spec));
Nfa pstartnext = pstart.GetNext();
pstartnext.SetEdge(Nfa.CCL);
pstartnext.SetCharSet(new CharSet());
pstartnext.GetCharSet().add('\n');
pstartnext.SetNext(pair.end); // accept '\n'
pstart.SetSib(NewNfa(spec));
Nfa pstartsib = pstart.GetSib();
pstartsib.SetEdge('\r');
pstartsib.SetNext(NewNfa(spec));
Nfa pstartsibnext = pstartsib.GetNext();
pstartsibnext.SetNext(null); // do NOT accept just '\r'
pstartsibnext.SetSib(NewNfa(spec));
pstartsibnext.GetSib().SetEdge('\n');
pstartsibnext.GetSib().SetNext(pair.end); // accept '\r\n'
return pair;
}
}
}