Parser.st 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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: aStream contents, String lf, '---------------', String lf, '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. accept: aVisitor
  347. aVisitor visitFailure: self
  348. ! !
  349. !PPFailure methodsFor: 'testing'!
  350. isParseFailure
  351. ^true
  352. !
  353. asString
  354. ^reason, ' at ', position asString
  355. ! !
  356. !PPFailure class methodsFor: 'instance creation'!
  357. reason: aString at: anInteger
  358. ^self new
  359. reason: aString at: anInteger;
  360. yourself
  361. ! !
  362. Object subclass: #SmalltalkParser
  363. instanceVariableNames: ''
  364. category: 'Parser'!
  365. !SmalltalkParser methodsFor: 'grammar'!
  366. parser
  367. | 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 |
  368. separator := (String cr, String space, String lf, String tab) asChoiceParser.
  369. comment := ('"' asCharacterParser, ('"' asParser not, PPAnyParser new) star, '"' asCharacterParser) flatten.
  370. ws := (separator / comment) star.
  371. identifier := ('a-z' asCharacterParser, 'a-zA-Z0-9' asCharacterParser star) flatten.
  372. keyword := (identifier, ':' asParser) flatten.
  373. className := ('A-Z' asCharacterParser, 'a-zA-Z0-9' asCharacterParser star) flatten.
  374. string := '''' asParser, ('''''' asParser / ('''' asParser not, PPAnyParser new)) star flatten, '''' asParser
  375. ==> [:node | ValueNode new value: ((node at: 2) replace: '''''' with: '''')].
  376. symbol := '#' asParser, 'a-zA-Z0-9' asCharacterParser plus flatten
  377. ==> [:node | ValueNode new value: node second].
  378. number := ('0-9' asCharacterParser plus, ('.' asParser, '0-9' asCharacterParser plus) optional) flatten
  379. ==> [:node | ValueNode new value: node asNumber].
  380. literal := PPDelegateParser new.
  381. literalArray := '#(' asParser, (ws, literal, ws) star, ')' asParser
  382. ==> [:node | ValueNode new value: (Array withAll: (node second collect: [:each | each second value]))].
  383. variable := identifier ==> [:token | VariableNode new value: token].
  384. classReference := className ==> [:token | ClassReferenceNode new value: token].
  385. reference := variable / classReference.
  386. binarySelector := '+*/=><,@%~|&-' asCharacterParser plus flatten.
  387. unarySelector := identifier.
  388. keywordPattern := (ws, keyword, ws, identifier) plus
  389. ==> [:nodes | Array
  390. with: ((nodes collect: [:each | each at: 2]) join: '')
  391. with: (nodes collect: [:each | each at: 4])].
  392. binaryPattern := ws, binarySelector, ws, identifier
  393. ==> [:node | Array with: node second with: (Array with: node fourth)].
  394. unaryPattern := ws, unarySelector
  395. ==> [:node | Array with: node second with: Array new].
  396. expression := PPDelegateParser new.
  397. expressions := expression, ((ws, '.' asParser, ws, expression) ==> [:node | node fourth]) star
  398. ==> [:node || result |
  399. result := Array with: node first.
  400. node second do: [:each | result add: each].
  401. result].
  402. assignment := variable, ws, ':=' asParser, ws, expression
  403. ==> [:node | AssignmentNode new left: node first; right: (node at: 5)].
  404. ret := '^' asParser, ws, expression, ws, '.' asParser optional
  405. ==> [:node | ReturnNode new
  406. addNode: node third;
  407. yourself].
  408. temps := '|' asParser, (ws, identifier) star, ws, '|' asParser
  409. ==> [:node | node second collect: [:each | each second]].
  410. blockParamList := (':' asParser, identifier, ws) plus, '|' asParser
  411. ==> [:node | node first collect: [:each | each second]].
  412. subexpression := '(' asParser, ws, expression, ws, ')' asParser
  413. ==> [:node | node third].
  414. 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]).
  415. sequence := temps optional, ws, statements optional, ws
  416. ==> [:node | SequenceNode new
  417. temps: node first;
  418. nodes: node third;
  419. yourself].
  420. block := '[' asParser, ws, blockParamList optional, ws, sequence optional, ws, ']' asParser
  421. ==> [:node |
  422. BlockNode new
  423. parameters: node third;
  424. addNode: (node at: 5) asBlockSequenceNode].
  425. operand := literal / reference / subexpression.
  426. literal parser: number / string / literalArray / symbol / block.
  427. unaryMessage := ws, unarySelector, ':' asParser not
  428. ==> [:node | SendNode new selector: node second].
  429. unaryTail := PPDelegateParser new.
  430. unaryTail parser: (unaryMessage, unaryTail optional
  431. ==> [:node |
  432. node second
  433. ifNil: [node first]
  434. ifNotNil: [node second valueForReceiver: node first]]).
  435. unarySend := operand, unaryTail optional
  436. ==> [:node |
  437. node second
  438. ifNil: [node first]
  439. ifNotNil: [node second valueForReceiver: node first]].
  440. binaryMessage := ws, binarySelector, ws, (unarySend / operand)
  441. ==> [:node |
  442. SendNode new
  443. selector: node second;
  444. arguments: (Array with: node fourth)].
  445. binaryTail := PPDelegateParser new.
  446. binaryTail parser: (binaryMessage, binaryTail optional
  447. ==> [:node |
  448. node second
  449. ifNil: [node first]
  450. ifNotNil: [ node second valueForReceiver: node first]]).
  451. binarySend := unarySend, binaryTail optional
  452. ==> [:node |
  453. node second
  454. ifNil: [node first]
  455. ifNotNil: [node second valueForReceiver: node first]].
  456. keywordPair := keyword, ws, binarySend.
  457. keywordMessage := (ws, keywordPair) plus
  458. ==> [:nodes |
  459. SendNode new
  460. selector: ((nodes collect: [:each | each second first]) join: '');
  461. arguments: (nodes collect: [:each | each second third])].
  462. keywordSend := binarySend, keywordMessage
  463. ==> [:node |
  464. node second valueForReceiver: node first].
  465. message := binaryMessage / unaryMessage / keywordMessage.
  466. cascade := (keywordSend / binarySend), (ws, ';' asParser, message) plus
  467. ==> [:node |
  468. node first cascadeNodeWithMessages:
  469. (node second collect: [:each | each third])].
  470. jsStatement := '<' asParser, ('>>' asParser / ('>' asParser not, PPAnyParser new)) star flatten, '>' asParser
  471. ==> [:node | JSStatementNode new
  472. source: node second;
  473. yourself].
  474. expression parser: assignment / cascade / keywordSend / binarySend / jsStatement.
  475. method := (ws, (keywordPattern / binaryPattern / unaryPattern), ws, sequence optional, ws) withSource
  476. ==> [:node |
  477. MethodNode new
  478. selector: node first second first;
  479. arguments: node first second second;
  480. addNode: node first fourth;
  481. source: node second;
  482. yourself].
  483. ^method, PPEOFParser new ==> [:node | node first]
  484. ! !
  485. !SmalltalkParser methodsFor: 'parsing'!
  486. parse: aStream
  487. ^self parser parse: aStream
  488. ! !
  489. !SmalltalkParser class methodsFor: 'instance creation'!
  490. parse: aStream
  491. ^self new
  492. parse: aStream
  493. ! !
  494. Object subclass: #ChunkParser
  495. instanceVariableNames: 'stream'
  496. category: 'Parser'!
  497. !ChunkParser methodsFor: 'accessing'!
  498. stream: aStream
  499. stream := aStream
  500. ! !
  501. !ChunkParser methodsFor: 'reading'!
  502. nextChunk
  503. "The chunk format (Smalltalk Interchange Format or Fileout format)
  504. is a trivial format but can be a bit tricky to understand:
  505. - Uses the exclamation mark as delimiter of chunks.
  506. - Inside a chunk a normal exclamation mark must be doubled.
  507. - A non empty chunk must be a valid Smalltalk expression.
  508. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  509. - The object created by the expression then takes over reading chunks.
  510. This metod returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  511. | char result chunk |
  512. result := '' writeStream.
  513. [char := stream next.
  514. char notNil] whileTrue: [
  515. char = '!!' ifTrue: [
  516. stream peek = '!!'
  517. ifTrue: [stream next "skipping the escape double"]
  518. ifFalse: [^result contents trimBoth "chunk end marker found"]].
  519. result nextPut: char].
  520. ^nil "a chunk needs to end with !!"
  521. ! !
  522. !ChunkParser class methodsFor: 'not yet classified'!
  523. on: aStream
  524. ^self new stream: aStream
  525. ! !
  526. Object subclass: #Importer
  527. instanceVariableNames: ''
  528. category: 'Parser'!
  529. !Importer methodsFor: 'fileIn'!
  530. import: aStream
  531. | chunk result parser lastEmpty |
  532. parser := ChunkParser on: aStream.
  533. lastEmpty := false.
  534. [chunk := parser nextChunk.
  535. chunk isNil] whileFalse: [
  536. chunk isEmpty
  537. ifTrue: [lastEmpty := true]
  538. ifFalse: [
  539. result := Compiler new loadExpression: chunk.
  540. lastEmpty
  541. ifTrue: [
  542. lastEmpty := false.
  543. result scanFrom: parser]]]
  544. ! !
  545. Object subclass: #Exporter
  546. instanceVariableNames: ''
  547. category: 'Parser'!
  548. !Exporter methodsFor: 'fileOut'!
  549. exportCategory: aString
  550. | stream |
  551. stream := '' writeStream.
  552. (Smalltalk current classes
  553. select: [:each | each category = aString])
  554. do: [:each | stream nextPutAll: (self export: each)].
  555. self exportCategoryExtensions: aString on: stream.
  556. ^stream contents
  557. !
  558. export: aClass
  559. | stream |
  560. stream := '' writeStream.
  561. self exportDefinitionOf: aClass on: stream.
  562. self exportMethodsOf: aClass on: stream.
  563. self exportMetaDefinitionOf: aClass on: stream.
  564. self exportMethodsOf: aClass class on: stream.
  565. ^stream contents
  566. ! !
  567. !Exporter methodsFor: 'private'!
  568. exportDefinitionOf: aClass on: aStream
  569. aStream
  570. nextPutAll: 'smalltalk.addClass(';
  571. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  572. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  573. nextPutAll: ', ['.
  574. aClass instanceVariableNames
  575. do: [:each | aStream nextPutAll: '''', each, '''']
  576. separatedBy: [aStream nextPutAll: ', '].
  577. aStream
  578. nextPutAll: '], ''';
  579. nextPutAll: aClass category, '''';
  580. nextPutAll: ');'.
  581. aClass comment notEmpty ifTrue: [
  582. aStream
  583. lf;
  584. nextPutAll: 'smalltalk.';
  585. nextPutAll: (self classNameFor: aClass);
  586. nextPutAll: '.comment=';
  587. nextPutAll: 'unescape(''', aClass comment escaped, ''')'].
  588. aStream lf
  589. !
  590. exportMetaDefinitionOf: aClass on: aStream
  591. aClass class instanceVariableNames isEmpty ifFalse: [
  592. aStream
  593. nextPutAll: 'smalltalk.', (self classNameFor: aClass class);
  594. nextPutAll: '.iVarNames = ['.
  595. aClass class instanceVariableNames
  596. do: [:each | aStream nextPutAll: '''', each, '''']
  597. separatedBy: [aStream nextPutAll: ','].
  598. aStream nextPutAll: '];', String lf]
  599. !
  600. exportMethodsOf: aClass on: aStream
  601. aClass methodDictionary values do: [:each |
  602. (each category match: '^\*') ifFalse: [
  603. self exportMethod: each of: aClass on: aStream]].
  604. aStream lf
  605. !
  606. classNameFor: aClass
  607. ^aClass isMetaclass
  608. ifTrue: [aClass instanceClass name, '.klass']
  609. ifFalse: [
  610. aClass isNil
  611. ifTrue: ['nil']
  612. ifFalse: [aClass name]]
  613. !
  614. exportMethod: aMethod of: aClass on: aStream
  615. aStream
  616. nextPutAll: 'smalltalk.addMethod(';lf;
  617. nextPutAll: '''', aMethod selector asSelector, ''',';lf;
  618. nextPutAll: 'smalltalk.method({';lf;
  619. nextPutAll: 'selector: ''', aMethod selector, ''',';lf;
  620. nextPutAll: 'category: ''', aMethod category, ''',';lf;
  621. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  622. nextPutAll: 'source: unescape(''', aMethod source escaped, '''),';lf;
  623. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript, ',';lf;
  624. nextPutAll: 'referencedClasses: ['.
  625. aMethod referencedClasses
  626. do: [:each | aStream nextPutAll: 'smalltalk.', (self classNameFor: each)]
  627. separatedBy: [aStream nextPutAll: ','].
  628. aStream
  629. nextPutAll: ']';lf;
  630. nextPutAll: '}),';lf;
  631. nextPutAll: 'smalltalk.', (self classNameFor: aClass);
  632. nextPutAll: ');';lf;lf
  633. !
  634. exportCategoryExtensions: aString on: aStream
  635. Smalltalk current classes, (Smalltalk current classes collect: [:each | each class]) do: [:each |
  636. each methodDictionary values do: [:method |
  637. method category = ('*', aString) ifTrue: [
  638. self exportMethod: method of: each on: aStream]]]
  639. ! !
  640. Exporter subclass: #ChunkExporter
  641. instanceVariableNames: ''
  642. category: 'Parser'!
  643. !ChunkExporter methodsFor: 'not yet classified'!
  644. exportDefinitionOf: aClass on: aStream
  645. "Chunk format."
  646. aStream
  647. nextPutAll: (self classNameFor: aClass superclass);
  648. nextPutAll: ' subclass: #', (self classNameFor: aClass); lf;
  649. nextPutAll: ' instanceVariableNames: '''.
  650. aClass instanceVariableNames
  651. do: [:each | aStream nextPutAll: each]
  652. separatedBy: [aStream nextPutAll: ' '].
  653. aStream
  654. nextPutAll: ''''; lf;
  655. nextPutAll: ' category: ''', aClass category, '''!!'; lf.
  656. aClass comment notEmpty ifTrue: [
  657. aStream
  658. nextPutAll: '!!', (self classNameFor: aClass), ' commentStamp!!';lf;
  659. nextPutAll: (self chunkEscape: aClass comment), '!!';lf].
  660. aStream lf
  661. !
  662. exportMethod: aMethod of: aClass on: aStream
  663. aStream
  664. lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;
  665. nextPutAll: '!!'
  666. !
  667. exportMethodsOf: aClass on: aStream
  668. aClass protocolsDo: [:category :methods |
  669. (category match: '^\*') ifFalse: [
  670. self
  671. exportMethods: methods
  672. category: category
  673. of: aClass
  674. on: aStream]]
  675. !
  676. exportMetaDefinitionOf: aClass on: aStream
  677. aClass class instanceVariableNames isEmpty ifFalse: [
  678. aStream
  679. nextPutAll: (self classNameFor: aClass class);
  680. nextPutAll: ' instanceVariableNames: '''.
  681. aClass class instanceVariableNames
  682. do: [:each | aStream nextPutAll: each]
  683. separatedBy: [aStream nextPutAll: ' '].
  684. aStream
  685. nextPutAll: '''!!'; lf; lf]
  686. !
  687. classNameFor: aClass
  688. ^aClass isMetaclass
  689. ifTrue: [aClass instanceClass name, ' class']
  690. ifFalse: [
  691. aClass isNil
  692. ifTrue: ['nil']
  693. ifFalse: [aClass name]]
  694. !
  695. chunkEscape: aString
  696. "Replace all occurrences of !! with !!!! and trim at both ends."
  697. ^(aString replace: '!!' with: '!!!!') trimBoth
  698. !
  699. exportCategoryExtensions: aString on: aStream
  700. "We need to override this one too since we need to group
  701. all methods in a given protocol under a leading methodsFor: chunk
  702. for that class."
  703. Smalltalk current classes, (Smalltalk current classes collect: [:each | each class]) do: [:each |
  704. each protocolsDo: [:category :methods |
  705. category = ('*', aString) ifTrue: [
  706. self exportMethods: methods category: category of: each on: aStream]]]
  707. !
  708. exportMethods: methods category: category of: aClass on: aStream
  709. aStream
  710. nextPutAll: '!!', (self classNameFor: aClass);
  711. nextPutAll: ' methodsFor: ''', category, '''!!'.
  712. methods do: [:each |
  713. self exportMethod: each of: aClass on: aStream].
  714. aStream nextPutAll: ' !!'; lf; lf
  715. ! !
  716. Exporter subclass: #StrippedExporter
  717. instanceVariableNames: ''
  718. category: 'Parser'!
  719. !StrippedExporter methodsFor: 'private'!
  720. exportDefinitionOf: aClass on: aStream
  721. aStream
  722. nextPutAll: 'smalltalk.addClass(';
  723. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  724. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  725. nextPutAll: ', ['.
  726. aClass instanceVariableNames
  727. do: [:each | aStream nextPutAll: '''', each, '''']
  728. separatedBy: [aStream nextPutAll: ', '].
  729. aStream
  730. nextPutAll: '], ''';
  731. nextPutAll: aClass category, '''';
  732. nextPutAll: ');'.
  733. aStream lf
  734. !
  735. exportMethod: aMethod of: aClass on: aStream
  736. aStream
  737. nextPutAll: 'smalltalk.addMethod(';lf;
  738. nextPutAll: '''', aMethod selector asSelector, ''',';lf;
  739. nextPutAll: 'smalltalk.method({';lf;
  740. nextPutAll: 'selector: ''', aMethod selector, ''',';lf;
  741. nextPutAll: 'fn: ', aMethod fn compiledSource;lf;
  742. nextPutAll: '}),';lf;
  743. nextPutAll: 'smalltalk.', (self classNameFor: aClass);
  744. nextPutAll: ');';lf;lf
  745. ! !