Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
8.5 KiB

  1. /*
  2. * Parser
  3. */
  4. #ifndef DUI_PARSER_PARSEROBJ_H_INCLUDED
  5. #define DUI_PARSER_PARSEROBJ_H_INCLUDED
  6. #pragma once
  7. namespace DirectUI
  8. {
  9. #define MAXIDENT 31
  10. ////////////////////////////////////////////////////////
  11. // Parser table definitions
  12. struct EnumTable
  13. {
  14. LPWSTR pszEnum;
  15. int nEnum;
  16. };
  17. typedef HRESULT (*PLAYTCREATE)(int, int*, Value**);
  18. struct LayoutTable
  19. {
  20. LPWSTR pszLaytType;
  21. PLAYTCREATE pfnLaytCreate;
  22. };
  23. typedef struct
  24. {
  25. LPWSTR pszElType;
  26. IClassInfo* pci;
  27. } ElementTable;
  28. struct SysColorTable
  29. {
  30. LPWSTR pszSysColor;
  31. int nSysColor;
  32. };
  33. ////////////////////////////////////////////////////////
  34. // Parser tree data structures
  35. // Parse tree nodes are any data structure dynamically allocated to store tree information
  36. // Parse tree node types
  37. #define NT_ValueNode 0
  38. #define NT_PropValPairNode 1
  39. #define NT_ElementNode 2
  40. #define NT_AttribNode 3
  41. #define NT_RuleNode 4
  42. #define NT_SheetNode 5
  43. // Tree node base class
  44. struct Node
  45. {
  46. BYTE nType;
  47. };
  48. // Value node
  49. #define VNT_Normal 0
  50. #define VNT_LayoutCreate 1
  51. #define VNT_SheetRef 2
  52. #define VNT_EnumFixup 3 // Map name to int Value once PropertyInfo is known
  53. struct LayoutCreate
  54. {
  55. union
  56. {
  57. PLAYTCREATE pfnLaytCreate;
  58. LPWSTR pszLayout; // Fixup happens immediately during Value creation
  59. };
  60. int dNumParams;
  61. int* pParams;
  62. };
  63. struct EnumsList
  64. {
  65. int dNumParams;
  66. LPWSTR* pEnums; // Enums to be OR'd
  67. };
  68. struct ValueNode : Node
  69. {
  70. BYTE nValueType;
  71. union
  72. {
  73. Value* pv; // VNT_Normal
  74. LayoutCreate lc; // VNT_LayoutCreate, created during Element creates
  75. LPWSTR psres; // VNT_SheetRef
  76. EnumsList el; // VNT_EnumFixup
  77. };
  78. };
  79. // Property/Value Pair
  80. #define PVPNT_Normal 0
  81. #define PVPNT_Fixup 1 // Map name to ppi once Element type is known
  82. struct PropValPairNode : Node
  83. {
  84. BYTE nPropValPairType;
  85. union
  86. {
  87. PropertyInfo* ppi; // PVPNT_Normal
  88. LPWSTR pszProperty; // PVPNT_Fixup
  89. };
  90. ValueNode* pvn;
  91. PropValPairNode* pNext;
  92. };
  93. // Element node
  94. struct ElementNode : Node
  95. {
  96. IClassInfo* pci;
  97. PropValPairNode* pPVNodes;
  98. LPWSTR pszResID;
  99. Value* pvContent;
  100. ElementNode* pChild;
  101. ElementNode* pNext;
  102. };
  103. // Sheet attribute node
  104. #define PALOGOP_Equal 0
  105. #define PALOGOP_NotEqual 1
  106. struct AttribNode : PropValPairNode
  107. {
  108. UINT nLogOp;
  109. };
  110. // Sheet rule node
  111. struct RuleNode : Node
  112. {
  113. IClassInfo* pci;
  114. AttribNode* pCondNodes;
  115. PropValPairNode* pDeclNodes;
  116. RuleNode* pNext;
  117. };
  118. // Sheet node
  119. struct SheetNode : Node
  120. {
  121. Value* pvSheet; // Create once all Rules are known
  122. RuleNode* pRules;
  123. LPWSTR pszResID;
  124. };
  125. // Intermediate parser data structures
  126. struct ParamsList
  127. {
  128. int dNumParams;
  129. int* pParams;
  130. };
  131. struct StartTag
  132. {
  133. WCHAR szTag[MAXIDENT];
  134. WCHAR szResID[MAXIDENT];
  135. PropValPairNode* pPVNodes;
  136. };
  137. // Parser
  138. typedef void (CALLBACK *PPARSEERRORCB)(LPCWSTR pszError, LPCWSTR pszToken, int dLine);
  139. class Parser
  140. {
  141. public:
  142. static HRESULT Create(const CHAR* pBuffer, int cCharCount, HANDLE* pHList, PPARSEERRORCB pfnErrorCB, OUT Parser** ppParser);
  143. static HRESULT Create(UINT uRCID, HANDLE* pHList, PPARSEERRORCB pfnErrorCB, OUT Parser** ppParser);
  144. static HRESULT Create(LPCWSTR pFile, HANDLE* pHList, PPARSEERRORCB pfnErrorCB, OUT Parser** ppParser);
  145. static HRESULT Create(const CHAR* pBuffer, int cCharCount, HINSTANCE hInst, PPARSEERRORCB pfnErrorCB, OUT Parser** ppParser);
  146. static HRESULT Create(UINT uRCID, HINSTANCE hInst, PPARSEERRORCB pfnErrorCB, OUT Parser** ppParser);
  147. static HRESULT Create(LPCWSTR pFile, HINSTANCE hInst, PPARSEERRORCB pfnErrorCB, OUT Parser** ppParser);
  148. void Destroy() { HDelete<Parser>(this); }
  149. HRESULT CreateElement(LPCWSTR pszResID, Element* peSubstitute, OUT Element** ppElement);
  150. virtual Value* GetSheet(LPCWSTR pszResID);
  151. LPCWSTR ResIDFromSheet(Value* pvSheet);
  152. void GetPath(LPCWSTR pIn, LPWSTR pOut, size_t cbOut);
  153. // Parser/scanner only use
  154. int _Input(CHAR* pBuffer, int cMaxChars);
  155. void* _TrackNodeAlloc(SIZE_T s); // Parse-tree node memory
  156. void _UnTrackNodeAlloc(Node* pn); // Parse-tree node memory
  157. void* _TrackAlloc(SIZE_T s); // Node-specific state
  158. void* _TrackTempAlloc(SIZE_T s); // Parse-time temporary memory
  159. void _TrackTempAlloc(void* pm); // Parse-time temporary memory
  160. void* _TrackTempReAlloc(void* pm, SIZE_T s); // Parse-time temporary memory
  161. void _UnTrackTempAlloc(void* pm); // Parse-time temporary memory
  162. void _ParseError(LPCWSTR pszError, LPCWSTR pszToken, int dLine);
  163. ValueNode* _CreateValueNode(BYTE nValueType, void* pData);
  164. PropValPairNode* _CreatePropValPairNode(LPCWSTR pszProperty, ValueNode* pvn, UINT* pnLogOp = NULL);
  165. RuleNode* _CreateRuleNode(LPCWSTR pszClass, AttribNode* pCondNodes, PropValPairNode* pDeclNodes);
  166. ElementNode* _CreateElementNode(StartTag* pst, Value* pvContent);
  167. SheetNode* _CreateSheetNode(LPCWSTR pszResID, RuleNode* pRuleNodes);
  168. static int _QuerySysMetric(int idx);
  169. static LPCWSTR _QuerySysMetricStr(int idx, LPWSTR psz, UINT c);
  170. bool WasParseError() { return _fParseError; }
  171. HANDLE GetHandle(int iHandle) { return _pHList[iHandle]; }
  172. HINSTANCE GetHInstance() { return static_cast<HINSTANCE>(GetHandle(0)); } // Always assume 0th item is the default HINSTANCE used
  173. static HRESULT ReplaceSheets(Element* pe, Parser* pFrom, Parser* pTo);
  174. DynamicArray<ElementNode*>* _pdaElementList; // Root Element list
  175. DynamicArray<SheetNode*>* _pdaSheetList; // Sheet list
  176. // Global parser context
  177. static Parser* g_pParserCtx;
  178. static bool g_fParseAbort;
  179. static HDC g_hDC;
  180. static int g_nDPI;
  181. static HRESULT g_hrParse; // Abnormal errors during parse
  182. Parser() { }
  183. HRESULT Initialize(const CHAR* pBuffer, int cCharCount, HANDLE* pHList, PPARSEERRORCB pfnErrorCB);
  184. HRESULT Initialize(UINT uRCID, HANDLE* pHList, PPARSEERRORCB pfnErrorCB);
  185. HRESULT Initialize(LPCWSTR pFile, HANDLE* pHList, PPARSEERRORCB pfnErrorCB);
  186. // Single HINSTANCE, setup internal default list
  187. // 0th entry is always default HINSTANCE when no handle is specified
  188. HRESULT Initialize(const CHAR* pBuffer, int cCharCount, HINSTANCE hInst, PPARSEERRORCB pfnErrorCB) { _hDefault = hInst; return Initialize(pBuffer, cCharCount, &_hDefault, pfnErrorCB); }
  189. HRESULT Initialize(UINT uRCID, HINSTANCE hInst, PPARSEERRORCB pfnErrorCB) { _hDefault = hInst; return Initialize(uRCID, &_hDefault, pfnErrorCB); }
  190. HRESULT Initialize(LPCWSTR pFile, HINSTANCE hInst, PPARSEERRORCB pfnErrorCB) { _hDefault = hInst; return Initialize(pFile, &_hDefault, pfnErrorCB); }
  191. virtual ~Parser();
  192. virtual bool ConvertEnum(LPCWSTR pszEnum, int* pEnum, PropertyInfo* ppi);
  193. virtual PLAYTCREATE ConvertLayout(LPCWSTR pszLayout);
  194. virtual IClassInfo* ConvertElement(LPCWSTR pszElement);
  195. private:
  196. HRESULT _ParseBuffer(const CHAR* pBuffer, int cCharCount);
  197. void _DestroyTables();
  198. // Error handling
  199. bool _fParseError;
  200. PPARSEERRORCB _pfnErrorCB;
  201. // Input callback buffer and tracking
  202. const CHAR* _pInputBuf;
  203. int _dInputChars;
  204. int _dInputPtr;
  205. // Parse tree allocations and temporary parse-time only allocations
  206. DynamicArray<Node*>* _pdaNodeMemTrack; // Parser nodes
  207. DynamicArray<void*>* _pdaMemTrack; // Parser node extra memory
  208. DynamicArray<void*>* _pdaTempMemTrack; // Temp parse-time only memory
  209. bool _FixupPropValPairNode(PropValPairNode* ppvpn, IClassInfo* pci, bool bRestrictVal);
  210. WCHAR _szDrive[MAX_PATH]; // Drive letter to the file being parsed
  211. WCHAR _szPath[MAX_PATH]; // Path to the file being parsed
  212. HANDLE* _pHList; // Pointer to handle list used for exposing runtime handles during parse
  213. HANDLE _hDefault; // Default handle list (1 item - default HINSTANCE) if no list is provided
  214. HRESULT _InstantiateElementNode(ElementNode* pn, Element* peSubstitute, Element* peParent, OUT Element** ppElement);
  215. };
  216. } // namespace DirectUI
  217. #endif // DUI_PARSER_PARSEROBJ_H_INCLUDED