Skip to content

Commit 73b9393

Browse files
Omar Waldmannweb-flow
Omar Waldmann
authored andcommitted
Merge pull request cnescatlab#126 from brigittehuynh/3.1.0-alpha.1
3.1.0 alpha.1
2 parents 64f6f51 + cbb8f20 commit 73b9393

File tree

14 files changed

+581
-64
lines changed

14 files changed

+581
-64
lines changed

fr.cnes.analysis.tools.shell.metrics/META-INF/MANIFEST.MF

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Require-Bundle: org.eclipse.core.runtime,
99
Bundle-ActivationPolicy: lazy
1010
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
1111
Bundle-Vendor: CNES
12+
Export-Package: fr.cnes.analysis.tools.shell.metrics

fr.cnes.analysis.tools.shell.rules/META-INF/MANIFEST.MF

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Bundle-SymbolicName: fr.cnes.analysis.tools.shell.rules;singleton:=true
55
Bundle-Version: 3.0.1.qualifier
66
Bundle-Activator: fr.cnes.analysis.tools.shell.rules.Activator
77
Require-Bundle: org.eclipse.core.runtime,
8-
fr.cnes.analysis.tools.analyzer;bundle-version="2.0.0"
8+
fr.cnes.analysis.tools.analyzer;bundle-version="2.0.0",
9+
fr.cnes.analysis.tools.shell.metrics;bundle-version="3.0.1"
910
Bundle-ActivationPolicy: lazy
1011
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
1112
Bundle-Vendor: CNES

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

+161-20
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import java.io.File;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.List;
23+
import java.util.EmptyStackException;
24+
import java.util.Stack;
2325

2426
import org.eclipse.core.runtime.Path;
2527

2628
import fr.cnes.analysis.tools.analyzer.datas.AbstractChecker;
2729
import fr.cnes.analysis.tools.analyzer.datas.CheckResult;
2830
import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
31+
import fr.cnes.analysis.tools.shell.metrics.Function;
2932

3033
%%
3134

@@ -40,21 +43,44 @@ import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
4043
%type List<CheckResult>
4144

4245

43-
%state COMMENT, NAMING, WRITE, STRING, FORLOOP, READ
46+
%state COMMENT, NAMING, WRITE, STRING, FORLOOP, READ, BEGINFUNC
4447

4548
COMMENT_WORD = \#
46-
FUNC = "function"
47-
SPACE = [\ \r\t\f]
48-
VAR = [a-zA-Z][a-zA-Z0-9\_]*
49+
FUNCT = {FNAME}{SPACE}*[\(]{SPACE}*[\)]
50+
FUNCTION = "function"
51+
FNAME = [a-zA-Z0-9\.\!\-\_\@\?\+]+
52+
NAME = [a-zA-Z\_][a-zA-Z0-9\_]*
53+
SPACE = [\ \r\t\f\space]
54+
SHELL_VAR = ([0-9]+|[\-\@\?\#\!\_\*\$])
55+
EXPANDED_VAR = [\$][\{](([\:]{SPACE}*[\-])|[a-zA-Z0-9\_\:\%\=\+\?\/\!\-\,\^\#\*\@]|([\[](([\:]{SPACE}*[\-])|[a-zA-Z0-9\_\/\:\%\=\+\?\!\$\-\,\^\#\*\@\[\]\{\}])+[\]]))+[\}]
56+
VAR = {NAME}|{EXPANDED_VAR}|([\$]({NAME}|{SHELL_VAR}))
57+
58+
FUNCSTART = \{ | \( | \(\( | \[\[ | "if" | "select" | "for" | "while" | "until"
59+
FUNCEND = \} | \) | \)\) | \]\] | "fi" | "done"
60+
61+
4962
FILEEXIST = \[{SPACE}+{OPTION}{SPACE}+(\")?(\{)?\$(\{)?{VAR}(\})?(\")?
5063
OPTION = \- ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "k" |
5164
"p" | "r" | "s" | "u" | "w" | "x" | "O" | "G" | "L" |
5265
"N" | "S" | "z" | "n")
5366

5467

5568
%{
56-
String location = "MAIN PROGRAM";
57-
List<String> variables = new ArrayList<String>();
69+
/* MAINPROGRAM: constant for main program localisation */
70+
private static final String MAINPROGRAM = "MAIN PROGRAM";
71+
72+
/* FunctionWithVariables is used here with only initialized variables in locals and glabals */
73+
private Stack<FunctionWithVariables> functionStack = new Stack<>();
74+
75+
/* location: the current function name, or main program, that is the initial value */
76+
private String location = MAINPROGRAM;
77+
/* functionLine: the beginning line of the function */
78+
int functionLine = 0;
79+
80+
/* parsedFileName: name of the current file */
81+
private String parsedFileName;
82+
83+
List<String> globalVariables = new ArrayList<String>();
5884

5985
public COMDATAInitialisation() {
6086
/** Initialize list with system variables **/
@@ -64,15 +90,80 @@ OPTION = \- ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "k" |
6490
"MACHTYPE", "OLDPWD", "OSTYPE", "PATH", "PIPESTATUS", "PPID", "PROMPT_COMMAND",
6591
"PS1", "PS2", "PS3", "PS4", "PWD", "REPLY", "SECONDS", "SHELLOPTS", "SHLVL", "TMOUT",
6692
"UID" };
67-
variables.addAll(Arrays.asList(systemVariables));
93+
globalVariables.addAll(Arrays.asList(systemVariables));
6894
}
6995

7096
@Override
7197
public void setInputFile(final File file) throws FileNotFoundException {
7298
super.setInputFile(file);
99+
this.parsedFileName = file.toString();
73100
this.zzReader = new FileReader(new Path(file.getAbsolutePath()).toOSString());
74101
}
75-
102+
103+
private void endLocation() throws JFlexException {
104+
try{
105+
Function functionFinished = functionStack.pop();
106+
if (!functionStack.empty()) {
107+
/* there is a current function: change location to this function */
108+
location = functionStack.peek().getName();
109+
} else {
110+
/* we are in the main program: change location to main */
111+
location = MAINPROGRAM;
112+
}
113+
}catch(EmptyStackException e){
114+
final String errorMessage = e.getMessage();
115+
throw new JFlexException(this.getClass().getName(), parsedFileName,
116+
errorMessage, yytext(), yyline, yycolumn);
117+
}
118+
}
119+
120+
/**
121+
* checkVariable: checks for violations on the current variable name (var).
122+
* Called from YYINITIAL and STRING.
123+
*/
124+
private void checkVariable(final String var) throws JFlexException {
125+
boolean found = false;
126+
if(!functionStack.empty()){
127+
/* we are in a function */
128+
if (functionStack.peek().getLocalVariables().contains(var))
129+
found = true;
130+
if (functionStack.peek().getGlobalVariables().contains(var))
131+
found = true;
132+
}
133+
if(!found && !globalVariables.contains(var)) {
134+
setError(location,"The variable $" + var + " is used before being initialized." , yyline+1);
135+
}
136+
}
137+
138+
/**
139+
* addVariable: adds the current variable name (var) to the list of variables : glabals if
140+
* in main, locals if in funtion.
141+
* Called from YYINITIAL, WRITE, FORLOOP and READ.
142+
*/
143+
private void addVariable(final String var) throws JFlexException {
144+
if(!functionStack.empty()){
145+
/* we are in a function */
146+
functionStack.peek().getLocalVariables().add(var);
147+
} else {
148+
/* we are in main */
149+
globalVariables.add(var);
150+
}
151+
}
152+
153+
/**
154+
* setGlobals: adds the current globals to the globals of pFunction.
155+
* If there is a higher level function, its locals are also added.
156+
* Called from BEGINFUNC.
157+
*/
158+
private void setGlobals(FunctionWithVariables pFunction) throws JFlexException {
159+
if(!functionStack.empty()){
160+
/* we are in a function: add the locals of the current function as globals of the new function */
161+
pFunction.getGlobalVariables().addAll(functionStack.peek().getLocalVariables());
162+
}
163+
/* in all cases add the current globals */
164+
pFunction.getGlobalVariables().addAll(globalVariables);
165+
}
166+
76167
%}
77168

78169
%eofval{
@@ -102,9 +193,15 @@ OPTION = \- ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "k" |
102193
/************************/
103194
<NAMING>
104195
{
105-
{VAR} {location = location + yytext(); yybegin(YYINITIAL);}
106-
\n {yybegin(YYINITIAL);}
107-
. {}
196+
{VAR} {
197+
location = yytext();
198+
functionLine = yyline+1;
199+
yybegin(BEGINFUNC);
200+
}
201+
\n {
202+
yybegin(YYINITIAL);
203+
}
204+
. {}
108205
}
109206

110207
/************************/
@@ -113,18 +210,41 @@ OPTION = \- ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "k" |
113210
<YYINITIAL>
114211
{
115212
{COMMENT_WORD} {yybegin(COMMENT);}
116-
{FUNC} {location = yytext(); yybegin(NAMING);}
117-
/** variables initialisation **/
213+
{FUNCTION} {yybegin(NAMING);}
214+
{FUNCT} {
215+
functionLine = yyline+1;
216+
location = yytext().substring(0,yytext().length()-2).trim();
217+
yybegin(BEGINFUNC);
218+
}
219+
{FUNCSTART} {
220+
if(!functionStack.empty()){
221+
if(functionStack.peek().getFinisher().equals(Function.finisherOf(yytext()))){
222+
functionStack.peek().addStarterRepetition();
223+
}
224+
}
225+
}
226+
{FUNCEND} {
227+
if(!functionStack.empty()){
228+
if(functionStack.peek().isFinisher(yytext())){
229+
if(functionStack.peek().getStarterRepetition()>0) {
230+
functionStack.peek().removeStarterRepetition();
231+
} else {
232+
endLocation();
233+
}
234+
}
235+
}
236+
}
237+
/** variables initialisation **/
118238
{VAR}{SPACE}*\= {String var = yytext().substring(0,yytext().length()-1).trim();
119-
variables.add(var);}
239+
addVariable(var);}
120240
/** Varible use found **/
121241
\${VAR} {String var = yytext().substring(1);
122-
if(!variables.contains(var)) setError(location,"The variable $" + var + " is used before being initialized." , yyline+1);}
242+
checkVariable(var);}
123243
"tee" | \>\> {yybegin(WRITE);}
124244
"for" {yybegin(FORLOOP);}
125245
"read" {yybegin(READ);}
126246
{FILEEXIST} {String var = yytext().replaceAll("\"", "").replaceAll("\\{", "").replaceAll("\\}", "").split("\\$")[1];
127-
variables.add(var);}
247+
addVariable(var);}
128248
{VAR} {}
129249
\" {yybegin(STRING);}
130250
. {}
@@ -137,7 +257,7 @@ OPTION = \- ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "k" |
137257
{
138258
\-{VAR} {}
139259
\$(\{)?{VAR} {String var = yytext().substring(1).replace("{","");
140-
variables.add(var);}
260+
addVariable(var);}
141261
\n | \; {yybegin(YYINITIAL);}
142262
. {}
143263
}
@@ -147,7 +267,7 @@ OPTION = \- ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "k" |
147267
/************************/
148268
<FORLOOP>
149269
{
150-
{VAR} {variables.add(yytext()); yybegin(YYINITIAL);}
270+
{VAR} {addVariable(yytext()); yybegin(YYINITIAL);}
151271
\n | \; {yybegin(YYINITIAL);}
152272
. {}
153273
}
@@ -159,7 +279,7 @@ OPTION = \- ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "k" |
159279
{
160280
\\\$ {}
161281
\$(\{)?{VAR} {String var = yytext().substring(1).replace("{","");
162-
if(!variables.contains(var)) setError(location,"The variable $" + var + " is used before being initialized." , yyline+1);}
282+
checkVariable(var);}
163283
\n | \; | \" {yybegin(YYINITIAL);}
164284
. {}
165285
}
@@ -169,11 +289,32 @@ OPTION = \- ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "k" |
169289
/************************/
170290
<READ>
171291
{
172-
{VAR} {variables.add(yytext()); }
292+
{VAR} {addVariable(yytext()); }
173293
\n | \; {yybegin(YYINITIAL);}
174294
. {}
175295
}
176296
297+
/************************/
298+
/* BEGINFUNC STATE */
299+
/************************/
300+
/*
301+
* This state target is to retrieve the function starter. For more information on fonction starter, have a look on {@link Function} class.
302+
* Pending this starter, the function ender can be defined.
303+
*
304+
*/
305+
<BEGINFUNC>
306+
{
307+
\(\) {}
308+
{FUNCSTART} {
309+
FunctionWithVariables function;
310+
function = new FunctionWithVariables(location, functionLine, yytext());
311+
setGlobals(function);
312+
functionStack.push(function);
313+
yybegin(YYINITIAL);
314+
}
315+
[^]|{SPACE} {}
316+
}
317+
177318
178319
/************************/
179320
/* ERROR STATE */

0 commit comments

Comments
 (0)