/* * annotation.cpp * * Created on: Aug 23, 2016 * Author: gregor */ #include #include #include "annotation.h" std::string trimLeft(std::string value) { while(isspace(value[0])) { value = value.substr(1, value.length() - 1); } return value; } std::string trimRight(std::string value) { while(isspace(value[value.length() - 1])) { value = value.substr(0, value.length() - 1); } return value; } std::string trim(std::string value) { return trimLeft(trimRight(value)); } std::vector segmentString(std::string input) { std::vector result; int nestingCount = 0; size_t lastSegmentStart = 0; for(size_t i = 0; i < input.length(); i++) { if(input[i] == ',' && nestingCount == 0) { std::string segment = trim(input.substr(lastSegmentStart, i - lastSegmentStart)); if(segment != "") { result.push_back(segment); } lastSegmentStart = i + 1; } else if(input[i] == '(') { nestingCount++; } else if(input[i] == ')') { nestingCount--; } } if(lastSegmentStart < input.length() - 1) { std::string segment = trim(input.substr(lastSegmentStart, input.length() - lastSegmentStart)); if(segment != "") { result.push_back(segment); } } std::cout << result.size() << " segments" << std::endl; std::cout << "segmented string: "; for(size_t i = 0; i < result.size(); i++) { std::cout << "\"" << result[i] << "\" "; } std::cout << std::endl; return result; } void Annotation::parseAnnotation() { std::string argumentsString; auto bracketsStart = annotationString.find('('); auto bracketsEnd = annotationString.rfind(')'); if(bracketsStart != std::string::npos && bracketsEnd != std::string::npos) { argumentsString = annotationString.substr(bracketsStart + 1, bracketsEnd - bracketsStart - 1); arguments = segmentString(argumentsString); } else if(bracketsStart != std::string::npos || bracketsEnd != std::string::npos) { // TODO syntax error in argument list std::cout << "ERROR: unable to parse annotation " << annotationString; return; } annotationName = trim(annotationString.substr(0, bracketsStart)); if(annotationName == "NoReflection") { std::cout << "no reflection" << std::endl; isBuiltin = true; builtinAnnotationType = NoReflection; } else if(annotationName == "ReadProperty") { std::cout << "read property" << std::endl; isBuiltin = true; builtinAnnotationType = ReadProperty; } else if(annotationName == "WriteProperty") { std::cout << "write property" << std::endl; isBuiltin = true; builtinAnnotationType = WriteProperty; } else if(annotationName == "RWProperty") { std::cout << "read-write property" << std::endl; isBuiltin = true; builtinAnnotationType = ReadWriteProperty; } else { std::cout << "non-builtin annotation " << annotationString << std::endl; isBuiltin = false; } } void AnnotationString::parseAnnotationString() { int nestingCount = 0; size_t lastAnnotationStart = 0; std::string annotationsString = getName(); std::vector segments = segmentString(annotationsString); for(std::string segment : segments) { this->annotations.push_back(new Annotation(segment)); } }