Skip to content

Commit 6b553e2

Browse files
committed
#150 Localization of the COM.FLOW.FileExistence rule. Test file updated
accordingly
1 parent a858b8e commit 6b553e2

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

fr.cnes.analysis.tools.shell.rules/lex/COMFLOWFileExistence.lex

+78-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import org.eclipse.core.runtime.Path;
2525
import fr.cnes.analysis.tools.analyzer.datas.AbstractChecker;
2626
import fr.cnes.analysis.tools.analyzer.datas.CheckResult;
2727
import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
28+
import java.util.EmptyStackException;
29+
import java.util.Stack;
30+
import fr.cnes.analysis.tools.shell.metrics.Function;
2831

2932
import java.util.logging.Logger;
3033

@@ -42,7 +45,7 @@ import java.util.logging.Logger;
4245
%type List<CheckResult>
4346

4447

45-
%state COMMENT, NAMING, FILE, STRING
48+
%state COMMENT, NAMING, FILE, STRING, BEGINFUNC
4649

4750
COMMENT_WORD = [\#]
4851
FUNCTION = "function"
@@ -54,6 +57,8 @@ SHELL_VAR = ([0-9]+|[\-\@\?\#\!\_\*\$])
5457
EXPANDED_VAR = [\!]?{NAME}([\:]|(([\%]?[\%])|([\#]?[\#]))|([\:]?[\=\+\?\-]))({NAME}|[\[]{NAME}[\]])|([\#]{NAME})
5558
VAR = ({NAME}|([\$][\{]({NAME}|{SHELL_VAR}|{EXPANDED_VAR})[\}])|([\$]({NAME}|{SHELL_VAR})))
5659

60+
FUNCSTART = \{ | \( | \(\( | \[\[ | "if" | "case" | "select" | "for" | "while" | "until"
61+
FUNCEND = \} | \) | \)\) | \]\] | "fi" | "esac" | "done"
5762

5863
OPERATOR_RIGHT = [\>]|[\>][\&]|[\&][\>]|[\>][\>]|[\>][\>][\>]
5964
OPERATOR_LEFT = [\<]|[\<][\&]|[\&][\<]|[\<][\<]|[\<][\<][\<]
@@ -92,10 +97,18 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
9297

9398
%{
9499
private static final Logger LOGGER = Logger.getLogger(COMFLOWFileExistence.class.getName());
95-
private String location = "MAIN PROGRAM";
96-
private String parsedFileName;
97-
100+
101+
/* MAINPROGRAM: constant for main program localisation */
102+
private static final String MAINPROGRAM = "MAIN PROGRAM";
103+
104+
String location = MAINPROGRAM;
105+
/* functionLine: the beginning line of the function */
106+
int functionLine = 0;
107+
108+
private String parsedFileName;
98109

110+
private Stack<Function> functionStack = new Stack<>();
111+
99112
List<String> filesExistence = new ArrayList<String>();
100113

101114
private String stringBeginner = "";
@@ -116,6 +129,23 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
116129
this.zzReader = new FileReader(new Path(file.getAbsolutePath()).toOSString());
117130
LOGGER.fine("end method setInputFile");
118131
}
132+
133+
private void endLocation() throws JFlexException {
134+
try{
135+
Function functionFinished = functionStack.pop();
136+
if (!functionStack.empty()) {
137+
/* there is a current function: change location to this function */
138+
location = functionStack.peek().getName();
139+
} else {
140+
/* we are in the main program: change location to main */
141+
location = MAINPROGRAM;
142+
}
143+
}catch(EmptyStackException e){
144+
final String errorMessage = e.getMessage();
145+
throw new JFlexException(this.getClass().getName(), parsedFileName,
146+
errorMessage, yytext(), yyline, yycolumn);
147+
}
148+
}
119149

120150
%}
121151

@@ -145,9 +175,10 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
145175
/************************/
146176
<NAMING>
147177
{
148-
{FNAME} {location = yytext();
178+
{FNAME} {location = yytext();
179+
functionLine = yyline+1;
149180
LOGGER.fine("["+this.parsedFileName+":"+(yyline+1)+":"+yycolumn+"] - NAMING -> YYINITIAL (Transition : VAR \""+yytext()+"\" )");
150-
yybegin(YYINITIAL);}
181+
yybegin(BEGINFUNC);}
151182
\n {
152183
LOGGER.fine("["+this.parsedFileName+":"+(yyline+1)+":"+yycolumn+"] - NAMING -> YYINITIAL (Transition : \\n )");
153184
yybegin(YYINITIAL);}
@@ -165,8 +196,28 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
165196
yybegin(NAMING);
166197
}
167198
{FUNCT} {
199+
functionLine = yyline+1;
168200
location = yytext().substring(0,yytext().length()-2).trim();
201+
yybegin(BEGINFUNC);
169202
}
203+
{FUNCSTART} {
204+
if(!functionStack.empty()){
205+
if(functionStack.peek().getFinisher().equals(Function.finisherOf(yytext()))){
206+
functionStack.peek().addStarterRepetition();
207+
}
208+
}
209+
}
210+
{FUNCEND} {
211+
if(!functionStack.empty()){
212+
if(functionStack.peek().isFinisher(yytext())){
213+
if(functionStack.peek().getStarterRepetition()>0) {
214+
functionStack.peek().removeStarterRepetition();
215+
} else {
216+
endLocation();
217+
}
218+
}
219+
}
220+
}
170221
{FILEEXIST} {
171222
int index = yytext().indexOf('-');
172223
String subfile = yytext().substring(index);
@@ -266,7 +317,28 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
266317
}
267318
. {}
268319
}
320+
/************************/
321+
/* BEGINFUNC STATE */
322+
/************************/
323+
/*
324+
* This state's target is to retrieve the function starter. For more information on fonction starter, have a look on {@link Function} class.
325+
* Pending this starter, the function ender can be defined.
326+
*
327+
*/
328+
<BEGINFUNC>
329+
{
330+
\(\) {}
331+
{FUNCSTART} {
332+
Function function;
333+
function = new Function(location, functionLine, yytext());
334+
functionStack.push(function);
335+
yybegin(YYINITIAL);
336+
}
337+
[^]|{SPACE} {}
338+
}
339+
269340

341+
270342

271343
/************************/
272344
/* ERROR STATE */

tests/fr.cnes.analysis.tools.shell.rules.test/src/fr/cnes/analysis/tools/shell/rules/COM/FLOW/FileExistence/TestCOMFLOWFileExistence.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public class TestCOMFLOWFileExistence {
3535

3636
public final static String ERROR_FILE = "error.sh";
3737
public final static String NO_ERROR_FILE = "noError.sh";
38-
public final static int[] LINES = { 8, 9, 11, 13, 15, 17, 18, 18, 20, 23, 26, 28 };
38+
public final static int[] LINES = { 8, 9, 11, 13, 15, 17, 18, 18, 20, 24, 27, 29 };
3939
public final static String[] LOCATIONS = { "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM",
40-
"MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM",
40+
"MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "my-function",
4141
"MAIN PROGRAM", "MAIN PROGRAM" };
4242
public final AbstractChecker rule = new COMFLOWFileExistence();
4343

tests/fr.cnes.analysis.tools.shell.rules.test/src/fr/cnes/analysis/tools/shell/rules/COM/FLOW/FileExistence/error.sh

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ grep ^2 $fictle > l1.tmp
1919

2020
date>$fichier
2121

22+
my-function ()
2223
if [ -e ${TAPE_DEV_REP}/${DATE_EXE}_${TYPE_ACTION}_${NIVEAU_SAUVEGARDE}_${MACHINE_S}.tar.gz ] ; then
2324
cat "${DATE_EXE}_${TYPE_ACTION}_${NIVEAU_SAUVEGARDE}_${MACHINE_S}.tar.gz" | Execute_Commande_C_Retour -d "cat - > ${TAPE_DEV_REP}/${DATE_EXE}_${TYPE_ACTION}_${NIVEAU_SAUVEGARDE}_${MACHINE_S}.tar.gz"
2425
fi

0 commit comments

Comments
 (0)