435
|
1 |
"{ Package: 'stx:goodies/petitparser/parsers/java' }"
|
|
2 |
|
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
|
5 |
PPJavaSyntax subclass:#PPJavaParser
|
|
6 |
instanceVariableNames:''
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'PetitJava-Core'
|
|
10 |
!
|
|
11 |
|
|
12 |
PPJavaParser comment:'A parser which creates an AST upor parsing the code'
|
|
13 |
!
|
|
14 |
|
|
15 |
!PPJavaParser methodsFor:'accessing'!
|
|
16 |
|
|
17 |
annotation
|
|
18 |
|
|
19 |
^ super annotation ==> [:nodes | PJAnnotationNode typeName: nodes second]
|
|
20 |
!
|
|
21 |
|
|
22 |
classModifierNotAnnotation
|
|
23 |
|
|
24 |
^super classModifierNotAnnotation ==> [:tokenKeyword | PJModifierNode keyword: tokenKeyword inputValue]
|
|
25 |
!
|
|
26 |
|
|
27 |
formalParameters
|
|
28 |
|
|
29 |
^ super formalParameters ==> [:nodes | nodes second]
|
|
30 |
!
|
|
31 |
|
|
32 |
methodModifierNotAnnotation
|
|
33 |
|
|
34 |
^super methodModifierNotAnnotation ==> [:tokenKeyword | PJModifierNode keyword: tokenKeyword inputValue]
|
|
35 |
!
|
|
36 |
|
|
37 |
qualifiedName
|
|
38 |
^ super qualifiedName
|
|
39 |
==> [ :nodes |
|
|
40 |
nodes second notEmpty
|
|
41 |
ifTrue: [ self nameFromQualified: (Array with: nodes first withAll: (nodes second collect: [ :e | e second value ])) ]
|
|
42 |
ifFalse: [ PJSimpleNameNode identifier: nodes first value ] ]
|
|
43 |
! !
|
|
44 |
|
|
45 |
!PPJavaParser methodsFor:'as yet unclassified'!
|
|
46 |
|
|
47 |
endOfLineComment
|
|
48 |
^ super endOfLineComment ==> [ :nodes | PJEndOfLineCommentsNode comment: nodes second .].
|
|
49 |
!
|
|
50 |
|
|
51 |
floatingPointLiteral
|
|
52 |
|
|
53 |
^super floatingPointLiteral trim ==> [ :token | PJFloatLiteralNode newFrom: token inputValue ]
|
|
54 |
!
|
|
55 |
|
|
56 |
separator
|
|
57 |
|
|
58 |
^super separator trim ==> [:token | PJSeparatorNode separatorValue: (token inputValue)]
|
|
59 |
!
|
|
60 |
|
|
61 |
traditionalComment
|
|
62 |
^ super traditionalComment ==> [ :nodes | PJTraditionalCommentsNode comment: (nodes second ).].
|
|
63 |
! !
|
|
64 |
|
|
65 |
!PPJavaParser methodsFor:'grammar-classes'!
|
|
66 |
|
|
67 |
normalClassDeclaration
|
|
68 |
|
|
69 |
^ super normalClassDeclaration
|
|
70 |
! !
|
|
71 |
|
|
72 |
!PPJavaParser methodsFor:'grammar-classes-method'!
|
|
73 |
|
|
74 |
block
|
|
75 |
^ super block ==> [ :nodes |
|
|
76 |
| blockNode |
|
|
77 |
blockNode := PJBlockNode new.
|
|
78 |
blockNode statements: nodes second.
|
|
79 |
blockNode]
|
|
80 |
!
|
|
81 |
|
|
82 |
constructorDeclaration
|
|
83 |
|
436
|
84 |
^ super constructorDeclaration ==> [:nodes |
|
|
85 |
|constructor|
|
|
86 |
|
|
87 |
constructor := PJConstructorDeclarationNode named: nodes third name.
|
|
88 |
constructor
|
|
89 |
statements: (nodes at: 8);
|
|
90 |
modifiers: nodes first;
|
|
91 |
returnType: nodes second;
|
|
92 |
parameters: nodes fourth.
|
|
93 |
constructor]
|
|
94 |
|
|
95 |
"Modified: / 21-04-2015 / 15:52:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
435
|
96 |
!
|
|
97 |
|
|
98 |
expressionStatement
|
|
99 |
^ super expressionStatement ==> [ :nodes |
|
|
100 |
| expressionStatementNode |
|
|
101 |
expressionStatementNode := PJExpressionStatementNode new.
|
|
102 |
expressionStatementNode expression: nodes first.
|
|
103 |
expressionStatementNode]
|
|
104 |
!
|
|
105 |
|
|
106 |
localVariableDeclaration
|
|
107 |
^ super localVariableDeclaration ==> [ :nodes |
|
|
108 |
| declarationNode |
|
|
109 |
declarationNode := PJLocalVariableDeclarationStatementNode new.
|
|
110 |
declarationNode
|
|
111 |
type: nodes second;
|
|
112 |
declarators: (nodes third second collect: [:each | each second]) asOrderedCollection.
|
|
113 |
declarationNode declarators addFirst: nodes third first.
|
|
114 |
declarationNode]
|
|
115 |
!
|
|
116 |
|
|
117 |
localVariableDeclarationStatement
|
|
118 |
^ super localVariableDeclarationStatement ==> [ :nodes | nodes first]
|
|
119 |
!
|
|
120 |
|
|
121 |
methodNotConstructorDeclaration
|
|
122 |
|
436
|
123 |
^ super methodNotConstructorDeclaration ==> [:nodes |
|
|
124 |
| methodDeclarationNode |
|
|
125 |
methodDeclarationNode := PJMethodDeclarationNode named: nodes fourth name.
|
|
126 |
methodDeclarationNode modifiers: nodes first.
|
|
127 |
methodDeclarationNode returnType: nodes third.
|
|
128 |
methodDeclarationNode body: (nodes at: 8).
|
|
129 |
methodDeclarationNode parameters: nodes fifth.
|
|
130 |
methodDeclarationNode
|
|
131 |
|
|
132 |
]
|
|
133 |
|
|
134 |
"Modified: / 21-04-2015 / 15:52:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
435
|
135 |
!
|
|
136 |
|
|
137 |
normalParameterDecl
|
|
138 |
|
|
139 |
^ super normalParameterDecl ==> [:nodes |
|
|
140 |
| declarator |
|
|
141 |
|
|
142 |
declarator := PJParameterDeclaratorNode new.
|
|
143 |
declarator
|
|
144 |
modifiers: nodes first;
|
|
145 |
type: nodes second;
|
|
146 |
identifier: nodes third;
|
|
147 |
arrayLevel: nodes fourth size.
|
|
148 |
declarator]
|
|
149 |
!
|
|
150 |
|
|
151 |
normalParametersDecls
|
|
152 |
^ super normalParametersDecls ==> [ :nodes |
|
|
153 |
| declarationNode |
|
|
154 |
((nodes second collect: [ :element | element second ]) asOrderedCollection)
|
|
155 |
addFirst: nodes first; yourself]
|
|
156 |
!
|
|
157 |
|
|
158 |
nullLiteral
|
|
159 |
^super nullLiteral trim ==> [:token | PJNullLiteralNode literalValue: (token inputValue)]
|
|
160 |
!
|
|
161 |
|
|
162 |
returnStatement
|
|
163 |
^ super returnStatement ==> [ :nodes |
|
|
164 |
| returnNode |
|
|
165 |
returnNode := PJReturnStatementNode new.
|
|
166 |
returnNode expression: nodes second.
|
|
167 |
returnNode]
|
|
168 |
!
|
|
169 |
|
|
170 |
variableDeclarator
|
|
171 |
|
|
172 |
^ super variableDeclarator ==> [:nodes |
|
|
173 |
| declarator |
|
|
174 |
|
|
175 |
declarator := PJVariableDeclaratorNode new.
|
|
176 |
declarator
|
|
177 |
variable: nodes first;
|
|
178 |
arrayLevel: nodes second size.
|
|
179 |
nodes third ifNotNilDo: [:node | declarator initializer: node second].
|
|
180 |
declarator]
|
|
181 |
! !
|
|
182 |
|
|
183 |
!PPJavaParser methodsFor:'grammar-literals-boolean'!
|
|
184 |
|
|
185 |
booleanLiteral
|
|
186 |
|
|
187 |
^ super booleanLiteral trim ==> [:token |
|
|
188 |
| value |
|
|
189 |
(token inputValue = 'false')
|
|
190 |
ifTrue: [ value := false]
|
|
191 |
ifFalse: [(token inputValue = 'true')
|
|
192 |
ifTrue: [value := true]
|
|
193 |
ifFalse: [self error: 'A Boolean Literal must be either false or true']].
|
|
194 |
PJBooleanLiteralNode booleanValue: value.]
|
|
195 |
! !
|
|
196 |
|
|
197 |
!PPJavaParser methodsFor:'grammar-literals-string'!
|
|
198 |
|
|
199 |
additiveExpression
|
|
200 |
|
|
201 |
^ super additiveExpression ==> self infixOpProcessor .
|
|
202 |
!
|
|
203 |
|
|
204 |
andExpression
|
|
205 |
|
|
206 |
^ super andExpression ==> self infixOpProcessor
|
|
207 |
!
|
|
208 |
|
|
209 |
characterLiteral
|
|
210 |
^super characterLiteral trim ==> [:token | PJCharacterLiteralNode literalValue: (token inputValue allButFirst allButLast)]
|
|
211 |
!
|
|
212 |
|
|
213 |
classOrInterfaceType
|
|
214 |
self flag: 'NA: temporary hack, should interpret nodes instead of returning first'.
|
|
215 |
^ super classOrInterfaceType ==> [ :nodes | nodes first ]
|
|
216 |
!
|
|
217 |
|
|
218 |
conditionalAndExpression
|
|
219 |
|
|
220 |
^ super conditionalAndExpression ==> self infixOpProcessor
|
|
221 |
!
|
|
222 |
|
|
223 |
conditionalExpression
|
|
224 |
|
|
225 |
^super conditionalExpression ==> [:node | node second ifNil: [node first] ifNotNil: [node]]
|
|
226 |
!
|
|
227 |
|
|
228 |
conditionalOrExpression
|
|
229 |
|
|
230 |
^ super conditionalOrExpression ==> self infixOpProcessor
|
|
231 |
!
|
|
232 |
|
|
233 |
equalityExpression
|
|
234 |
|
|
235 |
^ super equalityExpression ==> self infixOpProcessor
|
|
236 |
!
|
|
237 |
|
|
238 |
exclusiveOrExpression
|
|
239 |
|
|
240 |
^ super exclusiveOrExpression ==> self infixOpProcessor
|
|
241 |
!
|
|
242 |
|
|
243 |
expression
|
|
244 |
|
|
245 |
^super expression ==> [:node | node second ifNil: [node first] ifNotNil: [
|
|
246 |
|operation|
|
|
247 |
operation := PJInfixOperationNode new.
|
|
248 |
operation
|
|
249 |
left: node first;
|
|
250 |
operator: node second first inputValue asSymbol;
|
|
251 |
right: node second second.
|
|
252 |
operation]]
|
|
253 |
!
|
|
254 |
|
|
255 |
identifier
|
|
256 |
|
|
257 |
^super identifier ==> [:token | PJIdentifierNode newWithName: token inputValue]
|
|
258 |
!
|
|
259 |
|
|
260 |
identifierWithAccessors
|
|
261 |
|
|
262 |
^ super identifierWithAccessors ==> [:node | node second isEmpty & node third isNil ifTrue: [node first] ifFalse: [node]]
|
|
263 |
!
|
|
264 |
|
|
265 |
ifStatement
|
|
266 |
|
|
267 |
^super ifStatement ==> [:node |
|
|
268 |
| statementNode |
|
|
269 |
statementNode := PJIfStatementNode new.
|
|
270 |
statementNode
|
|
271 |
condition: node second;
|
|
272 |
thenPart: node third.
|
|
273 |
node fourth ifNotNil: [
|
|
274 |
statementNode elsePart: node fourth second].
|
|
275 |
statementNode]
|
|
276 |
!
|
|
277 |
|
|
278 |
inclusiveOrExpression
|
|
279 |
|
|
280 |
^super inclusiveOrExpression ==> self infixOpProcessor
|
|
281 |
!
|
|
282 |
|
|
283 |
infixOpProcessor
|
|
284 |
|
|
285 |
^ [:node |
|
|
286 |
(node second asOrderedCollection addFirst: node first; yourself) fold: [:left :current |
|
|
287 |
|operation|
|
|
288 |
operation := PJInfixOperationNode new.
|
|
289 |
operation
|
|
290 |
left: left;
|
|
291 |
operator: current first inputValue asSymbol;
|
|
292 |
right: current second.
|
|
293 |
operation]]
|
|
294 |
!
|
|
295 |
|
|
296 |
instanceofExpression
|
|
297 |
|
|
298 |
^super instanceofExpression ==> [:node | node second ifNil: [node first] ifNotNil: [node]]
|
|
299 |
!
|
|
300 |
|
|
301 |
integerLiteral
|
|
302 |
|
|
303 |
^super integerLiteral trim ==> [ :token | PJIntegerLiteralNode newFrom: token inputValue ]
|
|
304 |
!
|
|
305 |
|
|
306 |
multiplicativeExpression
|
|
307 |
^ super multiplicativeExpression ==> self infixOpProcessor
|
|
308 |
!
|
|
309 |
|
|
310 |
parExpression
|
|
311 |
|
|
312 |
^super parExpression ==> [ :nodes | nodes second ]
|
|
313 |
!
|
|
314 |
|
|
315 |
primaryWithselectors
|
|
316 |
^ super primaryWithselectors ==> [:node |
|
|
317 |
"JK: I have no idea, what this fold is supposed to do,
|
|
318 |
but #object: causes DNU, so I commented it out
|
|
319 |
"
|
|
320 |
node first ]
|
|
321 |
"(node second asOrderedCollection addFirst: node first; yourself) fold: [:inner :outer | outer object: inner]]
|
|
322 |
"
|
|
323 |
!
|
|
324 |
|
|
325 |
primitiveType
|
|
326 |
|
|
327 |
^super primitiveType ==> [:token | PJPrimitiveTypeNode type: token inputValue]
|
|
328 |
!
|
|
329 |
|
|
330 |
relationalExpression
|
|
331 |
|
|
332 |
^super relationalExpression ==> self infixOpProcessor
|
|
333 |
!
|
|
334 |
|
|
335 |
shiftExpression
|
|
336 |
|
|
337 |
^super shiftExpression ==> [:node | node second ifEmpty: [node first] ifNotEmpty: [node]]
|
|
338 |
!
|
|
339 |
|
|
340 |
stringLiteral
|
|
341 |
|
|
342 |
^super stringLiteral trim ==> [:token | PJStringLiteralNode literalValue: (token inputValue allButFirst allButLast)]
|
|
343 |
!
|
|
344 |
|
|
345 |
type
|
|
346 |
^ super type
|
|
347 |
==> [ :nodes |
|
|
348 |
| pjtype |
|
|
349 |
nodes second notEmpty
|
|
350 |
ifTrue: [ pjtype := PJArrayTypeNode elementType: nodes first dimensions: nodes second size ]
|
|
351 |
ifFalse: [ pjtype := nodes first ].
|
|
352 |
pjtype ]
|
|
353 |
!
|
|
354 |
|
|
355 |
unaryPostfixExpression
|
|
356 |
^ super unaryPostfixExpression ==> [:node | node second ifNil: [node first] ifNotNil: [node]]
|
|
357 |
!
|
|
358 |
|
|
359 |
whileStatement
|
|
360 |
|
|
361 |
^super whileStatement ==> [ :nodes |
|
|
362 |
| while |
|
|
363 |
while := PJWhileStatementNode new.
|
|
364 |
while expression: nodes second.
|
|
365 |
while statement: nodes third.
|
|
366 |
while ]
|
|
367 |
! !
|
|
368 |
|
|
369 |
!PPJavaParser methodsFor:'private'!
|
|
370 |
|
|
371 |
nameFromQualified: aCollection
|
|
372 |
|
|
373 |
^(aCollection size = 1)
|
|
374 |
ifTrue: [PJSimpleNameNode identifier: aCollection first]
|
|
375 |
ifFalse: [
|
|
376 |
PJQualifiedNameNode
|
|
377 |
name: (PJSimpleNameNode identifier: aCollection last)
|
|
378 |
qualifier: (self nameFromQualified: aCollection allButLast ).]
|
|
379 |
! !
|
|
380 |
|