Commit 96773a11 authored by Pierre Aubert's avatar Pierre Aubert
Browse files

Add include test

parent 4cb08f76
...@@ -16,3 +16,5 @@ add_subdirectory(TEST_VAR_DECL_STRING) ...@@ -16,3 +16,5 @@ add_subdirectory(TEST_VAR_DECL_STRING)
add_subdirectory(TEST_OPERATOR) add_subdirectory(TEST_OPERATOR)
add_subdirectory(TEST_OPERATOR_PRIORITY) add_subdirectory(TEST_OPERATOR_PRIORITY)
add_subdirectory(TEST_C_ENUM_INCLUDE)
project(Phoenix)
cmake_minimum_required(VERSION 3.0)
# compileTestGenerator(test_shadok_list "PTestParser" shadok.pparser)
# compileMainTestGenerator(test_shadok_list "PTestParser" shadok.pparser main_test.cpp)
# Only when the test will be OK
compileTestGenerator(test_c_enum_list_include "PEnumParser" c_enum_include.pdecparser main_test.cpp enum_def.c c_enum.pdecparser)
///The Curry coloration seems to be good, but it will be better to have the proper coloration for this language
///Value of the enum
Class EnumValue{
///name of the value
String name[csVariableName];
///Value of the enum value
UInt value[csUnsignedNumber];
}
Graph gEnumValue(EnumValue e){
e.name OPTIONAL{"=" e.value}
}
///Test enum definition
Class CEnum{
///Name of the enum
String name[csVariableName];
///Vector of EnumValue
Vector(EnumValue) vecValue;
}
///Parses an enum
Graph gEnum(CEnum s){
"enum" s.name "{" s.vecValue.pushBack(gEnumValue()) REPEAT{"," s.vecValue.pushBack(gEnumValue())} "}" ";"
}
///Main class
Class Source{
///Vector of CEnum
Vector(CEnum) vecEnum;
}
///Parses a complete source of CEnum
Graph gSource(Source s){
REPEAT{s.vecEnum.pushBack(gEnum())}
}
enum ShadokType{
HAUT,
BAS
};
enum AccessType{
PUBLIC,
PRIVATE,
PROTECTED
};
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
// For the option parsing
#include "OptionParser.h"
#include "string_utils.h"
// To get the generated parser
#include "PEnumParserFull.h"
using namespace std;
///Create the OptionParser of this program
/** @return OptionParser of this program
*/
OptionParser createOptionParser(){
OptionParser parser(true, __PROGRAM_VERSION__);
parser.setExampleLongOption("use_parser --input=fileName.shadok");
parser.setExampleShortOption("use_parser -i fileName.shadok");
parser.addOption("input", "i", OptionType::FILENAME, true, "input shadok file name");
return parser;
}
///Print the shadok file
/** @param fileName : file to be parsed
* @return 0 on success, -1 otherwise
*/
int printShadokFile(const std::string & fileName){
//We create the parser
PEnumParser parser;
if(!parser.load(fileName)){
cerr << "printShadokFile : can't load file '"<<fileName<<"'" << endl;
return -1;
}
std::list<std::string> listEnumName;
listEnumName.push_back("ShadokType");
listEnumName.push_back("AccessType");
std::list<std::string> listShadokAge;
listShadokAge.push_back("HAUT");
listShadokAge.push_back("BAS");
listShadokAge.push_back("PUBLIC");
listShadokAge.push_back("PRIVATE");
listShadokAge.push_back("PROTECTED");
//We get the parsed internal representation
Source & source = parser.getSourceS();
std::vector<CEnum> & vecEnum = Source_getVecEnum(source).getVecStmt();
std::string sourceStr(gSource(source));
std::cout << "printShadokFile : source converted to string = '"<<sourceStr<<"'" << std::endl;
if(vecEnum.size() == listEnumName.size()){
std::list<std::string>::iterator itName = listEnumName.begin();
std::list<std::string>::iterator itAge = listShadokAge.begin();
bool isSuccess(true);
for(std::vector<CEnum>::iterator it(vecEnum.begin()); it != vecEnum.end(); ++it){
cout << "CEnum(name = ";
if(CEnum_getName(*it) == *itName){
cout << termGreen();
}else{
cout << termRed();
isSuccess = false;
cout << "expect " << (*itName) << " but gives ";
}
cout << CEnum_getName(*it) << termDefault() << ", ";
std::vector<EnumValue> & vecValue = CEnum_getVecValue(*it).getVecStmt();
for(std::vector<EnumValue>::iterator itValue(vecValue.begin()); itValue != vecValue.end(); ++itValue){
std::string value(EnumValue_getName(*itValue));
if(value == *itAge){
cout << termGreen();
}else{
cout << termRed();
isSuccess = false;
cout << "expect " << (*itAge) << " but gives ";
}
cout << value << termDefault() << ", ";
++itAge;
}
cout << ")" << endl;
++itName;
}
if(isSuccess){return 0;}
else{return -1;}
}else{
cerr << "printShadokFile : "<<termRed()<<" error wrong number of 'enum' 2 expected but " << vecEnum.size() << " provided." << termDefault() << endl;
return -1;
}
return 0;
}
int main(int argc, char** argv){
OptionParser optionParser = createOptionParser();
optionParser.parseArgument(argc, argv);
const OptionMode & defaultMode = optionParser.getDefaultMode();
std::string fileName("");
defaultMode.getValue(fileName, "input");
return printShadokFile(fileName);
}
...@@ -5,6 +5,7 @@ set(SCRIPT_LAUNCH_TARGET ${CMAKE_SOURCE_DIR}/TESTS/scriptLauchTarget.sh.cmake) ...@@ -5,6 +5,7 @@ set(SCRIPT_LAUNCH_TARGET ${CMAKE_SOURCE_DIR}/TESTS/scriptLauchTarget.sh.cmake)
# createSourceTarget : target to be created # createSourceTarget : target to be created
# generatedParserName : name of the generated parser # generatedParserName : name of the generated parser
# baseInputFile : base of the input file (without directories) # baseInputFile : base of the input file (without directories)
# ARGN : extra source file, in case there is an include
function(compileLibTestGenerator createSourceTarget generatedParserName baseInputFile) function(compileLibTestGenerator createSourceTarget generatedParserName baseInputFile)
get_filename_component(baseOutputRepresentation ${baseInputFile} NAME_WE) get_filename_component(baseOutputRepresentation ${baseInputFile} NAME_WE)
...@@ -19,6 +20,11 @@ function(compileLibTestGenerator createSourceTarget generatedParserName baseInpu ...@@ -19,6 +20,11 @@ function(compileLibTestGenerator createSourceTarget generatedParserName baseInpu
"${CMAKE_CURRENT_BINARY_DIR}/${generatedParserName}_to_language.h" "${CMAKE_CURRENT_BINARY_DIR}/${generatedParserName}_to_language.h"
"${CMAKE_CURRENT_BINARY_DIR}/${generatedParserName}Operator.h" "${CMAKE_CURRENT_BINARY_DIR}/${generatedParserName}Operator.h"
) )
foreach(extraFile ${ARGN})
get_filename_component(baseExtraFile ${extraFile} NAME_WE)
list(APPEND outputFileCpp "${CMAKE_CURRENT_BINARY_DIR}/${baseExtraFile}.cpp")
list(APPEND outputFileH "${CMAKE_CURRENT_BINARY_DIR}/${baseExtraFile}.h")
endforeach(extraFile)
set(outputFile ${outputFileCpp} ${outputFileH}) set(outputFile ${outputFileCpp} ${outputFileH})
...@@ -49,8 +55,9 @@ endfunction(compileLibTestGenerator) ...@@ -49,8 +55,9 @@ endfunction(compileLibTestGenerator)
# generatedParserName : name of the generated parser # generatedParserName : name of the generated parser
# baseInputFile : base of the input file (without directories) # baseInputFile : base of the input file (without directories)
# mainSource : source of the test # mainSource : source of the test
# ARGN : extra source file, in case there is an include
function(compileMainTestGenerator createSourceTarget generatedParserName baseInputFile mainSource) function(compileMainTestGenerator createSourceTarget generatedParserName baseInputFile mainSource)
compileLibTestGenerator("${createSourceTarget}_parser" ${generatedParserName} ${baseInputFile}) compileLibTestGenerator("${createSourceTarget}_parser" ${generatedParserName} ${baseInputFile} ${ARGN})
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(${createSourceTarget} ${mainSource}) add_executable(${createSourceTarget} ${mainSource})
target_link_libraries(${createSourceTarget} "${createSourceTarget}_parser" phoenix_parser_stmt file_parser option_parser string_utils) target_link_libraries(${createSourceTarget} "${createSourceTarget}_parser" phoenix_parser_stmt file_parser option_parser string_utils)
...@@ -73,8 +80,9 @@ endfunction(compileBaseTestGenerator) ...@@ -73,8 +80,9 @@ endfunction(compileBaseTestGenerator)
# baseInputFile : base of the input file (without directories) # baseInputFile : base of the input file (without directories)
# mainSource : source of the test # mainSource : source of the test
# testFile : file to be used by the compiled test # testFile : file to be used by the compiled test
# ARGN : extra source file, in case there is an include
function(compileTestGenerator createSourceTarget generatedParserName baseInputFile mainSource testFile) function(compileTestGenerator createSourceTarget generatedParserName baseInputFile mainSource testFile)
compileMainTestGenerator(${createSourceTarget} ${generatedParserName} ${baseInputFile} ${mainSource}) compileMainTestGenerator(${createSourceTarget} ${generatedParserName} ${baseInputFile} ${mainSource} ${ARGN})
add_test(NAME Test_${createSourceTarget} add_test(NAME Test_${createSourceTarget}
COMMAND ${createSourceTarget} -i ${CMAKE_CURRENT_SOURCE_DIR}/${testFile}) COMMAND ${createSourceTarget} -i ${CMAKE_CURRENT_SOURCE_DIR}/${testFile})
endfunction(compileTestGenerator) endfunction(compileTestGenerator)
......
...@@ -69,28 +69,35 @@ PClassToken * PGraphParser::getClassToken(const std::string & className){ ...@@ -69,28 +69,35 @@ PClassToken * PGraphParser::getClassToken(const std::string & className){
bool PGraphParser::parseFile(){ bool PGraphParser::parseFile(){
if(!p_run) return false; if(!p_run) return false;
p_currentSource.setBaseName(eraseExtension(getFileName(p_parser->getFileName()))); p_currentSource.setBaseName(eraseExtension(getFileName(p_parser->getFileName())));
if(p_parser->isEndOfFile()){return true;}
if(isMatch("include")){ p_parser->skipWhiteSpace();
std::string fileNameDirectory(getDirectory(p_parser->getFileName())); //To parse the file we need to read char by char until we get something we know
if(!checkExprectedMatch("\"", "include")) return false; while(!p_parser->isEndOfFile() && p_run){ //If we are not at the end of the file
std::string fileName(p_parser->getUntilKeyWithoutPatern("\"")); if(p_parser->isEndOfFile()){return true;}
return load(fileNameDirectory + "/" + fileName); if(isMatch("include")){
}else if(parseClass()){} std::string fileNameDirectory(getDirectory(p_parser->getFileName()));
else if(parseOperator()){} if(!checkExprectedMatch("(", "include")) return false;
else if(parseGraph()){} std::string fileName(p_parser->getUntilKeyWithoutPatern(")"));
else if(parseCharSet()){} std::cerr << "PGraphParser::parseFile : load file '"<<fileName<<"'" << std::endl;
else if(parseVector()){} return load(fileNameDirectory + "/" + fileName);
else if(parseGlobalVar()){} }else if(parseClass()){}
else if(parseGlobalVarUse()){} else if(parseOperator()){}
else if(parseEnum()){} else if(parseGraph()){}
else if(parseCommentDef()){} else if(parseCharSet()){}
else if(parseIncludeDef()){} else if(parseVector()){}
else{ else if(parseGlobalVar()){}
errorAt(); else if(parseGlobalVarUse()){}
stopParsing(); else if(parseEnum()){}
return false; else if(parseCommentDef()){}
else if(parseIncludeDef()){}
else{
errorAt();
stopParsing();
return false;
}
p_parser->skipWhiteSpace();
skipComment();
} }
skipComment();
return true; return true;
} }
......
...@@ -10,8 +10,6 @@ ...@@ -10,8 +10,6 @@
#include "GraphParser/PGraphParser.h" #include "GraphParser/PGraphParser.h"
#include "CppBackEnd/cppBackEnd.h" #include "CppBackEnd/cppBackEnd.h"
using namespace std;
///Create the OptionParser of this program ///Create the OptionParser of this program
/** @return OptionParser of this program /** @return OptionParser of this program
*/ */
...@@ -41,7 +39,7 @@ int main(int argc, char** argv){ ...@@ -41,7 +39,7 @@ int main(int argc, char** argv){
PGraphParser parser; PGraphParser parser;
for(std::list<std::string>::iterator it(listInputFile.begin()); it != listInputFile.end(); ++it){ for(std::list<std::string>::iterator it(listInputFile.begin()); it != listInputFile.end(); ++it){
if(!parser.load(*it)){ if(!parser.load(*it)){
cerr << "Can't load file '"<<(*it)<<"'" << endl; std::cerr << "phoenix_parser_generator : Can't load file '"<<(*it)<<"'" << std::endl;
return -1; return -1;
} }
} }
...@@ -49,7 +47,7 @@ int main(int argc, char** argv){ ...@@ -49,7 +47,7 @@ int main(int argc, char** argv){
tabGraph.setName(name); tabGraph.setName(name);
tabGraphTokenUpdate(tabGraph); tabGraphTokenUpdate(tabGraph);
if(!graph_cpp_tabGraphToken(tabGraph)){ if(!graph_cpp_tabGraphToken(tabGraph)){
cerr << "Can't save the parsed PTabGraphToken" << endl; std::cerr << "phoenix_parser_generator : Can't save the parsed PTabGraphToken" << std::endl;
return -1; return -1;
} }
return 0; return 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment