Skip to content

Commit 00d3855

Browse files
author
Sebastiano Merlino
committed
Modified in order to improve IFTIME parsing
1 parent 0b5f6fb commit 00d3855

File tree

1 file changed

+156
-75
lines changed

1 file changed

+156
-75
lines changed

ael.y

Lines changed: 156 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,146 @@ char* alloc_string(char* d)
133133
return s;
134134
}
135135

136+
typedef struct
137+
{
138+
char* beginHour;
139+
char* endHour;
140+
char* beginMinutes;
141+
char* endMinutes;
142+
char* beginDay;
143+
char* endDay;
144+
char* beginDOM;
145+
char* endDOM;
146+
char* beginMonth;
147+
char* endMonth;
148+
149+
void destroyAll()
150+
{
151+
destroy_string(beginHour);
152+
destroy_string(endHour);
153+
destroy_string(beginMinutes);
154+
destroy_string(endMinutes);
155+
destroy_string(beginDay);
156+
destroy_string(endDay);
157+
destroy_string(beginDOM);
158+
destroy_string(endDOM);
159+
destroy_string(beginMonth);
160+
destroy_string(endMonth);
161+
}
162+
} TimeStruct;
163+
164+
165+
166+
string time2iftime(TimeStruct* ts)
167+
{
168+
stringstream ss;
169+
ss << "temp = os.date()" << endl;
170+
ss << "if (";
171+
bool first = true;
172+
if(ts->beginMonth != "")
173+
{
174+
ss << "temp.month >= " << ts->beginMonth << ((ts->endMonth != "") ? (" and temp.month < " + string(ts->endMonth)) : "");
175+
first = false;
176+
}
177+
if(ts->beginDOM != "")
178+
{
179+
if(!first)
180+
ss << " and ";
181+
ss << "temp.day >= " << ts->beginDOM << ((ts->endDOM != "") ? (" and temp.day < " + string(ts->endDOM)) : "");
182+
first = false;
183+
}
184+
if(ts->beginDay != "")
185+
{
186+
if(!first)
187+
ss << " and ";
188+
ss << "temp.wday >= " << ts->beginDay << ((ts->beginDay != "") ? (" and temp.wday < " + string(ts->endDay)) : "");
189+
first = false;
190+
}
191+
if(!first)
192+
ss << " and ";
193+
ss << "(temp.hour + 0.01 * temp.min) >= " << ts->beginHour << "." << ts->beginMinutes;
194+
ss << " and (temp.hour + 0.01 * temp.min) < " << ts->endHour << "." << ts->endMinutes;
195+
ss << ") then" << endl;
196+
return ss.str();
197+
}
198+
199+
void handleTimes(char* t1, char* t2, char* t3, char* day, char* md, char* m, TimeStruct* ts)
200+
{
201+
string bh(t1);
202+
string em(t3);
203+
vector<string> second_word = split(t2, '-');
204+
ts->beginHour = alloc_string((char*)trim(bh).data());
205+
ts->beginMinutes = alloc_string((char*)trim(second_word[0]).data());
206+
ts->endHour = alloc_string((char*)trim(second_word[1]).data());
207+
ts->endMinutes = alloc_string((char*)trim(em).data());
208+
string dw(day);
209+
string dayword(trim(dw));
210+
string beginDayString = "";
211+
string endDayString = "";
212+
if(dayword != "*")
213+
{
214+
vector<string> days = split(dayword, '-');
215+
beginDayString = sday2iday(trim(days[0]));
216+
if(days.size() > 1)
217+
{
218+
endDayString = sday2iday(trim(days[1]));
219+
}
220+
}
221+
ts->beginDay = alloc_string((char*)beginDayString.data());
222+
ts->endDay = alloc_string((char*)endDayString.data());
223+
string dom(md);
224+
string dayOfMonth(trim(dom));
225+
string beginDOMString = "";
226+
string endDOMString = "";
227+
if(dayOfMonth != "*")
228+
{
229+
vector<string> monthDays = split(dayOfMonth, '-');
230+
beginDOMString = trim(monthDays[0]);
231+
if(monthDays.size() > 1)
232+
{
233+
endDOMString = trim(monthDays[1]);
234+
}
235+
}
236+
ts->beginDOM = alloc_string((char*)beginDOMString.data());
237+
ts->endDOM = alloc_string((char*)endDOMString.data());
238+
string mon(m);
239+
string month(trim(mon));
240+
string beginMonthString = "";
241+
string endMonthString = "";
242+
if(month != "*")
243+
{
244+
vector<string> months = split(month, '-');
245+
beginMonthString = smonth2imonth(trim(months[0]));
246+
if(months.size() > 1)
247+
{
248+
endMonthString = smonth2imonth(trim(months[1]));
249+
}
250+
}
251+
ts->beginMonth = alloc_string((char*)beginMonthString.data());
252+
ts->endMonth = alloc_string((char*)endMonthString.data());
253+
}
254+
255+
void handleTimes(char* time, char* day, char* md, char* m, TimeStruct* ts)
256+
{
257+
char* t1;
258+
char* t2;
259+
char* t3;
260+
if(string(time) == "*")
261+
{
262+
t1 = (char*)"00";
263+
t2 = (char*)"00-23";
264+
t3 = (char*)"59";
265+
}
266+
else
267+
{
268+
vector<string> time_parts = split(time, ':');
269+
t1 = (char*) time_parts[0].data();
270+
t2 = (char*) time_parts[1].data();
271+
t3 = (char*) time_parts[2].data();
272+
}
273+
handleTimes(t1,t2,t3,day,md,m,ts);
274+
}
275+
136276
char* grow_string(char* head, char* tail)
137277
{
138278
char* s = (char*) calloc((strlen(head) + strlen(tail) + 2), sizeof(char));
@@ -517,89 +657,30 @@ random_head: RANDOM LPAREN implicit_expr_stat RPAREN
517657

518658
ifTime_head: IFTIME LPAREN word3_list COLON word3_list COLON word3_list PIPE word3_list PIPE word3_list PIPE word3_list RPAREN
519659
{
520-
string bh($3);
521-
string em($7);
522-
vector<string> second_word = split($5, '-');
523-
string beginHour(trim(bh));
524-
string beginMinutes(trim(second_word[0]));
525-
string endHour(trim(second_word[1]));
526-
string endMinutes(trim(em));
527-
string dw($9);
528-
string dayword(trim(dw));
529-
string beginDay = "";
530-
string endDay = "";
531-
if(dayword != "*")
532-
{
533-
vector<string> days = split(dayword, '-');
534-
beginDay = sday2iday(trim(days[0]));
535-
if(days.size() > 1)
536-
{
537-
endDay = sday2iday(trim(days[1]));
538-
}
539-
}
540-
string dom($11);
541-
string dayOfMonth(trim(dom));
542-
string beginDOM = "";
543-
string endDOM = "";
544-
if(dayOfMonth != "*")
545-
{
546-
vector<string> monthDays = split(dayOfMonth, '-');
547-
beginDOM = trim(monthDays[0]);
548-
if(monthDays.size() > 1)
549-
{
550-
endDOM = trim(monthDays[1]);
551-
}
552-
}
553-
string mon($13);
554-
string month(trim(mon));
555-
string beginMonth = "";
556-
string endMonth = "";
557-
if(month != "*")
558-
{
559-
vector<string> months = split(month, '-');
560-
beginMonth = smonth2imonth(trim(months[0]));
561-
if(months.size() > 1)
562-
{
563-
endMonth = smonth2imonth(trim(months[1]));
564-
}
565-
}
566-
stringstream ss;
567-
ss << "temp = os.date()" << endl;
568-
ss << "if (";
569-
bool first = true;
570-
if(beginMonth != "")
571-
{
572-
ss << "temp.month >= " << beginMonth << ((endMonth != "") ? (" and temp.month < " + endMonth) : "");
573-
first = false;
574-
}
575-
if(beginDOM != "")
576-
{
577-
if(!first)
578-
ss << " and ";
579-
ss << "temp.day >= " << beginDOM << ((endDOM != "") ? (" and temp.day < " + endDOM) : "");
580-
first = false;
581-
}
582-
if(beginDay != "")
583-
{
584-
if(!first)
585-
ss << " and ";
586-
ss << "temp.wday >= " << beginDay << ((beginDay != "") ? (" and temp.wday < " + endDay) : "");
587-
first = false;
588-
}
589-
if(!first)
590-
ss << " and ";
591-
ss << "(temp.hour + 0.01 * temp.min) >= " << beginHour << "." << beginMinutes;
592-
ss << " and (temp.hour + 0.01 * temp.min) < " << endHour << "." << endMinutes;
593-
ss << ") then" << endl;
594-
$$ = alloc_string((char*)ss.str().data());
660+
TimeStruct ts;
661+
handleTimes($3, $5, $7, $9, $11, $13, &ts);
662+
string s = time2iftime(&ts);
663+
$$ = alloc_string((char*)s.data());
595664
destroy_string($3);
596665
destroy_string($5);
597666
destroy_string($7);
598667
destroy_string($9);
599668
destroy_string($11);
600669
destroy_string($13);
670+
ts.destroyAll();
601671
}
602672
| IFTIME LPAREN word PIPE word3_list PIPE word3_list PIPE word3_list RPAREN
673+
{
674+
TimeStruct ts;
675+
handleTimes($3, $5, $7, $9, &ts);
676+
string s = time2iftime(&ts);
677+
$$ = alloc_string((char*)s.data());
678+
destroy_string($3);
679+
destroy_string($5);
680+
destroy_string($7);
681+
destroy_string($9);
682+
ts.destroyAll();
683+
}
603684
;
604685

605686
word3_list:

0 commit comments

Comments
 (0)