Skip to content

Commit ac75829

Browse files
committed
cnescatlab#150 Localization of the SH.SYNC.Signals rule
1 parent 1869499 commit ac75829

File tree

2 files changed

+109
-8
lines changed

2 files changed

+109
-8
lines changed

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

+108-7
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,23 +42,38 @@ import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
3942
%type List<CheckResult>
4043

4144

42-
%state COMMENT, NAMING, TRAP
45+
%state COMMENT, NAMING, TRAP, BEGINFUNC, STRING_SIMPLE, STRING_DOUBLE
4346

4447
COMMENT_WORD = \#
4548
FUNCTION = "function"
4649
FUNCT = {FNAME}{SPACE}*[\(]{SPACE}*[\)]
4750
FNAME = [a-zA-Z0-9\.\!\-\_\@\?\+]+
4851
SPACE = [\ \r\t\f]
4952
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 = [\\][\']
5158

5259
TRAP = "trap"
5360

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

5564
%{
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+
5770
private String parsedFileName;
5871

72+
/* functionLine: the beginning line of the function */
73+
int functionLine = 0;
74+
75+
private Stack<Function> functionStack = new Stack<>();
76+
5977
public SHSYNCSignals() {
6078

6179
}
@@ -67,7 +85,25 @@ TRAP = "trap"
6785
this.parsedFileName = file.toString();
6886
this.zzReader = new FileReader(new Path(file.getAbsolutePath()).toOSString());
6987
}
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+
71107
%}
72108

73109
%eofval{
@@ -98,7 +134,7 @@ TRAP = "trap"
98134
/************************/
99135
<NAMING>
100136
{
101-
{FNAME} {location = yytext(); yybegin(YYINITIAL);}
137+
{FNAME} {location = yytext(); functionLine = yyline+1; yybegin(BEGINFUNC);}
102138
\n {yybegin(YYINITIAL);}
103139
. {}
104140
}
@@ -110,8 +146,30 @@ TRAP = "trap"
110146
{
111147
{COMMENT_WORD} {yybegin(COMMENT);}
112148
{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);}
115173
{TRAP} {yybegin(TRAP);}
116174
[^] {}
117175
}
@@ -127,7 +185,50 @@ TRAP = "trap"
127185
. {}
128186
}
129187

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+
}
130220

221+
/************************/
222+
/* STRING_DOUBLE STATE */
223+
/************************/
224+
<STRING_DOUBLE>
225+
{
226+
{IGNORE_STRING_D} {}
227+
{STRING_D} {yybegin(YYINITIAL);}
228+
[^]|{SPACE} {}
229+
}
230+
231+
131232
/************************/
132233
/* ERROR STATE */
133234
/************************/

tests/fr.cnes.analysis.tools.shell.rules.test/src/fr/cnes/analysis/tools/shell/rules/SH/SYNC/Signals/TestSHSYNCSignals.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class TestSHSYNCSignals {
3636
public final static String ERROR_FILE = "error.sh";
3737
public final static String NO_ERROR_FILE = "noError.sh";
3838
public final static int[] LINES = { 57 };
39-
public final static String[] LOCATIONS = { "nettoie" };
39+
public final static String[] LOCATIONS = { "MAIN PROGRAM" };
4040
public final AbstractChecker rule = new SHSYNCSignals();
4141

4242
/**

0 commit comments

Comments
 (0)