Skip to content

Commit

Permalink
cnescatlab#122 Added violation location and string handling and updat…
Browse files Browse the repository at this point in the history
…ed unit tests

accordingly
  • Loading branch information
brigittehuynh committed Jun 25, 2018
1 parent ce1035c commit 187193f
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 15 deletions.
139 changes: 127 additions & 12 deletions fr.cnes.analysis.tools.shell.rules/lex/SHREFExport.lex
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.File;
import java.util.List;
import java.util.EmptyStackException;
import java.util.Stack;


import org.eclipse.core.runtime.Path;

import fr.cnes.analysis.tools.analyzer.datas.AbstractChecker;
import fr.cnes.analysis.tools.analyzer.datas.CheckResult;
import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
import fr.cnes.analysis.tools.shell.metrics.Function;

%%

Expand All @@ -39,22 +43,39 @@ import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
%type List<CheckResult>


%state COMMENT, NAMING
%state COMMENT, NAMING, BEGINFUNC, STRING_SIMPLE, STRING_DOUBLE

COMMENT_WORD = \#
FUNCTION = "function"
FUNCT = {VAR}{SPACE}*\(\)
SPACE = [\ \r\t\f]
VAR = [a-zA-Z][a-zA-Z0-9\_]*
STRING = \'[^\']*\' | \"[^\"]*\"
FUNCT = {FNAME}{SPACE}*[\(]{SPACE}*[\)]
FNAME = [a-zA-Z0-9\.\!\-\_\@\?\+]+
SPACE = [\ \r\t\f\space]
NAME = [a-zA-Z\_][a-zA-Z0-9\_]*
SHELL_VAR = ([0-9]+|[\-\@\?\#\!\_\*\$])
EXPANDED_VAR = [\$][\{](([\:]{SPACE}*[\-])|[a-zA-Z0-9\_\:\%\=\+\?\/\!\-\,\^\#\*\@]|([\[](([\:]{SPACE}*[\-])|[a-zA-Z0-9\_\/\:\%\=\+\?\!\$\-\,\^\#\*\@\[\]\{\}])+[\]]))+[\}]
VAR = {NAME}|{EXPANDED_VAR}|([\$]({NAME}|{SHELL_VAR}))

FUNCSTART = \{ | \( | \(\( | \[\[ | "if" | "select" | "for" | "while" | "until"
FUNCEND = \} | \) | \)\) | \]\] | "fi" | "done"

STRING_D = \"
IGNORE_STRING_D = [\\][\"]
STRING_S = \'
IGNORE_STRING_S = [\\][\']

EXPORT = "export"{SPACE}+\-"f"{SPACE}+{VAR}



%{
String location = "MAIN PROGRAM";
/* MAINPROGRAM: constant for main program localisation */
private static final String MAINPROGRAM = "MAIN PROGRAM";

String location = MAINPROGRAM;
/* functionLine: the beginning line of the function */
int functionLine = 0;

private String parsedFileName;
int length = 0;
private Stack<Function> functionStack = new Stack<>();

public SHREFExport() {

Expand All @@ -67,7 +88,24 @@ EXPORT = "export"{SPACE}+\-"f"{SPACE}+{VAR}
this.parsedFileName = file.toString();
this.zzReader = new FileReader(new Path(file.getAbsolutePath()).toOSString());
}


private void endLocation() throws JFlexException {
try{
Function functionFinished = functionStack.pop();
if (!functionStack.empty()) {
/* there is a current function: change location to this function */
location = functionStack.peek().getName();
} else {
/* we are in the main program: change location to main */
location = MAINPROGRAM;
}
}catch(EmptyStackException e){
final String errorMessage = e.getMessage();
throw new JFlexException(this.getClass().getName(), parsedFileName,
errorMessage, yytext(), yyline, yycolumn);
}
}

%}

%eofval{
Expand Down Expand Up @@ -98,7 +136,9 @@ EXPORT = "export"{SPACE}+\-"f"{SPACE}+{VAR}
/************************/
<NAMING>
{
{VAR} {location = yytext(); yybegin(YYINITIAL);}
{VAR} {location = yytext();
functionLine = yyline+1;
yybegin(BEGINFUNC);}
\n {yybegin(YYINITIAL);}
. {}
}
Expand All @@ -109,13 +149,88 @@ EXPORT = "export"{SPACE}+\-"f"{SPACE}+{VAR}
<YYINITIAL>
{
{COMMENT_WORD} {yybegin(COMMENT);}
{STRING_D} {
length+=yytext().length();
yybegin(STRING_DOUBLE);
}
{STRING_S} {
length+=yytext().length();
yybegin(STRING_SIMPLE);
}
{FUNCTION} {yybegin(NAMING);}
{FUNCT} {location = yytext().substring(0,yytext().length()-2).trim(); }
{STRING} {}
{FUNCT} {functionLine = yyline+1;
location = yytext().substring(0,yytext().length()-2).trim();
yybegin(BEGINFUNC);}
{FUNCSTART} {
if(!functionStack.empty()){
if(functionStack.peek().getFinisher().equals(Function.finisherOf(yytext()))){
functionStack.peek().addStarterRepetition();
}
}
}
{FUNCEND} {
if(!functionStack.empty()){
if(functionStack.peek().isFinisher(yytext())){
if(functionStack.peek().getStarterRepetition()>0) {
functionStack.peek().removeStarterRepetition();
} else {
endLocation();
}
}
}
}
{EXPORT} {setError(location,"It is forbidden to export functions.", yyline+1);}
[^] {}
}

/************************/
/* BEGINFUNC STATE */
/************************/
/*
* This state target is to retrieve the function starter. For more information on fonction starter, have a look on {@link Function} class.
* Pending this starter, the function ender can be defined.
*
*/
<BEGINFUNC>
{
\(\) {}
{FUNCSTART} {
Function function;
function = new Function(location, functionLine, yytext());
functionStack.push(function);
yybegin(YYINITIAL);
}
[^]|{SPACE} {}
}

/*
* The string states are designed to avoid problems due to patterns found in strings.
*/
/************************/
/* STRING_SIMPLE STATE */
/************************/
<STRING_SIMPLE>
{
{IGNORE_STRING_S} {length+=yytext().length();}
{STRING_S} {
length+=yytext().length();
yybegin(YYINITIAL);
}
[^]|{SPACE} {length+=yytext().length();}
}

/************************/
/* STRING_DOUBLE STATE */
/************************/
<STRING_DOUBLE>
{
{IGNORE_STRING_D} {length+=yytext().length();}
{STRING_D} {
length+=yytext().length();
yybegin(YYINITIAL);
}
[^]|{SPACE} {length+=yytext().length();}
}

/************************/
/* ERROR STATE */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class TestSHREFExport {

public final static String ERROR_FILE = "error.sh";
public final static String NO_ERROR_FILE = "noError.sh";
public final static int[] LINES = { 18 };
public final static String[] LOCATIONS = { "verifie_seuil_shell" };
public final static int[] LINES = { 18, 27, 30 };
public final static String[] LOCATIONS = { "MAIN PROGRAM", "testFunction","MAIN PROGRAM" };
public final AbstractChecker rule = new SHREFExport();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ export -f verifie_seuil_shell
# --- Variables locales
nom_script=$(basename $0)
echo "Variable exportee au $nom_script s'appelle MAX_SHELL_LEVEL"
echo "Founction exportee au $nom_script s'appelle verifie_seuil_shell"
echo "Founction exportee au $nom_script s'appelle verifie_seuil_shell"

function testFunction ()
{
export -f verifie_seuil_shell
}

export -f testFunction

0 comments on commit 187193f

Please # to comment.