Пример #1
0
 /**
  * return IObject
  */
 public function parse(string $aSource, ObjectContainer $aObjectContainer, UI $aUI)
 {
     $nProcIndex = 0;
     $aState = ParserStateDefault::singleton();
     $aCurrentObject = $aRootObject = new ObjectBase(0, $aSource->length() - 1, 0, '');
     while ($nProcIndex < $aSource->length()) {
         $aNewState = $aState->examineStateChange($aSource, $nProcIndex, $aCurrentObject);
         if ($aNewState) {
             // 切换状态
             if ($aNewState != $aState) {
                 //
                 $aCurrentObject = $aState->sleep($aCurrentObject, $aSource, $nProcIndex - 1);
                 $aCurrentObject = $aNewState->active($aCurrentObject, $aSource, $nProcIndex);
                 // 状态变化
                 $aState = ParserState::queryState($aCurrentObject);
             }
         } else {
             $aCurrentObject = $aState->complete($aCurrentObject, $aSource, $nProcIndex);
             if (!$aCurrentObject) {
                 break;
             }
             $aCurrentObject = ParserState::queryState($aCurrentObject)->wakeup($aCurrentObject, $aSource, $nProcIndex);
             // 状态变化
             $aState = ParserState::queryState($aCurrentObject);
         }
         $nProcIndex++;
     }
     // 未完成的对象
     if ($aCurrentObject != $aRootObject) {
         $aBuff = new OutputStreamBuffer();
         $aRootObject->printStruct($aBuff);
         throw new Exception("<pre>\r\n分析UI模板时遇到未完成的对象:%s\r\n%s\r\n</pre>", array($aCurrentObject->summary(), $aBuff));
     }
     $aObjectContainer->clear();
     foreach ($aRootObject->iterator() as $aObject) {
         $aObjectContainer->add($aObject);
     }
 }
Пример #2
0
 /**
  * @return ParserState
  */
 public static function queryState(IObject $aObject)
 {
     if ($aObject instanceof Tag) {
         return ParserStateTag::singleton();
     } else {
         if ($aObject instanceof AttributeValue) {
             return ParserStateAttribute::singleton();
         } else {
             if ($aObject instanceof Node) {
                 return ParserStateNode::singleton();
             } else {
                 if ($aObject instanceof Macro) {
                     return ParserStateMacro::singleton();
                 } else {
                     if ($aObject instanceof Text) {
                         return ParserStateText::singleton();
                     } else {
                         if ($aObject instanceof ObjectBase) {
                             return ParserStateDefault::singleton();
                         } else {
                             throw new Exception("!?");
                         }
                     }
                 }
             }
         }
     }
 }