Ticket #252: phc_xml_to_past_xml.xsl

File phc_xml_to_past_xml.xsl, 8.8 KB (added by H4ck3rm1k3, 13 years ago)

an updated phc xlst needs more work

Line 
1<?xml version='1.0'?>
2<xsl:stylesheet
3  xmlns:AST="http://www.phpcompiler.org/phc-1.1"
4  xmlns:past="http://www.parrotcode.org/PAST-0.1"
5  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
6  version="1.0" >
7
8<!--
9
10TODO: AST:source_rep
11
12-->
13<!--
14
15$Id: phc_xml_to_past_xml.xsl 33276 2008-11-27 20:12:54Z bernhard $
16
17This transformation takes a XML abstract syntax tree as generated
18by PHC from PHP source code. It generates an XML representation of a PAST data structure.
19
20-->
21<xsl:output method='xml' indent='yes' />
22
23<xsl:template match="/">
24  <xsl:apply-templates select="AST:PHP_script" />
25</xsl:template>
26
27<xsl:template match="AST:PHP_script" >
28  <xsl:apply-templates select="AST:PHP_class_def_list" />
29</xsl:template>
30
31<xsl:template match="AST:PHP_class_def_list" >
32  <xsl:apply-templates select="AST:PHP_class_def" />
33</xsl:template>
34
35<xsl:template match="AST:PHP_class_def" >
36</xsl:template>
37<xsl:template match="AST:PHP_class_def[AST:CLASS_NAME[AST:value='%MAIN%']]">
38  <xsl:apply-templates select="AST:PHP_member_list" />
39</xsl:template>
40
41<xsl:template match="AST:PHP_member_list" >
42  <xsl:apply-templates select="AST:PHP_method" />
43</xsl:template>
44
45<xsl:template match="AST:PHP_method" >
46</xsl:template>
47<xsl:template match="AST:PHP_method[AST:PHP_signature[AST:METHOD_NAME[AST:value='%run%']]]">
48  <xsl:apply-templates select="AST:PHP_statement_list" />
49</xsl:template>
50
51<xsl:template match="AST:PHP_statement_list" >
52  <past:Stmts>
53    <xsl:apply-templates select="AST:PHP_eval_expr" />
54    <xsl:apply-templates select="AST:PHP_if" />
55  </past:Stmts>
56</xsl:template>
57
58<xsl:template match="AST:PHP_eval_expr" >
59  <xsl:apply-templates select="AST:PHP_method_invocation" />
60  <xsl:apply-templates select="AST:PHP_assignment" />
61</xsl:template>
62
63<xsl:template match="AST:PHP_method_invocation" >
64  <past:Op>
65    <xsl:attribute name="name" ><xsl:value-of select="AST:METHOD_NAME/AST:value" /></xsl:attribute>
66    <xsl:apply-templates select="AST:PHP_actual_parameter_list" />
67  </past:Op>
68</xsl:template>
69
70<xsl:template match="AST:PHP_actual_parameter_list" >
71  <xsl:apply-templates select="AST:PHP_actual_parameter" />
72</xsl:template>
73
74<xsl:template match="AST:PHP_actual_parameter" >
75  <xsl:apply-templates select="  AST:PHP_method_invocation
76                               | AST:STRING
77                               | AST:INT
78                               | AST:BOOL
79                               | AST:NULL
80                               | AST:REAL
81                               | AST:PHP_bin_op
82                               | AST:PHP_unary_op
83                               | AST:PHP_variable
84                               | AST:PHP_constant" />
85</xsl:template>
86
87<xsl:template match="AST:PHP_bin_op" >
88  <past:Op>
89
90    <xsl:attribute name="pirop" >
91      <xsl:choose>
92        <xsl:when test="AST:OP/AST:value = '+'" >n_add</xsl:when>
93        <xsl:when test="AST:OP/AST:value = '-'" >n_sub</xsl:when>
94        <xsl:when test="AST:OP/AST:value = '*'" >n_mul</xsl:when>
95        <xsl:when test="AST:OP/AST:value = '/'" >n_div</xsl:when>
96        <xsl:when test="AST:OP/AST:value = '%'" >n_mod</xsl:when>
97      </xsl:choose>
98    </xsl:attribute>
99
100    <xsl:attribute name="name" >
101      <xsl:choose>
102        <xsl:when test="AST:OP/AST:value/@encoding = 'base64'" >
103          <xsl:choose>
104            <xsl:when test="AST:OP/AST:value = 'PA=='" >infix:&lt;</xsl:when>
105            <xsl:when test="AST:OP/AST:value = 'PD0='" >infix:&lt;=</xsl:when>
106            <xsl:when test="AST:OP/AST:value = 'Pj0='" >infix:&gt;=</xsl:when>
107            <xsl:when test="AST:OP/AST:value = 'Pg=='" >infix:&gt;</xsl:when>
108            <xsl:when test="AST:OP/AST:value = 'Jg=='" >infix:&amp;</xsl:when>
109          </xsl:choose>
110        </xsl:when>
111        <xsl:when test="AST:OP/AST:value = '&amp;&amp;'" >infix:AND</xsl:when>
112        <xsl:when test="AST:OP/AST:value = '||'" >infix:OR</xsl:when>
113        <xsl:otherwise>
114          <xsl:value-of select="concat('infix:', AST:OP/AST:value)" />
115        </xsl:otherwise>
116      </xsl:choose>
117    </xsl:attribute>
118
119    <xsl:apply-templates select="  AST:STRING
120                                 | AST:INT
121                                 | AST:REAL
122                                 | AST:PHP_bin_op
123                                 | AST:PHP_unary_op
124                                 | AST:PHP_variable
125                                 | AST:PHP_constant" />
126
127  </past:Op>
128</xsl:template>
129
130<xsl:template match="AST:PHP_unary_op" >
131  <past:Op >
132    <xsl:attribute name="name" >
133      <xsl:value-of select="concat('prefix:', AST:OP/AST:value)" />
134    </xsl:attribute>
135    <xsl:apply-templates select="  AST:INT
136                                 | AST:REAL
137                                 | AST:PHP_bin_op
138                                 | AST:PHP_unary_op" />
139  </past:Op>
140</xsl:template>
141
142<!-- value assignment -->
143<xsl:template match="AST:PHP_assignment[string(AST:BOOL) = 'false']" >
144  <past:Op name='infix:=' pasttype='bind' >
145    <xsl:apply-templates select="AST:PHP_variable" />
146    <xsl:apply-templates select="  AST:STRING
147                                 | AST:INT
148                                 | AST:PHP_bin_op
149                                 | AST:PHP_constant" />
150  </past:Op>
151</xsl:template>
152
153<!-- aliasing -->
154<xsl:template match="AST:PHP_assignment[string(AST:BOOL) = 'true']" >
155  <past:Op name='infix::=' pasttype='bind' >
156    <xsl:apply-templates select="AST:PHP_variable" />
157    <xsl:apply-templates select="  AST:STRING
158                                 | AST:INT
159                                 | AST:PHP_bin_op
160                                 | AST:PHP_constant" />
161  </past:Op>
162</xsl:template>
163
164<xsl:template match="AST:PHP_variable" >
165  <past:Var scope="package" >
166    <xsl:attribute name="name" >
167      <xsl:value-of select="concat( '$', AST:VARIABLE_NAME/AST:value )" />
168    </xsl:attribute>
169  </past:Var>
170</xsl:template>
171
172<xsl:template match="AST:PHP_variable[ AST:PHP_expr_list/AST:STRING | AST:PHP_expr_list/AST:INT ]" >
173  <past:Var scope="keyed" viviself="Undef" lvalue="1" >
174    <xsl:choose>
175      <xsl:when test="AST:VARIABLE_NAME/AST:value = '_GET' or AST:VARIABLE_NAME/AST:value = '_POST'" >
176        <past:Var scope="package" >
177          <xsl:attribute name="name" ><xsl:value-of select="concat( '$', AST:VARIABLE_NAME/AST:value )" /></xsl:attribute>
178        </past:Var>
179      </xsl:when>
180      <xsl:otherwise>
181        <past:Var viviself="PhpArray" scope="package" lvalue="1" >
182          <xsl:attribute name="name" ><xsl:value-of select="concat( '$', AST:VARIABLE_NAME/AST:value )" /></xsl:attribute>
183        </past:Var>
184      </xsl:otherwise>
185    </xsl:choose>
186    <xsl:apply-templates select="  AST:PHP_expr_list/AST:STRING
187                                 | AST:PHP_expr_list/AST:INT" />
188  </past:Var>
189</xsl:template>
190
191<xsl:template match="AST:PHP_constant" >
192  <past:Op name="constant" >
193    <past:Val returns="PhpString" >
194      <xsl:attribute name="value" ><xsl:value-of select="AST:CONSTANT_NAME/AST:value" /></xsl:attribute>
195    </past:Val>
196  </past:Op>
197</xsl:template>
198
199<xsl:template match="AST:STRING" >
200  <past:Val returns="PhpString" >
201    <xsl:attribute name="encoding" ><xsl:value-of select="AST:value/@encoding" /></xsl:attribute>
202    <xsl:attribute name="value" ><xsl:value-of select="AST:value" /></xsl:attribute>
203  </past:Val>
204</xsl:template>
205
206<xsl:template match="AST:INT" >
207  <past:Val returns="PhpInteger" >
208    <xsl:attribute name="value" ><xsl:value-of select="AST:value" /></xsl:attribute>
209  </past:Val>
210</xsl:template>
211
212<xsl:template match="AST:BOOL" >
213  <past:Val returns="PhpBoolean" >
214    <xsl:attribute name="value" ><xsl:choose>
215      <xsl:when test="AST:value = 'True'"  >1</xsl:when>
216      <xsl:when test="AST:value = 'False'" >0</xsl:when>
217    </xsl:choose></xsl:attribute>
218  </past:Val>
219</xsl:template>
220
221<xsl:template match="AST:BOOL" >
222  <past:Val returns="PhpNull" >
223    <xsl:attribute name="value" >0</xsl:attribute>
224  </past:Val>
225</xsl:template>
226
227<!-- looks like phc is running into a floating point issue -->
228<xsl:template match="AST:REAL" >
229  <past:Val returns='PhpFloat' >
230    <xsl:attribute name="value" ><xsl:value-of select="AST:source_rep" /></xsl:attribute>
231  </past:Val>
232</xsl:template>
233
234<xsl:template match="  AST:value
235                     | AST:source_rep" >
236  <xsl:value-of select="." />
237</xsl:template>
238
239<xsl:template match="AST:PHP_if" >
240  <past:Op pasttype="if" >
241    <xsl:apply-templates select="  AST:STRING
242                                 | AST:INT
243                                 | AST:REAL
244                                 | AST:PHP_bin_op
245                                 | AST:PHP_unary_op
246                                 | AST:PHP_variable
247                                 | AST:PHP_constant" />
248    <past:Block blocktype="immediate" >
249      <xsl:apply-templates select="AST:PHP_statement_list[1]" />
250    </past:Block>
251    <past:Block blocktype="immediate" >
252      <xsl:apply-templates select="AST:PHP_statement_list[2]" />
253    </past:Block>
254  </past:Op>
255</xsl:template>
256
257</xsl:stylesheet>