-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCruParser.js
More file actions
214 lines (177 loc) · 5.19 KB
/
Copy pathCruParser.js
File metadata and controls
214 lines (177 loc) · 5.19 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var UE = require('./UE');
var Creneau = require('./Creneau');
// CruParser
var CruParser = function(sTokenize, sParsedSymb){
// The list of UE parsed from the input file.
this.parsedUE = [];
this.symb = ["+","code","créneau",""];
this.showTokenize = sTokenize;
this.showParsedSymbols = sParsedSymb;
this.errorCount = 0;
}
// Parser procedure
// tokenize : tranform the data input into a list
// <eol> = CRLF
CruParser.prototype.tokenize = function(data){
var separator = /(\+|\r\n|\s|,P=|,S=|,H=|,|\/\/\r\n|\r\+|\/\/)/;
data = data.split(separator);
data = data.filter((val, idx) => !val.match(separator));
return data;
}
// parse : analyze data by calling the first non terminal rule of the grammar
CruParser.prototype.parse = function(data){
var tData = this.tokenize(data);
if(this.showTokenize){
// console.log(tData);
}
this.listUE(tData);
}
// Parser operand
CruParser.prototype.errMsg = function(msg, input){
this.errorCount++;
// console.log("Parsing Error ! on "+input+" -- msg : "+msg);
}
// Read and return a symbol from input
CruParser.prototype.next = function(input){
var curS = input.shift();
if(this.showParsedSymbols){
//console.log(curS);
}
return curS
}
// accept : verify if the arg s is part of the language symbols.
CruParser.prototype.accept = function(s){
var idx = this.symb.indexOf(s);
// index 0 exists
if(idx === -1){
this.errMsg("symbol "+s+" unknown", [" "]);
return false;
}
return idx;
}
// check : check whether the arg elt is on the head of the list
CruParser.prototype.check = function(s, input){
if(this.accept(input[0]) == this.accept(s)){
return true;
}
return false;
}
// expect : expect the next symbol to be s.
CruParser.prototype.expect = function(s, input){
if(s == this.next(input)){
//console.log("Reckognized! "+s)
return true;
}else{
this.errMsg("symbol "+s+" doesn't match", input);
}
return false;
}
// Parser rules
// <liste_ue> = *(<ue>)
CruParser.prototype.listUE = function(input){
//console.log(input);
this.ue(input);
}
// UE = "+" <body>
CruParser.prototype.ue = function(input){
//if(this.check("+", input)){
this.expect("", input);
var args = this.code(input);
var ue = new UE(args, []);
while (input[0]!==""){
var cren = this.creneaux(input, ue);
}
this.parsedUE.push(ue);
if(input.length > 1){
this.ue(input);
}
return true;
//}else{
//return false;
//}
}
// code = 2ALPHA 2DIGIT CRLF
CruParser.prototype.code = function(input){
var curS = this.next(input);
if(matched = curS.match(/[A-Z]{1,4}\d{0,3}/)){
return matched[0];
}else{
this.errMsg("Invalid code", input);
return 0;
}
}
//creneau = “1” “,” type “,”nbplaces“,” jour “,” horaire “,” groupe_cours “,” salle “//” CRLF
CruParser.prototype.creneaux = function (input, curUE){
var ty = this.type(input);
var nbP = this.nbplaces(input);
var j = this.jour(input);
var h = this.horaire(input);
var gC = this.groupe_cours(input);
var s = this.salle(input);
var creneau = new Creneau(ty, nbP, j, h, gC, s);
curUE.addCreneau(creneau);
}
// type = (“C”/”D”/”T’) 1DIGIT
CruParser.prototype.type = function(input){
var curS = this.next(input);
var curS = this.next(input);
//console.log("curS=" + curS);
if(matched = curS.match(/[CDT]\d/)){
return matched[0];
}else{
this.errMsg("Invalid type", input);
}
}
// nbplaces = “P=” 2*3DIGIT
CruParser.prototype.nbplaces = function(input){
var curS = this.next(input);
//console.log("curS=" + curS);
if(matched = curS.match(/\d{1,3}/)){
return matched[0];
}else{
this.errMsg("Invalid nombre de place", input);
}
}
// jour = “H=” (“L”/”MA”/”ME”/”J”/”V”/”S”) WSP
CruParser.prototype.jour = function(input){
var curS = this.next(input);
//console.log("curS=" + curS);
if(matched = curS.match(/(L|MA|ME|J|V|S)/)){
return matched[0];
}else{
this.errMsg("Invalid jour", input);
}
}
// horaire = 1*2DIGIT “:” 2DIGIT “-” 1*2DIGIT “:” 2DIGIT
CruParser.prototype.horaire = function(input){
var curS = this.next(input);
//console.log("curS=" + curS);
if(matched = curS.match(/\d{1,2}:\d{2}-\d{1,2}:\d{2}/)){
return matched[0];
}else{
this.errMsg("Invalid horaire", input);
}
}
// groupe_cours = “F” 1DIGIT
CruParser.prototype.groupe_cours = function(input){
var curS = this.next(input);
//console.log("curS=" + curS);
if(matched = curS.match(/F\d|[A-Z]{1,2}/)){
return matched[0];
}else{
this.errMsg("Invalid groupe_cours", input);
}
}
// salle = “S= (1ALPHA 3DIGIT)/(%s”EXT” 1DIGIT)
CruParser.prototype.salle = function(input){
var curS = this.next(input);
matched = curS.match(/([A-Za-z]\d{3}|EXT\d)/)
//console.log("curS=" + curS);
if(matched != null){
return matched[0];
}else{
this.errMsg("Invalid salle", input);
}
var curS = this.next(input);
}
module.exports = CruParser;