640
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4
|
3 |
"{ Package: 'stx:goodies/petitparser' }"
|
0
|
4 |
|
640
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
0
|
7 |
PPDelegateParser subclass:#PPCompositeParser
|
182
|
8 |
instanceVariableNames:'dependencies'
|
0
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'PetitParser-Tools'
|
|
12 |
!
|
|
13 |
|
|
14 |
|
|
15 |
!PPCompositeParser class methodsFor:'instance creation'!
|
|
16 |
|
|
17 |
new
|
|
18 |
"Answer a new parser starting at the default start symbol."
|
|
19 |
|
|
20 |
^ self newStartingAt: self startSymbol
|
|
21 |
!
|
|
22 |
|
|
23 |
newStartingAt: aSymbol
|
182
|
24 |
"Answer a new parser starting at aSymbol. The code makes sure to resolve all dependent parsers correctly."
|
0
|
25 |
|
182
|
26 |
| parsers remaining |
|
|
27 |
parsers := IdentityDictionary new.
|
|
28 |
remaining := OrderedCollection with: self.
|
|
29 |
[ remaining isEmpty ] whileFalse: [
|
|
30 |
| dependency |
|
|
31 |
dependency := remaining removeLast.
|
|
32 |
(parsers includesKey: dependency) ifFalse: [
|
|
33 |
parsers at: dependency put: dependency basicNew.
|
|
34 |
remaining addAll: dependency dependencies ] ].
|
|
35 |
parsers keysAndValuesDo: [ :class :parser |
|
|
36 |
| dependencies |
|
|
37 |
dependencies := IdentityDictionary new.
|
|
38 |
class dependencies
|
|
39 |
do: [ :dependency | dependencies at: dependency put: (parsers at: dependency) ].
|
|
40 |
parser
|
|
41 |
initializeStartingAt: (class == self
|
|
42 |
ifTrue: [ aSymbol ]
|
|
43 |
ifFalse: [ class startSymbol ])
|
|
44 |
dependencies: dependencies ].
|
|
45 |
parsers keysAndValuesDo: [ :class :parser |
|
|
46 |
parser setParser: (parser perform: parser children first name).
|
|
47 |
parser productionNames keysAndValuesDo: [ :key :value |
|
|
48 |
(parser instVarAt: key) setParser: (parser perform: value) ] ].
|
|
49 |
^ parsers at: self
|
0
|
50 |
! !
|
|
51 |
|
|
52 |
!PPCompositeParser class methodsFor:'accessing'!
|
|
53 |
|
182
|
54 |
dependencies
|
|
55 |
"Answer a collection of PPCompositeParser classes that this parser directly dependends on. Override this method in subclasses to declare dependent parsers. The default implementation does not depend on other PPCompositeParser."
|
|
56 |
|
|
57 |
^ #()
|
|
58 |
!
|
|
59 |
|
0
|
60 |
ignoredNames
|
|
61 |
"Answer a collection of instance-variables that should not be automatically initialized with productions, but that are used internal to the composite parser."
|
|
62 |
|
|
63 |
^ PPCompositeParser allInstVarNames
|
|
64 |
!
|
|
65 |
|
|
66 |
startSymbol
|
|
67 |
"Answer the method that represents the default start symbol."
|
|
68 |
|
|
69 |
^ #start
|
|
70 |
! !
|
|
71 |
|
|
72 |
!PPCompositeParser class methodsFor:'parsing'!
|
|
73 |
|
|
74 |
parse: anObject
|
|
75 |
^ self parse: anObject startingAt: self startSymbol
|
|
76 |
!
|
|
77 |
|
|
78 |
parse: anObject onError: aBlock
|
|
79 |
^ self parse: anObject startingAt: self startSymbol onError: aBlock
|
|
80 |
!
|
|
81 |
|
|
82 |
parse: anObject startingAt: aSymbol
|
|
83 |
^ (self newStartingAt: aSymbol) parse: anObject
|
|
84 |
!
|
|
85 |
|
|
86 |
parse: anObject startingAt: aSymbol onError: aBlock
|
|
87 |
^ (self newStartingAt: aSymbol) parse: anObject onError: aBlock
|
|
88 |
! !
|
|
89 |
|
|
90 |
!PPCompositeParser methodsFor:'accessing'!
|
|
91 |
|
|
92 |
start
|
|
93 |
"Answer the production to start this parser with."
|
28
|
94 |
|
0
|
95 |
self subclassResponsibility
|
|
96 |
! !
|
|
97 |
|
|
98 |
!PPCompositeParser methodsFor:'initialization'!
|
|
99 |
|
|
100 |
initializeStartingAt: aSymbol
|
|
101 |
| allVariableNames ignoredVariableNames productionIndexesAndNames |
|
28
|
102 |
self initialize.
|
0
|
103 |
|
|
104 |
"find all the productions that need to be initialized"
|
|
105 |
allVariableNames := self class allInstVarNames
|
|
106 |
collect: [ :each | each asSymbol ].
|
|
107 |
ignoredVariableNames := self class ignoredNames
|
|
108 |
collect: [ :each | each asSymbol ].
|
|
109 |
productionIndexesAndNames := ((1 to: self class instSize)
|
|
110 |
collect: [ :index | index -> (allVariableNames at: index) ])
|
|
111 |
reject: [ :assoc | ignoredVariableNames includes: assoc value ].
|
28
|
112 |
|
0
|
113 |
"initialize productions with an undefined parser to be replaced later"
|
|
114 |
parser := PPUnresolvedParser named: aSymbol.
|
|
115 |
productionIndexesAndNames do: [ :assoc |
|
|
116 |
self instVarAt: assoc key put: (PPUnresolvedParser named: assoc value) ].
|
|
117 |
parser def: (self perform: aSymbol).
|
28
|
118 |
|
0
|
119 |
"resolve unresolved parsers with their actual implementation"
|
|
120 |
productionIndexesAndNames do: [ :assoc |
|
|
121 |
(self respondsTo: assoc value)
|
|
122 |
ifFalse: [ self error: 'Unable to initialize ' , assoc value printString ]
|
|
123 |
ifTrue: [ (self instVarAt: assoc key) def: (self perform: assoc value) ] ]
|
182
|
124 |
!
|
|
125 |
|
|
126 |
initializeStartingAt: aSymbol dependencies: aDictionary
|
|
127 |
self initialize.
|
|
128 |
parser := PPDelegateParser named: aSymbol.
|
|
129 |
self productionNames keysAndValuesDo: [ :key :value |
|
|
130 |
self instVarAt: key put: (PPDelegateParser named: value) ].
|
|
131 |
dependencies := aDictionary
|
0
|
132 |
! !
|
|
133 |
|
|
134 |
!PPCompositeParser methodsFor:'querying'!
|
|
135 |
|
182
|
136 |
dependencyAt: aClass
|
640
|
137 |
"Answer the dependent parser aClass. Throws an error if this parser class is not declared in the method #dependencies on the class-side of the receiver."
|
|
138 |
|
|
139 |
^ dependencies at: aClass ifAbsent: [ self error: 'Undeclared dependency in ' , self className , ' to ' , aClass name ]
|
|
140 |
|
|
141 |
"Modified: / 28-06-2019 / 09:12:51 / Claus Gittinger"
|
182
|
142 |
!
|
|
143 |
|
0
|
144 |
productionAt: aSymbol
|
|
145 |
"Answer the production named aSymbol."
|
28
|
146 |
|
0
|
147 |
^ self productionAt: aSymbol ifAbsent: [ nil ]
|
|
148 |
!
|
|
149 |
|
|
150 |
productionAt: aSymbol ifAbsent: aBlock
|
|
151 |
"Answer the production named aSymbol, if there is no such production answer the result of evaluating aBlock."
|
28
|
152 |
|
0
|
153 |
(self class ignoredNames includes: aSymbol asString)
|
|
154 |
ifTrue: [ ^ aBlock value ].
|
|
155 |
(self class startSymbol = aSymbol)
|
|
156 |
ifTrue: [ ^ parser ].
|
|
157 |
^ self instVarAt: (self class allInstVarNames
|
|
158 |
indexOf: aSymbol asString
|
|
159 |
ifAbsent: [ ^ aBlock value ])
|
182
|
160 |
!
|
|
161 |
|
|
162 |
productionNames
|
|
163 |
"Answer a dictionary of slot indexes and production names."
|
|
164 |
|
|
165 |
| productionNames ignoredNames |
|
|
166 |
productionNames := Dictionary new.
|
|
167 |
ignoredNames := self class ignoredNames
|
|
168 |
collect: [ :each | each asSymbol ].
|
|
169 |
self class allInstVarNames keysAndValuesDo: [ :key :value |
|
|
170 |
(ignoredNames includes: value asSymbol)
|
|
171 |
ifFalse: [ productionNames at: key put: value asSymbol ] ].
|
|
172 |
^ productionNames
|
0
|
173 |
! !
|
|
174 |
|
|
175 |
!PPCompositeParser class methodsFor:'documentation'!
|
|
176 |
|
28
|
177 |
version
|
640
|
178 |
^ '$Header$'
|
28
|
179 |
!
|
|
180 |
|
|
181 |
version_CVS
|
640
|
182 |
^ '$Header$'
|
28
|
183 |
!
|
|
184 |
|
0
|
185 |
version_SVN
|
640
|
186 |
^ '$Id$'
|
0
|
187 |
! !
|
182
|
188 |
|