Source code: org/sablecc/sablecc/AddProdTransformAndAltTransform.java
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 package org.sablecc.sablecc;
9
10 import org.sablecc.sablecc.analysis.*;
11 import org.sablecc.sablecc.node.*;
12 import java.util.*;
13 import java.io.*;
14
15 /*
16 * AddProdTransformAndAltTransform
17 *
18 * This class provide the second part of the support by SableCC3.x.x
19 * for SableCC2.x.x grammars.
20 * Its role is to add Productions and Alternatives transformations within
21 * Productions section.
22 * Assuming this is run after the eventual AddAstProductions it also add
23 * default transformations to productions and alternatives which have not
24 * not specified them.
25 */
26
27 public class AddProdTransformAndAltTransform extends DepthFirstAdapter
28 {
29
30 private String currentProdId;
31 private String currentAlt;
32
33 public void inAProd(final AProd production)
34 {
35 currentProdId = production.getId().getText();
36
37 if(production.getArrow() == null)
38 {
39 AElem elem = new AElem(null, new AProductionSpecifier(), new TId(currentProdId), null);
40 LinkedList listOfProdTransformElem = new LinkedList();
41 listOfProdTransformElem.add(elem);
42 production.setProdTransform(listOfProdTransformElem);
43 production.setArrow(new TArrow());
44 }
45 }
46
47 private int i;
48 private LinkedList list;
49
50 public void inAParsedAlt(AParsedAlt alt)
51 {
52 if(alt.getAltTransform() == null)
53 {
54 currentAlt = currentProdId;
55 list = new LinkedList();
56 AProdName aProdName = new AProdName(new TId(currentProdId), null);
57
58 if(alt.getAltName() != null)
59 {
60 aProdName.setProdNameTail( new TId(alt.getAltName().getText()) );
61 }
62
63 if( alt.getElems().size() > 0 )
64 {
65 Object temp[] = alt.getElems().toArray();
66
67 for(i = 0; i < temp.length; i++)
68 {
69 ((PElem) temp[i]).apply(new DepthFirstAdapter()
70 {
71 public void caseAElem(AElem elem)
72 {
73 PTerm term;
74 String termId;
75 boolean elemNameExplicitelySpecified = false;
76
77 if(elem.getElemName() != null)
78 {
79 termId = elem.getElemName().getText();
80 elemNameExplicitelySpecified = true;
81 }
82 else
83 {
84 termId = elem.getId().getText();
85 }
86
87 if( (elem.getUnOp() != null) &&
88 ( (elem.getUnOp() instanceof AStarUnOp) || (elem.getUnOp() instanceof APlusUnOp) ) )
89 {
90 LinkedList listP = new LinkedList();
91 if( !elemNameExplicitelySpecified && (elem.getSpecifier()!= null) )
92 {
93 if(elem.getSpecifier() instanceof ATokenSpecifier)
94 {
95 listP.add( new ASimpleListTerm(new ATokenSpecifier(), new TId(termId), null ) );
96 term = new AListTerm(new TLBkt(), listP);
97 }
98 else
99 {
100 listP.add( new ASimpleListTerm(new AProductionSpecifier(), new TId(termId), null ) );
101 term = new AListTerm(new TLBkt(), listP);
102 }
103 }
104 else
105 {
106 listP.add( new ASimpleListTerm(null, new TId(termId), null) );
107 term = new AListTerm(new TLBkt(), listP);
108 }
109 }
110 else
111 {
112 if( !elemNameExplicitelySpecified && (elem.getSpecifier()!= null) )
113 {
114
115 if(elem.getSpecifier() instanceof ATokenSpecifier)
116 {
117 term = new ASimpleTerm( new ATokenSpecifier(), new TId(termId), null);
118 }
119 else
120 {
121 term = new ASimpleTerm( new AProductionSpecifier(), new TId(termId), null);
122 }
123 }
124 else
125 {
126 term = new ASimpleTerm( null, new TId(termId), null);
127 }
128 }
129
130 list.add(term);
131 }
132 }
133 );
134 }
135 }
136
137 ANewTerm newTerm = new ANewTerm(aProdName, new TLPar(), list);
138 LinkedList lst = new LinkedList();
139 lst.add(newTerm);
140
141 alt.setAltTransform(new AAltTransform(new TLBrace(), lst, new TRBrace()));
142 }
143 }
144 }