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)
add_subdirectory(TEST_OPERATOR)
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)
# createSourceTarget : target to be created
# generatedParserName : name of the generated parser
# baseInputFile : base of the input file (without directories)
# ARGN : extra source file, in case there is an include
function(compileLibTestGenerator createSourceTarget generatedParserName baseInputFile)
get_filename_component(baseOutputRepresentation ${baseInputFile} NAME_WE)
......@@ -19,6 +20,11 @@ function(compileLibTestGenerator createSourceTarget generatedParserName baseInpu
"${CMAKE_CURRENT_BINARY_DIR}/${generatedParserName}_to_language.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})
......@@ -49,8 +55,9 @@ endfunction(compileLibTestGenerator)
# generatedParserName : name of the generated parser
# baseInputFile : base of the input file (without directories)
# mainSource : source of the test
# ARGN : extra source file, in case there is an include
function(compileMainTestGenerator createSourceTarget generatedParserName baseInputFile mainSource)
compileLibTestGenerator("${createSourceTarget}_parser" ${generatedParserName} ${baseInputFile})
compileLibTestGenerator("${createSourceTarget}_parser" ${generatedParserName} ${baseInputFile} ${ARGN})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(${createSourceTarget} ${mainSource})
target_link_libraries(${createSourceTarget} "${createSourceTarget}_parser" phoenix_parser_stmt file_parser option_parser string_utils)
......@@ -73,8 +80,9 @@ endfunction(compileBaseTestGenerator)
# baseInputFile : base of the input file (without directories)
# mainSource : source of the 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)
compileMainTestGenerator(${createSourceTarget} ${generatedParserName} ${baseInputFile} ${mainSource})
compileMainTestGenerator(${createSourceTarget} ${generatedParserName} ${baseInputFile} ${mainSource} ${ARGN})
add_test(NAME Test_${createSourceTarget}
COMMAND ${createSourceTarget} -i ${CMAKE_CURRENT_SOURCE_DIR}/${testFile})
endfunction(compileTestGenerator)
......
......@@ -69,28 +69,35 @@ PClassToken * PGraphParser::getClassToken(const std::string & className){
bool PGraphParser::parseFile(){
if(!p_run) return false;
p_currentSource.setBaseName(eraseExtension(getFileName(p_parser->getFileName())));
if(p_parser->isEndOfFile()){return true;}
if(isMatch("include")){
std::string fileNameDirectory(getDirectory(p_parser->getFileName()));
if(!checkExprectedMatch("\"", "include")) return false;
std::string fileName(p_parser->getUntilKeyWithoutPatern("\""));
return load(fileNameDirectory + "/" + fileName);
}else if(parseClass()){}
else if(parseOperator()){}
else if(parseGraph()){}
else if(parseCharSet()){}
else if(parseVector()){}
else if(parseGlobalVar()){}
else if(parseGlobalVarUse()){}
else if(parseEnum()){}
else if(parseCommentDef()){}
else if(parseIncludeDef()){}
else{
errorAt();
stopParsing();
return false;
p_parser->skipWhiteSpace();
//To parse the file we need to read char by char until we get something we know
while(!p_parser->isEndOfFile() && p_run){ //If we are not at the end of the file
if(p_parser->isEndOfFile()){return true;}
if(isMatch("include")){
std::string fileNameDirectory(getDirectory(p_parser->getFileName()));
if(!checkExprectedMatch("(", "include")) return false;
std::string fileName(p_parser->getUntilKeyWithoutPatern(")"));
std::cerr << "PGraphParser::parseFile : load file '"<<fileName<<"'" << std::endl;
return load(fileNameDirectory + "/" + fileName);
}else if(parseClass()){}
else if(parseOperator()){}
else if(parseGraph()){}
else if(parseCharSet()){}
else if(parseVector()){}
else if(parseGlobalVar()){}
else if(parseGlobalVarUse()){}
else if(parseEnum()){}
else if(parseCommentDef()){}
else if(parseIncludeDef()){}
else{
errorAt();
stopParsing();
return false;
}
p_parser->skipWhiteSpace();
skipComment();
}
skipComment();
return true;
}
......
......@@ -10,8 +10,6 @@
#include "GraphParser/PGraphParser.h"
#include "CppBackEnd/cppBackEnd.h"
using namespace std;
///Create the OptionParser of this program
/** @return OptionParser of this program
*/
......@@ -41,7 +39,7 @@ int main(int argc, char** argv){
PGraphParser parser;
for(std::list<std::string>::iterator it(listInputFile.begin()); it != listInputFile.end(); ++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;
}
}
......@@ -49,7 +47,7 @@ int main(int argc, char** argv){
tabGraph.setName(name);
tabGraphTokenUpdate(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 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