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