Parser.st 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. Object subclass: #PPParser
  2. instanceVariableNames: 'memo'
  3. category: 'Parser'!
  4. !PPParser methodsFor: 'accessing'!
  5. memo
  6. ^memo
  7. ! !
  8. !PPParser methodsFor: 'initialization'!
  9. initialize
  10. memo := Dictionary new
  11. ! !
  12. !PPParser methodsFor: 'operations'!
  13. flatten
  14. ^PPFlattenParser on: self
  15. !
  16. withSource
  17. ^PPSourceParser on: self
  18. !
  19. ==> aBlock
  20. ^PPActionParser on: self block: aBlock
  21. !
  22. , aParser
  23. ^PPSequenceParser with: self with: aParser
  24. !
  25. / aParser
  26. ^PPChoiceParser with: self with: aParser
  27. !
  28. plus
  29. ^PPRepeatingParser on: self min: 1
  30. !
  31. star
  32. ^PPRepeatingParser on: self min: 0
  33. !
  34. not
  35. ^PPNotParser on: self
  36. !
  37. optional
  38. ^self / PPEpsilonParser new
  39. !
  40. memoizedParse: aStream
  41. | start end node |
  42. start := aStream position.
  43. ^self memo at: start
  44. ifPresent: [:value |
  45. aStream position: (self memo at: start) second.
  46. value first]
  47. ifAbsent: [
  48. node := self parse: aStream.
  49. end := aStream position.
  50. self memo at: start put: (Array with: node with: end).
  51. node]
  52. ! !
  53. !PPParser methodsFor: 'parsing'!
  54. parse: aStream
  55. self subclassResponsibility
  56. !
  57. parseAll: aStream
  58. | result |
  59. result := (PPSequenceParser with: self with: PPEOFParser new) memoizedParse: aStream.
  60. ^result isParseFailure
  61. ifTrue: [self error: (result messageFor: aStream contents)]
  62. ifFalse: [result first]
  63. ! !
  64. PPParser subclass: #PPEOFParser
  65. instanceVariableNames: ''
  66. category: 'Parser'!
  67. !PPEOFParser methodsFor: 'parsing'!
  68. parse: aStream
  69. ^aStream atEnd
  70. ifFalse: [
  71. PPFailure new reason: 'EOF expected' at: aStream position]
  72. ifTrue: [nil]
  73. ! !
  74. PPParser subclass: #PPAnyParser
  75. instanceVariableNames: ''
  76. category: 'Parser'!
  77. !PPAnyParser methodsFor: 'parsing'!
  78. parse: aStream
  79. ^aStream atEnd
  80. ifTrue: [PPFailure new
  81. reason: 'did not expect EOF' at: aStream position]
  82. ifFalse: [aStream next]
  83. ! !
  84. PPParser subclass: #PPEpsilonParser
  85. instanceVariableNames: ''
  86. category: 'Parser'!
  87. !PPEpsilonParser methodsFor: 'parsing'!
  88. parse: aStream
  89. ^nil
  90. ! !
  91. PPParser subclass: #PPStringParser
  92. instanceVariableNames: 'string'
  93. category: 'Parser'!
  94. !PPStringParser methodsFor: 'accessing'!
  95. string
  96. ^string
  97. !
  98. string: aString
  99. string := aString
  100. ! !
  101. !PPStringParser methodsFor: 'parsing'!
  102. parse: aStream
  103. | position result |
  104. position := aStream position.
  105. result := aStream next: self string size.
  106. ^result = self string
  107. ifTrue: [result]
  108. ifFalse: [
  109. aStream position: position.
  110. PPFailure new reason: 'Expected ', self string, ' but got ', (result at: position) printString; yourself]
  111. ! !
  112. PPParser subclass: #PPCharacterParser
  113. instanceVariableNames: 'regexp'
  114. category: 'Parser'!
  115. !PPCharacterParser methodsFor: 'accessing'!
  116. string: aString
  117. regexp := RegularExpression fromString: '[', aString, ']'
  118. ! !
  119. !PPCharacterParser methodsFor: 'parsing'!
  120. parse: aStream
  121. ^(aStream peek notNil and: [self match: aStream peek])
  122. ifTrue: [aStream next]
  123. ifFalse: [PPFailure new reason: 'Could not match' at: aStream position]
  124. ! !
  125. !PPCharacterParser methodsFor: 'private'!
  126. match: aString
  127. ^aString match: regexp
  128. ! !
  129. PPParser subclass: #PPListParser
  130. instanceVariableNames: 'parsers'
  131. category: 'Parser'!
  132. !PPListParser methodsFor: 'accessing'!
  133. parsers
  134. ^parsers ifNil: [#()]
  135. !
  136. parsers: aCollection
  137. parsers := aCollection
  138. ! !
  139. !PPListParser methodsFor: 'copying'!
  140. copyWith: aParser
  141. ^self class withAll: (self parsers copyWith: aParser)
  142. ! !
  143. !PPListParser class methodsFor: 'instance creation'!
  144. withAll: aCollection
  145. ^self new
  146. parsers: aCollection;
  147. yourself
  148. !
  149. with: aParser with: anotherParser
  150. ^self withAll: (Array with: aParser with: anotherParser)
  151. ! !
  152. PPListParser subclass: #PPSequenceParser
  153. instanceVariableNames: ''
  154. category: 'Parser'!
  155. !PPSequenceParser methodsFor: 'copying'!
  156. , aRule
  157. ^self copyWith: aRule
  158. ! !
  159. !PPSequenceParser methodsFor: 'parsing'!
  160. parse: aStream
  161. | start elements element |
  162. start := aStream position.
  163. elements := #().
  164. self parsers
  165. detect: [:each |
  166. element := each memoizedParse: aStream.
  167. elements add: element.
  168. element isParseFailure]
  169. ifNone: [].
  170. ^element isParseFailure
  171. ifFalse: [elements]
  172. ifTrue: [aStream position: start. element]
  173. ! !
  174. PPListParser subclass: #PPChoiceParser
  175. instanceVariableNames: ''
  176. category: 'Parser'!
  177. !PPChoiceParser methodsFor: 'copying'!
  178. / aRule
  179. ^self copyWith: aRule
  180. ! !
  181. !PPChoiceParser methodsFor: 'parsing'!
  182. parse: aStream
  183. | result |
  184. self parsers
  185. detect: [:each |
  186. result := each memoizedParse: aStream.
  187. result isParseFailure not]
  188. ifNone: [].
  189. ^result
  190. ! !
  191. PPParser subclass: #PPDelegateParser
  192. instanceVariableNames: 'parser'
  193. category: 'Parser'!
  194. !PPDelegateParser methodsFor: 'accessing'!
  195. parser
  196. ^parser
  197. !
  198. parser: aParser
  199. parser := aParser
  200. ! !
  201. !PPDelegateParser methodsFor: 'parsing'!
  202. parse: aStream
  203. ^self parser memoizedParse: aStream
  204. ! !
  205. !PPDelegateParser class methodsFor: 'instance creation'!
  206. on: aParser
  207. ^self new
  208. parser: aParser;
  209. yourself
  210. ! !
  211. PPDelegateParser subclass: #PPAndParser
  212. instanceVariableNames: ''
  213. category: 'Parser'!
  214. !PPAndParser methodsFor: 'parsing'!
  215. parse: aStream
  216. ^self basicParse: aStream
  217. !
  218. basicParse: aStream
  219. | element position |
  220. position := aStream position.
  221. element := self parser memoizedParse: aStream.
  222. aStream position: position.
  223. ^element
  224. ! !
  225. PPAndParser subclass: #PPNotParser
  226. instanceVariableNames: ''
  227. category: 'Parser'!
  228. !PPNotParser methodsFor: 'parsing'!
  229. parse: aStream
  230. | element |
  231. element := self basicParse: aStream.
  232. ^element isParseFailure
  233. ifTrue: [nil]
  234. ifFalse: [PPFailure reason: element at: aStream position]
  235. ! !
  236. PPDelegateParser subclass: #PPActionParser
  237. instanceVariableNames: 'block'
  238. category: 'Parser'!
  239. !PPActionParser methodsFor: 'accessing'!
  240. block
  241. ^block
  242. !
  243. block: aBlock
  244. block := aBlock
  245. ! !
  246. !PPActionParser methodsFor: 'parsing'!
  247. parse: aStream
  248. | element |
  249. element := self parser memoizedParse: aStream.
  250. ^element isParseFailure
  251. ifFalse: [self block value: element]
  252. ifTrue: [element]
  253. ! !
  254. !PPActionParser class methodsFor: 'instance creation'!
  255. on: aParser block: aBlock
  256. ^self new
  257. parser: aParser;
  258. block: aBlock;
  259. yourself
  260. ! !
  261. PPDelegateParser subclass: #PPFlattenParser
  262. instanceVariableNames: ''
  263. category: 'Parser'!
  264. !PPFlattenParser methodsFor: 'parsing'!
  265. parse: aStream
  266. | start element stop |
  267. start := aStream position.
  268. element := self parser memoizedParse: aStream.
  269. ^element isParseFailure
  270. ifTrue: [element]
  271. ifFalse: [aStream collection
  272. copyFrom: start + 1
  273. to: aStream position]
  274. ! !
  275. PPDelegateParser subclass: #PPSourceParser
  276. instanceVariableNames: ''
  277. category: 'Parser'!
  278. !PPSourceParser methodsFor: 'parsing'!
  279. parse: aStream
  280. | start element stop result |
  281. start := aStream position.
  282. element := self parser memoizedParse: aStream.
  283. ^element isParseFailure
  284. ifTrue: [element]
  285. ifFalse: [result := aStream collection copyFrom: start + 1 to: aStream position.
  286. Array with: element with: result].
  287. ! !
  288. PPDelegateParser subclass: #PPRepeatingParser
  289. instanceVariableNames: 'min'
  290. category: 'Parser'!
  291. !PPRepeatingParser methodsFor: 'accessing'!
  292. min
  293. ^min
  294. !
  295. min: aNumber
  296. min := aNumber
  297. ! !
  298. !PPRepeatingParser methodsFor: 'parsing'!
  299. parse: aStream
  300. | start element elements failure |
  301. start := aStream position.
  302. elements := Array new.
  303. [(elements size < self min) and: [failure isNil]] whileTrue: [
  304. element := self parser memoizedParse: aStream.
  305. element isParseFailure
  306. ifFalse: [elements addLast: element]
  307. ifTrue: [aStream position: start.
  308. failure := element]].
  309. ^failure ifNil: [
  310. [failure isNil] whileTrue: [
  311. element := self parser memoizedParse: aStream.
  312. element isParseFailure
  313. ifTrue: [failure := element]
  314. ifFalse: [elements addLast: element]].
  315. elements]
  316. ifNotNil: [failure].
  317. ! !
  318. !PPRepeatingParser class methodsFor: 'instance creation'!
  319. on: aParser min: aNumber
  320. ^self new
  321. parser: aParser;
  322. min: aNumber;
  323. yourself
  324. ! !
  325. Object subclass: #PPFailure
  326. instanceVariableNames: 'position reason'
  327. category: 'Parser'!
  328. !PPFailure methodsFor: 'accessing'!
  329. position
  330. ^position ifNil: [0]
  331. !
  332. position: aNumber
  333. position := aNumber
  334. !
  335. reason
  336. ^reason ifNil: ['']
  337. !
  338. reason: aString
  339. reason := aString
  340. !
  341. reason: aString at: anInteger
  342. self
  343. reason: aString;
  344. position: anInteger
  345. ! !
  346. !PPFailure methodsFor: 'testing'!
  347. isParseFailure
  348. ^true
  349. ! !
  350. !PPFailure class methodsFor: 'instance creation'!
  351. reason: aString at: anInteger
  352. ^self new
  353. reason: aString at: anInteger;
  354. yourself
  355. ! !
  356. Object subclass: #SmalltalkParser
  357. instanceVariableNames: ''
  358. category: 'Parser'!
  359. !SmalltalkParser methodsFor: 'grammar'!
  360. parser
  361. | method expression separator comment ws identifier keyword className string symbol number literalArray variable reference classReference literal ret methodParser expressionParser keyword unarySelector binarySelector keywordPattern unaryPattern binaryPattern assignment temps blockParamList block expression expressions subexpression statements sequence operand unaryMessage unarySend unaryTail binaryMessage binarySend binaryTail keywordMessage keywordSend keywordPair cascade message jsStatement |
  362. separator := (String cr, String space, String lf, String tab) asChoiceParser.
  363. comment := ('"' asCharacterParser, ('"' asParser not, PPAnyParser new) star, '"' asCharacterParser) flatten.
  364. ws := (separator / comment) star.
  365. identifier := ('a-z' asCharacterParser, 'a-zA-Z0-9' asCharacterParser star) flatten.
  366. keyword := (identifier, ':' asParser) flatten.
  367. className := ('A-Z' asCharacterParser, 'a-zA-Z0-9' asCharacterParser star) flatten.
  368. string := '''' asParser, ('''''' asParser / ('''' asParser not, PPAnyParser new)) star flatten, '''' asParser
  369. ==> [:node | ValueNode new value: ((node at: 2) replace: '''''' with: '''')].
  370. symbol := '#' asParser, 'a-zA-Z0-9' asCharacterParser plus flatten
  371. ==> [:node | ValueNode new value: node second].
  372. number := ('0-9' asCharacterParser plus, ('.' asParser, '0-9' asCharacterParser plus) optional) flatten
  373. ==> [:node | ValueNode new value: node asNumber].
  374. literal := PPDelegateParser new.
  375. literalArray := '#(' asParser, (ws, literal, ws) star, ')' asParser
  376. ==> [:node | ValueNode new value: (Array withAll: (node second collect: [:each | each second value]))].
  377. variable := identifier ==> [:token | VariableNode new value: token].
  378. classReference := className ==> [:token | ClassReferenceNode new value: token].
  379. reference := variable / classReference.
  380. binarySelector := '+*/=><,@%~-' asCharacterParser plus flatten.
  381. unarySelector := identifier.
  382. keywordPattern := (ws, keyword, ws, identifier) plus
  383. ==> [:nodes | Array
  384. with: ((nodes collect: [:each | each at: 2]) join: '')
  385. with: (nodes collect: [:each | each at: 4])].
  386. binaryPattern := ws, binarySelector, ws, identifier
  387. ==> [:node | Array with: node second with: (Array with: node fourth)].
  388. unaryPattern := ws, unarySelector
  389. ==> [:node | Array with: node second with: Array new].
  390. expression := PPDelegateParser new.
  391. expressions := expression, ((ws, '.' asParser, ws, expression) ==> [:node | node fourth]) star
  392. ==> [:node || result |
  393. result := Array with: node first.
  394. node second do: [:each | result add: each].
  395. result].
  396. assignment := reference, ws, ':=' asParser, ws, expression
  397. ==> [:node | AssignmentNode new left: node first; right: (node at: 5)].
  398. ret := '^' asParser, ws, expression, ws, '.' asParser optional
  399. ==> [:node | ReturnNode new
  400. addNode: node third;
  401. yourself].
  402. temps := '|' asParser, (ws, identifier) star, ws, '|' asParser
  403. ==> [:node | node second collect: [:each | each second]].
  404. blockParamList := (':' asParser, identifier, ws) plus, '|' asParser
  405. ==> [:node | node first collect: [:each | each second]].
  406. subexpression := '(' asParser, ws, expression, ws, ')' asParser
  407. ==> [:node | node third].
  408. statements := (ret ==> [:node | Array with: node]) / (expressions, ws, '.' asParser, ws, ret ==> [:node | node first add: (node at: 5); yourself]) / (expressions , '.' asParser optional ==> [:node | node first]).
  409. sequence := temps optional, ws, statements optional, ws
  410. ==> [:node | SequenceNode new
  411. temps: node first;
  412. nodes: node third;
  413. yourself].
  414. block := '[' asParser, ws, blockParamList optional, ws, sequence optional, ws, ']' asParser
  415. ==> [:node |
  416. BlockNode new
  417. parameters: node third;
  418. addNode: (node at: 5) asBlockSequenceNode].
  419. operand := literal / reference / subexpression.
  420. literal parser: number / string / literalArray / symbol / block.
  421. unaryMessage := ws, unarySelector, ':' asParser not
  422. ==> [:node | SendNode new selector: node second].
  423. unaryTail := PPDelegateParser new.
  424. unaryTail parser: (unaryMessage, unaryTail optional
  425. ==> [:node |
  426. node second
  427. ifNil: [node first]
  428. ifNotNil: [node second valueForReceiver: node first]]).
  429. unarySend := operand, unaryTail optional
  430. ==> [:node |
  431. node second
  432. ifNil: [node first]
  433. ifNotNil: [node second valueForReceiver: node first]].
  434. binaryMessage := ws, binarySelector, ws, (unarySend / operand)
  435. ==> [:node |
  436. SendNode new
  437. selector: node second;
  438. arguments: (Array with: node fourth)].
  439. binaryTail := PPDelegateParser new.
  440. binaryTail parser: (binaryMessage, binaryTail optional
  441. ==> [:node |
  442. node second
  443. ifNil: [node first]
  444. ifNotNil: [ node second valueForReceiver: node first]]).
  445. binarySend := unarySend, binaryTail optional
  446. ==> [:node |
  447. node second
  448. ifNil: [node first]
  449. ifNotNil: [node second valueForReceiver: node first]].
  450. keywordPair := keyword, ws, binarySend.
  451. keywordMessage := (ws, keywordPair) plus
  452. ==> [:nodes |
  453. SendNode new
  454. selector: ((nodes collect: [:each | each second first]) join: '');
  455. arguments: (nodes collect: [:each | each second third])].
  456. keywordSend := binarySend, keywordMessage
  457. ==> [:node |
  458. node second valueForReceiver: node first].
  459. message := binaryMessage / unaryMessage / keywordMessage.
  460. cascade := (keywordSend / binarySend), (ws, ';' asParser, message) plus
  461. ==> [:node |
  462. node first cascadeNodeWithMessages:
  463. (node second collect: [:each | each third])].
  464. jsStatement := '{' asParser, ws, string, ws, '}' asParser
  465. ==> [:node | JSStatementNode new
  466. source: node third;
  467. yourself].
  468. expression parser: assignment / cascade / keywordSend / binarySend / jsStatement.
  469. method := (ws, (keywordPattern / binaryPattern / unaryPattern), ws, sequence optional, ws) withSource
  470. ==> [:node |
  471. MethodNode new
  472. selector: node first second first;
  473. arguments: node first second second;
  474. addNode: node first fourth;
  475. source: node second;
  476. yourself].
  477. ^method, PPEOFParser new ==> [:node | node first]
  478. ! !
  479. !SmalltalkParser methodsFor: 'parsing'!
  480. parse: aStream
  481. ^self parser parse: aStream
  482. ! !
  483. !SmalltalkParser class methodsFor: 'instance creation'!
  484. parse: aStream
  485. ^self new
  486. parse: aStream
  487. ! !
  488. Object subclass: #Chunk
  489. instanceVariableNames: 'contents'
  490. category: 'Parser'!
  491. !Chunk methodsFor: 'accessing'!
  492. contents
  493. ^contents ifNil: ['']
  494. !
  495. contents: aString
  496. contents := aString
  497. ! !
  498. !Chunk methodsFor: 'testing'!
  499. isEmptyChunk
  500. ^false
  501. !
  502. isInstructionChunk
  503. ^false
  504. ! !
  505. Chunk subclass: #InstructionChunk
  506. instanceVariableNames: ''
  507. category: 'Parser'!
  508. !InstructionChunk methodsFor: 'testing'!
  509. isInstructionChunk
  510. ^true
  511. ! !
  512. Chunk subclass: #EmptyChunk
  513. instanceVariableNames: ''
  514. category: 'Parser'!
  515. !EmptyChunk methodsFor: 'testing'!
  516. isEmptyChunk
  517. ^true
  518. ! !
  519. Object subclass: #ChunkParser
  520. instanceVariableNames: 'parser, separator, eof, ws, chunk, emptyChunk, instructionChunk'
  521. category: 'Parser'!
  522. !ChunkParser methodsFor: ''!
  523. instructionChunk
  524. ^instructionChunk ifNil: [
  525. instructionChunk := self ws, '!' asParser, self chunk
  526. ==> [:node | InstructionChunk new contents: node last contents]]
  527. ! !
  528. !ChunkParser methodsFor: 'accessing'!
  529. parser
  530. ^parser ifNil: [
  531. parser := self instructionChunk / self emptyChunk / self chunk / self eof]
  532. !
  533. eof
  534. ^eof ifNil: [eof := self ws, PPEOFParser new ==> [:node | nil]]
  535. !
  536. separator
  537. ^separator ifNil: [separator := (String cr, String space, String lf, String tab) asChoiceParser]
  538. !
  539. ws
  540. ^ws ifNil: [ws := self separator star]
  541. !
  542. chunk
  543. ^chunk ifNil: [chunk := self ws, ('!!' asParser / ('!' asParser not, PPAnyParser new)) plus flatten, '!' asParser ==> [:node | Chunk new contents: (node second replace: '!!' with: '!')]]
  544. !
  545. emptyChunk
  546. ^emptyChunk ifNil: [emptyChunk := self separator plus, '!' asParser, self ws ==> [:node | EmptyChunk new]]
  547. ! !
  548. Object subclass: #Importer
  549. instanceVariableNames: 'chunkParser'
  550. category: 'Parser'!
  551. !Importer methodsFor: 'accessing'!
  552. chunkParser
  553. ^chunkParser ifNil: [chunkParser := ChunkParser new parser]
  554. ! !
  555. !Importer methodsFor: 'fileIn'!
  556. import: aStream
  557. aStream atEnd ifFalse: [
  558. | nextChunk |
  559. nextChunk := self chunkParser parse: aStream.
  560. nextChunk ifNotNil: [
  561. nextChunk isInstructionChunk
  562. ifTrue: [(Compiler new loadExpression: nextChunk contents)
  563. scanFrom: aStream]
  564. ifFalse: [Compiler new loadExpression: nextChunk contents].
  565. self import: aStream]]
  566. ! !
  567. Object subclass: #Exporter
  568. instanceVariableNames: ''
  569. category: 'Parser'!
  570. !Exporter methodsFor: 'fileOut'!
  571. exportCategory: aString
  572. | stream |
  573. stream := '' writeStream.
  574. (Smalltalk current classes
  575. select: [:each | each category = aString])
  576. do: [:each | stream nextPutAll: (self export: each)].
  577. self exportCategoryExtensions: aString on: stream.
  578. ^stream contents
  579. !
  580. export: aClass
  581. | stream |
  582. stream := '' writeStream.
  583. self exportDefinitionOf: aClass on: stream.
  584. self exportMethodsOf: aClass on: stream.
  585. self exportMetaDefinitionOf: aClass on: stream.
  586. self exportMethodsOf: aClass class on: stream.
  587. ^stream contents
  588. ! !
  589. !Exporter methodsFor: 'private'!
  590. exportDefinitionOf: aClass on: aStream
  591. aStream
  592. nextPutAll: 'smalltalk.addClass(';
  593. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  594. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  595. nextPutAll: ', ['.
  596. aClass instanceVariableNames
  597. do: [:each | aStream nextPutAll: '''', each, '''']
  598. separatedBy: [aStream nextPutAll: ', '].
  599. aStream
  600. nextPutAll: '], ''';
  601. nextPutAll: aClass category, '''';
  602. nextPutAll: ');'.
  603. aClass comment notEmpty ifTrue: [
  604. aStream
  605. lf;
  606. nextPutAll: 'smalltalk.';
  607. nextPutAll: (self classNameFor: aClass);
  608. nextPutAll: '.comment=';
  609. nextPutAll: 'unescape(''', aClass comment escaped, ''')'].
  610. aStream lf
  611. !
  612. exportMetaDefinitionOf: aClass on: aStream
  613. aClass class instanceVariableNames isEmpty ifFalse: [
  614. aStream
  615. nextPutAll: 'smalltalk.', (self classNameFor: aClass class);
  616. nextPutAll: '.iVarNames = ['.
  617. aClass class instanceVariableNames
  618. do: [:each | aStream nextPutAll: '''', each, '''']
  619. separatedBy: [aStream nextPutAll: ','].
  620. aStream nextPutAll: '];', String lf]
  621. !
  622. exportMethodsOf: aClass on: aStream
  623. aClass methodDictionary values do: [:each |
  624. (each category match: '^\*') ifFalse: [
  625. self exportMethod: each of: aClass on: aStream]].
  626. aStream lf
  627. !
  628. classNameFor: aClass
  629. ^aClass isMetaclass
  630. ifTrue: [aClass instanceClass name, '.klass']
  631. ifFalse: [
  632. aClass isNil
  633. ifTrue: ['nil']
  634. ifFalse: [aClass name]]
  635. !
  636. exportMethod: aMethod of: aClass on: aStream
  637. aStream
  638. nextPutAll: 'smalltalk.addMethod(';lf;
  639. nextPutAll: '''', aMethod selector asSelector, ''',';lf;
  640. nextPutAll: 'smalltalk.method({';lf;
  641. nextPutAll: 'selector: ''', aMethod selector, ''',';lf;
  642. nextPutAll: 'category: ''', aMethod category, ''',';lf;
  643. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  644. nextPutAll: 'source: unescape(''', aMethod source escaped, '''),';lf;
  645. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript, ',';lf;
  646. nextPutAll: 'referencedClasses: ['.
  647. aMethod referencedClasses
  648. do: [:each | aStream nextPutAll: 'smalltalk.', (self classNameFor: each)]
  649. separatedBy: [aStream nextPutAll: ','].
  650. aStream
  651. nextPutAll: ']';lf;
  652. nextPutAll: '}),';lf;
  653. nextPutAll: 'smalltalk.', (self classNameFor: aClass);
  654. nextPutAll: ');';lf;lf
  655. !
  656. exportCategoryExtensions: aString on: aStream
  657. Smalltalk current classes, (Smalltalk current classes collect: [:each | each class]) do: [:each |
  658. each methodDictionary values do: [:method |
  659. method category = ('*', aString) ifTrue: [
  660. self exportMethod: method of: each on: aStream]]]
  661. ! !
  662. Exporter subclass: #ChunkExporter
  663. instanceVariableNames: ''
  664. category: 'Parser'!
  665. !ChunkExporter methodsFor: 'not yet classified'!
  666. exportDefinitionOf: aClass on: aStream
  667. "Chunk format."
  668. aStream
  669. nextPutAll: (self classNameFor: aClass superclass);
  670. nextPutAll: ' subclass: #', (self classNameFor: aClass); lf;
  671. nextPutAll: ' instanceVariableNames: '''.
  672. aClass instanceVariableNames
  673. do: [:each | aStream nextPutAll: each]
  674. separatedBy: [aStream nextPutAll: ' '].
  675. aStream
  676. nextPutAll: ''''; lf;
  677. nextPutAll: ' category: ''', aClass category, '''!!'; lf.
  678. aClass comment notEmpty ifTrue: [
  679. aStream
  680. nextPutAll: '!!', (self classNameFor: aClass), ' commentStamp!!';lf;
  681. nextPutAll: (self chunkEscape: aClass comment), '!!';lf].
  682. aStream lf
  683. !
  684. exportMethod: aMethod of: aClass on: aStream
  685. aStream
  686. lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;
  687. nextPutAll: '!!'
  688. !
  689. exportMethodsOf: aClass on: aStream
  690. | methodsByCategory |
  691. methodsByCategory := Dictionary new.
  692. aClass methodDictionary values do: [:m |
  693. (methodsByCategory at: m category ifAbsentPut: [Array new])
  694. add: m].
  695. aClass protocols do: [:category |
  696. aStream
  697. nextPutAll: '!', (self classNameFor: aClass);
  698. nextPutAll: ' methodsFor: ''', category, '''!'.
  699. (methodsByCategory at: category) do: [:each |
  700. self exportMethod: each of: aClass on: aStream].
  701. aStream nextPutAll: ' !'; lf; lf]
  702. !
  703. exportMetaDefinitionOf: aClass on: aStream
  704. aClass class instanceVariableNames isEmpty ifFalse: [
  705. aStream
  706. nextPutAll: (self classNameFor: aClass class);
  707. nextPutAll: ' instanceVariableNames: '''.
  708. aClass class instanceVariableNames
  709. do: [:each | aStream nextPutAll: each]
  710. separatedBy: [aStream nextPutAll: ' '].
  711. aStream
  712. nextPutAll: '''!!'; lf; lf]
  713. !
  714. classNameFor: aClass
  715. ^aClass isMetaclass
  716. ifTrue: [aClass instanceClass name, ' class']
  717. ifFalse: [
  718. aClass isNil
  719. ifTrue: ['nil']
  720. ifFalse: [aClass name]]
  721. ! !