Ignore:
Timestamp:
Apr 23, 2007, 11:10:18 AM (17 years ago)
Author:
arnault
Message:

Text formatting
Sending warnings & errors to stderr
Using internally PWD for every cd/pwd
CL 327

File:
1 edited

Legend:

Unmodified
Added
Removed
  • CMT/HEAD/source/cmt_regexp.cxx

    r11 r400  
    1717
    1818static int tab_level = 0;
     19
    1920static void tab ()
    2021{
    2122  for (int i = 0; i < tab_level; i++)
    2223    {
    23       cout << "  ";
     24      cerr << "  ";
    2425    }
    2526}
     
    287288
    288289const cmt_regexp::iterator cmt_regexp_node::match (const cmt_string& /*text*/,
    289                                             int /*pos*/) const
     290                                                   int /*pos*/) const
    290291{
    291292  return (cmt_regexp::iterator::null());
     
    345346void cmt_char_node::dump () const
    346347{
    347   tab (); cout << "char>(" << this << ") c=" << _c << endl;
     348  tab (); cerr << "char>(" << this << ") c=" << _c << endl;
    348349}
    349350
     
    357358
    358359const cmt_regexp::iterator cmt_string_node::match (const cmt_string& text,
    359                                                   int pos) const
     360                                                   int pos) const
    360361{
    361362  if ((pos < 0) || (pos > text.size ()))
     
    378379void cmt_string_node::dump () const
    379380{
    380   tab (); cout << "string (" << this << ") s=[" << _s << "]" << endl;
     381  tab (); cerr << "string (" << this << ") s=[" << _s << "]" << endl;
    381382}
    382383
     
    399400      switch (c)
    400401        {
    401           case '-':
    402             i++;
    403             {
    404               char c1 = _choices[_choices.size () - 1];
    405               char c2 = list[i];
    406               int j;
    407               int j0 = (c1 < c2) ? c1 : c2;
    408               int j1 = (c1 > c2) ? c1 : c2;
    409               for (j = j0; j <= j1; j++)
    410                 {
    411                   _choices += j;
    412                 }
    413             }
    414             break;
    415           case '\\':
    416             i++;
    417             c = list[i];
    418             switch (c)
    419               {
    420                 case '[':
    421                 case ']':
    422                 case '(':
    423                 case ')':
    424                 case '.':
    425                 case '*':
    426                 case '?':
    427                 case '^':
    428                 case '$':
    429                 case '\\':
    430                   c = '\\';
    431                   break;
    432                 case 'r':
    433                   c = '\r';
    434                   break;
    435                 case 't':
    436                   c = '\t';
    437                   break;
    438                 case 'n':
    439                   c = '\n';
    440                   break;
    441                 default:
    442                   break;
    443               }
    444           default:
    445             _choices += c;
    446             break;
     402        case '-':
     403          i++;
     404          {
     405            char c1 = _choices[_choices.size () - 1];
     406            char c2 = list[i];
     407            int j;
     408            int j0 = (c1 < c2) ? c1 : c2;
     409            int j1 = (c1 > c2) ? c1 : c2;
     410            for (j = j0; j <= j1; j++)
     411              {
     412                _choices += j;
     413              }
     414          }
     415          break;
     416        case '\\':
     417          i++;
     418          c = list[i];
     419          switch (c)
     420            {
     421            case '[':
     422            case ']':
     423            case '(':
     424            case ')':
     425            case '.':
     426            case '*':
     427            case '?':
     428            case '^':
     429            case '$':
     430            case '\\':
     431              c = '\\';
     432              break;
     433            case 'r':
     434              c = '\r';
     435              break;
     436            case 't':
     437              c = '\t';
     438              break;
     439            case 'n':
     440              c = '\n';
     441              break;
     442            default:
     443              break;
     444            }
     445        default:
     446          _choices += c;
     447          break;
    447448        }
    448449    }
     
    450451
    451452const cmt_regexp::iterator cmt_char_list_node::match (const cmt_string& text,
    452                                                      int pos) const
     453                                                      int pos) const
    453454{
    454455  if ((pos < 0) || (pos > text.size ()))
     
    471472void cmt_char_list_node::dump () const
    472473{
    473   tab (); cout << "char_list (" << this << ") list=[" << _list << "] choices=[" << _choices << "]" << endl;
     474  tab (); cerr << "char_list (" << this << ") list=[" << _list << "] choices=[" << _choices << "]" << endl;
    474475}
    475476
     
    478479//----------------------------------------------------------
    479480cmt_not_char_list_node::cmt_not_char_list_node (cmt_string list) :
    480         cmt_char_list_node (list)
     481  cmt_char_list_node (list)
    481482{
    482483}
    483484
    484485const cmt_regexp::iterator cmt_not_char_list_node::match (const cmt_string& text,
    485                                                       int pos) const
     486                                                          int pos) const
    486487{
    487488  if ((pos < 0) || (pos > text.size ()))
     
    504505void cmt_not_char_list_node::dump () const
    505506{
    506   tab (); cout << "not_char_list (" << this << ") list=[" << _list << "] choices=[" << _choices << "]" << endl;
     507  tab (); cerr << "not_char_list (" << this << ") list=[" << _list << "] choices=[" << _choices << "]" << endl;
    507508}
    508509
     
    512513//----------------------------------------------------------
    513514const cmt_regexp::iterator cmt_any_node::match (const cmt_string& text,
    514                                             int pos) const
     515                                                int pos) const
    515516{
    516517  if ((pos < 0) | (pos >= text.size ()))
     
    524525void cmt_any_node::dump () const
    525526{
    526   tab (); cout << "any (" << this << ") " << endl;
     527  tab (); cerr << "any (" << this << ") " << endl;
    527528}
    528529
     
    540541
    541542const cmt_regexp::iterator cmt_zero_one::match (const cmt_string& text,
    542                                                int pos) const
     543                                                int pos) const
    543544{
    544545  if ((pos < 0) || (pos > text.size ()))
     
    564565void cmt_zero_one::dump () const
    565566{
    566   tab (); cout << "zero_one (" << this << ") " << endl;
     567  tab (); cerr << "zero_one (" << this << ") " << endl;
     568
    567569  if (_node != 0)
    568570    {
     
    602604void cmt_many_node::dump () const
    603605{
    604   tab (); cout << "many (" << this << ") " << endl;
     606  tab (); cerr << "many (" << this << ") " << endl;
     607
    605608  if (_node != 0)
    606609    {
     
    609612      tab_level--;
    610613    }
     614
    611615  tab_level++;
    612616  _follower.dump ();
     
    624628
    625629const cmt_regexp::iterator cmt_zero_more::match (const cmt_string& text,
    626                                             int pos) const
     630                                                int pos) const
    627631{
    628632  if ((pos < 0) || (pos > text.size ()))
     
    699703void cmt_zero_more::dump () const
    700704{
    701   tab (); cout << "zero_more (" << this << ") " << endl;
     705  tab (); cerr << "zero_more (" << this << ") " << endl;
     706
    702707  if (_node != 0)
    703708    {
     
    706711      tab_level--;
    707712    }
     713
    708714  _follower.dump ();
    709715}
     
    717723
    718724const cmt_regexp::iterator cmt_one_more::match (const cmt_string& text,
    719                                             int pos) const
     725                                                int pos) const
    720726{
    721727  if ((pos < 0) || (pos > text.size ()))
     
    798804void cmt_one_more::dump () const
    799805{
    800   tab (); cout << "one_more (" << this << ") " << endl;
     806  tab (); cerr << "one_more (" << this << ") " << endl;
     807
    801808  if (_node != 0)
    802809    {
     
    805812      tab_level--;
    806813    }
     814
    807815  tab_level++;
    808816  _follower.dump ();
     
    816824//----------------------------------------------------------
    817825const cmt_regexp::iterator cmt_begin_node::match (const cmt_string& /*text*/,
    818                                                  int pos) const
     826                                                  int pos) const
    819827{
    820828  if (pos == 0) return (cmt_regexp::iterator (pos, 0));
     
    824832void cmt_begin_node::dump () const
    825833{
    826   tab (); cout << "begin (" << this << ") " << endl;
     834  tab (); cerr << "begin (" << this << ") " << endl;
    827835}
    828836//----------------------------------------------------------
     
    838846void cmt_end_node::dump () const
    839847{
    840   tab (); cout << "end (" << this << ") " << endl;
     848  tab (); cerr << "end (" << this << ") " << endl;
    841849}
    842850//----------------------------------------------------------
     
    930938void cmt_regexp_node_set::dump () const
    931939{
    932   tab (); cout << "regexp_node_set (" << this << ") " << endl;
     940  tab (); cerr << "regexp_node_set (" << this << ") " << endl;
    933941}
    934942
    935943void cmt_regexp_node_set::dump (const cmt_string& title) const
    936944{
    937   tab (); cout << "Set (" << this << ") father=" << _father << " pars=" << _parentheses << endl;
     945  tab (); cerr << "Set (" << this << ") father=" << _father << " pars=" << _parentheses << endl;
     946
    938947  for (int i = 0; i < _nodes.size (); i++)
    939948    {
     
    943952          if (i > 0)
    944953            {
    945               tab (); cout << title << endl;
     954              tab (); cerr << title << endl;
    946955            }
    947956          tab_level++;
     
    950959        }
    951960    }
    952   tab (); cout << "EndSet (" << this << ")" << endl;
     961
     962  tab (); cerr << "EndSet (" << this << ")" << endl;
    953963}
    954964//----------------------------------------------------------
     
    978988 
    979989  bool dbg = CmtSystem::testenv ("CMTTESTREGEXP");
    980   if (dbg) {tab (); cout << "match and (" << this << ") pos=" << pos << endl;}
     990
     991  if (dbg) {tab (); cerr << "match and (" << this << ") pos=" << pos << endl;}
    981992
    982993  for (i = 0; i < _nodes.size (); i++)
     
    988999      if (dbg) tab_level--;
    9891000
    990       if (dbg) {tab (); cout << "  -> it(" << n << ") p=" << it._pos << " l=" << it._length << endl;}
     1001      if (dbg) {tab (); cerr << "  -> it(" << n << ") p=" << it._pos << " l=" << it._length << endl;}
    9911002     
    9921003      if (it == cmt_regexp::iterator::null ()) return (it);
     
    9961007    }
    9971008
    998     // All nodes match
     1009  // All nodes match
    9991010 
    10001011  return (cmt_regexp::iterator (pos, total));
     
    11051116 
    11061117  bool dbg = CmtSystem::testenv ("CMTTESTREGEXP");
    1107   if (dbg) {tab (); cout << "match or (" << this << ") pos=" << pos << endl;}
     1118
     1119  if (dbg) {tab (); cerr << "match or (" << this << ") pos=" << pos << endl;}
    11081120
    11091121  int i;
     
    11271139        }
    11281140
    1129         //        at least one or-ed expression matches
     1141      //        at least one or-ed expression matches
    11301142      // if (it != cmt_regexp::iterator::null ()) return (it);
    11311143    }
     
    11751187    }
    11761188
    1177     //
    1178     // The root is the cmt_or_node which will be returned. It is
    1179     // the top of the hierarchy.
    1180     //
    1181     //  top is the running cmt_and_node.
    1182     //
     1189  //
     1190  // The root is the cmt_or_node which will be returned. It is
     1191  // the top of the hierarchy.
     1192  //
     1193  //  top is the running cmt_and_node.
     1194  //
    11831195  cmt_regexp_node_set* or_root = 0;
    11841196  cmt_regexp_node_set* top_and = 0;
    11851197 
    1186     // abcd
    1187     // ab|cd
    1188     // a|b|cd
    1189     // a|b*|cd
    1190     // a|b*|cd?e
     1198  // abcd
     1199  // ab|cd
     1200  // a|b|cd
     1201  // a|b*|cd
     1202  // a|b*|cd?e
     1203  //
     1204  // exp     : and
     1205  //         | exp '|' and
     1206  //
     1207  // and     : unary
     1208  //         | unary and
     1209  //
     1210  // unary   : primary '*'
     1211  //         | primary '?'
     1212  //
     1213  // primary : '[' opt_begin opt_chars opt_end ']'
     1214  //         | '^'
     1215  //         | '$'
     1216  //         | char
     1217  //         | '(' exp ')'
     1218  //
     1219 
     1220  {
    11911221    //
    1192     // exp     : and
    1193     //         | exp '|' and
     1222    // First we build an cmt_or_node (corresponding to the
     1223    // first grammatical rule)
    11941224    //
    1195     // and     : unary
    1196     //         | unary and
     1225    //  Then cmt_and_nodes are pushed into it.
     1226    //  and standard nodes are pushed into the running (top_and) cmt_and_node
    11971227    //
    1198     // unary   : primary '*'
    1199     //         | primary '?'
    1200     //
    1201     // primary : '[' opt_begin opt_chars opt_end ']'
    1202     //         | '^'
    1203     //         | '$'
    1204     //         | char
    1205     //         | '(' exp ')'
    1206     //
    1207  
    1208   {
    1209       //
    1210       // First we build an cmt_or_node (corresponding to the
    1211       // first grammatical rule)
    1212       //
    1213       //  Then cmt_and_nodes are pushed into it.
    1214       //  and standard nodes are pushed into the running (top_and) cmt_and_node
    1215       //
    12161228    or_root = new cmt_or_node (0);
    12171229    top_and = new cmt_and_node (or_root);
     
    12251237      switch (c)
    12261238        {
    1227           case '[':
     1239        case '[':
    12281240          {
    1229               //
    1230               // The case is
    1231               //
    1232               //  exp   : '['     char ... ']'
    1233               //  exp   : '[' '^' char ... ']'
    1234               //
     1241            //
     1242            // The case is
     1243            //
     1244            //  exp   : '['     char ... ']'
     1245            //  exp   : '[' '^' char ... ']'
     1246            //
    12351247
    12361248            if (i >= expression.size ())
    12371249              {
    1238                   // syntax error : unbalanced '['
     1250                // syntax error : unbalanced '['
    12391251                delete or_root;
    12401252                return;
     
    12541266                switch (c)
    12551267                  {
    1256                     case ']':
    1257                       done = true;
    1258                       break;
    1259                     case '^':
    1260                       if (i == i0) has_not = true;
    1261                       else choices += c;
    1262                       break;
    1263                     case '\\':
    1264                       choices += c;
    1265                       if (i >= expression.size ())
    1266                         {
    1267                             // syntax error : unbalanced '[' and unfinished
    1268                             // escape sequence
    1269                           delete or_root;
    1270                           return;
    1271                         }
    1272                       i++;
    1273                       c = expression[i];
    1274                       choices += c;
    1275                       break;
    1276                     default:
    1277                       choices += c;
    1278                       break;
     1268                  case ']':
     1269                    done = true;
     1270                    break;
     1271                  case '^':
     1272                    if (i == i0) has_not = true;
     1273                    else choices += c;
     1274                    break;
     1275                  case '\\':
     1276                    choices += c;
     1277                    if (i >= expression.size ())
     1278                      {
     1279                        // syntax error : unbalanced '[' and unfinished
     1280                        // escape sequence
     1281                        delete or_root;
     1282                        return;
     1283                      }
     1284                    i++;
     1285                    c = expression[i];
     1286                    choices += c;
     1287                    break;
     1288                  default:
     1289                    choices += c;
     1290                    break;
    12791291                  }
    12801292                if (done) break;
     
    12831295            if (!done)
    12841296              {
    1285                   // syntax error : unbalanced '['
     1297                // syntax error : unbalanced '['
    12861298                delete or_root;
    12871299                return;
     
    12931305          }
    12941306          break;
    1295           case '*':
     1307        case '*':
    12961308          {
    1297               //
    1298               //  exp : exp '*'
    1299               //
     1309            //
     1310            //  exp : exp '*'
     1311            //
    13001312            if (top_and->nodes () == 0)
    13011313              {
    1302                   // Syntax error : '*' is not preceded by an expression
     1314                // Syntax error : '*' is not preceded by an expression
    13031315                delete or_root;
    13041316                return;
     
    13091321          }
    13101322          break;
    1311           case '+':
     1323        case '+':
    13121324          {
    1313               //
    1314               //  exp : exp '+'
    1315               //
     1325            //
     1326            //  exp : exp '+'
     1327            //
    13161328            if (top_and->nodes () == 0)
    13171329              {
    1318                   // Syntax error : '+' is not preceded by an expression
     1330                // Syntax error : '+' is not preceded by an expression
    13191331                delete or_root;
    13201332                return;
     
    13251337          }
    13261338          break;
    1327           case '?':
     1339        case '?':
    13281340          {
    1329               //
    1330               //  exp : exp '?'
    1331               //
     1341            //
     1342            //  exp : exp '?'
     1343            //
    13321344            if (top_and->nodes () == 0)
    13331345              {
    1334                   // Syntax error : '?' is not preceded by an expression
     1346                // Syntax error : '?' is not preceded by an expression
    13351347                delete or_root;
    13361348                return;
     
    13411353          }
    13421354          break;
    1343           case '.':
    1344               //
    1345               //  exp : '.'
    1346               //
    1347             top_and->push (new cmt_any_node ());
    1348             break;
    1349           case '(':
     1355        case '.':
     1356          //
     1357          //  exp : '.'
     1358          //
     1359          top_and->push (new cmt_any_node ());
     1360          break;
     1361        case '(':
    13501362          {
    1351               //
    1352               //  exp : '(' exp ')'
    1353               //
     1363            //
     1364            //  exp : '(' exp ')'
     1365            //
    13541366            if (top_and->parentheses ())
    13551367              {
    1356                   // This should never happen.
     1368                // This should never happen.
    13571369                delete or_root;
    13581370                return;
     
    13611373            top_and->set_parentheses (true);
    13621374           
    1363               //
    1364               // A new complete expression is started.
    1365               //  -> do as for top_and parsing.
    1366               //
     1375            //
     1376            // A new complete expression is started.
     1377            //  -> do as for top_and parsing.
     1378            //
    13671379           
    13681380            top_and = new cmt_and_node (new cmt_or_node (top_and));
    13691381          }
    13701382          break;
    1371           case ')':
     1383        case ')':
    13721384          {
    1373               //
    1374               //  exp : '(' exp ')'
    1375               //
    1376            
    1377               // top_and is the cmt_and_node into which new nodes are pushed.
     1385            //
     1386            //  exp : '(' exp ')'
     1387            //
     1388           
     1389            // top_and is the cmt_and_node into which new nodes are pushed.
    13781390            cmt_regexp_node_set* or_node = top_and->father ();
    13791391            if (or_node == 0)
    13801392              {
    1381                   // This should never happen : top_and should always be
    1382                   // at least an cmt_and_node hanging at an cmt_or_node
     1393                // This should never happen : top_and should always be
     1394                // at least an cmt_and_node hanging at an cmt_or_node
    13831395                delete or_root;
    13841396                return;
    13851397              }
    13861398           
    1387               //
    1388               // The last cmt_and_node was empty, thus we had either '()' or '(...|)'
    1389               //
     1399            //
     1400            // The last cmt_and_node was empty, thus we had either '()' or '(...|)'
     1401            //
    13901402           
    13911403            if (top_and->nodes () == 0)
     
    14021414            if (top_and == 0)
    14031415              {
    1404                   // Syntax error : too many ')'
     1416                // Syntax error : too many ')'
    14051417                delete or_root;
    14061418                return;
    14071419              }
    14081420           
    1409               //
    1410               // top_and is now the previous running cmt_and_node where the '('
    1411               // was originally met its top_and node contains the parenthesized
    1412               // sub expression  If this one is empty, (due to an empty '()'
    1413               // expression) then it may simply be discarded.
    1414               //
     1421            //
     1422            // top_and is now the previous running cmt_and_node where the '('
     1423            // was originally met its top_and node contains the parenthesized
     1424            // sub expression  If this one is empty, (due to an empty '()'
     1425            // expression) then it may simply be discarded.
     1426            //
    14151427           
    14161428            if (!top_and->parentheses ())
    14171429              {
    1418                   // Syntax error : too many ')'
     1430                // Syntax error : too many ')'
    14191431                delete or_root;
    14201432                return;
     
    14431455         
    14441456          break;
    1445           case '|':
     1457        case '|':
    14461458          {
    1447               //
    1448               //  exp : exp '|' exp
    1449               //
     1459            //
     1460            //  exp : exp '|' exp
     1461            //
    14501462
    14511463            cmt_regexp_node_set* or_node = top_and->father ();
     
    14531465            top_and->reduce ();
    14541466           
    1455               //
    1456               // or is the father cmt_or_node, which only contains cmt_and_nodes
    1457               //
     1467            //
     1468            // or is the father cmt_or_node, which only contains cmt_and_nodes
     1469            //
    14581470           
    14591471            const cmt_regexp_node_set* and_node = (cmt_regexp_node_set*) or_node->top ();
    14601472            if (and_node->nodes () == 0)
    14611473              {
    1462                   // the previous node was empty.
    1463                   // we may discard it
     1474                // the previous node was empty.
     1475                // we may discard it
    14641476                or_node->pop ();
    14651477              }
     
    14681480          }
    14691481          break;
    1470           case '^':
    1471               //
    1472               //  exp : '^'
    1473               //
    1474             top_and->push (new cmt_begin_node ());
    1475             break;
    1476           case '$':
    1477               //
    1478               //  exp : '$'
    1479               //
    1480             top_and->push (new cmt_end_node ());
    1481             break;
    1482           case '\\':
    1483             if (i >= expression.size ())
    1484               {
    1485                 delete or_root;
    1486                 return;
    1487               }
    1488             i++;
    1489             c = expression[i];
    1490             switch (c)
    1491               {
    1492                 case '[':
    1493                 case ']':
    1494                 case '(':
    1495                 case ')':
    1496                 case '.':
    1497                 case '*':
    1498                 case '?':
    1499                 case '^':
    1500                 case '$':
    1501                 case '\\':
    1502                   break;
    1503                 case 'r':
    1504                   c = '\r';
    1505                   break;
    1506                 case 't':
    1507                   c = '\t';
    1508                   break;
    1509                 case 'n':
    1510                   c = '\n';
    1511                   break;
    1512                 default:
    1513                   break;
    1514               }
    1515           default:
    1516             top_and->push (new cmt_char_node (c));
    1517             break;
     1482        case '^':
     1483          //
     1484          //  exp : '^'
     1485          //
     1486          top_and->push (new cmt_begin_node ());
     1487          break;
     1488        case '$':
     1489          //
     1490          //  exp : '$'
     1491          //
     1492          top_and->push (new cmt_end_node ());
     1493          break;
     1494        case '\\':
     1495          if (i >= expression.size ())
     1496            {
     1497              delete or_root;
     1498              return;
     1499            }
     1500          i++;
     1501          c = expression[i];
     1502          switch (c)
     1503            {
     1504            case '[':
     1505            case ']':
     1506            case '(':
     1507            case ')':
     1508            case '.':
     1509            case '*':
     1510            case '?':
     1511            case '^':
     1512            case '$':
     1513            case '\\':
     1514              break;
     1515            case 'r':
     1516              c = '\r';
     1517              break;
     1518            case 't':
     1519              c = '\t';
     1520              break;
     1521            case 'n':
     1522              c = '\n';
     1523              break;
     1524            default:
     1525              break;
     1526            }
     1527        default:
     1528          top_and->push (new cmt_char_node (c));
     1529          break;
    15181530        }
    15191531    }
     
    15251537      if (or_root->nodes () == 1)
    15261538        {
    1527             //
    1528             // Check whether there is at least one non-empty
    1529             // cmt_and_node
    1530             //
     1539          //
     1540          // Check whether there is at least one non-empty
     1541          // cmt_and_node
     1542          //
    15311543          if (and_node->nodes () == 0)
    15321544            {
Note: See TracChangeset for help on using the changeset viewer.