Пример #1
0
//-----------------------------------------------
//--- scpi_SetCommandError ---
//return :	0 => no error.
//			-1=> error.
int scpi_SetCommandError(STR_PARSER *p, int err)
{
	if(p) {
		if(_DEV_LUA == p->dev) {
			pushErrorQueue( &(p->errQueue), err);
			return 0;
		}
		else if(p->pComd) {
			p->pComd->err = err;
//			printf("Command Error<%d>:%s\n", err, Error[err]);
			return 0;
		}
	}
	return -1;
}
Пример #2
0
/// Function name  : generateXMLTree
// Description     : Generate an XML Tree from an input string of XML
// 
// CONST TCHAR*         szXML        : [in]     Input string containing the XML
// CONST UINT           iInputLength : [in]     Length of input string, in characters
// CONST TCHAR*         szFileName   : [in]     Filename   [Only used for error reporting]
// HWND                 hParentWnd   : [in]     Ignored
// XML_TREE*           &pOutput      : [out]    New XMLTree, You are responsible for destroying it
// OPERATION_PROGRESS*  pProgress    : [in/out][optional] Operation progress object, if feedback is required
// ERROR_QUEUE*         pErrorQueue  : [in/out] Error message queue
// 
// Return Value   : OR_SUCCESS - File was parsed successfully, any errors were ignored by the user
//                  OR_ABORTED - File was NOT parsed successfully due to the user aborting after minor errors
// 
BearScriptAPI
OPERATION_RESULT  generateXMLTree(CONST TCHAR*  szXML, CONST UINT  iInputLength, CONST TCHAR*  szFileName, HWND  hParentWnd, XML_TREE*  &pOutput, OPERATION_PROGRESS*  pProgress, ERROR_QUEUE*  pErrorQueue)
{
   OPERATION_RESULT   eResult;         // Operation result
   XML_TOKENISER*     pToken;          // XML token currently being processed
   XML_TAG_STACK*     pStack;          // XML parse stack
   XML_TREE_NODE*     pCurrentNode;    // Parent for the node currently being processed
   ERROR_STACK*       pError;          // Current error, if any

   // Prepare
   eResult  = OR_SUCCESS;
   pError   = NULL;

   __try
   {
      // Prepare
      pOutput      = createXMLTree();
      pCurrentNode = pOutput->pRoot;
      
      // Create parsing objects
      pToken = createXMLTokeniser(szFileName, szXML);
      pStack = createXMLTagStack();

      // [OPTIONAL] Define progress based on the number of pages processed
      if (pProgress)
         updateOperationProgressMaximum(pProgress, iInputLength);

      // Iterate through tokens
      while (findNextXMLToken(pToken, pError))
      {
         // Update progress object, if present
         if (pProgress)
            updateOperationProgressValue(pProgress, pToken->szNextToken - pToken->szSource);

         // [CHECK] Ensure token was parsed succesfully
         if (!pError)
         {
            switch (pToken->eType)
            {
            /// [COMMENT, SCHEMA, WHITESPACE] Ignore...
            case XTT_COMMENT_TAG:
            case XTT_CDATA_TAG:
            case XTT_SCHEMA_TAG:
            case XTT_WHITESPACE:
               continue;

            /// [TAG/TEXT] Process if unblocked, skip otherwise
            case XTT_OPENING_TAG:
            case XTT_SELF_CLOSING_TAG:
            case XTT_CLOSING_TAG:
            case XTT_TEXT:
               // [BLOCKING] Skip all tags until </sourcetext> is found
               if (pToken->bBlocking)
               {
                  if (pToken->eType != XTT_CLOSING_TAG OR !utilCompareString(pToken->szText, "sourcetext"))
                     continue;
                  
                  pToken->bBlocking = FALSE;
               }

               // Examine tag
               switch (pToken->eType)
               {
               /// [OPENING TAG] Add tag to stack + Add node to tree + Set as 'current node'
               case XTT_OPENING_TAG:
                  // Push tag onto stack
                  pushXMLTagStack(pStack, pToken);

                  // Append new node to 'CurrentNode' + Update 'CurrentNode'
                  appendChildToXMLTreeNode(pOutput, pCurrentNode, pToken->pNode);      //pNewNode = createXMLTreeNode(pCurrentNode, pToken);
                  pCurrentNode = pToken->pNode;

                  // [BLOCK] Skip all tags within <sourcetext>
                  if (utilCompareString(pToken->szText, "sourcetext"))
                     pToken->bBlocking = TRUE;
                  break;
                  
               /// [SELF CLOSING TAG] Add node to tree
               case XTT_SELF_CLOSING_TAG:
                  // Append new node to 'CurrentNode'
                  appendChildToXMLTreeNode(pOutput, pCurrentNode, pToken->pNode);      // pNewNode = createXMLTreeNode(pCurrentNode, pToken);
                  break;
                  
               /// [CLOSING TAG] Ensure tag matches the currently open tag
               case XTT_CLOSING_TAG:
                  // [CHECK] Ensure stack isn't empty
                  if (!getStackItemCount(pStack))
                     // [ERROR] "An unexpected closing tag </%s> was detected on line %d of '%s'"
                     pError = generateDualError(HERE(IDS_XML_UNEXPECTED_CLOSING_TAG), pToken->szText, pToken->iLineNumber, pToken->szFileName);

                  // [CHECK] Ensure tags match
                  else if (utilCompareStringVariables(pToken->szText, topXMLTagStack(pStack)))
                  {
                     // [SUCCESS] Remove matching tag. Set parent as 'Current'
                     popXMLTagStack(pStack);
                     pCurrentNode = pCurrentNode->pParent;
                  }
                  else  // [ERROR] "The closing tag </%s> on line %d does not match the currently open <%s> tag in '%s'"
                     pError = generateDualError(HERE(IDS_XML_MISMATCHED_CLOSING_TAG), pToken->szText, pToken->iLineNumber, topXMLTagStack(pStack), pToken->szFileName);
                  break;

               /// [TEXT] Save text in the current node
               case XTT_TEXT:
                  // [CHECK] Ensure stack isn't empty / Node doesn't already have text
                  if (getStackItemCount(pStack) AND !pCurrentNode->szText)
                     // [SUCCESS] Save node text
                     pCurrentNode->szText = utilDuplicateString(pToken->szText, pToken->iCount);
                  else
                     // [ERROR] "Unexpected text '%s' was detected on line %d of '%s'"
                     pError = generateDualError(HERE(IDS_XML_UNEXPECTED_TEXT), pToken->szText, pToken->iLineNumber, pToken->szFileName);
                  break;
               } // END: switch (pToken->eType)
            }

            // [DEBUG]
            //debugXMLToken(pToken);

            /// [SYNTAX ERROR] "A minor error has been detected in the syntax of the XML file '%s'"
            if (pError)
            {
               generateOutputTextFromError(pError);
               //enhanceError(pError, ERROR_ID(IDS_XML_MINOR_SYNTAX_ERROR), szFileName);
               attachXMLToError(pError, pToken->szNextToken, pToken->szNextToken - pToken->szSource);
            }

         } // END: if (!pError)
         /*else
            /// [PARSE ERROR] "A minor error has occurred while parsing the XML file '%s'"
            enhanceError(pError, ERROR_ID(IDS_XML_MINOR_PARSING_ERROR), szFileName);*/

         // [ERROR] Abort
         if (pError)
         {
            pushErrorQueue(pErrorQueue, pError);
            eResult = OR_FAILURE;
            break;
         }

      } // END: while (findNextXMLToken(pToken, pError))

      /// [CHECK] Detect any open tags remaining
      while (eResult == OR_SUCCESS AND getStackItemCount(pStack))
      {
         // [WARNING] "An unclosed <%s> tag was detected on an unknown line of '%s'"
         pushErrorQueue(pErrorQueue, generateDualWarning(HERE(IDS_XML_UNCLOSED_TAG_REMAINING), topXMLTagStack(pStack), pToken->szFileName));
         generateOutputTextFromLastError(pErrorQueue);
         popXMLTagStack(pStack);
      }

      // Cleanup and return result
      deleteXMLTagStack(pStack);
      deleteXMLTokeniser(pToken);
      return eResult;

      //// [ERROR] Destroy output tree
      //if (eResult != OR_SUCCESS)
      //   deleteXMLTree(pOutput);
   }
   __except (pushException(pErrorQueue))
   {
      EXCEPTION3("Unable to parse XML file '%s' at token '%s' on line %u", szFileName, pToken->szText, pToken->iLineNumber);
      return OR_FAILURE;
   }
}