Skip to content

Commit a858b8e

Browse files
committed
#150 Localized the COM.FLOW.FilePath rule. Test file updated accordingly
1 parent 3039b60 commit a858b8e

File tree

3 files changed

+83
-11
lines changed

3 files changed

+83
-11
lines changed

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

+77-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ import java.io.FileNotFoundException;
1818
import java.io.FileReader;
1919
import java.io.File;
2020
import java.util.List;
21+
import java.util.EmptyStackException;
22+
import java.util.Stack;
2123

2224
import org.eclipse.core.runtime.Path;
2325

2426
import fr.cnes.analysis.tools.analyzer.datas.AbstractChecker;
2527
import fr.cnes.analysis.tools.analyzer.datas.CheckResult;
2628
import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
29+
import fr.cnes.analysis.tools.shell.metrics.Function;
2730

2831
%%
2932

@@ -39,7 +42,7 @@ import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
3942
%type List<CheckResult>
4043

4144

42-
%state COMMENT, NAMING, FILE, STRING
45+
%state COMMENT, NAMING, FILE, STRING, BEGINFUNC
4346

4447
COMMENT_WORD = \#
4548
FUNCTION = "function"
@@ -81,14 +84,25 @@ REDIRECT_IGNORE = ([0-2]({OPERATOR_LEFT}|{OPERATOR_RL})) | (({OPERATOR_RIGHT}|{
8184

8285
STRING_ESCAPED = [\\]{STRING}
8386
IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
84-
87+
88+
FUNCSTART = \{ | \( | \(\( | \[\[ | "if" | "case" | "select" | "for" | "while" | "until"
89+
FUNCEND = \} | \) | \)\) | \]\] | "fi" | "esac" | "done"
90+
8591
%{
86-
String location = "MAIN PROGRAM";
92+
/* MAINPROGRAM: constant for main program localisation */
93+
private static final String MAINPROGRAM = "MAIN PROGRAM";
94+
95+
String location = MAINPROGRAM;
96+
/* functionLine: the beginning line of the function */
97+
int functionLine = 0;
98+
8799
private String parsedFileName;
88100

89101
private String stringBeginner = "";
90102
private boolean escapeNext = false;
91103

104+
private Stack<Function> functionStack = new Stack<>();
105+
92106

93107
public COMFLOWFilePath() {
94108
}
@@ -100,7 +114,24 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
100114
this.parsedFileName = file.toString();
101115
this.zzReader = new FileReader(new Path(file.getAbsolutePath()).toOSString());
102116
}
103-
117+
118+
private void endLocation() throws JFlexException {
119+
try{
120+
Function functionFinished = functionStack.pop();
121+
if (!functionStack.empty()) {
122+
/* there is a current function: change location to this function */
123+
location = functionStack.peek().getName();
124+
} else {
125+
/* we are in the main program: change location to main */
126+
location = MAINPROGRAM;
127+
}
128+
}catch(EmptyStackException e){
129+
final String errorMessage = e.getMessage();
130+
throw new JFlexException(this.getClass().getName(), parsedFileName,
131+
errorMessage, yytext(), yyline, yycolumn);
132+
}
133+
}
134+
104135
%}
105136

106137
%eofval{
@@ -130,7 +161,7 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
130161
/************************/
131162
<NAMING>
132163
{
133-
{FNAME} {location = yytext(); yybegin(YYINITIAL);}
164+
{FNAME} {location = yytext(); functionLine = yyline+1; yybegin(BEGINFUNC);}
134165
\n {yybegin(YYINITIAL);}
135166
. {}
136167
}
@@ -142,7 +173,28 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
142173
{
143174
{COMMENT_WORD} {if (!escapeNext) {yybegin(COMMENT);}}
144175
{FUNCTION} {yybegin(NAMING);}
145-
{FUNCT} {location = yytext().substring(0,yytext().length()-2).trim();}
176+
{FUNCT} {functionLine = yyline+1;
177+
location = yytext().substring(0,yytext().length()-2).trim();
178+
yybegin(BEGINFUNC);
179+
}
180+
{FUNCSTART} {
181+
if(!functionStack.empty()){
182+
if(functionStack.peek().getFinisher().equals(Function.finisherOf(yytext()))){
183+
functionStack.peek().addStarterRepetition();
184+
}
185+
}
186+
}
187+
{FUNCEND} {
188+
if(!functionStack.empty()){
189+
if(functionStack.peek().isFinisher(yytext())){
190+
if(functionStack.peek().getStarterRepetition()>0) {
191+
functionStack.peek().removeStarterRepetition();
192+
} else {
193+
endLocation();
194+
}
195+
}
196+
}
197+
}
146198
{IGNORE} {}
147199
{FILEEXIST} {
148200
int index = yytext().indexOf('-');
@@ -210,6 +262,25 @@ IGNORE = {REDIRECT_IGNORE} | {STRING_ESCAPED} | ([\\][\#]) | "ssh"
210262
[^] {escapeNext=false;}
211263
}
212264

265+
/************************/
266+
/* BEGINFUNC STATE */
267+
/************************/
268+
/*
269+
* This state target is to retrieve the function starter. For more information on fonction starter, have a look on {@link Function} class.
270+
* Pending this starter, the function ender can be defined.
271+
*
272+
*/
273+
<BEGINFUNC>
274+
{
275+
\(\) {}
276+
{FUNCSTART} {
277+
Function function;
278+
function = new Function(location, functionLine, yytext());
279+
functionStack.push(function);
280+
yybegin(YYINITIAL);
281+
}
282+
[^]|{SPACE} {}
283+
}
213284

214285
/************************/
215286
/* ERROR STATE */

tests/fr.cnes.analysis.tools.shell.rules.test/src/fr/cnes/analysis/tools/shell/rules/COM/FLOW/FilePath/TestCOMFLOWFilePath.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public class TestCOMFLOWFilePath {
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 = { 7, 8, 9, 11, 13, 15, 17, 18, 18, 20, 22, 23, 26, 28, 28};
38+
public final static int[] LINES = { 7, 8, 9, 11, 13, 15, 17, 18, 18, 20, 23, 24, 27, 29, 29};
3939
public final static String[] LOCATIONS = { "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM",
4040
"MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM",
41-
"MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM"};
41+
"my-function", "my-function", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM"};
4242
public final AbstractChecker rule = new COMFLOWFilePath();
4343

4444
/**

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ grep ^2 $fictle > l1.tmp
1919

2020
date>$fichier
2121

22-
if [ -e ${TAPE_DEV_REP}/${DATE_EXE}_${TYPE_ACTION}_${NIVEAU_SAUVEGARDE}_${MACHINE_S}.tar.gz ] ; then
23-
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"
24-
fi
22+
my-function ()
23+
if [ -e ${TAPE_DEV_REP}/${DATE_EXE}_${TYPE_ACTION}_${NIVEAU_SAUVEGARDE}_${MACHINE_S}.tar.gz ] ; then
24+
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"
25+
fi
2526

2627
grep -v "P" $ficsynthese
2728

0 commit comments

Comments
 (0)