123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <stdio.h>
- #include <string.h> /* for strchr */
- #include <string>
- #include <vector>
- #include "pcrecpp.h"
- #include "pcre_stringpiece.h"
- #include "pcre_scanner.h"
- #define FLAGS_unittest_stack_size 49152
- #define CHECK_EQ(a, b) do { \
- if ( (a) != (b) ) { \
- fprintf(stderr, "%s:%d: Check failed because %s != %s\n", \
- __FILE__, __LINE__, #a, #b); \
- exit(1); \
- } \
- } while (0)
- using std::vector;
- using std::string;
- using pcrecpp::StringPiece;
- using pcrecpp::Scanner;
- static void TestScanner() {
- const char input[] = "\n"
- "alpha = 1; // this sets alpha\n"
- "bravo = 2; // bravo is set here\n"
- "gamma = 33; /* and here is gamma */\n";
- const char *re = "(\\w+) = (\\d+);";
- Scanner s(input);
- string var;
- int number;
- s.SkipCXXComments();
- s.set_save_comments(true);
- vector<StringPiece> comments;
- s.Consume(re, &var, &number);
- CHECK_EQ(var, "alpha");
- CHECK_EQ(number, 1);
- CHECK_EQ(s.LineNumber(), 3);
- s.GetNextComments(&comments);
- CHECK_EQ(comments.size(), 1);
- CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
- comments.resize(0);
- s.Consume(re, &var, &number);
- CHECK_EQ(var, "bravo");
- CHECK_EQ(number, 2);
- s.GetNextComments(&comments);
- CHECK_EQ(comments.size(), 1);
- CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
- comments.resize(0);
- s.Consume(re, &var, &number);
- CHECK_EQ(var, "gamma");
- CHECK_EQ(number, 33);
- s.GetNextComments(&comments);
- CHECK_EQ(comments.size(), 1);
- CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
- comments.resize(0);
- s.GetComments(0, sizeof(input), &comments);
- CHECK_EQ(comments.size(), 3);
- CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
- CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
- CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
- comments.resize(0);
- s.GetComments(0, (int)(strchr(input, '/') - input), &comments);
- CHECK_EQ(comments.size(), 0);
- comments.resize(0);
- s.GetComments((int)(strchr(input, '/') - input - 1), sizeof(input),
- &comments);
- CHECK_EQ(comments.size(), 3);
- CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
- CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
- CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
- comments.resize(0);
- s.GetComments((int)(strchr(input, '/') - input - 1),
- (int)(strchr(input + 1, '\n') - input + 1), &comments);
- CHECK_EQ(comments.size(), 1);
- CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
- comments.resize(0);
- }
- static void TestBigComment() {
- string input;
- for (int i = 0; i < 1024; ++i) {
- char buf[1024];
- sprintf(buf, " # Comment %d\n", i);
- input += buf;
- }
- input += "name = value;\n";
- Scanner s(input.c_str());
- s.SetSkipExpression("\\s+|#.*\n");
- string name;
- string value;
- s.Consume("(\\w+) = (\\w+);", &name, &value);
- CHECK_EQ(name, "name");
- CHECK_EQ(value, "value");
- }
- int main(int argc, char** argv) {
- (void)argc;
- (void)argv;
- TestScanner();
- TestBigComment();
-
- printf("OK\n");
- return 0;
- }
|