|
PH Logo
Logo intepreter modeled after UCB Logo.
|
00001 /* 00002 * ListArrayParser.cpp 00003 * 00004 * Author: Paul Hamilton 00005 * Date: 7 Jul 2011 00006 * 00007 */ 00008 #include "ListArrayParser.h" 00009 00010 #include "Thing.h" 00011 #include "Array.h" 00012 #include "List.h" 00013 00014 using namespace std; 00015 00016 namespace phlogo { 00017 00018 static const char LIST_START = '['; 00019 static const char LIST_END = ']'; 00020 static const char ARRAY_START = '{'; 00021 static const char ARRAY_END = '}'; 00022 static const char *LIST_ARRAY_CHARS = "[{}],"; 00023 static const char LIST_SEP = ','; 00024 static const char SPACE = ' '; 00025 00026 void ListArrayParser::addString(tListArray *list, const std::string &s) { 00027 pThing thing(new Thing()); 00028 thing->set(s); 00029 list->push_back(thing); 00030 } 00031 00032 size_t ListArrayParser::addList(tListArray *list, const string &word, size_t pos) { 00033 pThing thing(new Thing()); 00034 List l = thing->allocList(); 00035 size_t newpos = fromWord(l._list, word, pos); 00036 list->push_back(thing); 00037 return newpos; 00038 } 00039 00040 size_t ListArrayParser::addArray(tListArray *list, const string &word, size_t pos) { 00041 pThing thing(new Thing()); 00042 Array a = thing->allocArray(0); 00043 size_t newpos = fromWord(a._array, word, pos); 00044 list->push_back(thing); 00045 return newpos; 00046 } 00047 00048 size_t ListArrayParser::fromWord(tListArray *list, const string &word, size_t pos) { 00049 00050 if (word[pos] == LIST_START || word[pos] == ARRAY_START) { 00051 pos++; 00052 size_t nextpos = word.find_first_of(LIST_ARRAY_CHARS, pos); 00053 if (word[nextpos] == ARRAY_END || word[nextpos] == LIST_END) { 00054 if (nextpos > pos) 00055 addString(list, word.substr(pos, nextpos-pos)); 00056 return nextpos+1; 00057 } 00058 else if (word[nextpos] == LIST_START) { 00059 nextpos = addList(list, word, nextpos); 00060 while (word[nextpos] == LIST_SEP) { 00061 nextpos++; 00062 if (word[nextpos] == LIST_START) 00063 nextpos = addList(list, word, nextpos); 00064 else if (word[nextpos] == ARRAY_START) 00065 nextpos = addArray(list, word, nextpos); 00066 else { 00067 pos = nextpos; 00068 nextpos = word.find_first_of(LIST_ARRAY_CHARS, pos); 00069 addString(list, word.substr(pos, nextpos-pos)); 00070 } 00071 } 00072 00073 return nextpos+1; 00074 } 00075 else if (word[nextpos] == ARRAY_START) { 00076 nextpos = addArray(list, word, nextpos); 00077 while (word[nextpos] == LIST_SEP) { 00078 nextpos++; 00079 if (word[nextpos] == LIST_START) 00080 nextpos = addList(list, word, nextpos); 00081 else if (word[nextpos] == ARRAY_START) 00082 nextpos = addArray(list, word, nextpos); 00083 else { 00084 pos = nextpos; 00085 nextpos = word.find_first_of(LIST_ARRAY_CHARS, pos); 00086 addString(list, word.substr(pos, nextpos-pos)); 00087 } 00088 } 00089 00090 return nextpos+1; 00091 } 00092 else { 00093 addString(list, word.substr(pos, nextpos-pos)); 00094 pos = nextpos+1; 00095 nextpos = word.find_first_of(LIST_ARRAY_CHARS, pos); 00096 while (word[nextpos] == LIST_SEP) { 00097 addString(list, word.substr(pos, nextpos-pos)); 00098 pos = nextpos+1; 00099 nextpos = word.find_first_of(LIST_ARRAY_CHARS, pos); 00100 } 00101 if (word[nextpos] == LIST_START) 00102 nextpos = addList(list, word, nextpos); 00103 else if (word[nextpos] == ARRAY_START) 00104 nextpos = addArray(list, word, nextpos); 00105 else 00106 addString(list, word.substr(pos, nextpos-pos)); 00107 00108 return nextpos+1; 00109 } 00110 } 00111 return string::npos; 00112 00113 } 00114 00115 string ListArrayParser::toString(const wListIter &begin, 00116 const wListIter &end) { 00117 00118 string s = ""; 00119 for (wList::const_iterator i=begin; i != end; i++) { 00120 if (s.length() != 0) 00121 s += SPACE; 00122 s += *i; 00123 } 00124 return s; 00125 00126 } 00127 00128 }
1.7.4