static void findJsTags (void) { g_assert (symbols == NULL); g_assert (tags == NULL); JSNode * global = js_node_new_from_file (getInputFileName()); JSContext *my_cx; GList *calls = NULL; my_cx = js_context_new_from_node (global, &calls); findTags (my_cx); /*Members*/ get_member_list (my_cx); g_list_foreach (tags, (GFunc)makeTagEntry, NULL); findGlobal (my_cx); g_list_free (symbols); symbols = NULL; g_list_free (tags); tags = NULL; }
/* * Grab the C parser and hand it up to findObjCOrObjCppTags */ static boolean findObjCTags (const unsigned int passCount) { parserDefinition *parser = 0; if ( isHeaderFile() ) { parser = getCppParser(); } else { parser = getCParser(); } boolean ret = findTags(passCount, parser); return ret; }
static void findTags (JSContext *my_cx) { GList *i; g_assert (my_cx != NULL); if (my_cx->func_name) { const char *name = my_cx->func_name; gint line = my_cx->bline; if (name) { tagEntryInfo *tag = (tagEntryInfo*)malloc (sizeof (tagEntryInfo)); initTagEntry (tag, name); get_file_pos (line, &tag->filePosition, File.fp); tag->lineNumber = line; tag->isFileScope = 1; tag->kindName = "class"; tag->kind = 'c'; symbols = g_list_append (symbols, g_strdup (name)); if (my_cx->ret_type) tag->extensionFields.returnType = my_cx->ret_type->data; if (my_cx->func_arg) { gchar *str = NULL, *t; GList *i; for (i = my_cx->func_arg; i; i = g_list_next (i)) { g_assert (i->data != NULL); if (i == my_cx->func_arg) str = g_strdup_printf ("( %s", (gchar*)i->data); else { t = g_strdup_printf ("%s, %s", str, (gchar*)i->data); g_free (str); str = t; } } t = g_strdup_printf ("%s)", str); g_free (str); str = t; tag->extensionFields.signature = str; } tags = g_list_append (tags, tag); } } for (i = my_cx->childs; i; i = g_list_next (i)) { findTags (i->data); } }
static void findZephirTags (void) { findTags (TRUE); }
static void findPhpTags (void) { findTags (FALSE); }
int main(int argc, char *argv[]) { // change argv to strings std::vector<std::string> args; for (uint16_t i = 1; i < argc; i++) { args.push_back(argv[i]); } // parse arguments std::string name = "", tag = ""; std::vector<struct attribute> attributes; bool wantHelp = false, trim = false, keep = false, silent = false; for (uint16_t i = 0; i < args.size(); i++) { std::string arg = args.at(i); // current argument std::string option = "", parameter = ""; if (arg[0] == '-') { const int DASH_N = 2; // number of dashes // if long option if (arg.substr(0,DASH_N) == "--") { size_t eqPos = arg.find("=", DASH_N); if (arg == "--help" || arg == "--trim" || arg == "--keep" || arg == "--silent") eqPos = 0; // to prevent errors when no '=' if (eqPos == std::string::npos) { if (!silent) std::cerr << "Equal sign missing at argument " << i << std::endl; return ARGUMENTS_EXIT; } option = arg.substr(DASH_N, eqPos-DASH_N); parameter = arg.substr(eqPos+1); } else // if short option { option = arg[1]; if (i < args.size()-1) { std::string narg = args.at(i+1); // next argument parameter = narg; } } } else { continue; // argument is not an option } //std::cout << i << ": " << option << "=" << parameter << std::endl; //debug if (option == "f" || option == "file") { name = parameter; } else if (option == "h" || option == "help") { wantHelp = true; } else if (option == "t" || option == "tag") { if (parameter != "") { tag = parameter; } } else if (option == "a" || option == "attribute") { if (parameter != "") { size_t eqPos = parameter.find("="); if (eqPos == std::string::npos) { if (!silent) std::cerr << "Wrong attribute parameters at argument " << i << std::endl; return ARGUMENTS_EXIT; } std::string parameterName = parameter.substr(0, eqPos); std::string parameterValue = parameter.substr(eqPos+1); struct attribute a = { parameterName, parameterValue }; attributes.push_back(a); } } else if (option == "trim") { trim = true; } else if (option == "keep") { keep = true; } else if (option == "silent") { silent = true; } } std::string data = ""; if (name == "" && !wantHelp) { //read from stdin std::string line; while (std::getline(std::cin, line) ) { data += line + "\n"; } } // 'help' in arguments if ( (data == "" && name == "") || wantHelp) { help(argc, argv); return ARGUMENTS_EXIT; } //if file name provided if (name != "") { // open file InputFile *file = new InputFile(name); if (!file->isGood()) { if (!silent) { std::cerr << "Cannot open file " << name; std::cerr << std::endl; } delete file; return FILE_EXIT; } // get data data = file->getData(); // close file delete file; } //left trim data from spaces and tabs if (trim) { std::istringstream iss(data); data = ""; std::string line; while (getline(iss, line) ) { size_t i = 0; for (i = 0; i < line.size(); i++) { if (line[i] != ' ' && line[i] != '\t') break; } line = line.substr(i)+"\n"; data += line; } } // get tags std::vector<std::string> tags; tags = findTags(data, tag, silent); // filter by attributes std::vector<std::string> elements; elements = filterByAttributes(tags, attributes, silent); //print results for (size_t i = 0; i < elements.size(); i++) { if (keep) { std::cout << elements.at(i) << std::endl; } else { size_t beg = 0; if (tag == "") { tag = "html"; beg = elements.at(i).find("<"+tag); } size_t tagOpenEnd = elements.at(i).find(">", beg); size_t tagCloseBeg = elements.at(i).find("</"+tag, tagOpenEnd); size_t lastClose = tagCloseBeg; while (lastClose != std::string::npos) { tagCloseBeg = lastClose; lastClose = elements.at(i).find("</"+tag, lastClose+1); } if (tagOpenEnd == std::string::npos || tagCloseBeg == std::string::npos) { if (!silent) std::cerr << "Wrong element tag syntax!" << std::endl; std::cout << elements.at(i) << std::endl; continue; } std::string e = elements.at(i).substr(tagOpenEnd+1, tagCloseBeg-tagOpenEnd+1-2); std::cout << e << std::endl << std::endl; } } return GOOD_EXIT; }
/* Finds all the tags which start with the supllied tag from the supplied html. */ bool getElements(const std::u32string& html, const std::u32string& tagStartWithAttrs, std::vector<Position>& positions) { size_t tagEndLen; if(tagStartWithAttrs.length() == 0 || tagStartWithAttrs[0] != U'<') { fprintf(stderr, "invalid tagStartWithAttrs supplied: must start with <\n"); return false; } TagItems tagItems(tagStartWithAttrs); tagEndLen = tagItems.tagEnd.length(); std::map<std::u32string, std::vector<Position> > tagLocations; tagLocations[tagStartWithAttrs] = std::vector<Position>(); tagLocations[tagItems.tagStart] = std::vector<Position>(); tagLocations[tagItems.tagEnd] = std::vector<Position>(); // search all occurrences of tagStartWithAttrs, tagStart and tagEnd findTags(html, tagLocations); std::vector<Position>::iterator endItr = tagLocations[tagItems.tagEnd].begin(); std::vector<Position>::iterator startItr = tagLocations[tagItems.tagStart].begin(); if(startItr == tagLocations[tagItems.tagStart].end()) return true; std::stack<Position> stack; std::map<int, int> startTagMap; for(std::vector<Position>::iterator itr = tagLocations[tagStartWithAttrs].begin(); itr != tagLocations[tagStartWithAttrs].end(); ++itr) { startTagMap[itr->start] = itr->start; } stack.push(*startItr); ++startItr; // match start tags with end tags while(stack.size() > 0 && endItr != tagLocations[tagItems.tagEnd].end()) { // check if the end tag closes the current tag in the stack if(endItr->start > stack.top().start && (startItr == tagLocations[tagItems.tagStart].end() || endItr->start < startItr->start)) { Position pos = stack.top(); pos.len = endItr->start + tagEndLen - pos.start; ++endItr; stack.pop(); if(startTagMap.find(pos.start) != startTagMap.end()) { // matches tag with attributes positions.push_back(pos); } // only add new start tag to the stack if there are none in the stack. // we want to close the tags in the stack next if(stack.size() == 0 && startItr != tagLocations[tagItems.tagStart].end()) { stack.push(*startItr); ++startItr; } } else { // the end tag did not match the start tag, must search for an embedded element. // add a start tag if(startItr != tagLocations[tagItems.tagStart].end()) { if(startItr != tagLocations[tagItems.tagStart].end()) { stack.push(*startItr); ++startItr; } } } } return true; }