Skip to content

Commit

Permalink
cnescatlab#49 cnescatlab#117 cnescatlab#138 Added COM.INST.BoolNegati…
Browse files Browse the repository at this point in the history
…on rule, with localization

(including case-esac). Tests updated accordingly.
  • Loading branch information
brigittehuynh committed Jul 22, 2018
1 parent 2bca723 commit 4bbd166
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
107 changes: 101 additions & 6 deletions fr.cnes.analysis.tools.shell.rules/lex/COMINSTBoolNegation.lex
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ 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,27 +42,42 @@ import fr.cnes.analysis.tools.analyzer.exception.JFlexException;
%type List<CheckResult>


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

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

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

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

NOT = \!
OPER = \&\& | \|\| | \-"o" | \-"a"



%{
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;
/** Bool to knkow if there are open brackets **/
int bracket = 0, brace = 0, parenth = 0;

private Stack<Function> functionStack = new Stack<>();

public COMINSTBoolNegation() {
}

Expand All @@ -71,6 +89,22 @@ OPER = \&\& | \|\| | \-"o" | \-"a"
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);
}
}

%}

Expand Down Expand Up @@ -101,7 +135,7 @@ OPER = \&\& | \|\| | \-"o" | \-"a"
/************************/
<NAMING>
{
{FNAME} {location = yytext(); yybegin(YYINITIAL);}
{FNAME} {location = yytext(); yybegin(BEGINFUNC);}
\n {yybegin(YYINITIAL);}
. {}
}
Expand All @@ -112,10 +146,29 @@ OPER = \&\& | \|\| | \-"o" | \-"a"
<YYINITIAL>
{
{COMMENT_WORD} {yybegin(COMMENT);}
{STRING_D} {yybegin(STRING_DOUBLE);}
{STRING_S} {yybegin(STRING_SIMPLE);}
{FUNCTION} {yybegin(NAMING);}
{FUNCT} {location = yytext().substring(0,yytext().length()-2).trim();}
{FUNCT} {location = yytext().substring(0,yytext().length()-2).trim(); yybegin(BEGINFUNC);}
{NOT} {bracket=0; brace=0; parenth=0; yybegin(LOGICAL);}
{STRING} {}
{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();
}
}
}
}
[^] {}
}

Expand All @@ -136,6 +189,48 @@ OPER = \&\& | \|\| | \-"o" | \-"a"
. {}
}

/************************/
/* 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} {}
{STRING_S} {yybegin(YYINITIAL);}
[^]|{SPACE} {}
}

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

/************************/
/* ERROR STATE */
Expand Down
5 changes: 5 additions & 0 deletions fr.cnes.analysis.tools.shell.rules/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
id="fr.cnes.analysis.tools.shell.rules.COMFLOWRecursion"
name="COM.FLOW.Recursion" languageId="fr.cnes.analysis.tools.languages.shell">
</check>
<check
class="fr.cnes.analysis.tools.shell.rules.COMINSTBoolNegation"
id="fr.cnes.analysis.tools.shell.rules.COMINSTBoolNegation"
name="COM.INST.BoolNegation" languageId="fr.cnes.analysis.tools.languages.shell">
</check>
<check
class="fr.cnes.analysis.tools.shell.rules.COMINSTCodeComment"
id="fr.cnes.analysis.tools.shell.rules.COMINSTCodeComment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class TestCOMINSTBoolNegation {
public final static String NO_ERROR_FILE = "noError.sh";
public final static int[] LINES = { 9, 13, 17, 22, 27, 33, 40, 46 };
public final static String[] LOCATIONS = { "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM", "MAIN PROGRAM",
"MAIN PROGRAM", "test1", "test", "test" };
"MAIN PROGRAM", "test1", "test", "MAIN PROGRAM" };
public final AbstractChecker rule = new COMINSTBoolNegation();

/**
Expand Down

0 comments on commit 4bbd166

Please # to comment.