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.

336 lines
10 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: YYBase.cxx
  7. //
  8. // Contents: Custom base class for YYPARSER
  9. //
  10. // History: 18-Apr-2000 KitmanH Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #pragma hdrstop
  14. #include "yybase.hxx"
  15. #include "parser.h"
  16. #include "flexcpp.h"
  17. #include "parsepl.h"
  18. void StripQuotes(WCHAR *wcsPhrase);
  19. //+-------------------------------------------------------------------------
  20. //
  21. // Member: CTripYYBase::CTripYYBase, public
  22. //
  23. // Synopsis: Constructor
  24. //
  25. // Arguments: [ColumnMapper] -- Column Mapper
  26. // [locale] -- locale
  27. // [yylex] -- Lexer
  28. //
  29. // History: 18-Apr-2000 KitmanH Created
  30. //
  31. //--------------------------------------------------------------------------
  32. CTripYYBase::CTripYYBase( IColumnMapper & ColumnMapper,
  33. LCID & locale,
  34. YYLEXER & yylex )
  35. : _yylex( yylex ),
  36. _ColumnMapper(ColumnMapper),
  37. _lcid(locale)
  38. {
  39. InitState();
  40. fDeferredPop = FALSE;
  41. }
  42. //+-------------------------------------------------------------------------
  43. //
  44. // Member: CTripYYBase::~CTripYYBase, public
  45. //
  46. // Synopsis: Destructor
  47. //
  48. // History: 18-Apr-2000 KitmanH Created
  49. //
  50. //--------------------------------------------------------------------------
  51. CTripYYBase::~CTripYYBase()
  52. {
  53. }
  54. //+-------------------------------------------------------------------------
  55. //
  56. // Member: CTripYYBase::yyprimebuffer, public
  57. //
  58. // Synopsis: Prime lexer with text (passthrough to lexer)
  59. //
  60. // Arguments: [pszBuffer] -- Buffer
  61. //
  62. // History: 18-Apr-2000 KitmanH Moved from YYPARSER
  63. //
  64. //--------------------------------------------------------------------------
  65. void CTripYYBase::yyprimebuffer(const YY_CHAR *pszBuffer)
  66. {
  67. _yylex.yyprimebuffer(pszBuffer);
  68. }
  69. //+-------------------------------------------------------------------------
  70. //
  71. // Member: CTripYYBase::triperror, protected
  72. //
  73. // Synopsis: Report parsing errors
  74. //
  75. // Arguments: [szError] -- Error string
  76. //
  77. // History: 18-Apr-2000 KitmanH Moved from YYPARSER
  78. //
  79. //--------------------------------------------------------------------------
  80. void CTripYYBase::triperror( char const * szError )
  81. {
  82. }
  83. //+-------------------------------------------------------------------------
  84. //
  85. // Member: CTripYYBase::InitState, public
  86. //
  87. // Synopsis: Initialize property and generate method
  88. //
  89. // History: 01-Oct-1997 emilyb created
  90. // 18-Apr-2000 KitmanH Moved from YYPARSER
  91. //
  92. //--------------------------------------------------------------------------
  93. void CTripYYBase::InitState(void)
  94. {
  95. // Push makes a copy of what is passed in.
  96. PushProperty(L"contents");
  97. _currentState.iGenerateMethod = GENERATE_METHOD_EXACT;
  98. // We don't use xwszPropName field of _currentState. Instead,
  99. // we use the prop name stack to get the appropriate propname.
  100. }
  101. //+---------------------------------------------------------------------------
  102. //
  103. // Member: CTripYYBase::GetCurrentProperty, public
  104. //
  105. // Synopsis: Return info on current prop
  106. //
  107. // Arguments: [pp_ps] -- filled with CDbColId * for current prop
  108. // [dbType] -- set to DBTYPE for current prop
  109. //
  110. // History: 01-Oct-1997 emilyb created
  111. // 10-Apr-1999 KrishnaN Modified to use stack
  112. // 18-Apr-2000 KitmanH Moved from YYPARSER
  113. //
  114. //----------------------------------------------------------------------------
  115. void CTripYYBase::GetCurrentProperty(CDbColId ** pp_ps, DBTYPE *dbType)
  116. {
  117. // Get the top most property off of the stack and use it
  118. if ( S_OK != _ColumnMapper.GetPropInfoFromName(
  119. _propNameStack.Get(_propNameStack.Count() - 1),
  120. (DBID **) pp_ps,
  121. dbType,
  122. 0 ) )
  123. THROW( CParserException( QPARSE_E_NO_SUCH_PROPERTY ) );
  124. }
  125. //+---------------------------------------------------------------------------
  126. //
  127. // Member: CTripYYBase::PushProperty, public
  128. //
  129. // Synopsis: Pushes current property onto stack.
  130. //
  131. // Arguments: [pwszProperty] - property
  132. //
  133. // History: 01-Apr-1998 KrishnaN created
  134. // 18-Apr-2000 KitmanH Moved from YYPARSER
  135. //
  136. //----------------------------------------------------------------------------
  137. void CTripYYBase::PushProperty( WCHAR const * wszProperty)
  138. {
  139. // Make a copy and save it. The copy will be automatically deleted
  140. // when the stack self destructs.
  141. int iLen = wcslen(wszProperty) + 1;
  142. XPtrST<WCHAR> xwszPropertyCopy(new WCHAR[iLen]);
  143. RtlCopyMemory(xwszPropertyCopy.GetPointer(), wszProperty, sizeof(WCHAR) * iLen);
  144. _propNameStack.Push(xwszPropertyCopy.GetPointer());
  145. xwszPropertyCopy.Acquire();
  146. }
  147. //+---------------------------------------------------------------------------
  148. //
  149. // Member: CTripYYBase::PopProperty, public
  150. //
  151. // Synopsis: Pops the current property off the stack
  152. //
  153. // History: 10-Apr-1998 KrishnaN created
  154. // 18-Apr-2000 KitmanH Moved from YYPARSER
  155. //
  156. //----------------------------------------------------------------------------
  157. void CTripYYBase::PopProperty(void)
  158. {
  159. // pop the property name off of the stack and delete it
  160. delete _propNameStack.Pop();
  161. }
  162. //+---------------------------------------------------------------------------
  163. //
  164. // Member: CTripYYBase::SetCurrentGenerate, public
  165. //
  166. // Synopsis: Sets current generate method
  167. //
  168. // Arguments: [iGenerateMethod] - generate method
  169. //
  170. // History: 01-Oct-1997 emilyb created
  171. // 18-Apr-2000 KitmanH Moved from YYPARSER
  172. //
  173. //----------------------------------------------------------------------------
  174. void CTripYYBase::SetCurrentGenerate(int iGenerateMethod)
  175. {
  176. _currentState.iGenerateMethod = iGenerateMethod;
  177. }
  178. //+---------------------------------------------------------------------------
  179. //
  180. // Member: CTripYYBase::GetCurrentGenerate, public
  181. //
  182. // Synopsis: Return info on current generate method
  183. //
  184. // Arguments: [iGenerateMethod] -- set to current generate method
  185. //
  186. // History: 01-Oct-1997 emilyb created
  187. // 18-Apr-2000 KitmanH Moved from YYPARSER
  188. //
  189. //----------------------------------------------------------------------------
  190. void CTripYYBase::GetCurrentGenerate(int *iGenerateMethod)
  191. {
  192. *iGenerateMethod = _currentState.iGenerateMethod;
  193. }
  194. //+---------------------------------------------------------------------------
  195. //
  196. // Member: CTripYYBase::SaveState, public
  197. //
  198. // Synopsis: Saves current state on state stack, and inits new state
  199. //
  200. // History: 01-Oct-1997 emilyb created
  201. // 18-Apr-2000 KitmanH Moved from YYPARSER
  202. //
  203. //----------------------------------------------------------------------------
  204. void CTripYYBase::SaveState(void)
  205. {
  206. XPtr <STATE> xState( new STATE );
  207. xState.GetPointer()->iGenerateMethod = _currentState.iGenerateMethod;
  208. // When you save the state, pop the propname off of the
  209. // stack and save the ptr.
  210. xState.GetPointer()->xwszPropName.Set( _propNameStack.Pop() );
  211. _savedStates.Push( xState.GetPointer() );
  212. xState.Acquire();
  213. InitState();
  214. }
  215. //+---------------------------------------------------------------------------
  216. //
  217. // Member: CTripYYBase::RestoreState, public
  218. //
  219. // Synopsis: Restores state from state stack
  220. //
  221. // History: 01-Oct-1997 emilyb created
  222. // 18-Apr-2000 KitmanH Moved from YYPARSER
  223. //
  224. //----------------------------------------------------------------------------
  225. void CTripYYBase::RestoreState(void)
  226. {
  227. XPtr <STATE> xState (_savedStates.Pop());
  228. _currentState.iGenerateMethod = xState.GetPointer()->iGenerateMethod;
  229. Win4Assert(xState.GetPointer()->xwszPropName.GetPointer());
  230. // Push the saved state onto the stack
  231. _propNameStack.Push(xState.GetPointer()->xwszPropName.GetPointer());
  232. xState.GetPointer()->xwszPropName.Acquire();
  233. }
  234. //+---------------------------------------------------------------------------
  235. //
  236. // Member: CTripYYBase::BuildPhrase
  237. //
  238. // Synopsis: Builds a phrase node.
  239. //
  240. // Arguments: [wcsPhrase] - The phrase
  241. // [iGenMethod] - The generation method
  242. //
  243. // History: 01-Apr-1998 KrishnaN created
  244. // 18-Apr-2000 KitmanH Moved from YYPARSER
  245. //
  246. //----------------------------------------------------------------------------
  247. CDbContentRestriction * CTripYYBase::BuildPhrase(WCHAR *wcsPhrase, int iGenMethod)
  248. {
  249. Win4Assert(wcsPhrase);
  250. if (0 == *wcsPhrase)
  251. THROW( CException( QPARSE_E_EXPECTING_PHRASE ) );
  252. CDbColId *pps;
  253. DBTYPE dbType;
  254. GetCurrentProperty(&pps, &dbType);
  255. // We used the property. Now pop it off if need be
  256. if (fDeferredPop)
  257. PopProperty();
  258. // generation method stripped in some cases, but not all.
  259. // if it's there, use it
  260. LPWSTR pLast = wcsPhrase + wcslen(wcsPhrase) - 1;
  261. if (L'"' == *wcsPhrase && L'"' == *pLast)
  262. {
  263. StripQuotes(wcsPhrase);
  264. if (0 == *wcsPhrase)
  265. THROW( CException( QPARSE_E_EXPECTING_PHRASE ) );
  266. }
  267. else
  268. {
  269. if ( L'*' == *pLast) // prefix
  270. {
  271. *pLast-- = L'\0';
  272. SetCurrentGenerate(GENERATE_METHOD_PREFIX);
  273. }
  274. if ( L'*' == *pLast) // inflect
  275. {
  276. *pLast-- = L'\0';
  277. SetCurrentGenerate(GENERATE_METHOD_INFLECT);
  278. }
  279. }
  280. int fuzzy;
  281. GetCurrentGenerate(&fuzzy);
  282. if (0 != iGenMethod)
  283. fuzzy = iGenMethod;
  284. // Clear generation method so it won't rub off on the following phrase
  285. SetCurrentGenerate(GENERATE_METHOD_EXACT);
  286. XDbContentRestriction pRest( new CDbContentRestriction (wcsPhrase, *pps, fuzzy, _lcid));
  287. if( pRest.IsNull() || !pRest->IsValid() )
  288. THROW( CException( E_OUTOFMEMORY ) );
  289. return pRest.Acquire();
  290. }