This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.rs
148 lines (141 loc) · 4.82 KB
/
debug.rs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//use std::str;
use EAssertStart = super::parse::AssertStart;
use EAssertEnd = super::parse::AssertEnd;
use EAssertWordBoundary = super::parse::AssertWordBoundary;
use EAssertNonWordBoundary = super::parse::AssertNonWordBoundary;
use super::parse::{
Expression,
Literal,
AnyLiteral,
RangeLiteral,
CharacterClass,
Concatenate,
Alternate,
SubExpression,
Question,
Star,
Plus,
ExactRepetition,
UnboundedRepetition,
BoundedRepetition
};
use super::parse::{
QuantifierType,
Greedy,
NonGreedy
};
use super::compile::{
Instruction,
Char,
Range,
Any,
Fork,
Jump,
ConditionalJumpEq,
ConditionalJumpLE,
Increment,
SaveStart,
SaveEnd,
AssertStart,
AssertEnd,
AssertWordBoundary,
AssertNonWordBoundary,
Accept
};
pub fn print_expression_tree(expression: &Expression) {
print_expression_tree_recursive(expression, 0);
}
fn print_expression_tree_recursive(expression: &Expression, indent: uint) {
do indent.times {
print(" ");
}
match *expression {
Literal(c) => println!("Literal({})", c),
AnyLiteral => println("Any"),
RangeLiteral(start, end) => println!("Range({}, {})", start, end),
CharacterClass(ref ranges) => {
println("CharacterClass");
for &(start, end) in ranges.iter() {
do (indent + 1).times {
print(" ");
}
print!("[{}, {}]", start as u32, end as u32);
print!(" <{}, {}>", start, end);
println("");
}
}
Concatenate(ref left, ref right) => {
println("Concatenate");
print_expression_tree_recursive(*left, indent + 1);
print_expression_tree_recursive(*right, indent + 1);
}
Alternate(ref left, ref right) => {
println("Alternate");
print_expression_tree_recursive(*left, indent + 1);
print_expression_tree_recursive(*right, indent + 1);
}
SubExpression(ref e, capture_slot) => {
match capture_slot {
Some(slot) => println!("Group {}", slot),
None => println("Non-capture Group")
}
print_expression_tree_recursive(*e, indent + 1);
}
Question(ref e, typ) => {
println("Question " + quantifier_type_to_str(typ));
print_expression_tree_recursive(*e, indent + 1);
}
Star(ref e, typ) => {
println("Star " + quantifier_type_to_str(typ));
print_expression_tree_recursive(*e, indent + 1);
}
Plus(ref e, typ) => {
println("Plus " + quantifier_type_to_str(typ));
print_expression_tree_recursive(*e, indent + 1);
}
ExactRepetition(ref e, count, typ) => {
println!("ExactRepetition {} {}", count, quantifier_type_to_str(typ));
print_expression_tree_recursive(*e, indent + 1);
}
UnboundedRepetition(ref e, low, typ) => {
println!("Unboundedrepetition {}- {}", low, quantifier_type_to_str(typ));
print_expression_tree_recursive(*e, indent + 1);
}
BoundedRepetition(ref e, low, high, typ) => {
println!("BoundedRepetition {}-{} {}", low, high, quantifier_type_to_str(typ));
print_expression_tree_recursive(*e, indent + 1);
}
EAssertStart => println("AssertStart"),
EAssertEnd => println("AssertEnd"),
EAssertWordBoundary => println("AssertWordBoundary"),
EAssertNonWordBoundary => println("AssertNonWordBoundary")
}
}
fn quantifier_type_to_str(typ: QuantifierType) -> &'static str {
match typ {
Greedy => "Greedy",
NonGreedy => "NonGreedy"
}
}
pub fn print_code(instructions: &[Instruction]) {
for (pc, instruction) in instructions.iter().enumerate() {
print!("{} ", pc);
match *instruction {
Char(c) => println!("CHR {}", c),
Any => println("ANY"),
Range(start, end) => println!("RNG {} {}", start, end),
Fork(left, right) => println!("FRK {} {}", left, right),
Jump(new_pc) => println!("JMP {}", new_pc),
ConditionalJumpEq(register, value, new_pc) => println!("JEQ {} {} {}", register, value, new_pc),
ConditionalJumpLE(register, value, new_pc) => println!("JLE {} {} {}", register, value, new_pc),
Increment(register) => println!("INC {}", register),
SaveStart(group) => println!("SVS {}", group),
SaveEnd(group) => println!("SVE {}", group),
Accept => println("ACC"),
AssertStart => println("AS^"),
AssertEnd => println("AS$"),
AssertWordBoundary => println("ASb"),
AssertNonWordBoundary => println("ASB")
}
}
}