PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/sablecc-3.2/src/org/sablecc/sablecc/InternalTransformationsToGrammar.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 649 lines | 500 code | 112 blank | 37 comment | 67 complexity | c89704b570a5ba641ace008744cf13b7 MD5 | raw file
  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * This file is part of SableCC. *
  3. * See the file "LICENSE" for copyright information and the *
  4. * terms and conditions for copying, distribution and *
  5. * modification of SableCC. *
  6. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  7. /*
  8. * Last Modification date : October, the 11th 2003
  9. * Goal of the modification : addition of specifier to
  10. * the generated production($prod) to handle list of element.
  11. *
  12. *
  13. */
  14. package org.sablecc.sablecc;
  15. import java.util.*;
  16. import org.sablecc.sablecc.analysis.*;
  17. import org.sablecc.sablecc.node.*;
  18. import java.io.*;
  19. public class InternalTransformationsToGrammar extends DepthFirstAdapter
  20. {
  21. private ResolveIds ids;
  22. private ResolveAltIds altIds;
  23. private ResolveTransformIds transformIds;
  24. private String currentProd;
  25. private int currentAlt;
  26. private String currentAltName;
  27. private boolean processingAst;
  28. private boolean processingProdTransform;
  29. static final int NONE = 0;
  30. static final int STAR = 1;
  31. static final int QMARK = 2;
  32. static final int PLUS = 3;
  33. int count;
  34. int elem;
  35. private LinkedList listSimpleTermTransform;
  36. public final Map simpleTermTransform;
  37. Map mapProductionTransformations;
  38. Map simpleTermOrsimpleListTermTypes;
  39. private Map isElementIsAlist = new TypedTreeMap(
  40. StringComparator.instance,
  41. StringCast.instance,
  42. StringCast.instance);
  43. private LinkedList listProd;
  44. public InternalTransformationsToGrammar(ResolveIds ids, ResolveAltIds altIds,
  45. ResolveTransformIds transformIds,
  46. LinkedList listSimpleTermTransform,
  47. Map simpleTermTransform,
  48. Map mapProductionTransformations,
  49. Map simpleTermOrsimpleListTermTypes)
  50. {
  51. this.ids = ids;
  52. this.altIds = altIds;
  53. this.transformIds = transformIds;
  54. this.listSimpleTermTransform = listSimpleTermTransform;
  55. this.simpleTermTransform = simpleTermTransform;
  56. this.mapProductionTransformations = mapProductionTransformations;
  57. this.simpleTermOrsimpleListTermTypes = simpleTermOrsimpleListTermTypes;
  58. }
  59. public void inAProductions(AProductions node)
  60. {
  61. listProd = node.getProds();
  62. }
  63. private LinkedList listOfAlts;
  64. public void inAAst(AAst node)
  65. {
  66. processingAst = true;
  67. }
  68. public void outAAst(AAst node)
  69. {
  70. processingAst = false;
  71. }
  72. public void caseAProd(AProd node)
  73. {
  74. currentProd = (String) ids.names.get(node);
  75. listOfAlts = new LinkedList();
  76. Object[] list_alt = (Object[])node.getAlts().toArray();
  77. for(int i=0; i<list_alt.length; i++)
  78. {
  79. ((PAlt) list_alt[i]).apply(this);
  80. }
  81. node.setAlts(listOfAlts);
  82. }
  83. private LinkedList listElems;
  84. private AAlt aParsedAlt;
  85. private LinkedList listElemsAltTransform;
  86. private String currentNewAltName;
  87. boolean countElementNecessary;
  88. LinkedList listOfAlternativeElemsWHaveName;
  89. public void caseAAlt(AAlt node)
  90. {
  91. count = 1;
  92. currentAltName = (String) ids.names.get(node);
  93. AAltTransform currentAltTransform = (AAltTransform)node.getAltTransform();
  94. listOfAlternativeElemsWHaveName = new LinkedList();
  95. node.apply(new DepthFirstAdapter()
  96. {
  97. public void inAElem(AElem node)
  98. {
  99. InternalTransformationsToGrammar.this.setOut(node, new Integer(NONE));
  100. }
  101. public void caseAStarUnOp(AStarUnOp node)
  102. {
  103. count *= 2;
  104. InternalTransformationsToGrammar.this.setOut(node.parent(), new Integer(STAR));
  105. }
  106. public void caseAQMarkUnOp(AQMarkUnOp node)
  107. {
  108. count *= 2;
  109. InternalTransformationsToGrammar.this.setOut(node.parent(), new Integer(QMARK));
  110. }
  111. public void caseAPlusUnOp(APlusUnOp node)
  112. {
  113. InternalTransformationsToGrammar.this.setOut(node.parent(), new Integer(PLUS));
  114. }
  115. }
  116. );
  117. if(count == 1)
  118. {
  119. listElems = new LinkedList();
  120. listElemsAltTransform = new LinkedList();
  121. countElementNecessary = false;
  122. Object temp[] = node.getElems().toArray();
  123. for(int i = 0; i < temp.length; i++)
  124. {
  125. Object obj = temp[i];
  126. if( ((AElem)obj).getUnOp() != null &&
  127. ( ((AElem)obj).getUnOp() instanceof AQMarkUnOp ||
  128. ((AElem)obj).getUnOp() instanceof AStarUnOp )
  129. )
  130. {
  131. if(!countElementNecessary)
  132. {
  133. countElementNecessary = true;
  134. }
  135. }
  136. }
  137. for(int i = 0; i < temp.length; i++)
  138. {
  139. ((PElem)temp[i]).apply(this);
  140. }
  141. TId nameOfAlt = null;
  142. if(node.getAltName() != null)
  143. {
  144. nameOfAlt = (TId)node.getAltName().clone();
  145. }
  146. currentNewAltName = currentProd + "." + currentAltName.toLowerCase();
  147. altIds.alts_elems.put(currentNewAltName, listElemsAltTransform);
  148. altIds.alts_elems_list_elemName.put(currentNewAltName, listOfAlternativeElemsWHaveName);
  149. AAltTransform altTransform = (AAltTransform)currentAltTransform.clone();
  150. AltTransformAdapter altTransformAdapter =
  151. new AltTransformAdapter(simpleTermTransform, listSimpleTermTransform,
  152. currentNewAltName, altIds,
  153. isElementIsAlist,
  154. simpleTermOrsimpleListTermTypes);
  155. altTransform.apply(altTransformAdapter);
  156. aParsedAlt = new AAlt(nameOfAlt, listElems, altTransform);
  157. ids.names.put(aParsedAlt, ids.names.get(node));
  158. listOfAlts.add(aParsedAlt);
  159. }
  160. else
  161. {
  162. int max = count;
  163. AAltTransform altTransform;
  164. for(count = 0; count < max; count++)
  165. {
  166. listElems = new LinkedList();
  167. listElemsAltTransform = new LinkedList();
  168. elem = 0;
  169. currentNewAltName = currentProd + "." + currentAltName.toLowerCase()+(count + 1);
  170. countElementNecessary = false;
  171. Object temp[] = node.getElems().toArray();
  172. for(int i = 0; i < temp.length; i++)
  173. {
  174. Object obj = temp[i];
  175. if( ((AElem)obj).getUnOp() != null &&
  176. ( ((AElem)obj).getUnOp() instanceof AQMarkUnOp ||
  177. ((AElem)obj).getUnOp() instanceof AStarUnOp )
  178. )
  179. {
  180. if(!countElementNecessary)
  181. {
  182. countElementNecessary = true;
  183. }
  184. }
  185. }
  186. for(int i = 0; i < temp.length; i++)
  187. {
  188. ((PElem)temp[i]).apply(this);
  189. }
  190. altIds.alts_elems.put(currentNewAltName, listElemsAltTransform);
  191. altIds.alts_elems_list_elemName.put(currentNewAltName, listOfAlternativeElemsWHaveName);
  192. altTransform = (AAltTransform)currentAltTransform.clone();
  193. AltTransformAdapter altTransformAdapter =
  194. new AltTransformAdapter(simpleTermTransform, listSimpleTermTransform,
  195. currentNewAltName, altIds,
  196. isElementIsAlist,
  197. simpleTermOrsimpleListTermTypes);
  198. altTransform.apply(altTransformAdapter);
  199. aParsedAlt = new AAlt(new TId(currentAltName.toLowerCase()+(count + 1)), listElems, altTransform);
  200. String currentAltInlining;
  201. currentAltInlining = "A" + ids.name(aParsedAlt.getAltName().getText()) + currentProd;
  202. ids.names.put(aParsedAlt, currentAltInlining);
  203. listOfAlts.add(aParsedAlt);
  204. }
  205. }
  206. }
  207. LinkedList checkCreationOfXElem = new TypedLinkedList(StringCast.instance);
  208. //It's also available for Ignored alternatives
  209. public void caseAElem(AElem node)
  210. {
  211. if(!processingAst)
  212. {
  213. int op = ((Integer) getOut(node)).intValue();
  214. String name = (String) ids.elemTypes.get(node);
  215. String numero = (countElementNecessary == true ? ""+(count+1) : "" );
  216. String qMarkOrPlusElemType;
  217. String elemNameOfElem = null;
  218. TId aElemName = null;
  219. PSpecifier specifier = null;
  220. if(node.getElemName() != null)
  221. {
  222. elemNameOfElem = node.getElemName().getText();
  223. aElemName = new TId(elemNameOfElem);
  224. }
  225. if(node.getSpecifier() != null)
  226. {
  227. if(node.getSpecifier() instanceof ATokenSpecifier)
  228. {
  229. specifier = new ATokenSpecifier();
  230. }
  231. else
  232. {
  233. specifier = new AProductionSpecifier();
  234. }
  235. }
  236. AElem aElem = null;
  237. String elemName = node.getId().getText();
  238. boolean ok = false;
  239. boolean oklist = false;
  240. switch(op)
  241. {
  242. case NONE:
  243. {
  244. aElem = new AElem(aElemName, specifier, new TId(elemName), null);
  245. if(elemNameOfElem != null)
  246. {
  247. ids.names.put(aElem, ids.name(elemNameOfElem));
  248. }
  249. else
  250. {
  251. ids.names.put(aElem, ids.name(elemName));
  252. }
  253. ok = true;
  254. }
  255. break;
  256. case STAR:
  257. {
  258. if((count & (1 << elem)) != 0)
  259. {
  260. qMarkOrPlusElemType = (String)ids.elemTypes.get(node);
  261. LinkedList tmpProdTransform = (LinkedList)mapProductionTransformations.get(qMarkOrPlusElemType);
  262. if(!checkCreationOfXElem.contains("$" + elemName))
  263. {
  264. checkCreationOfXElem.add("$" + elemName);
  265. listProd.add( createXelemProduction("$" + elemName, qMarkOrPlusElemType,
  266. name, tmpProdTransform) );
  267. }
  268. elemName = "$" + elemName;
  269. aElem = new AElem(aElemName, new AProductionSpecifier(), new TId(elemName), null);
  270. if(elemNameOfElem != null)
  271. {
  272. ids.names.put(aElem, ids.name(elemNameOfElem));
  273. }
  274. else
  275. {
  276. ids.names.put(aElem, ids.name(elemName));
  277. }
  278. ok = true;
  279. oklist = true;
  280. }
  281. elem++;
  282. }
  283. break;
  284. case QMARK:
  285. {
  286. if((count & (1 << elem)) != 0)
  287. {
  288. aElem = new AElem(aElemName, specifier, new TId(elemName), null);
  289. if(elemNameOfElem != null)
  290. {
  291. ids.names.put(aElem, ids.name(elemNameOfElem));
  292. }
  293. else
  294. {
  295. ids.names.put(aElem, ids.name(elemName));
  296. }
  297. ok = true;
  298. }
  299. elem++;
  300. }
  301. break;
  302. case PLUS:
  303. {
  304. qMarkOrPlusElemType = (String)ids.elemTypes.get(node);
  305. LinkedList tmpProdTransform = (LinkedList)mapProductionTransformations.get(qMarkOrPlusElemType);
  306. if(!checkCreationOfXElem.contains("$" + elemName))
  307. {
  308. checkCreationOfXElem.add("$" + elemName);
  309. listProd.add( createXelemProduction("$" + elemName, qMarkOrPlusElemType,
  310. name, tmpProdTransform) );
  311. }
  312. elemName = "$" + elemName;
  313. aElem = new AElem(aElemName, new AProductionSpecifier(), new TId(elemName), null);
  314. if(elemNameOfElem != null)
  315. {
  316. ids.names.put(aElem, ids.name(elemNameOfElem));
  317. }
  318. else
  319. {
  320. ids.names.put(aElem, ids.name(elemName));
  321. }
  322. ok = true;
  323. oklist = true;
  324. }
  325. break;
  326. }
  327. if(ok)
  328. {
  329. if(aElemName != null)
  330. {
  331. listElemsAltTransform.add(aElemName.getText());
  332. if(oklist)
  333. {
  334. if(elemNameOfElem != null)
  335. {
  336. listOfAlternativeElemsWHaveName.add(elemNameOfElem);
  337. }
  338. isElementIsAlist.put(currentProd+"."+currentAltName.toLowerCase()+numero+aElemName.getText(),
  339. node.getId().getText());
  340. }
  341. }
  342. else
  343. {
  344. listElemsAltTransform.add(elemName);
  345. if(oklist)
  346. {
  347. if(elemNameOfElem != null)
  348. {
  349. listOfAlternativeElemsWHaveName.add(elemNameOfElem);
  350. }
  351. isElementIsAlist.put(currentProd+"."+currentAltName.toLowerCase()+numero+node.getId().getText(),
  352. node.getId().getText());
  353. }
  354. }
  355. }
  356. if(aElem != null)
  357. {
  358. listElems.add(aElem);
  359. }
  360. }
  361. }
  362. /*
  363. This method creates the production for star(*) and plus(+) substitution in the grammar
  364. elem* -> $elem |
  365. elem
  366. This creates the production ::
  367. $elem {-> elem* }
  368. = {nonTerminal} $elem elem {-> [$elem.elem elem] }
  369. | {terminal} elem {-> [elem] }
  370. ;
  371. */
  372. public AProd createXelemProduction(final String name, final String elemTypeName,
  373. String XproductionName,
  374. LinkedList nodeProdTransform)
  375. {
  376. final String rname = name.substring(1);
  377. LinkedList listOfAltsXelem = new LinkedList();
  378. if(nodeProdTransform != null)
  379. {
  380. nodeProdTransform = (LinkedList)cloneList(nodeProdTransform);
  381. //Creation of the production transformation for Xelem
  382. //if the production transformation is introduced by the software
  383. if(nodeProdTransform.size() == 1)
  384. {
  385. AElem elem = (AElem)nodeProdTransform.get(0);
  386. if(elem.getUnOp() == null && elem.getId().getText().equals(rname))
  387. {
  388. LinkedList elemsProdTransform = new LinkedList();
  389. elemsProdTransform.add( new AElem( null, new AProductionSpecifier(), new TId(rname), new AStarUnOp() ) );
  390. nodeProdTransform = elemsProdTransform;
  391. }
  392. }
  393. }
  394. //That means elem is token type
  395. else
  396. {
  397. String name_resolved = ids.name(name);
  398. LinkedList elemsProdTransform = new LinkedList();
  399. elemsProdTransform.add( new AElem( null, new ATokenSpecifier(), new TId(rname), new AStarUnOp() ) );
  400. nodeProdTransform = elemsProdTransform;
  401. }
  402. final LinkedList listProdTransformationOfXelem = new LinkedList();
  403. AElem []temp_listProdTransform = (AElem[])nodeProdTransform.toArray(new AElem[0]);
  404. for(int i=0; i<temp_listProdTransform.length; i++)
  405. {
  406. temp_listProdTransform[i].apply( new DepthFirstAdapter()
  407. {
  408. public void caseAElem(AElem node)
  409. {
  410. //The production transformation needs to have a star operator.
  411. node.setUnOp(new AStarUnOp(new TStar()));
  412. if(node.getElemName() != null)
  413. {
  414. listProdTransformationOfXelem.add( node.getElemName().getText() );
  415. }
  416. else
  417. {
  418. listProdTransformationOfXelem.add( node.getId().getText() );
  419. }
  420. }
  421. }
  422. );
  423. }
  424. //creation of the first AltTransform node
  425. AElem[] prodTransformElems = (AElem[]) nodeProdTransform.toArray(new AElem[0]);
  426. final LinkedList listTerms_first = new LinkedList();
  427. for(int i = 0; i < prodTransformElems.length; i++)
  428. {
  429. prodTransformElems[i].apply(new AnalysisAdapter()
  430. {
  431. public void caseAElem(AElem node)
  432. {
  433. String tmpNodeName = ( (node.getElemName() == null) ? node.getId().getText() :
  434. node.getElemName().getText() );
  435. LinkedList listAListTerm_first = new LinkedList();
  436. if(elemTypeName.startsWith("T"))
  437. {
  438. listAListTerm_first.add(new ASimpleListTerm(new ATokenSpecifier(),
  439. new TId(rname), null ));
  440. }
  441. else
  442. {
  443. listAListTerm_first.add(new ASimpleListTerm(new AProductionSpecifier(),
  444. new TId(rname),new TId(tmpNodeName) ) );
  445. }
  446. listTerms_first.add( new AListTerm(new TLBkt(), listAListTerm_first) );
  447. }
  448. }
  449. );
  450. }
  451. AAltTransform aAltTransform = new AAltTransform(new TLBrace(), listTerms_first, new TRBrace());
  452. //create the first list of elems of an alternative
  453. LinkedList elems = new LinkedList();
  454. AElem aElemFirstTobeAdded;
  455. //the elem is a token
  456. if(elemTypeName.startsWith("T"))
  457. {
  458. aElemFirstTobeAdded = new AElem(null, new ATokenSpecifier(), new TId(rname), null);
  459. }
  460. else
  461. {
  462. aElemFirstTobeAdded = new AElem(null, new AProductionSpecifier(), new TId(rname), null);
  463. }
  464. elems.add(aElemFirstTobeAdded);
  465. //creation of the first alternative
  466. AAlt aParsedAlt = new AAlt(new TId("terminal"), elems, aAltTransform);
  467. String terminal_altName = "ATerminal" + ids.name(name);
  468. listOfAltsXelem.add(aParsedAlt);
  469. //create the second AltTransform node
  470. prodTransformElems = (AElem[]) nodeProdTransform.toArray(new AElem[0]);
  471. final LinkedList listTerms_second = new LinkedList();
  472. for(int i = 0; i < prodTransformElems.length; i++)
  473. {
  474. prodTransformElems[i].apply(new AnalysisAdapter()
  475. {
  476. public void caseAElem(AElem node)
  477. {
  478. String tmpNodeName = ( (node.getElemName() == null) ? node.getId().getText() :
  479. node.getElemName().getText() );
  480. LinkedList listAListTerm_second = new LinkedList();
  481. listAListTerm_second.add(new ASimpleListTerm(null, new TId(name),
  482. new TId(tmpNodeName)) );
  483. if(elemTypeName.startsWith("T"))
  484. {
  485. listAListTerm_second.add(new ASimpleListTerm(new ATokenSpecifier(),
  486. new TId(rname), null ));
  487. }
  488. else
  489. {
  490. listAListTerm_second.add(new ASimpleListTerm(new AProductionSpecifier(),
  491. new TId(rname),
  492. new TId(tmpNodeName) ) );
  493. }
  494. listTerms_second.add(new AListTerm(new TLBkt(), listAListTerm_second));
  495. }
  496. }
  497. );
  498. }
  499. aAltTransform = new AAltTransform(new TLBrace(), listTerms_second, new TRBrace());
  500. //creation of the second list of elems of an alternative :: two elems
  501. elems = new LinkedList();
  502. //first elem
  503. AElem aElemSecondTobeAdded = new AElem(null, new AProductionSpecifier(), new TId(name), null);
  504. elems.add(aElemSecondTobeAdded);
  505. //second elem
  506. if(elemTypeName.startsWith("T"))
  507. {
  508. aElemSecondTobeAdded = new AElem(null, new ATokenSpecifier(), new TId(rname), null);
  509. }
  510. else
  511. {
  512. aElemSecondTobeAdded = new AElem(null, new AProductionSpecifier(), new TId(rname), null);
  513. }
  514. elems.add(aElemSecondTobeAdded);
  515. aParsedAlt = new AAlt(new TId("non_terminal"), elems, aAltTransform);
  516. String nonTerminal_altName = "ANonTerminal" + ids.name(name);
  517. listOfAltsXelem.add(aParsedAlt);
  518. AProd prodToReturn = new AProd(new TId(name), new TArrow(), nodeProdTransform, listOfAltsXelem);
  519. prodToReturn.apply(ids);
  520. prodToReturn.apply(transformIds.getProdTransformIds());
  521. return prodToReturn;
  522. }
  523. private List cloneList(List list)
  524. {
  525. List clone = new LinkedList();
  526. for(Iterator i = list.iterator(); i.hasNext();)
  527. {
  528. clone.add(((Node) i.next()).clone());
  529. }
  530. return clone;
  531. }
  532. private String xproductionType(String name)
  533. {
  534. return "P$" + name.substring(1).toLowerCase();
  535. }
  536. }