"Fossies" - the Fresh Open Source Software Archive

Member "OpenSP-1.5.2/lib/ParserApp.cxx" (21 Jul 2005, 11319 Bytes) of package /linux/misc/old/OpenSP-1.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ParserApp.cxx" see the Fossies "Dox" file reference documentation.

    1 // Copyright (c) 1996 James Clark
    2 // See the file COPYING for copying permission.
    3 
    4 #ifdef __GNUG__
    5 #pragma implementation
    6 #endif
    7 
    8 #include "splib.h"
    9 #include "ParserApp.h"
   10 #include "ParserAppMessages.h"
   11 #include "MessageArg.h"
   12 #include "Location.h"
   13 #include "macros.h"
   14 #include "sptchar.h"
   15 #include "ArcEngine.h"
   16 
   17 #include <limits.h>
   18 #include <errno.h>
   19 #include <stdlib.h>
   20 
   21 #ifndef DEFAULT_ERROR_LIMIT
   22 #define DEFAULT_ERROR_LIMIT 200
   23 #endif
   24 
   25 #ifdef SP_NAMESPACE
   26 namespace SP_NAMESPACE {
   27 #endif
   28 
   29 ParserApp::ParserApp(const char *requiredInternalCode)
   30 : EntityApp(requiredInternalCode),
   31   errorLimit_(DEFAULT_ERROR_LIMIT)
   32 {
   33   registerOption('a', SP_T("activate"), ParserAppMessages::name,
   34                  ParserAppMessages::aHelp);
   35   registerOption('A', SP_T("architecture"), ParserAppMessages::name,
   36                  ParserAppMessages::AHelp);
   37   registerOption('E', SP_T("max-errors"), ParserAppMessages::number,
   38                  ParserAppMessages::EHelp);
   39   registerOption('e', SP_T("open-entities"), ParserAppMessages::eHelp);
   40   registerOption('g', SP_T("open-elements"), ParserAppMessages::gHelp);
   41   registerOption('n', SP_T("error-numbers"), ParserAppMessages::nHelp);
   42   registerOption('x', SP_T("references"), ParserAppMessages::xHelp);
   43   registerOption('i', SP_T("include"), ParserAppMessages::name,
   44                  ParserAppMessages::iHelp);
   45   registerOption('w', SP_T("warning"), ParserAppMessages::type, 
   46                  ParserAppMessages::wHelp);
   47 }
   48 
   49 void ParserApp::initParser(const StringC &sysid)
   50 {
   51   SgmlParser::Params params;
   52   params.sysid = sysid;
   53   params.entityManager = entityManager().pointer();
   54   params.options = &options_;
   55   parser_.init(params);
   56   if (arcNames_.size() > 0)
   57     parser_.activateLinkType(arcNames_[0]);
   58   for (size_t i = 0; i < activeLinkTypes_.size(); i++) 
   59     parser_.activateLinkType(convertInput(activeLinkTypes_[i]));
   60   allLinkTypesActivated();
   61 }
   62 
   63 void ParserApp::allLinkTypesActivated()
   64 {
   65   parser_.allLinkTypesActivated();
   66 }
   67 
   68 int ParserApp::processSysid(const StringC &sysid)
   69 {
   70   initParser(sysid);
   71   ErrorCountEventHandler *eceh = makeEventHandler();
   72   if (errorLimit_)
   73     eceh->setErrorLimit(errorLimit_);   
   74   return generateEvents(eceh);
   75 }
   76 
   77 int ParserApp::generateEvents(ErrorCountEventHandler *eceh)
   78 {
   79   Owner<EventHandler> eh(eceh);
   80   parseAll(parser_, *eh, eceh->cancelPtr());
   81   unsigned errorCount = eceh->errorCount();
   82   if (errorLimit_ != 0 && errorCount >= errorLimit_)
   83     message(ParserAppMessages::errorLimitExceeded,
   84         NumberMessageArg(errorLimit_));
   85   return errorCount > 0;
   86 }
   87 
   88 void ParserApp::parseAll(SgmlParser &parser,
   89              EventHandler &eh,
   90              const volatile sig_atomic_t *cancelPtr)
   91 {
   92   if (arcNames_.size() > 0) {
   93     SelectOneArcDirector director(arcNames_, eh);
   94     ArcEngine::parseAll(parser, director, director, cancelPtr);
   95   }
   96   else
   97     parser.parseAll(eh, cancelPtr);
   98 }
   99 
  100 void ParserApp::processOption(AppChar opt, const AppChar *arg)
  101 {
  102   switch (opt) {
  103   case 'a':
  104     // activate link
  105     activeLinkTypes_.push_back(arg);
  106     break;
  107   case 'A':
  108     arcNames_.push_back(convertInput(arg));
  109     break;
  110   case 'E':
  111     {
  112       AppChar *end;
  113       unsigned long n = tcstoul((AppChar *)arg, &end, 10);
  114       if ((n == 0 && end == arg)
  115       || *end != SP_T('\0')
  116       || (n == ULONG_MAX && errno == ERANGE)
  117       || n > UINT_MAX)
  118     message(ParserAppMessages::badErrorLimit);
  119       else
  120     errorLimit_ = unsigned(n);
  121     }
  122     break;
  123   case 'e':
  124     // describe open entities in error messages
  125     addOption(MessageReporter::openEntities);
  126     break;
  127   case 'g':
  128     // show gis of open elements in error messages
  129     addOption(MessageReporter::openElements);
  130     break;
  131   case 'n':
  132     // show message number in error messages
  133     addOption(MessageReporter::messageNumbers);
  134     break;
  135   case 'x':
  136     // show relevant clauses in error messages
  137     addOption(MessageReporter::clauses);
  138     break;
  139   case 'i':
  140     // pretend that arg is defined as INCLUDE
  141     options_.includes.push_back(convertInput(arg));
  142     break;
  143   case 'w':
  144     if (!enableWarning(arg))
  145       message(ParserAppMessages::unknownWarning,
  146           StringMessageArg(convertInput(arg)));
  147     break;
  148   default:
  149     EntityApp::processOption(opt, arg);
  150     break;
  151   }
  152 }
  153 
  154 Boolean ParserApp::enableWarning(const AppChar *s)
  155 {
  156   enum { groupAll = 01, groupMinTag = 02, groupXML = 04 };
  157   static struct {
  158     // Explicit qualifier works around CodeWarrior bug
  159     const CmdLineApp::AppChar *name;
  160     PackedBoolean ParserOptions::*ptr;
  161     unsigned char groups;
  162   } table[] = {
  163     { SP_T("mixed"), &ParserOptions::warnMixedContent, groupAll },
  164     { SP_T("should"), &ParserOptions::warnShould, groupAll },
  165     { SP_T("duplicate"), &ParserOptions::warnDuplicateEntity, 0 },
  166     { SP_T("default"), &ParserOptions::warnDefaultEntityReference, groupAll },
  167     { SP_T("undefined"), &ParserOptions::warnUndefinedElement, groupAll },
  168     { SP_T("sgmldecl"), &ParserOptions::warnSgmlDecl, groupAll },
  169     { SP_T("unclosed"), &ParserOptions::noUnclosedTag, groupAll|groupMinTag },
  170     { SP_T("net"), &ParserOptions::noNet, groupMinTag },
  171     { SP_T("empty"), &ParserOptions::warnEmptyTag, groupAll|groupMinTag },
  172     { SP_T("unused-map"), &ParserOptions::warnUnusedMap, groupAll },
  173     { SP_T("unused-param"), &ParserOptions::warnUnusedParam, groupAll },
  174     { SP_T("notation-sysid"), &ParserOptions::warnNotationSystemId, 0 },
  175     { SP_T("inclusion"), &ParserOptions::warnInclusion, groupXML },
  176     { SP_T("exclusion"), &ParserOptions::warnExclusion, groupXML },
  177     { SP_T("rcdata-content"), &ParserOptions::warnRcdataContent, groupXML },
  178     { SP_T("cdata-content"), &ParserOptions::warnCdataContent, groupXML },
  179     { SP_T("ps-comment"), &ParserOptions::warnPsComment, groupXML },
  180     { SP_T("attlist-group-decl"), &ParserOptions::warnAttlistGroupDecl, groupXML },
  181     { SP_T("element-group-decl"), &ParserOptions::warnElementGroupDecl, groupXML },
  182     { SP_T("pi-entity"), &ParserOptions::warnPiEntity, groupXML },
  183     { SP_T("internal-sdata-entity"), &ParserOptions::warnInternalSdataEntity, groupXML },
  184     { SP_T("internal-cdata-entity"), &ParserOptions::warnInternalCdataEntity, groupXML },
  185     { SP_T("external-sdata-entity"), &ParserOptions::warnExternalSdataEntity, groupXML },
  186     { SP_T("external-cdata-entity"), &ParserOptions::warnExternalCdataEntity, groupXML },
  187     { SP_T("bracket-entity"), &ParserOptions::warnBracketEntity, groupXML },
  188     { SP_T("data-atts"), &ParserOptions::warnDataAttributes, groupXML },
  189     { SP_T("missing-system-id"), &ParserOptions::warnMissingSystemId, groupXML },
  190     { SP_T("conref"), &ParserOptions::warnConref, groupXML },
  191     { SP_T("current"), &ParserOptions::warnCurrent, groupXML },
  192     { SP_T("nutoken-decl-value"), &ParserOptions::warnNutokenDeclaredValue, groupXML },
  193     { SP_T("number-decl-value"), &ParserOptions::warnNumberDeclaredValue, groupXML },
  194     { SP_T("name-decl-value"), &ParserOptions::warnNameDeclaredValue, groupXML },
  195     { SP_T("named-char-ref"), &ParserOptions::warnNamedCharRef, groupXML },
  196     { SP_T("refc"), &ParserOptions::warnRefc, groupXML },
  197     { SP_T("temp-ms"), &ParserOptions::warnTempMarkedSection, groupXML },
  198     { SP_T("rcdata-ms"), &ParserOptions::warnRcdataMarkedSection, groupXML },
  199     { SP_T("instance-include-ms"), &ParserOptions::warnInstanceIncludeMarkedSection, groupXML },
  200     { SP_T("instance-ignore-ms"), &ParserOptions::warnInstanceIgnoreMarkedSection, groupXML },
  201     { SP_T("and-group"), &ParserOptions::warnAndGroup, groupXML },
  202     { SP_T("rank"), &ParserOptions::warnRank, groupXML },
  203     { SP_T("empty-comment-decl"), &ParserOptions::warnEmptyCommentDecl, groupXML },
  204     { SP_T("att-value-not-literal"), &ParserOptions::warnAttributeValueNotLiteral, groupXML },
  205     { SP_T("missing-att-name"), &ParserOptions::warnMissingAttributeName, groupXML },
  206     { SP_T("comment-decl-s"), &ParserOptions::warnCommentDeclS, groupXML },
  207     { SP_T("comment-decl-multiple"), &ParserOptions::warnCommentDeclMultiple, groupXML },
  208     { SP_T("missing-status-keyword"), &ParserOptions::warnMissingStatusKeyword, groupXML },
  209     { SP_T("multiple-status-keyword"), &ParserOptions::warnMultipleStatusKeyword, groupXML },
  210     { SP_T("instance-param-entity"), &ParserOptions::warnInstanceParamEntityRef, groupXML },
  211     { SP_T("min-param"), &ParserOptions::warnMinimizationParam, groupXML },
  212     { SP_T("mixed-content-xml"), &ParserOptions::warnMixedContentRepOrGroup, groupXML },
  213     { SP_T("name-group-not-or"), &ParserOptions::warnNameGroupNotOr, groupXML },
  214     { SP_T("pi-missing-name"), &ParserOptions::warnPiMissingName, groupXML },
  215     { SP_T("instance-status-keyword-s"), &ParserOptions::warnInstanceStatusKeywordSpecS, groupXML },
  216     { SP_T("external-data-entity-ref"), &ParserOptions::warnExternalDataEntityRef, groupXML },
  217     { SP_T("att-value-external-entity-ref"), &ParserOptions::warnAttributeValueExternalEntityRef, groupXML },
  218     { SP_T("data-delim"), &ParserOptions::warnDataDelim, groupXML },
  219     { SP_T("explicit-sgml-decl"), &ParserOptions::warnExplicitSgmlDecl, groupXML },
  220     { SP_T("internal-subset-ms"), &ParserOptions::warnInternalSubsetMarkedSection, groupXML },
  221     { SP_T("default-entity"), &ParserOptions::warnDefaultEntityDecl, groupXML },
  222     { SP_T("non-sgml-char-ref"), &ParserOptions::warnNonSgmlCharRef, groupXML },
  223     { SP_T("internal-subset-ps-param-entity"), &ParserOptions::warnInternalSubsetPsParamEntityRef, groupXML },
  224     { SP_T("internal-subset-ts-param-entity"), &ParserOptions::warnInternalSubsetTsParamEntityRef, groupXML },
  225     { SP_T("internal-subset-literal-param-entity"), &ParserOptions::warnInternalSubsetLiteralParamEntityRef, groupXML },
  226     { SP_T("immediate-recursion"), &ParserOptions::warnImmediateRecursion, 0 },
  227     { SP_T("fully-declared"), &ParserOptions::fullyDeclared, 0 },
  228     { SP_T("fully-tagged"), &ParserOptions::fullyTagged, 0 },
  229     { SP_T("amply-tagged-recursive"), &ParserOptions::amplyTagged, 0 },
  230     { SP_T("amply-tagged"), &ParserOptions::amplyTaggedAnyother, 0 },
  231     { SP_T("type-valid"), &ParserOptions::valid, 0 },
  232     { SP_T("entity-ref"), &ParserOptions::entityRef, 0 },
  233     { SP_T("external-entity-ref"), &ParserOptions::externalEntityRef, 0 },
  234     { SP_T("integral"), &ParserOptions::integral, 0 },
  235     { SP_T("idref"), &ParserOptions::errorIdref, 0 },
  236     { SP_T("significant"), &ParserOptions::errorSignificant, 0 },
  237     { SP_T("afdr"), &ParserOptions::errorAfdr, 0 },
  238   };
  239   static struct {
  240     const CmdLineApp::AppChar *name;
  241     unsigned char flag;
  242   } groupTable[] = {
  243     { SP_T("all"), groupAll },
  244     { SP_T("min-tag"), groupMinTag },
  245     { SP_T("xml"), groupXML },
  246   };
  247   PackedBoolean val = 1;
  248   if (tcsncmp(s, SP_T("no-"), 3) == 0) {
  249     s += 3;
  250     val = 0;
  251   }
  252   for (size_t i = 0; i < SIZEOF(groupTable); i++)
  253     if (tcscmp(s, groupTable[i].name) == 0) {
  254       for (size_t j = 0; j < SIZEOF(table); j++)
  255     if (table[j].groups & groupTable[i].flag) {
  256       // Use parentheses to work around Watcom 10.0a bug.
  257       (options_.*(table[j].ptr)) = val;
  258     }
  259       return 1;
  260     }
  261   for (size_t i = 0; i < SIZEOF(table); i++)
  262     if (tcscmp(s, table[i].name) == 0) {
  263       // Use parentheses to work around Watcom 10.0a bug.
  264       (options_.*(table[i].ptr)) = val;
  265       return 1;
  266     }
  267   if (tcscmp(s, SP_T("valid")) == 0) {
  268     options_.typeValid = val;
  269     return 1;
  270   }
  271   return 0;
  272 }
  273 
  274 #ifdef SP_NAMESPACE
  275 }
  276 #endif