char *test_parser_thrashing() { glob_t test_files; unsigned int i = 0; int nparsed = 0; int delta = 0; int tests_run = 0; int execs_run = 0; int unfinished = 0; int errors = 0; int rc = glob("tests/and_suite/*", 0, NULL, &test_files); mu_assert(rc == 0, "Failed to glob file sin tests/and_suite/*"); for(i = 0; i < test_files.gl_pathc; i++) { FILE *infile = fopen(test_files.gl_pathv[i], "r"); mu_assert(infile != NULL, "Failed to open test file."); bstring data = bread((bNread)fread, infile); fclose(infile); mu_assert(data != NULL, "Failed to read test file."); tests_run++; http_parser p = setup_parser(); nparsed = 0; delta = 0; while(nparsed < blength(data)) { debug("json PARSING: %d of %d at %s", nparsed, blength(data), bdataofs(data, nparsed)); delta = http_parser_execute(&p, bdata(data), blength(data), nparsed); execs_run++; if(delta == 0) { break; } if(!http_parser_finish(&p)) { unfinished++; } nparsed += delta; if(http_parser_has_error(&p)) { errors++; } debug("TEST %s results: delta %d, has_error: %d, is_finished: %d", test_files.gl_pathv[i], nparsed, http_parser_has_error(&p), http_parser_is_finished(&p)); http_parser_init(&p); // reset for the next try } } debug("HTTP PARSING: tests_run: %d, execs_run: %d, unfinished: %d, errors: %d", tests_run, execs_run, unfinished, errors); return NULL; }
xml_data_node *xml_string_read(const char *string, xml_parse_options *opts) { xml_parse_info parse_info; int length = strlen(string); /* set up the parser */ if (!setup_parser(&parse_info, opts)) return NULL; /* parse the data */ if (XML_Parse(parse_info.parser, string, length, TRUE) == XML_STATUS_ERROR) { if (opts && opts->error) { opts->error->error_message = XML_ErrorString(XML_GetErrorCode(parse_info.parser)); opts->error->error_line = XML_GetCurrentLineNumber(parse_info.parser); opts->error->error_column = XML_GetCurrentColumnNumber(parse_info.parser); } xml_file_free(parse_info.rootnode); XML_ParserFree(parse_info.parser); return NULL; } /* free the parser */ XML_ParserFree(parse_info.parser); /* return the root node */ return parse_info.rootnode; }
Document createDocument(std::istream& stream, const ParseParameters& params, const std::string& id) { Platform xmlplat; std::string data; // Try to intelligently size the read buffer based upon the // stream length. std::streampos pos = stream.tellg(); stream.seekg(0, std::ios::end); std::streampos len = stream.tellg() - pos; if (len > 0) data.reserve(static_cast<std::string::size_type>(len)); stream.seekg(pos); data.assign(std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>()); xercesc::MemBufInputSource source(reinterpret_cast<const XMLByte *>(data.c_str()), static_cast<XMLSize_t>(data.size()), String(id)); xercesc::XercesDOMParser parser; setup_parser(parser, params); read_source(parser, source); return Document(parser.adoptDocument(), true); }
void reset_parser(bool clear_cin = 1) { delete op_stack; delete num_stack; if( clear_cin ) { while(cin.peek() != CHAR_NEWLINE) cin.get(); } setup_parser(); cout << "Enter '#' to reference the last successful computation." <<endl; }
Document createDocument(const boost::filesystem::path& file, const ParseParameters& params) { Platform xmlplat; xercesc::LocalFileInputSource source(String(file.generic_string())); xercesc::XercesDOMParser parser; setup_parser(parser, params); read_source(parser, source); return Document(parser.adoptDocument(), true); }
char *test_http11_parser_basics() { http_parser p = setup_parser(); int rc = 0; rc = http_parser_finish(&p); mu_assert(rc == 0, "Should NOT be finished if nothing parsed."); rc = http_parser_has_error(&p); mu_assert(rc == 0, "Should not have an error at the beginning."); rc = http_parser_is_finished(&p); mu_assert(rc == 0, "Should not be finished since never handed anything."); return NULL; }
int main( int argc, char* argv[] ) { setup_parser(); char new_char = 0; double new_num = 0; bool first_char = 1; while(cin.get(new_char)){ if(first_char && CHAR_NEWLINE == new_char) break; first_char=0; #if DEBUG==1 cout << "Parser Status: " << status << endl; #endif /* N.B.: cin >> double does weird things like eating operators. Do not want! */ if ( isdigit (new_char) || CHAR_DOT == new_char || CHAR_COMMA == new_char) { #if DEBUG==1 cout << "Found digit '" << new_char << "' " <<endl; #endif num_stream -> put(new_char); if ( !isdigit (cin.peek()) && cin.peek() != CHAR_COMMA && cin.peek() != CHAR_DOT ){ /* If next digit is not a number, we are clear to stop parsing and push it to the stack. */ #if DEBUG==1 cout << "last_term_num: " << last_term_num << endl; #endif if( last_term_num ) { cerr << "Two consecutive numbers with no operator in between." << endl; status = PARSER_STATUS_SYNTAX; } (*num_stream) >> new_num; num_stack -> push( new_num); check_unm = 0; last_term_num = 1; num_stream -> clear(); } }
Document createDocument(const std::string& text, const ParseParameters& params, const std::string& id) { Platform xmlplat; xercesc::MemBufInputSource source(reinterpret_cast<const XMLByte *>(text.c_str()), static_cast<XMLSize_t>(text.size()), String(id)); xercesc::XercesDOMParser parser; setup_parser(parser, params); read_source(parser, source); return Document(parser.adoptDocument(), true); }
xml_data_node *xml_file_read(mame_file *file, xml_parse_options *opts) { xml_parse_info parse_info; int done; /* set up the parser */ if (!setup_parser(&parse_info, opts)) return NULL; /* loop through the file and parse it */ do { char tempbuf[TEMP_BUFFER_SIZE]; /* read as much as we can */ int bytes = mame_fread(file, tempbuf, sizeof(tempbuf)); done = mame_feof(file); /* parse the data */ if (XML_Parse(parse_info.parser, tempbuf, bytes, done) == XML_STATUS_ERROR) { if (opts && opts->error) { opts->error->error_message = XML_ErrorString(XML_GetErrorCode(parse_info.parser)); opts->error->error_line = XML_GetCurrentLineNumber(parse_info.parser); opts->error->error_column = XML_GetCurrentColumnNumber(parse_info.parser); } xml_file_free(parse_info.rootnode); XML_ParserFree(parse_info.parser); return NULL; } } while (!done); /* free the parser */ XML_ParserFree(parse_info.parser); /* return the root node */ return parse_info.rootnode; }
int main(int argc, char **argv) { FILE *input = 0; stable_Init(1024, &my_symbols); setup_parser(); setup_walker(); if (argc > 1) { printf("input = %s\n", argv[1]); input = fopen(argv[1], "r"); } if (input) { run_parser(input); } else { push_tree("Value", 0); push_tree("Symbol", 0); push_tree("ParameterName", 1); push_tree("LetAssign", 2); stack_Dup(&the_trees); push_tree("Statement", 0); push_tree("Let", 3); stack_Dup(&the_trees); push_tree("Block", 2); } if (!the_trees.top) return 0; Node_test value = the_trees.top->value; node_Print(0, value); if (!run_walker(value)) return 1; return 0; }
int main(int argc, char **argv) { FILE *fp; char buf[4096]; size_t *chunks; size_t n_chunks; hubbub_parser *parser; uint32_t i; buf_t got = { NULL, 0, 0 }; if (argc != 2) { printf("Usage: %s <filename>\n", argv[0]); return 1; } fp = fopen(argv[1], "rb"); if (fp == NULL) { printf("Failed opening %s\n", argv[1]); return 1; } /* Format: * #chunks <n> * <n> lines * #data * <data> */ assert(fgets(buf, sizeof(buf), fp) != NULL); assert(strncmp(buf, "#chunks ", sizeof("#chunks ") - 1) == 0); n_chunks = atoi(buf + sizeof("#chunks ") - 1); chunks = malloc(n_chunks * sizeof(size_t)); assert(chunks != NULL); for (i = 0; i < n_chunks; i++) { assert(fgets(buf, sizeof(buf), fp) != NULL); chunks[i] = atoi(buf); } assert(fgets(buf, sizeof(buf), fp) != NULL); assert(strcmp(buf, "#data\n") == 0); parser = setup_parser(); for (i = 0; i < n_chunks; i++) { ssize_t bytes_read; assert(chunks[i] <= sizeof(buf)); bytes_read = fread(buf, 1, chunks[i], fp); assert((size_t)(bytes_read) == chunks[i]); assert(hubbub_parser_parse_chunk(parser, (uint8_t *) buf, chunks[i]) == HUBBUB_OK); } assert(hubbub_parser_completed(parser) == HUBBUB_OK); node_print(&got, Document, 0); printf("%s", got.buf); hubbub_parser_destroy(parser); while (Document) { node_t *victim = Document; Document = victim->next; delete_node(victim); } Document = NULL; printf("PASS\n"); fclose(fp); free(got.buf); return 0; }
char *test_parser_thrashing() { glob_t test_files; int i = 0; int nparsed = 0; int tests_run = 0; int execs_run = 0; int unfinished = 0; int errors = 0; int rc = glob("tests/client_suite/*", 0, NULL, &test_files); mu_assert(rc == 0, "Failed to glob file sin tests/client_suite/*"); for(i = 0; i < test_files.gl_pathc; i++) { debug("TESTING: %s", test_files.gl_pathv[i]); bstring data = load_test_case(test_files.gl_pathv[i]); mu_assert(data != NULL, "Failed to load test case."); tests_run++; httpclient_parser p = setup_parser(); p.data = &p; nparsed = httpclient_parser_execute(&p, bdata(data), blength(data), 0); execs_run++; if(!httpclient_parser_finish(&p)) { unfinished++; } if(httpclient_parser_has_error(&p)) { errors++; debug("TEST %s results: delta %d, has_error: %d, is_finished: %d", test_files.gl_pathv[i], nparsed, httpclient_parser_has_error(&p), httpclient_parser_is_finished(&p)); } else if(p.chunked) { int start = p.body_start; do { httpclient_parser_init(&p); nparsed = httpclient_parser_execute(&p, bdata(data), blength(data), start); mu_assert(p.body_start > start, "Didn't go past start."); mu_assert(p.chunked, "Should still be chunked."); start = p.body_start + p.content_len + 1; debug("CHUNK: length: %d, done: %d, start: %d", p.content_len, p.chunks_done, start); } while(!p.chunks_done && p.content_len > 0 && !httpclient_parser_has_error(&p)); mu_assert(p.chunks_done, "Should have chunks_done set."); mu_assert(p.content_len == 0, "Should have 0 content_len too."); } } debug("HTTP PARSING: tests_run: %d, execs_run: %d, unfinished: %d, errors: %d", tests_run, execs_run, unfinished, errors); return NULL; }
int main(int argc, char **argv) { FILE *fp; char line[2048]; bool reprocess = false; bool passed = true; hubbub_parser *parser = NULL; enum reading_state state = EXPECT_DATA; buf_t expected = { NULL, 0, 0 }; buf_t got = { NULL, 0, 0 }; if (argc != 2) { printf("Usage: %s <filename>\n", argv[0]); return 1; } fp = fopen(argv[1], "rb"); if (fp == NULL) { printf("Failed opening %s\n", argv[1]); return 1; } /* We rely on lines not being anywhere near 2048 characters... */ while (reprocess || (passed && fgets(line, sizeof line, fp) == line)) { reprocess = false; switch (state) { case ERASE_DATA: buf_clear(&got); buf_clear(&expected); hubbub_parser_destroy(parser); while (Document) { node_t *victim = Document; Document = victim->next; delete_node(victim); } Document = NULL; state = EXPECT_DATA; case EXPECT_DATA: if (strcmp(line, "#data\n") == 0) { parser = setup_parser(); state = READING_DATA; } break; case READING_DATA: case READING_DATA_AFTER_FIRST: if (strcmp(line, "#errors\n") == 0) { assert(hubbub_parser_completed(parser) == HUBBUB_OK); state = READING_ERRORS; } else { size_t len = strlen(line); if (state == READING_DATA_AFTER_FIRST) { assert(hubbub_parser_parse_chunk(parser, (uint8_t *)"\n", 1) == HUBBUB_OK); } else { state = READING_DATA_AFTER_FIRST; } printf(": %s", line); assert(hubbub_parser_parse_chunk(parser, (uint8_t *)line, len - 1) == HUBBUB_OK); } break; case READING_ERRORS: if (strcmp(line, "#document-fragment\n") == 0) { state = ERASE_DATA; reprocess = true; } if (strcmp(line, "#document\n") == 0) state = READING_TREE; else { } break; case READING_TREE: if (strcmp(line, "#data\n") == 0) { node_print(&got, Document, 0); /* Trim off the last newline */ expected.buf[strlen(expected.buf) - 1] = '\0'; passed = !strcmp(got.buf, expected.buf); if (!passed) { printf("expected:\n"); printf("%s", expected.buf); printf("got:\n"); printf("%s", got.buf); } state = ERASE_DATA; reprocess = true; } else { buf_add(&expected, line); } break; } } if (Document != NULL) { node_print(&got, Document, 0); passed = !strcmp(got.buf, expected.buf); if (!passed) { printf("expected:\n"); printf("%s", expected.buf); printf("got:\n"); printf("%s", got.buf); } hubbub_parser_destroy(parser); while (Document) { node_t *victim = Document; Document = victim->next; delete_node(victim); } } printf("%s\n", passed ? "PASS" : "FAIL"); fclose(fp); free(got.buf); free(expected.buf); return 0; }