huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "UserTestTraits.hpp"
#include "t009lexer.hpp"
 
#include <sys/types.h>
 
#include <iostream>
#include <sstream>
#include <fstream>
 
using namespace Antlr3Test;
using namespace std;
 
int testValid(string const& data);
int testMalformedInput(string const& data);
 
static t009lexer *lxr;
 
struct TokenData
{
   t009lexerTokens::Tokens type;
   unsigned start;
   unsigned stop;
   const char* text;
};
 
static TokenData ExpectedTokens[] =
{
   // "085"
   { t009lexerTokens::DIGIT, 0, 0, "0"},
   { t009lexerTokens::DIGIT, 1, 1, "8"},
   { t009lexerTokens::DIGIT, 2, 2, "5"},
   { t009lexerTokens::EOF_TOKEN, 3, 3, "<EOF>"}
};
 
int main (int argc, char *argv[])
{
   testValid("085"); 
   testMalformedInput("2a");
   return 0;
}
 
int testValid(string const& data)
{
   t009lexerTraits::InputStreamType* input    = new t009lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
                                              ANTLR_ENC_8BIT,
                                              data.length(), //strlen(data.c_str()),
                                              (ANTLR_UINT8*)"t009");
   if (lxr == NULL)
       lxr = new t009lexer(input);
   else
       lxr->setCharStream(input);
 
   std::cout << "testValid: \"" << data << '"' <<std::endl;
 
   std::cout << "Text:"  << '\t'
         << "Type:"  << '\t'
         << "Start:" << '\t'
         << "Stop:"  << '\t'
         << "Text:"  << '\t' << std::endl;
   
   for(unsigned i = 0; i < sizeof(ExpectedTokens)/sizeof(TokenData) ; i++)
   {
       // nextToken does not allocate any new Token instance(the same instance is returned again and again)
       t009lexerTraits::CommonTokenType *token = lxr->nextToken();
 
       size_t startIndex = ((const char*)token->get_startIndex()) - data.c_str();
       size_t stopIndex = ((const char*)token->get_stopIndex()) - data.c_str();
 
       std::cout << token->getText()
             << '\t' << (token->getType()       == ExpectedTokens[i].type ?  "OK" : "Fail")
             << '\t' << (startIndex == ExpectedTokens[i].start ? "OK" : "Fail")
             << '\t' << (stopIndex  == ExpectedTokens[i].stop ?  "OK" : "Fail")
             << '\t' << (token->getText()       == ExpectedTokens[i].text ?  "OK" : "Fail")
             << std::endl;
       
   }
   delete lxr; lxr = NULL;
   delete input; 
   return 0;
}
 
int testMalformedInput(string const& data)
{
   t009lexerTraits::InputStreamType* input    = new t009lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
                                              ANTLR_ENC_8BIT,
                                              data.length(), //strlen(data.c_str()),
                                              (ANTLR_UINT8*)"t009");
   if (lxr == NULL)
       lxr = new t009lexer(input);
   else
       lxr->setCharStream(input);
 
   std::cout << "testMalformedInput: \"" << data << '"' <<std::endl;
   
   t009lexerTraits::CommonTokenType *token;
   token = lxr->nextToken();
   std::cout << token->getText() << std::endl;
   token = lxr->nextToken();
   std::cout << token->getText() << std::endl;
 
   //except antlr3.MismatchedSetException as exc:
   //   # TODO: This should provide more useful information
   //   self.assertIsNone(exc.expecting)
   //   self.assertEqual(exc.unexpectedType, 'a')
   //   self.assertEqual(exc.charPositionInLine, 1)
   //   self.assertEqual(exc.line, 1)
 
   delete lxr; lxr = NULL;
   delete input; 
   return 0;
}