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