-
Notifications
You must be signed in to change notification settings - Fork 2
/
expression.rb
148 lines (117 loc) · 2.49 KB
/
expression.rb
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
require "./staff.rb"
require 'colorize' if $DEBUG_project > 0
class MetaExpression
attr_accessor :value, :parent, :first;
def initialize(parent = nil)
@parent = parent;
@value = [] ;
@first = nil ;
end
def cmp(token_or_meta)
if token_or_meta.class == Token then
return 0;
else
return 0.1;
end
end
def print_tree(n = 0)
@value.each do |token|
if token.class == Token then
print " "*n*4;
p token;
else
token.print_tree(n+1)
end
end
end
def separate_first!
v = @value[0];
prev = nil;
while v.class != Token do
prev = v;
v = v.value[0];
end
@first = v;
if prev != nil then
prev.value = prev.value[1..-1]
while prev.value == [] && prev != self do
prev = prev.parent
prev.value = prev.value[1..-1]
end
else
@value = @value[1..-1]
end
end
def get_last_token()
if @value.last.class == MetaExpression then
return @value.last.get_last_token
else
return @value.last
end
end
def get_first_token()
if @value.first.class == MetaExpression then
return @value.first.get_first_token
else
return @value.first
end
end
def
def min_previous_space
get_first_token.min_previous_space
end
def min_follow_space
get_last_token.min_follow_space
end
def inspect
return to_s
end
def to_s
return @value.to_s
end
end
class Expression
attr_accessor :tokens
def initialize(string_expr = nil)
parse(string_expr) if string_expr != nil;
end
def parse(string_expr)
do_parse = true
@tokens = [];
str_index = 0;
tkn_index = 0;
while (do_parse) do
is_cmp = false
TypeData.regexp_array.each do |regexp|
res = regexp[0].match(string_expr);
if res != nil then
is_cmp = true;
@tokens += [Token.new(res[0], regexp[1], regexp[2], regexp[3], regexp[4], regexp[5])]
size_of_token = res[0].size;
@tokens.last.str_index = str_index;
@tokens.last.tkn_index = tkn_index;
str_index += size_of_token;
tkn_index += 1 if regexp[2]; # significant tokens only
string_expr = string_expr[size_of_token..-1];
break;
end
end
do_parse = is_cmp;
end
end
# Delete tokens, wich marked as insignificant + reorded arrays by size
def erase_insignificant_tokens!
i = 0;
while i < @tokens.size do
if @tokens[i].necessary == false then
@tokens.delete_at(i);
i -=1;
end
i+=1;
end
return self;
end
def print_tokens()
@tokens.each_with_index{|x,i| p [i,x];}
end
end