author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sun, 26 Oct 2014 01:28:07 +0000 | |
changeset 392 | 9b297f0d949c |
parent 391 | 553a5456963b |
child 393 | 00381102a9b5 |
permissions | -rw-r--r-- |
391
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
1 |
"{ Package: 'stx:goodies/petitparser/compiler' }" |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
2 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
3 |
PPCListNode subclass:#PPCTrimmingTokenNode |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
4 |
instanceVariableNames:'tokenClass' |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
5 |
classVariableNames:'' |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
6 |
poolDictionaries:'' |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
7 |
category:'PetitCompiler-Nodes' |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
8 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
9 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
10 |
!PPCTrimmingTokenNode methodsFor:'accessing'! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
11 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
12 |
child |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
13 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
14 |
^ children at: 2 |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
15 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
16 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
17 |
child: anObject |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
18 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
19 |
children at: 2 put: anObject |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
20 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
21 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
22 |
compileWhitespace: compiler |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
23 |
compiler add: 'context atWs ifFalse: ['. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
24 |
compiler indent. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
25 |
compiler call: (self whitespace compileWith: compiler). |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
26 |
compiler add: 'context setWs.'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
27 |
compiler dedent. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
28 |
compiler add: '].'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
29 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
30 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
31 |
initialize |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
32 |
super initialize. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
33 |
children := Array new: 2 |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
34 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
35 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
36 |
rewrite: changeStatus |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
37 |
| | |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
38 |
super rewrite: changeStatus. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
39 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
40 |
(self allNodes anySatisfy: [ :node | node asFast ~= node ]) ifTrue: [ |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
41 |
changeStatus change. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
42 |
self replace: self whitespace with: (self whitespace transform: [ :node | node asFast ]). |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
43 |
self replace: self child with: (self child transform: [:node | node asFast]). |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
44 |
] |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
45 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
46 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
47 |
tokenClass |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
48 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
49 |
^ tokenClass |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
50 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
51 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
52 |
tokenClass: anObject |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
53 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
54 |
tokenClass := anObject |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
55 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
56 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
57 |
whitespace |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
58 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
59 |
^ children at: 1 |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
60 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
61 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
62 |
whitespace: anObject |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
63 |
(anObject name isNil and: [ self child name isNotNil ]) ifTrue: [ |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
64 |
anObject name: self child name, '_water'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
65 |
]. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
66 |
children at: 1 put: anObject |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
67 |
! ! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
68 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
69 |
!PPCTrimmingTokenNode methodsFor:'analyzing'! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
70 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
71 |
acceptsEpsilon |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
72 |
^ self child acceptsEpsilonOpenSet: (IdentitySet with: self). |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
73 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
74 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
75 |
acceptsEpsilonOpenSet: set |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
76 |
(set includes: self child) ifFalse: [ |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
77 |
set add: self child. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
78 |
^ self child acceptsEpsilonOpenSet: set |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
79 |
]. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
80 |
^ false |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
81 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
82 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
83 |
firstSetSuchThat: block into: aCollection openSet: aSet |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
84 |
(aSet includes: self) ifTrue: [ ^ aCollection ]. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
85 |
aSet add: self. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
86 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
87 |
(block value: self) ifTrue: [ aCollection add: self. ^ aCollection ]. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
88 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
89 |
^ self child firstSetSuchThat: block into: aCollection openSet: aSet. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
90 |
! ! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
91 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
92 |
!PPCTrimmingTokenNode methodsFor:'as yet unclassified'! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
93 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
94 |
compileWith: compiler effect: effect id: id |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
95 |
| guardSetId guardSet | |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
96 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
97 |
compiler startMethod: id. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
98 |
compiler startTokenMode. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
99 |
compiler addVariable: 'start'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
100 |
compiler addVariable: 'end'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
101 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
102 |
self compileWhitespace: compiler. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
103 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
104 |
(compiler guards and: [ (guardSet := compiler guardCharSet: self) isNil not ]) ifTrue: [ |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
105 |
guardSetId := id, '_guard'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
106 |
compiler addConstant: guardSet as: guardSetId. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
107 |
compiler add: 'context atEnd ifTrue: [ ^ self error ].'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
108 |
compiler add: '(', guardSetId, ' value: context peek) ifFalse: [ ^ self error ].'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
109 |
]. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
110 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
111 |
compiler add: 'start := context position + 1.'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
112 |
compiler call: (self child compileWith: compiler). |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
113 |
compiler add: 'error ifTrue: [ ^ self ].'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
114 |
compiler add: 'end := context position.'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
115 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
116 |
self compileWhitespace: compiler. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
117 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
118 |
compiler add: '^ ', tokenClass asString, ' on: (context collection) |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
119 |
start: start |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
120 |
stop: end |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
121 |
value: nil'. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
122 |
compiler stopTokenMode. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
123 |
^ compiler stopMethod. |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
124 |
! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
125 |
|
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
126 |
prefix |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
127 |
^ #token |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
128 |
! ! |
553a5456963b
Ported PetitCompiler-(Tests).
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff
changeset
|
129 |