/NacaTrans/src/parser/Cobol/elements/CEvaluate.java

https://github.com/charleso/naca · Java · 283 lines · 230 code · 12 blank · 41 comment · 48 complexity · e8eb8ae0d031a7aa897a7cbd1d34d118 MD5 · raw file

  1. /*
  2. * NacaRTTests - Naca Tests for NacaRT support.
  3. *
  4. * Copyright (c) 2005, 2006, 2007, 2008 Publicitas SA.
  5. * Licensed under GPL (GPL-LICENSE.txt) license.
  6. */
  7. /*
  8. * Created on Jul 19, 2004
  9. *
  10. * To change the template for this generated file go to
  11. * Window>Preferences>Java>Code Generation>Code and Comments
  12. */
  13. package parser.Cobol.elements;
  14. import java.util.Vector;
  15. import lexer.*;
  16. import lexer.Cobol.CCobolKeywordList;
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.Element;
  19. import parser.Cobol.CCobolElement;
  20. import parser.condition.CCondAndStatement;
  21. import parser.condition.CCondEqualsStatement;
  22. import parser.condition.CCondGreaterStatement;
  23. import parser.condition.CCondLessStatement;
  24. import parser.condition.CCondNotStatement;
  25. import parser.condition.CCondOrStatement;
  26. import parser.expression.CConstantTerminal;
  27. import parser.expression.CExpression;
  28. import parser.expression.CTermExpression;
  29. import semantic.CBaseLanguageEntity;
  30. import semantic.CBaseEntityFactory;
  31. import semantic.Verbs.CEntitySwitchCase;
  32. import utils.CGlobalEntityCounter;
  33. import utils.Transcoder;
  34. /**
  35. * @author U930CV
  36. *
  37. * To change the template for this generated type comment go to
  38. * Window>Preferences>Java>Code Generation>Code and Comments
  39. */
  40. public class CEvaluate extends CCobolElement
  41. {
  42. /**
  43. * @param line
  44. */
  45. public CEvaluate(int line) {
  46. super(line);
  47. }
  48. /* (non-Javadoc)
  49. * @see parser.CLanguageElement#Parse(lexer.CTokenList)
  50. */
  51. protected boolean DoParsing()
  52. { // start parsing EVALUATE statement
  53. CBaseToken tokEval = GetCurrentToken() ;
  54. if (tokEval.GetKeyword() != CCobolKeywordList.EVALUATE)
  55. {
  56. Transcoder.logError(getLine(), "Expecting 'EVALUATE' keyword") ;
  57. return false ;
  58. }
  59. CGlobalEntityCounter.GetInstance().CountCobolVerb(tokEval.GetKeyword().m_Name) ;
  60. // read evaluate expression
  61. boolean bDone = false ;
  62. while (!bDone)
  63. {
  64. CBaseToken tokId = GetNext() ;
  65. CExpression term = ReadExpression() ;
  66. if (term == null)
  67. {
  68. Transcoder.logError(getLine(), "Can't read terminal to evaluate") ;
  69. return false ;
  70. }
  71. else
  72. {
  73. m_arrIdToEval.add(term) ;
  74. }
  75. CBaseToken tokAlso = GetCurrentToken() ;
  76. if (tokAlso.GetKeyword() != CCobolKeywordList.ALSO)
  77. {
  78. bDone = true ;
  79. }
  80. else
  81. {
  82. //GetNext();
  83. }
  84. }
  85. // read WHEN statements
  86. bDone = false ;
  87. while (!bDone)
  88. {
  89. CBaseToken tokWhen = GetCurrentToken();
  90. if (tokWhen.GetType()==CTokenType.KEYWORD && tokWhen.GetKeyword()==CCobolKeywordList.WHEN)
  91. {
  92. CExpression cond = ReadWhenCondition();
  93. if (cond == null)
  94. {
  95. Transcoder.logError(getLine(), "Can't read condition") ;
  96. return false ;
  97. }
  98. CCobolElement eWhen = new CWhenBloc(cond, tokWhen.getLine());
  99. AddChild(eWhen) ;
  100. if (!Parse(eWhen))
  101. {
  102. Transcoder.logError(getLine(), "Can't parse WHEN statement") ;
  103. return false ;
  104. }
  105. }
  106. // else if (tokWhen.GetType() == CTokenType.COMMENT)
  107. // {
  108. // if (!ParseComment())
  109. // {
  110. // return false ;
  111. // }
  112. // }
  113. else if (tokWhen.GetType() == CTokenType.KEYWORD && tokWhen.GetKeyword()==CCobolKeywordList.END_EVALUATE)
  114. {
  115. GetNext() ;
  116. bDone = true ;
  117. }
  118. else
  119. {
  120. //m_Logger.error("ERROR Line " +getLine()+ " : " + "Unexpected token : " + tokWhen.GetValue()) ;
  121. bDone = true ;
  122. }
  123. }
  124. return true ;
  125. }
  126. protected CExpression ReadWhenCondition()
  127. {
  128. CExpression condGlobal = null ;
  129. boolean bDone = false ;
  130. while (!bDone)
  131. { // there are maybe several 'when' clauses
  132. int i = 0;
  133. CBaseToken tokWhen = GetCurrentToken();
  134. CExpression cond = null;
  135. if (tokWhen.GetKeyword() == CCobolKeywordList.WHEN)
  136. {
  137. CBaseToken tokOther = GetNext();
  138. if (tokOther.GetKeyword() == CCobolKeywordList.OTHER)
  139. {
  140. GetNext() ;
  141. return new CTermExpression(tokOther.getLine(), new CConstantTerminal("OTHER")) ;
  142. }
  143. else
  144. {
  145. cond = ReadSingleCondition(i) ;
  146. i++ ;
  147. while (m_arrIdToEval.size() > i)
  148. { // there must be some 'also' statements
  149. CBaseToken tok = GetCurrentToken();
  150. if (tok.GetKeyword() == CCobolKeywordList.ALSO)
  151. {
  152. GetNext() ;
  153. CExpression tempcond = ReadSingleCondition(i) ;
  154. i++ ;
  155. if (tempcond != null)
  156. {
  157. if (cond == null)
  158. {
  159. cond = tempcond;
  160. }
  161. else
  162. {
  163. cond = new CCondAndStatement(tok.getLine(), cond, tempcond) ;
  164. }
  165. }
  166. }
  167. else
  168. {
  169. Transcoder.logError(getLine(), "Expecting 'ALSO' keyword") ;
  170. return null ; // there must be a 'also' keyword, because there are more than one eval expressions
  171. }
  172. }
  173. }
  174. }
  175. else
  176. {
  177. bDone = true ;
  178. }
  179. if (cond != null)
  180. {
  181. if (condGlobal == null)
  182. {
  183. condGlobal = cond ;
  184. }
  185. else
  186. {
  187. condGlobal = new CCondOrStatement(tokWhen.getLine(), condGlobal, cond) ;
  188. }
  189. }
  190. }
  191. return condGlobal ;
  192. }
  193. protected CExpression ReadSingleCondition(int i)
  194. {
  195. CExpression cond = null ;
  196. CBaseToken tok = GetCurrentToken() ;
  197. if (tok.GetKeyword() == CCobolKeywordList.ANY)
  198. {
  199. GetNext();
  200. return null ; // in this case the statement is trivial.
  201. }
  202. else if (tok.GetKeyword() == CCobolKeywordList.NOT)
  203. {
  204. GetNext() ; // consume NOT
  205. CExpression term = ReadExpression() ;
  206. if (term != null)
  207. {
  208. cond = new CCondEqualsStatement(tok.getLine(), m_arrIdToEval.get(i), term) ;
  209. cond = new CCondNotStatement(tok.getLine(), cond) ;
  210. }
  211. else
  212. {
  213. Transcoder.logError(getLine(), "Expecting terminal in expression") ;
  214. return null ; // a terminal must be found
  215. }
  216. }
  217. else
  218. {
  219. CExpression term = ReadExpression() ;
  220. if (term != null)
  221. {
  222. CExpression expr = m_arrIdToEval.get(i) ;
  223. CBaseToken tokThru = GetCurrentToken() ;
  224. if (tokThru.GetKeyword() == CCobolKeywordList.THRU || tokThru.GetKeyword() == CCobolKeywordList.THROUGH)
  225. {
  226. GetNext();
  227. CExpression term2 = ReadExpression() ;
  228. if (term2 != null)
  229. {
  230. CExpression e1 = new CCondGreaterStatement(tokThru.getLine(), expr, term, true) ;
  231. CExpression e2 = new CCondLessStatement(tokThru.getLine(), expr, term2, true) ;
  232. cond = new CCondAndStatement(tokThru.getLine(), e1, e2);
  233. }
  234. else
  235. {
  236. Transcoder.logError(getLine(), "Can't read expression") ;
  237. return null ;
  238. }
  239. }
  240. else
  241. {
  242. cond = new CCondEqualsStatement(tokThru.getLine(), expr, term) ;
  243. }
  244. }
  245. else
  246. {
  247. Transcoder.logError(getLine(), "Expecting terminal in expression") ;
  248. return null ; // a terminal must be found
  249. }
  250. }
  251. return cond ;
  252. }
  253. /* (non-Javadoc)
  254. * @see parser.CLanguageElement#ExportCustom(org.w3c.dom.Document)
  255. */
  256. protected Element ExportCustom(Document root)
  257. {
  258. Element e = root.createElement("Evaluate") ;
  259. return e ;
  260. }
  261. protected Vector<CExpression> m_arrIdToEval = new Vector<CExpression>() ;
  262. /* (non-Javadoc)
  263. * @see parser.CBaseElement#DoCustomSemanticAnalysis(semantic.CBaseSemanticEntity, semantic.CBaseSemanticEntityFactory)
  264. */
  265. protected CBaseLanguageEntity DoCustomSemanticAnalysis(CBaseLanguageEntity parent, CBaseEntityFactory factory)
  266. {
  267. CEntitySwitchCase e = factory.NewEntitySwitchCase(getLine()) ;
  268. parent.AddChild(e) ;
  269. return e ;
  270. }
  271. }