@@ -18,12 +18,15 @@ import java.io.FileNotFoundException;
18
18
import java. io. FileReader;
19
19
import java. io. File;
20
20
import java. util. List;
21
+ import java. util. EmptyStackException;
22
+ import java. util. Stack;
21
23
22
24
import org. eclipse. core. runtime. Path;
23
25
24
26
import fr. cnes. analysis. tools. analyzer. datas. AbstractChecker;
25
27
import fr. cnes. analysis. tools. analyzer. datas. CheckResult;
26
28
import fr. cnes. analysis. tools. analyzer. exception. JFlexException;
29
+ import fr. cnes. analysis. tools. shell. metrics. Function;
27
30
28
31
% %
29
32
@@ -39,23 +42,38 @@ import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
39
42
%type List<CheckResult>
40
43
41
44
42
- %state COMMENT , NAMING , TRAP
45
+ %state COMMENT , NAMING , TRAP , BEGINFUNC , STRING_SIMPLE , STRING_DOUBLE
43
46
44
47
COMMENT_WORD = \#
45
48
FUNCTION = "function"
46
49
FUNCT = {FNAME}{SPACE} * [ \( ] {SPACE} * [ \) ]
47
50
FNAME = [ a- zA- Z0- 9\.\!\-\_\@\?\+ ] +
48
51
SPACE = [ \ \r\t\f]
49
52
VAR = [ a- zA- Z][ a- zA- Z0- 9\_ ] *
50
- STRING = \' [^ \' ] * \' | \" [^ \" ] * \"
53
+
54
+ STRING_D = \"
55
+ IGNORE_STRING_D = [ \\ ][ \" ]
56
+ STRING_S = \'
57
+ IGNORE_STRING_S = [ \\ ][ \' ]
51
58
52
59
TRAP = "trap"
53
60
61
+ FUNCSTART = \{ | \( | \(\( | \[\[ | "if" | "case" | "select" | "for" | "while" | "until"
62
+ FUNCEND = \} | \) | \)\) | \]\] | "fi" | "esac" | "done"
54
63
55
64
%{
56
- String location = " MAIN PROGRAM" ;
65
+ /* MAINPROGRAM: constant for main program localisation */
66
+ private static final String MAINPROGRAM = " MAIN PROGRAM" ;
67
+
68
+ String location = MAINPROGRAM ;
69
+
57
70
private String parsedFileName;
58
71
72
+ /* functionLine: the beginning line of the function */
73
+ int functionLine = 0 ;
74
+
75
+ private Stack<Function > functionStack = new Stack<> ();
76
+
59
77
public SHSYNCSignals() {
60
78
61
79
}
@@ -67,7 +85,25 @@ TRAP = "trap"
67
85
this . parsedFileName = file. toString();
68
86
this . zzReader = new FileReader (new Path (file. getAbsolutePath()). toOSString());
69
87
}
70
-
88
+
89
+ private void endLocation() throws JFlexException {
90
+ try {
91
+ Function functionFinished = functionStack. pop();
92
+ if (! functionStack. empty()) {
93
+ /* there is a current function: change location to this function */
94
+ location = functionStack. peek(). getName();
95
+ } else {
96
+ /* we are in the main program: change location to main */
97
+ location = MAINPROGRAM ;
98
+ }
99
+ }catch (EmptyStackException e){
100
+ final String errorMessage = e. getMessage();
101
+ throw new JFlexException (this . getClass(). getName(), parsedFileName,
102
+ errorMessage, yytext(), yyline, yycolumn);
103
+ }
104
+ }
105
+
106
+
71
107
%}
72
108
73
109
%eofval{
@@ -98,7 +134,7 @@ TRAP = "trap"
98
134
/* ***********************/
99
135
<NAMING>
100
136
{
101
- {FNAME } {location = yytext(); yybegin(YYINITIAL );}
137
+ {FNAME } {location = yytext(); functionLine = yyline + 1 ; yybegin(BEGINFUNC );}
102
138
\n {yybegin(YYINITIAL );}
103
139
. {}
104
140
}
@@ -110,8 +146,30 @@ TRAP = "trap"
110
146
{
111
147
{COMMENT_WORD } {yybegin(COMMENT );}
112
148
{FUNCTION } {yybegin(NAMING );}
113
- {FUNCT } {location = yytext(). substring(0 ,yytext(). length()- 2 ). trim(); }
114
- {STRING } {}
149
+ {FUNCT } {functionLine = yyline+ 1 ;
150
+ location = yytext(). substring(0 ,yytext(). length()- 2 ). trim();
151
+ yybegin(BEGINFUNC );
152
+ }
153
+ {FUNCSTART } {
154
+ if (! functionStack. empty()){
155
+ if (functionStack. peek(). getFinisher(). equals(Function . finisherOf(yytext()))){
156
+ functionStack. peek(). addStarterRepetition();
157
+ }
158
+ }
159
+ }
160
+ {FUNCEND } {
161
+ if (! functionStack. empty()){
162
+ if (functionStack. peek(). isFinisher(yytext())){
163
+ if (functionStack. peek(). getStarterRepetition()> 0 ) {
164
+ functionStack. peek(). removeStarterRepetition();
165
+ } else {
166
+ endLocation();
167
+ }
168
+ }
169
+ }
170
+ }
171
+ {STRING_D } {yybegin(STRING_DOUBLE );}
172
+ {STRING_S } {yybegin(STRING_SIMPLE );}
115
173
{TRAP } {yybegin(TRAP );}
116
174
[^ ] {}
117
175
}
@@ -127,7 +185,50 @@ TRAP = "trap"
127
185
. {}
128
186
}
129
187
188
+ /* ***********************/
189
+ /* BEGINFUNC STATE */
190
+ /* ***********************/
191
+ /*
192
+ * This state target is to retrieve the function starter. For more information on fonction starter, have a look on {@link Function} class.
193
+ * Pending this starter, the function ender can be defined.
194
+ *
195
+ */
196
+ <BEGINFUNC>
197
+ {
198
+ \(\) {}
199
+ {FUNCSTART } {
200
+ Function function;
201
+ function = new Function (location, functionLine, yytext());
202
+ functionStack. push(function);
203
+ yybegin(YYINITIAL );
204
+ }
205
+ [^ ]| {SPACE } {}
206
+ }
207
+
208
+ /*
209
+ * The string states are designed to avoid problems due to patterns found in strings.
210
+ */
211
+ /* ***********************/
212
+ /* STRING_SIMPLE STATE */
213
+ /* ***********************/
214
+ <STRING_SIMPLE>
215
+ {
216
+ {IGNORE_STRING_S } {}
217
+ {STRING_S } {yybegin(YYINITIAL );}
218
+ [^ ]| {SPACE } {}
219
+ }
130
220
221
+ /* ***********************/
222
+ /* STRING_DOUBLE STATE */
223
+ /* ***********************/
224
+ <STRING_DOUBLE>
225
+ {
226
+ {IGNORE_STRING_D } {}
227
+ {STRING_D } {yybegin(YYINITIAL );}
228
+ [^ ]| {SPACE } {}
229
+ }
230
+
231
+
131
232
/* ***********************/
132
233
/* ERROR STATE */
133
234
/* ***********************/
0 commit comments