|
| 1 | +// |
| 2 | +// Created by gmook on 4/2/2021. |
| 3 | +// |
| 4 | + |
| 5 | +#include "JSONParser.hpp" |
| 6 | + |
| 7 | +JSONParser::JSONParser(JSONTokenizer &tokenizer): tokenizer{tokenizer} {} |
| 8 | + |
| 9 | +Pair JSONParser::parseAPair(){ |
| 10 | + // Create a instance of JSONToken called token |
| 11 | + // and populate it with a token from tokenizer |
| 12 | + // using getToken() |
| 13 | + JSONToken token = tokenizer.getToken(); |
| 14 | + // DO a check to see if the token received is of string datatype. |
| 15 | + // We know something is of string datatype because we are not reading the data |
| 16 | + // the tokenizer has already done that. Instead we are checking the tokens. Each time |
| 17 | + // a token has been made a boolean is changed alongside of its creation. For instance, when a "ID" token is found |
| 18 | + // it sets a boolean called retrieveString() to be true. Therefore, we can use these boolean flags to make checks |
| 19 | + // and further parse the data. |
| 20 | + if(!token.retrieveString()){ |
| 21 | + // If not a string then exit |
| 22 | + // Everything below is part of a error message |
| 23 | + std::cout << "EXITING: parseAPair expected string, but instead received: " << std::endl; |
| 24 | + std::cout << "-----> "; |
| 25 | + token.print(); |
| 26 | + std::cout << " <-----"; |
| 27 | + exit(9); |
| 28 | + } |
| 29 | + // When the first IF statement does not hit that means a string is found. Therefore, |
| 30 | + // create a variable of string datatype that stores the string. A function has already |
| 31 | + // been built out inside of the token class to get this string and that function is fetchQuotedString() |
| 32 | + std::string firstPartOfPair = token.fetchQuotedString(); |
| 33 | + // Get next token using getToken() |
| 34 | + token = tokenizer.getToken(); |
| 35 | + if(!token.boolCreateColonJsonToken()){ |
| 36 | + // If not a colon then exit |
| 37 | + // Everything below is part of a error message |
| 38 | + std::cout << "EXITING: parseAPair expected colon, but instead received: " << std::endl; |
| 39 | + std::cout << "-----> "; |
| 40 | + token.print(); |
| 41 | + std::cout << " <-----"; |
| 42 | + exit(9); |
| 43 | + } |
| 44 | + // Get next token using getToken() |
| 45 | + // Allows us to discard ":" since this is |
| 46 | + // not a token we have to save |
| 47 | + token = tokenizer.getToken(); |
| 48 | + |
| 49 | + // IF the token is a GPA |
| 50 | + if(token.gpaF()){ |
| 51 | + // Create a variable of double datatype set it equal |
| 52 | + // to the GPA using a function from token that returns gpa |
| 53 | + double secondPartOfPair = token.grabGPA(); |
| 54 | + // We already have the first value of the pair so we insert our |
| 55 | + // newly created double into the second value of the pair |
| 56 | + Pair pair(firstPartOfPair, secondPartOfPair); |
| 57 | + return pair; |
| 58 | + } |
| 59 | + //IF the token is a string |
| 60 | + else if(token.retrieveString()){ |
| 61 | + // Create a new variable with a datatype string |
| 62 | + // use the function from token to retrieve the string and set |
| 63 | + // it equal to that string |
| 64 | + std::string secondPartOfPair = token.fetchQuotedString(); |
| 65 | + // We already have the first value of the pair so we insert our |
| 66 | + // newly created string into the second value of the pair |
| 67 | + Pair pair(firstPartOfPair, secondPartOfPair); |
| 68 | + return pair; |
| 69 | + } |
| 70 | + else{ |
| 71 | + std::cout << "EXITING: Neither a string or double datatype were found:"; |
| 72 | + std::cout << "-----> "; |
| 73 | + token.print(); |
| 74 | + std::cout << " <----- "; |
| 75 | + exit(9); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// This ENTIRE function was given by Professor |
| 80 | +// It is used as a model for parseAPair and parseJSONEntity |
| 81 | +// However, it is pretty much replicated in order |
| 82 | +// to make parseJSONEntity because it holds the same structure |
| 83 | +EntityInstance JSONParser::parseJSONObject() { |
| 84 | + // parseJSONObject is responsible for parsing a JSON object. As such, |
| 85 | + // the first token is expected to be an open brace. |
| 86 | + JSONToken token = tokenizer.getToken(); |
| 87 | + if( ! token.boolCreateOpenCurlyJsonToken() ) { |
| 88 | + std::cout << "Error: JSONParser::parseJSONObject: Expected an open brace, but found" << std::endl; |
| 89 | + token.print(); |
| 90 | + std::cout << "Terminating..." << std::endl; |
| 91 | + exit(1); |
| 92 | + } |
| 93 | + EntityInstance instance; |
| 94 | + do { |
| 95 | + Pair pair = parseAPair(); |
| 96 | + instance.addPair(pair); |
| 97 | + token = tokenizer.getToken(); |
| 98 | + } while( token.boolCreateCommaJsonToken() ); // after having read a pair, we expect a comma |
| 99 | + // So, the above loop terminates when we did find a comma. |
| 100 | + // that means we have parsed an entire object. In that case, token must contain |
| 101 | + // the close brace of that object. |
| 102 | + if( ! token.boolCreateClosedCurlyJsonToken() ) { |
| 103 | + std::cout << "Error: JSONParser::parseJSONObject: Expected a close brace, but found" << std::endl; |
| 104 | + token.print(); |
| 105 | + std::cout << "Terminating..." << std::endl; |
| 106 | + exit(1); |
| 107 | + } |
| 108 | + |
| 109 | + //**********MATRIX CREATION AID************ |
| 110 | + //1. |
| 111 | + // Make a pair in the currentMatrix with ID and GPA |
| 112 | + // Create a variable of datatype string that stores the ID |
| 113 | + std::string existingID = instance.locateId(); |
| 114 | + //2. |
| 115 | + // Create a variable of datatype double that stores GPA |
| 116 | + double existingGPA = instance.locateGpa(); |
| 117 | + //1 & 2. |
| 118 | + // Push back a pair into the currentMatrix |
| 119 | + // std::make_pair MUST be used to create the pair of ID and GPA |
| 120 | + currentMatrix.push_back(std::make_pair(existingID, existingGPA)); |
| 121 | + //****************************************** |
| 122 | + return instance; |
| 123 | +} |
| 124 | + |
| 125 | +// Similar to parseJSONObject() |
| 126 | +EntitySet JSONParser::parseJSONEntity(){ |
| 127 | + JSONToken token = tokenizer.getToken(); |
| 128 | + if(!token.boolCreateOpenSquareBracketJsonToken()){ |
| 129 | + std::cout << "EXITING: parseJSONEntity expected open bracket, instead:" << std::endl; |
| 130 | + std::cout << "-----> "; |
| 131 | + token.print(); |
| 132 | + std::cout << " <-----"; |
| 133 | + exit(9); |
| 134 | + } |
| 135 | + EntitySet set; |
| 136 | + do{ |
| 137 | + EntityInstance instance = parseJSONObject(); |
| 138 | + set.addEntity(instance); |
| 139 | + token = tokenizer.getToken(); |
| 140 | + } while(token.boolCreateCommaJsonToken()); |
| 141 | + if(!token.boolCreateClosedSquareBracketJsonToken()){ |
| 142 | + std::cout << "EXITING: parseJSONEntity expected close bracket, instead:" << std::endl; |
| 143 | + std::cout << "-----> "; |
| 144 | + token.print(); |
| 145 | + std::cout << " <-----"; |
| 146 | + exit(9); |
| 147 | + } |
| 148 | + set.setterForMatrix(currentMatrix); |
| 149 | + return set; |
| 150 | +} |
| 151 | + |
| 152 | + |
0 commit comments