Reverted indentation checks to make it easier to merge

This commit is contained in:
Marius Kintel 2017-01-23 00:41:56 -05:00
parent c132819e4b
commit 6b72971ecd

View file

@ -5,8 +5,8 @@
#include <vector>
struct GroupInfo {
std::string commentString;
int lineNo;
std::string commentString;
int lineNo;
};
typedef std::vector <GroupInfo> GroupList;
@ -17,57 +17,57 @@ typedef std::vector <GroupInfo> GroupList;
*/
static int getLineToStop( const std::string &fulltext){
int lineNo=1;
bool inString=false;
for (unsigned int i=0; i<fulltext.length(); i++) {
// increase line number
if (fulltext[i] == '\n') {
lineNo++;
continue;
}
int lineNo=1;
bool inString=false;
for (unsigned int i=0; i<fulltext.length(); i++) {
// increase line number
if (fulltext[i] == '\n') {
lineNo++;
continue;
}
// skip escaped quotes inside strings
if (inString && fulltext.compare(i, 2, "\\\"") == 0) {
i++;
continue;
}
// skip escaped quotes inside strings
if (inString && fulltext.compare(i, 2, "\\\"") == 0) {
i++;
continue;
}
//start or end of string negate the checkpoint
if (fulltext[i] == '"') {
inString = !inString;
continue;
}
//start or end of string negate the checkpoint
if (fulltext[i] == '"') {
inString = !inString;
continue;
}
if (!inString && fulltext.compare(i, 2, "//") == 0) {
i++;
while (fulltext[i] != '\n' && i<fulltext.length()) i++;
lineNo++;
continue;
}
if (!inString && fulltext.compare(i, 2, "//") == 0) {
i++;
while (fulltext[i] != '\n' && i<fulltext.length()) i++;
lineNo++;
continue;
}
//start of multi line comment if check is true
if (!inString && fulltext.compare(i, 2, "/*") == 0) {
i ++;
if(i<fulltext.length()) {
i++;
}
else {
continue;
}
// till */ every character is comment
while (fulltext.compare(i, 2, "*/") != 0 && i<fulltext.length()) {
if(fulltext[i]=='\n'){
lineNo++;
}
i++;
}
}
//start of multi line comment if check is true
if (!inString && fulltext.compare(i, 2, "/*") == 0) {
i++;
if(i<fulltext.length()) {
i++;
}
else {
continue;
}
// till */ every character is comment
while (fulltext.compare(i, 2, "*/") != 0 && i<fulltext.length()) {
if(fulltext[i]=='\n'){
lineNo++;
}
i++;
}
}
if (fulltext[i]== '{') {
return lineNo;
}
}
return lineNo;
if (fulltext[i]== '{') {
return lineNo;
}
}
return lineNo;
}
@ -77,41 +77,41 @@ static int getLineToStop( const std::string &fulltext){
*/
static std::string getComment(const std::string &fulltext, int line)
{
if (line < 1) return "";
if (line < 1) return "";
// Locate line
unsigned int start = 0;
for (; start<fulltext.length() ; start++) {
if (line <= 1) break;
if (fulltext[start] == '\n') line--;
}
// Locate line
unsigned int start = 0;
for (; start<fulltext.length() ; start++) {
if (line <= 1) break;
if (fulltext[start] == '\n') line--;
}
int end = start + 1;
while (fulltext[end] != '\n') end++;
int end = start + 1;
while (fulltext[end] != '\n') end++;
std::string comment = fulltext.substr(start, end - start);
std::string comment = fulltext.substr(start, end - start);
// Locate comment
unsigned int startText = 0;
int noOfSemicolon = 0;
bool inString = false;
for (;startText < comment.length() - 1;startText++) {
if (inString && comment.compare(startText, 2, "\\\"") == 0) {
startText++;
continue;
}
if (comment[startText] == '"') inString = !inString;
if (!inString) {
if (comment.compare(startText, 2, "//") == 0) break;
if (comment[startText] == ';' && noOfSemicolon > 0) return "";
if (comment[startText] == ';') noOfSemicolon++;
}
}
if (startText + 2 > comment.length()) return "";
// Locate comment
unsigned int startText = 0;
int noOfSemicolon = 0;
bool inString = false;
for (;startText < comment.length() - 1;startText++) {
if (inString && comment.compare(startText, 2, "\\\"") == 0) {
startText++;
continue;
}
if (comment[startText] == '"') inString = !inString;
if (!inString) {
if (comment.compare(startText, 2, "//") == 0) break;
if (comment[startText] == ';' && noOfSemicolon > 0) return "";
if (comment[startText] == ';') noOfSemicolon++;
}
}
if (startText + 2 > comment.length()) return "";
std::string result = comment.substr(startText + 2);
return result;
std::string result = comment.substr(startText + 2);
return result;
}
/*
@ -120,36 +120,36 @@ static std::string getComment(const std::string &fulltext, int line)
*/
static std::string getDescription(const std::string &fulltext, int line)
{
if (line < 1) return "";
if (line < 1) return "";
unsigned int start = 0;
for (; start<fulltext.length() ; start++) {
if (line <= 1) break;
if (fulltext[start] == '\n') line--;
}
unsigned int start = 0;
for (; start<fulltext.length() ; start++) {
if (line <= 1) break;
if (fulltext[start] == '\n') line--;
}
// not a valid description
if (fulltext.compare(start, 2, "//") != 0) return "";
// Jump over the two forward slashes
start = start+2;
//Jump over all the spaces
while (fulltext[start] == ' ' || fulltext[start] == '\t') start++;
std::string retString = "";
// go till the end of the line
while (fulltext[start] != '\n') {
// replace // with space
if (fulltext.compare(start, 2, "//") == 0) {
retString += " ";
start++;
} else {
retString += fulltext[start];
}
start++;
}
return retString;
// not a valid description
if (fulltext.compare(start, 2, "//") != 0) return "";
// Jump over the two forward slashes
start = start+2;
//Jump over all the spaces
while (fulltext[start] == ' ' || fulltext[start] == '\t') start++;
std::string retString = "";
// go till the end of the line
while (fulltext[start] != '\n') {
// replace // with space
if (fulltext.compare(start, 2, "//") == 0) {
retString += " ";
start++;
} else {
retString += fulltext[start];
}
start++;
}
return retString;
}
/*
@ -157,41 +157,42 @@ static std::string getDescription(const std::string &fulltext, int line)
*/
static GroupInfo createGroup(std::string comment,int lineNo)
{
//store info related to group
GroupInfo groupInfo;
std::string finalGroupName; //Final group name
std::string groupName; //group name
bool isGroupName = false;
for (unsigned int it = 0; it < comment.length();it++) {
//store info related to group
GroupInfo groupInfo;
std::string finalGroupName; //Final group name
std::string groupName; //group name
bool isGroupName = false;
for (unsigned int it = 0; it < comment.length();it++) {
//Start of Group Name
if (comment[it] == '[') {
isGroupName = true;
continue;
}
//Start of Group Name
if (comment[it] == '[') {
isGroupName = true;
continue;
}
//End of Group Name
if (comment[it] == ']') {
isGroupName = false;
//Setting of group name
if (!finalGroupName.empty()) {
finalGroupName = finalGroupName + "-" + groupName;
} else {
finalGroupName = finalGroupName + groupName;
}
groupName.clear();
continue;
}
//End of Group Name
if (comment[it] == ']') {
isGroupName = false;
//collect characters if it belong to group name
if (isGroupName) {
groupName += comment[it];
}
}
//Setting of group name
if (!finalGroupName.empty()) {
finalGroupName = finalGroupName + "-" + groupName;
} else {
finalGroupName = finalGroupName + groupName;
}
groupName.clear();
continue;
}
groupInfo.commentString = finalGroupName;
groupInfo.lineNo = lineNo;
return groupInfo;
//collect characters if it belong to group name
if (isGroupName) {
groupName += comment[it];
}
}
groupInfo.commentString = finalGroupName;
groupInfo.lineNo = lineNo;
return groupInfo;
}
@ -201,117 +202,117 @@ static GroupInfo createGroup(std::string comment,int lineNo)
*/
static GroupList collectGroups(const std::string &fulltext)
{
GroupList groupList; // container of all group names
int lineNo = 1; // tracks line number
bool inString = false; // check if its string or (line-) comment
GroupList groupList; // container of all group names
int lineNo = 1; // tracks line number
bool inString = false; // check if its string or (line-) comment
// iterate through whole scad file
for (unsigned int i=0; i<fulltext.length(); i++) {
// increase line number
if (fulltext[i] == '\n') {
lineNo++;
continue;
}
// iterate through whole scad file
for (unsigned int i=0; i<fulltext.length(); i++) {
// increase line number
if (fulltext[i] == '\n') {
lineNo++;
continue;
}
// skip escaped quotes inside strings
if (inString && fulltext.compare(i, 2, "\\\"") == 0) {
i++;
continue;
}
// skip escaped quotes inside strings
if (inString && fulltext.compare(i, 2, "\\\"") == 0) {
i++;
continue;
}
//start or end of string negate the checkpoint
if (fulltext[i] == '"') {
inString = !inString;
continue;
}
//start or end of string negate the checkpoint
if (fulltext[i] == '"') {
inString = !inString;
continue;
}
if (!inString && fulltext.compare(i, 2, "//") == 0) {
i++;
while (fulltext[i] != '\n' && i<fulltext.length() ) i++;
lineNo++;
continue;
}
if (!inString && fulltext.compare(i, 2, "//") == 0) {
i++;
while (fulltext[i] != '\n' && i<fulltext.length() ) i++;
lineNo++;
continue;
}
//start of multi line comment if check is true
if (!inString && fulltext.compare(i, 2, "/*") == 0) {
//store comment
std::string comment;
i++;
if(i<fulltext.length()) {
i++;
}
else {
continue;
}
bool isGroup=true;
// till */ every character is comment
while (fulltext.compare(i, 2, "*/") != 0 && i<fulltext.length()) {
if(fulltext[i]=='\n'){
lineNo++;
isGroup=false;
}
comment += fulltext[i];
i++;
}
//start of multi line comment if check is true
if (!inString && fulltext.compare(i, 2, "/*") == 0) {
//store comment
std::string comment;
i++;
if(i<fulltext.length()) {
i++;
}
else {
continue;
}
bool isGroup=true;
// till */ every character is comment
while (fulltext.compare(i, 2, "*/") != 0 && i<fulltext.length()) {
if(fulltext[i]=='\n'){
lineNo++;
isGroup=false;
}
comment += fulltext[i];
i++;
}
if(isGroup)
groupList.push_back(createGroup(comment,lineNo));
}
}
return groupList;
if(isGroup)
groupList.push_back(createGroup(comment,lineNo));
}
}
return groupList;
}
/*!
Insert Parameters in AST of given scad file
form of annotations
*/
void CommentParser::collectParameters(const char *fulltext, FileModule *root_module)
{
// Get all groups of parameters
GroupList groupList = collectGroups(std::string(fulltext));
int parseTill=getLineToStop(fulltext);
// Extract parameters for all literal assignments
for (auto &assignment : root_module->scope.assignments) {
if (!assignment.expr.get()->isLiteral()) continue; // Only consider literals
// get location of assignment node
int firstLine = assignment.location().firstLine();
if(firstLine>=parseTill ) continue;
// making list to add annotations
AnnotationList *annotationList = new AnnotationList();
// Get all groups of parameters
GroupList groupList = collectGroups(std::string(fulltext));
int parseTill=getLineToStop(fulltext);
// Extract parameters for all literal assignments
for (auto &assignment : root_module->scope.assignments) {
if (!assignment.expr.get()->isLiteral()) continue; // Only consider literals
// Extracting the parameter comment
std::string comment = getComment(std::string(fulltext), firstLine);
// getting the node for parameter annnotataion
shared_ptr<Expression> params = CommentParser::parser(comment.c_str());
if (!params) {
params = shared_ptr<Expression>(new Literal(ValuePtr(std::string(""))));
}
// get location of assignment node
int firstLine = assignment.location().firstLine();
// adding parameter to the list
annotationList->push_back(Annotation("Parameter", params));
if(firstLine>=parseTill) continue;
// making list to add annotations
AnnotationList *annotationList = new AnnotationList();
// Extracting the parameter comment
std::string comment = getComment(std::string(fulltext), firstLine);
// getting the node for parameter annnotataion
shared_ptr<Expression> params = CommentParser::parser(comment.c_str());
if (!params) {
params = shared_ptr<Expression>(new Literal(ValuePtr(std::string(""))));
}
//extracting the description
std::string descr = getDescription(std::string(fulltext), firstLine - 1);
if (descr != "") {
//creating node for description
shared_ptr<Expression> expr(new Literal(ValuePtr(std::string(descr.c_str()))));
annotationList->push_back(Annotation("Description", expr));
}
// Look for the group to which the given assignment belong
int i=0;
for (;i<groupList.size() && groupList[i].lineNo<firstLine;i++);
i--;
if (i >= 0) {
//creating node for description
shared_ptr<Expression> expr(new Literal(ValuePtr(groupList[i].commentString)));
annotationList->push_back(Annotation("Group", expr));
}
assignment.addAnnotations(annotationList);
}
// adding parameter to the list
annotationList->push_back(Annotation("Parameter", params));
//extracting the description
std::string descr = getDescription(std::string(fulltext), firstLine - 1);
if (descr != "") {
//creating node for description
shared_ptr<Expression> expr(new Literal(ValuePtr(std::string(descr.c_str()))));
annotationList->push_back(Annotation("Description", expr));
}
// Look for the group to which the given assignment belong
int i=0;
for (;i<groupList.size() && groupList[i].lineNo<firstLine;i++);
i--;
if (i >= 0) {
//creating node for description
shared_ptr<Expression> expr(new Literal(ValuePtr(groupList[i].commentString)));
annotationList->push_back(Annotation("Group", expr));
}
assignment.addAnnotations(annotationList);
}
}