-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOMeta2.pck.st
1169 lines (864 loc) · 40.4 KB
/
OMeta2.pck.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'From Cuis 5.0 [latest update: #4642] on 6 July 2021 at 12:10:19 am'!
'Description OMeta2 (often referred to just as ''OMeta'') is a PEG-based (Parsing Expression Grammar) Object Oriented pattern matching language. More simply put, it''s a parser which you can use to write other parsers. A few of its unique characteristics:
1) It is a parasitic language which is a hybrid of its own OMeta syntax and the host language, in this case Smalltalk.
2) It is written in itself. There is a preload file (see below) which provides a minimal Smalltalk implementation of OMeta, and then this package is overlaid on it effectively reimplementing OMeta in OMeta.
3) As you can see from 2, it is a dynamic parser which is very compatible with the Smalltalk ethos. In many cases, you can modify the definition of an existing parser while it is running.
4) While it was originally written to help prototype new programming languages, it can just as easily parse just about anything you can think of from data files to network packets.
Note: OMeta2Preload.st needs to be filed in before loading this package (handled by OMeta2Package class>>prePackageInstall)
This package is based on the original work by Alessandro Warth http://www.tinlizzie.org/ometa/OMeta.sar with updates from OMeta2-Preload-hmm.14.mcz OMeta2-Postload-hmm.12.mcz and modified and extended by Phil Bellalouna for Cuis.
Since the original implementation of OMeta2 has been abandoned by its author, I''ve decided to not retain absolute compatiblity with it. Mostly this means that I''ve made a few minor extensions to the core language that are (mostly) backwards compatible and fixed some bugs.'!
!provides: 'OMeta2' 2 17!
SystemOrganization addCategory: 'OMeta2'!
!classDefinition: #OMeta2Package category: 'OMeta2'!
CodePackage subclass: #OMeta2Package
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2Package class' category: 'OMeta2'!
OMeta2Package class
instanceVariableNames: ''!
!OMeta2Package commentStamp: '<historical>' prior: 0!
My #prePackageInstall method ensures that OMeta2Preload (a non-package prerequisite) is loaded before this package loads.!
!OMeta2Package class methodsFor: 'installing' stamp: 'pb 4/22/2020 03:50:43'!
prePackageInstall
(Smalltalk classNamed: 'OMeta2Base') ifNil: [
"If warnings are enabled they will currently likely overwhelm the transcript"
Preferences
setPreference: #warnAboutNonLocalReturnsInExceptionHandlers
toValue: false.
Transcript
show: 'Loading OMeta2Preload.st';
finishEntry.
ChangeSet fileIn: (self package fullFileName asFileEntry parent concatPathComponentsAsFile: 'OMeta2Preload.st' asPathComponents).
Transcript
show: 'Finished loading OMeta2Preload.st';
finishEntry ].! !
!OMeta2Package class methodsFor: 'private-meta-development' stamp: 'pb 4/27/2019 23:48:34'!
isAbstract
^ thisContext methodClass == self class.! !
OMeta2Package prePackageInstall!
!classDefinition: #OMeta2 category: 'OMeta2'!
OMeta2Base subclass: #OMeta2
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2 class' category: 'OMeta2'!
OMeta2 class
instanceVariableNames: ''!
!classDefinition: #OMeta2Flattener category: 'OMeta2'!
OMeta2 subclass: #OMeta2Flattener
instanceVariableNames: 'ws'
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2Flattener class' category: 'OMeta2'!
OMeta2Flattener class
instanceVariableNames: ''!
!classDefinition: #OMeta2NullOptimizer category: 'OMeta2'!
OMeta2 subclass: #OMeta2NullOptimizer
instanceVariableNames: 'didSomething'
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2NullOptimizer class' category: 'OMeta2'!
OMeta2NullOptimizer class
instanceVariableNames: ''!
!classDefinition: #OMeta2AndOrOptimizer category: 'OMeta2'!
OMeta2NullOptimizer subclass: #OMeta2AndOrOptimizer
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2AndOrOptimizer class' category: 'OMeta2'!
OMeta2AndOrOptimizer class
instanceVariableNames: ''!
!classDefinition: #OMeta2FinalizeOptimizer category: 'OMeta2'!
OMeta2NullOptimizer subclass: #OMeta2FinalizeOptimizer
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2FinalizeOptimizer class' category: 'OMeta2'!
OMeta2FinalizeOptimizer class
instanceVariableNames: ''!
!classDefinition: #OMeta2Optimizer category: 'OMeta2'!
OMeta2 subclass: #OMeta2Optimizer
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2Optimizer class' category: 'OMeta2'!
OMeta2Optimizer class
instanceVariableNames: ''!
!classDefinition: #OMeta2RuleParser category: 'OMeta2'!
OMeta2 subclass: #OMeta2RuleParser
instanceVariableNames: 'temps'
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2RuleParser class' category: 'OMeta2'!
OMeta2RuleParser class
instanceVariableNames: ''!
!classDefinition: #OMeta2RuleTranslator category: 'OMeta2'!
OMeta2 subclass: #OMeta2RuleTranslator
instanceVariableNames: 'grammarClass'
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2RuleTranslator class' category: 'OMeta2'!
OMeta2RuleTranslator class
instanceVariableNames: ''!
!classDefinition: #OMeta2SqueakSmalltalkRecognizer category: 'OMeta2'!
OMeta2 subclass: #OMeta2SqueakSmalltalkRecognizer
instanceVariableNames: 'useArgTypeTable'
classVariableNames: 'ArgTypeTable TypeTable'
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2SqueakSmalltalkRecognizer class' category: 'OMeta2'!
OMeta2SqueakSmalltalkRecognizer class
instanceVariableNames: ''!
!classDefinition: #OMeta2CuisSmalltalkRecognizer category: 'OMeta2'!
OMeta2SqueakSmalltalkRecognizer subclass: #OMeta2CuisSmalltalkRecognizer
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'OMeta2'!
!classDefinition: 'OMeta2CuisSmalltalkRecognizer class' category: 'OMeta2'!
OMeta2CuisSmalltalkRecognizer class
instanceVariableNames: ''!
!OMeta2 commentStamp: '<historical>' prior: 0!
OMeta Implementation
Parser Notes:
========
If your input consists of strings you want to parse (source code, text files, etc.), you most likely want to call the class-side 'MatchAll' methods (i.e. #matchAll:with:). If your input consists of structured data (nested arrays etc) to parse, you probably want to call the class-side 'Match' methods (i.e. #match:with:).
For more unconventional parsing needs, you will likely need to override some OMeta2Base methods in your parser subclass. It is highly suggested that you do not change OMeta2Base directly as any bugs or changes in behavior will likely break OMeta2 itself. Therefore, overriding in your subclass is the recommended approach.
Todos:
====
* implement OMeta -> Squeak translator
* implement Squeak parser
* implement OMeta/Squeak "compiler", make it OMeta2's compilerClass
* rewrite #empty and #firstAndRest in OMeta syntax
* consider implementing position-related functionality (see http://www.tinlizzie.org/ometa-js/ometa-base.js)
* consider the optimization suggestions in the comments of OMeta2Lib's methods!
!OMeta2Flattener commentStamp: '<historical>' prior: 0!
OMeta2Flattener is phase 4 of OMeta code compilation. I take the rendered code tree produced by phase 3 and flatten it into a single string of source code that will be passed to the Smalltalk compiler or parser.
OMeta implementation!
!OMeta2NullOptimizer commentStamp: '<historical>' prior: 0!
I am an identity transform used to build OMeta AST optimizers. Typically each subclass represents an optimization pass performing a specific type of optimization returning the transformed AST. Optimizers, such as OMeta2AndOrOptimizer, are called during phase 2.
OMeta implementation!
!OMeta2AndOrOptimizer commentStamp: '<historical>' prior: 0!
Syntax tree grammar used by OMeta2Optimizer in stage 2 of compilation.
OMeta implementation!
!OMeta2FinalizeOptimizer commentStamp: '<historical>' prior: 0!
Wrap #And nodes with a #TrueIfTrue node when necessary
OMeta implementation!
!OMeta2Optimizer commentStamp: '<historical>' prior: 0!
OMeta2Optimizer is phase 2 of OMeta code compilation. I take the parse tree from phase 1 and apply a syntax tree grammar (OMeta2AndOrOptimizer) which provides transformations to produce a simplified syntax tree.
OMeta implementation!
!OMeta2RuleParser commentStamp: '<historical>' prior: 0!
OMeta2RuleParser is phase 1 of OMeta code compilation. I take arbitrary source code and produce an syntax tree of raw operations in a Lispy nested list containing elements in the form #(arg1. arg2...) where:
arg1 is the OMeta primitive to perform (#SemAct, #And, #App, #ConsBy, #Form, #IdxConsBy, #Opt, #Or, #Lookahead, #Many, #Many1, #Not, #SemPred, #Rule, #Set, #SuperApp) each corresponding to an OMeta primitive operation:
- #And and #Or correspond to Smalltalk #and: and #or:
- #SemAct and #Rule indicate a method call defined in arg2
- #Set indicates a collection (implemented as an array)
- #App, #ConsBy, #Form, #IdxConsBy, #Opt, #Lookahead, #Many, #Many1, #Not, #SemPred and #SuperApp have corresponding methods in OMeta2Base
arg2 is either an operand for the atomic operation (an OMeta or application rule name or semantic predicate/action code snippet) or a list of child operations in the case of #And and #Or operations.
arg3, if it exists, is either an operand for the atomic operation (often an operand for the rule supplied in arg2 such as a string literal) or a list of child operations.
And so on... arg4 and greater occur in more complex #And and #Or lists or for rules/method calls requiring additional parameters passed.
The syntax tree produced represents the worst case application logic as no optmization is performed yet. Stages 2 and 3 require this simple form to perform their transformations. (i.e. any changes/extensions here will have significant implications downstream)
OMeta implementation!
!OMeta2RuleTranslator commentStamp: '<historical>' prior: 0!
OMeta2RuleTranslator is phase 3 of OMeta code compilation. I take the parse tree from phase 1 or 2 and translate it to a list-based tree containing rendered Smalltalk code snippets.
OMeta implementation!
!OMeta2SqueakSmalltalkRecognizer commentStamp: '<historical>' prior: 0!
I am a null Smalltalk parser for the Squeak dialect. While I don't actually *do* anything with parsed Smalltalk code, I am useful as a minimal test to determine if any given text is valid Smalltalk code. I also serve as an example of parsing a programming language with OMeta.
OMeta implementation!
!OMeta2CuisSmalltalkRecognizer commentStamp: '<historical>' prior: 0!
Extensions specific to Cuis!
!OMeta2 class methodsFor: 'methods' stamp: 'pb 5/22/2020 03:09:24'!
methodNodeFor: sourceCode noPattern: aBoolean
(self ometa2RuleParserClass isOMeta2Rule: sourceCode) ifTrue: [ ^ nil ].
^ super
methodNodeFor: sourceCode
noPattern: aBoolean.! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:02'!
name =
spaces nsName! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:42:37'!
comment =
$" <(~$" anything)*> $" $.?! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/2/2020 14:26:03'!
empty
^ true.! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/16/2020 22:07:13'!
end =
// Are we at the end of the input stream?
~anything! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/22/2015 18:25'!
exactly :wanted =
anything:got ?[wanted = got] -> [wanted]! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/2/2020 14:26:16'!
firstAndRest
"A meta rule for 'first rest+' where the rule names are substituted with the values of first and rest. See listOf for another example of a meta rule. FIXME - rewrite in OMeta"
| first rest |
first := self privateOMetaApply: #anything.
rest := self privateOMetaApply: #anything.
^ self pvtOMetaGenericMany: [self privateOMetaApply: rest] into: (OrderedCollection with: (self privateOMetaApply: first))! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/16/2020 22:07:23'!
fromTo :x :y =
// Match the sequence from x to y. Example to match a comment: fromTo("/*", "*/"):commentText
seq(x) (~seq(y) anything)*:enclosed seq(y) -> [enclosed]! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/16/2020 22:07:27'!
fromToOrEnd :x :y =
// Match the sequence from x to y. Example to match a comment: fromTo("/*", "*/"):commentText
seq(x) (~seq(y) anything)*:enclosed (seq(y) | end) -> [enclosed]! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/16/2020 22:07:32'!
listOf :rule :delim =
// Match :rule one or more times delimited by :delim returning the results as a collection
applyRule(rule):x (token(delim) applyRule(rule))*:xs -> [xs addFirst: x; yourself]
| empty -> [#()]! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/5/2020 07:33:00'!
notLast :rule =
applyRule(rule):ans &applyRule(rule) -> [ans]! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/22/2015 18:31'!
number =
anything:x ?[x isNumber] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/16/2020 22:07:40'!
range :a :b =
// Is the next object within the range of a .. b? Example usage: checking if an input character falls within the given range.
anything:x ?[a <= x & (x <= b)] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/22/2015 18:34'!
string =
anything:x ?[x isString] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar' stamp: 'pb 5/22/2015 18:34'!
symbol =
anything:x ?[x isSymbol] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar - character' stamp: 'pb 5/16/2020 22:00:46'!
char =
anything:x ?[x class == Character] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar - character' stamp: 'pb 5/16/2020 22:04:53'!
digit =
// Inlined for performance
// char:x ?[x isDigit] -> [x]
anything:x ?[x class == Character and: [x isDigit]] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar - character' stamp: 'pb 5/16/2020 22:04:48'!
letter =
// Inlined for performance
// char:x ?[x isLetter] -> [x]
anything:x ?[x class == Character and: [x isLetter]] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar - character' stamp: 'pb 5/16/2020 22:04:43'!
letterOrDigit =
// Inlined for performance
// char:x ?[x isAlphaNumeric] -> [x]
anything:x ?[x class == Character and: [x isAlphaNumeric]] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar - character' stamp: 'pb 5/16/2020 22:11:31'!
lower =
// Inlined for performance
// char:x ?[x isLowercase] -> [x]
anything:x ?[x class == Character and: [x isLowercase]] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar - character' stamp: 'pb 5/16/2020 22:03:45'!
space =
// Inlined for performance
// char:x ?[x numericValue <= 32] -> [x]
anything:x ?[x class == Character and: [x numericValue <= 32]] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar - character' stamp: 'pb 5/22/2015 18:33'!
spaces =
space*! !
!OMeta2 methodsFor: 'ometa grammar - character' stamp: 'pb 5/16/2020 22:04:11'!
upper =
// Inlined for performance
// char:x ?[x isUppercase] -> [x]
anything:x ?[x class == Character and: [x isUppercase]] -> [x]! !
!OMeta2 methodsFor: 'ometa grammar - text' stamp: 'pb 5/16/2020 22:07:54'!
fromToEndOfLine :from =
/* Match the sequence :from to either the end of line or the input stream. Example to match a comment: fromToEndOfLine("//"):commentText
FIXME - if 2nd param could be made optional and default to lf, this rule would be usable by any type of object */
fromToOrEnd(from, `Character newLineCharacter asString`)! !
!OMeta2 methodsFor: 'ometa grammar - text' stamp: 'pb 5/22/2015 18:34'!
token :t =
spaces seq(t)! !
!OMeta2 methodsFor: 'private' stamp: 'pb 4/22/2020 03:39:18'!
pvtParseLog: aString
Transcript
show: aString;
finishEntry.! !
!OMeta2 class methodsFor: 'matching' stamp: 'pb 4/22/2020 03:52:13'!
matchAll: aSequenceableCollection with: aRule ifFail: failBlock
^ [
self
matchAll: aSequenceableCollection
with: aRule ]
on: OM2Fail
do: [ :ex |
ex return: failBlock value ].! !
!OMeta2Flattener methodsFor: 'grammar root' stamp: 'pb 5/22/2015 18:40'!
flatten :tree =
iFlatten(tree) -> [ws contents]! !
!OMeta2Flattener methodsFor: 'private' stamp: 'pb 5/22/2015 18:41'!
iFlatten =
string:s [ws nextPutAll: s]
| {iFlatten*}! !
!OMeta2Flattener methodsFor: 'initialization' stamp: 'aw 2/18/2009 15:05'!
initialize
super initialize.
ws := (String new: 64) writeStream! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:44'!
and = trans*:xs -> [xs addFirst: #And; yourself]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:44'!
consby = trans:x -> [{#ConsBy. x}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:44'!
form = trans:x -> [{#Form. x}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:01'!
helped = ?[didSomething]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:44'!
idxconsby = trans:x -> [{#IdxConsBy. x}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:44'!
lookahead = trans:x -> [{#Lookahead. x}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:44'!
many = trans:x -> [{#Many. x}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:44'!
many1 = trans:x -> [{#Many1. x}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:45'!
not = trans:x -> [{#Not. x}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:45'!
opt = trans:x -> [{#Opt. x}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:45'!
or = trans*:xs -> [xs addFirst: #Or; yourself]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:45'!
rule = _:name _:temps trans:body -> [{#Rule. name. temps. body}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:45'!
set = _:name trans:val -> [{#Set. name. val}]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:00'!
setHelped = [didSomething := true]! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'pb 5/5/2020 07:32:54'!
trans =
{:t [t asLowercase asSymbol]:t ?[self class canUnderstand: t] applyRule(t):ans} -> [ans]
| _! !
!OMeta2NullOptimizer methodsFor: 'private' stamp: 'pb 4/27/2020 20:59:38'!
trueiftrue =
trans:x -> [{#TrueIfTrue. x}]! !
!OMeta2NullOptimizer methodsFor: 'initialization' stamp: 'aw 5/20/2009 12:30'!
initialize
super initialize.
didSomething := false! !
!OMeta2NullOptimizer methodsFor: 'grammar root' stamp: 'aw 5/20/2009 12:02'!
optimize = trans:x helped -> [x]! !
!OMeta2AndOrOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:46'!
and =
trans:x end setHelped -> [x]
| transInside(#And):xs -> [xs addFirst: #And; yourself]! !
!OMeta2AndOrOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:46'!
or =
trans:x end setHelped -> [x]
| transInside(#Or):xs -> [xs addFirst: #Or; yourself]! !
!OMeta2AndOrOptimizer methodsFor: 'private' stamp: 'aw 5/20/2009 12:36'!
transInside :t =
{exactly(t) transInside(t):xs} transInside(t):ys setHelped -> [xs, ys]
| trans:x transInside(t):xs -> [xs addFirst: x; yourself]
| empty -> [OrderedCollection new]! !
!OMeta2FinalizeOptimizer methodsFor: 'private' stamp: 'pb 4/30/2020 23:08:55'!
rule =
_:name _:temps trans:body
-> [OrderedCollection new
add: #Rule;
add: name;
add: temps;
add: ((#(#Or #TrueIfTrue) includes: body first)
ifTrue: [body]
ifFalse: [self setHelped. {#TrueIfTrue. body}]);
yourself]! !
!OMeta2FinalizeOptimizer methodsFor: 'private' stamp: 'pb 4/28/2020 01:12:29'!
set =
_:name trans:val
-> [{#Set.
name.
val first = #And
ifTrue: [
self setHelped.
{#TrueIfTrue. val}]
ifFalse: [val]}]! !
!OMeta2Optimizer methodsFor: 'grammar root' stamp: 'pb 4/27/2020 20:42:56'!
finalOptimizeRule = _:ans (OMeta2FinalizeOptimizer.optimize(ans):ans)* -> [ans]! !
!OMeta2Optimizer methodsFor: 'grammar root' stamp: 'pb 6/7/2015 17:47'!
optimizeRule = _:ans (OMeta2AndOrOptimizer.optimize(ans):ans)* -> [ans]! !
!OMeta2Optimizer class methodsFor: 'optimize' stamp: 'pb 4/27/2020 22:31:33'!
optimizeOMetaAst: ast
^ self
match:
(self
match: ast
with: #optimizeRule)
with: #finalOptimizeRule.! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 4/25/2020 01:00'!
application =
"^" name:rule args:as -> [{#SuperApp. rule}, as]
| name:grm $. nsName:rule args:as -> [{#App. #pvtOMetaForeignParser. grm. ('#', rule) asSymbol}, as]
| name:rule args:as -> [{#App. rule}, as]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/2/2020 01:29:30'!
args =
/* FIXME
$( (
listOf(#squeakArgExprOrWrappedInnerBlock, ',')
| listOf(#squeakExpression, '.')
):ans ")" -> [ans]
*/
$( listOf(#squeakArgExprOrWrappedInnerBlock, ','):ans ")" -> [ans] // new syntax
| $( listOf(#squeakExpression, '.'):ans ")" -> ["Transcript show: 'legacy listOf syntax with args: ', ans asString; finishEntry." ans] // legacy syntax
| ~$( -> [#()]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:00'!
characterLiteral =
spaces $$ char:c -> [{#App. #exactly. c storeString}]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:01'!
characters =
"``" (~($' $') char)*:xs $' $' -> [{#App. #seq. (String withAll: xs) storeString}]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/2/2020 00:12:28'!
expr =
// Wrap the sub-expressions with an 'or'. If it's not needed, we'll optimize it away later.
listOf(#expr4, '|'):xs -> [(OrderedCollection with: #Or) addAll: xs; yourself]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/2/2020 14:34:33'!
expr1 =
// Handle any syntactic sugar (brackets etc) and the rest of the expression
(keyword('true') | keyword('false') | keyword('nil')):lit -> [{#App. #exactly. lit}]
| application
| semanticAction
| semanticPredicate
| characters
| tokenSugar
| stringLiteral
| symbolLiteral
| numberLiteral
| characterLiteral
| "{" expr:e "}" -> [{#Form. e}]
| "<" expr:e ">" -> [{#ConsBy. e}]
| "@<" expr:e ">" -> [{#IdxConsBy. e}]
| "(" expr:e ")" -> [e]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 6/11/2015 18:49'!
expr2 =
// Handle lookahead
"~" expr2:x -> [{#Not. x}]
| "&" expr2:x -> [{#Lookahead. x}]
| expr1! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 6/11/2015 18:49'!
expr3 =
// Handle arity and variable assignment
expr2:x optIter(x):x
( $: nsName:n [temps add: n] -> [{#Set. n. x}]
| empty -> [x]
)
| ":" nsName:n [temps add: n] -> [{#Set. n. {#App. #anything}}]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 6/11/2015 18:57'!
expr4 =
// Wrap related rule sub-expressions with an 'and'. If it's not needed, we'll optimize it away later
expr3*:xs -> [(OrderedCollection with: #And) addAll: xs; yourself]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:02'!
keyword :xs =
token(xs) ~letterOrDigit -> [xs]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:03'!
nameFirst =
letter! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:03'!
nameRest =
nameFirst | digit! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/2/2020 00:31:49'!
nsName =
firstAndRest(#nameFirst, #nameRest):xs -> [(String withAll: xs) asSymbol]
| $_ -> [#anything]
! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:04'!
numberLiteral =
spaces ($- spaces -> [-1] | empty -> [1]):sign digit+:ds
-> [{#App. #exactly. (sign * (String withAll: ds) asNumber) storeString}]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:04'!
optIter :x =
"*" -> [{#Many. x}]
| "+" -> [{#Many1. x}]
| "?" ~$[ -> [{#Opt. x}]
| empty -> [x]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:05'!
rulePart :ruleName =
name:n ?[n = ruleName] expr4:b1
( "=" expr:b2 -> [{#And. b1. b2}]
| empty -> [b1]
)! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 6/16/2020 10:58:09'!
ruleProduction =
"->"! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 6/16/2020 10:58:16'!
semanticAction =
ruleProduction? "[" (squeakExpression:expr | squeakInnerBlock:blk) $]
-> [expr ifNil: [Array with: #SemBlkAct with: blk] ifNotNil: [Array with: #SemAct with: expr]]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 4/29/2020 00:31:55'!
semanticPredicate =
"?[" (squeakExpression:expr | squeakInnerBlock:blk) $]
-> [expr ifNil: [Array with: #SemBlkPred with: blk] ifNotNil: [Array with: #SemPred with: expr]]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/16/2020 21:36:59'!
space =
^space | fromTo('/*', '*/') | fromToEndOfLine('//')! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/2/2020 19:27:58'!
squeakArgExprOrWrappedInnerBlock =
[self pvtOMetaGetExternalSmalltalkRecognizerClass]:recog
recog.squeakArgExprOrWrappedInnerBlock:ans spaces -> [ans]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/2/2020 19:28:13'!
squeakExpression =
[self pvtOMetaGetExternalSmalltalkRecognizerClass]:recog
recog.squeakExpr:ans spaces -> [ans]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/2/2020 19:28:24'!
squeakInnerBlock =
[self pvtOMetaGetExternalSmalltalkRecognizerClass]:recog
recog.squeakInnerBlock:ans spaces -> [ans]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:06'!
stringLiteral =
"'" ($' $' -> [$'] | ~$' char)*:xs $' -> [{#App. #exactly. (String withAll: xs) storeString}]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:06'!
symbolLiteral =
// TODO: make this accept, e.g., #foo:bar:
"#" nsName:s -> [{#App. #exactly. s storeString}]! !
!OMeta2RuleParser methodsFor: 'private' stamp: 'pb 5/22/2015 19:07'!
tokenSugar =
token('"') (~$" char)*:xs $" -> [{#App. #token. (String withAll: xs) storeString}]! !
!OMeta2RuleParser methodsFor: 'initialization' stamp: 'aw 2/18/2009 15:04'!
initialize
super initialize.
temps := IdentitySet new! !
!OMeta2RuleParser methodsFor: 'grammar root' stamp: 'pb 4/27/2020 01:45:58'!
ometaRule =
&(^space* nsName):n rulePart(n):x ("," rulePart(n))*:xs spaces end
-> [{#Rule. n. temps asSortedCollection. (OrderedCollection with: #Or with: x) addAll: xs; yourself}]! !
!OMeta2RuleParser methodsFor: 'grammar root' stamp: 'pb 5/2/2020 14:36:30'!
rule =
squeakRule
| ometaRule! !
!OMeta2RuleParser methodsFor: 'grammar root' stamp: 'pb 4/27/2020 01:46:09'!
squeakRule =
// This isn't OMeta code so we'll handoff to Squeak when the time comes
~(^space* nsName expr4 "=") <char*>:squeakCode -> [{#Squeak. squeakCode}]! !
!OMeta2RuleParser class methodsFor: 'testing' stamp: 'pb 5/17/2020 03:14:16'!
isOMeta2Rule: aString
^ [
(self
matchAll: aString
with: #rule) first ~= #Squeak ]
on: OM2Fail
do: [ :ex |
ex return: false ].! !
!OMeta2RuleTranslator methodsFor: 'private-ometa2preload' stamp: 'pb 7/3/2021 14:00'!
pvtGenerateProgn: bodyStr comment: optionalCommentStr
| commentStr |
commentStr := optionalCommentStr
ifNil: ['']
ifNotNil: ['"', optionalCommentStr, '"'].
^ 'true ifTrue: [', commentStr, bodyStr, ']'! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/28/2020 23:53:08'!
and =
trans*:xs -> [{self delim: xs with: '. '}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/30/2020 23:05:41'!
app =
[self flag: #privateOMetaApply:;
flag: #privateOMetaApply:withArgs:]
symbol:rule
( anything+:args
-> [args size = 1
ifTrue: [{'(self privateOMetaApply: '. rule storeString. ' withArgument: '. args first. ')'}]
ifFalse: [{'(self privateOMetaApply: '. rule storeString. ' withArgs: {'. (self delim: args with: '. '). '})'}]
]
| [{'(self privateOMetaApply: '. rule storeString. ')'}]
)! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/25/2020 01:00'!
consby =
[self flag: #pvtOMetaConsumedBy:]
trans:x -> [{'(self pvtOMetaConsumedBy: ['. x. '])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'aw 2/18/2009 14:43'!
delim: aSequenceableCollection with: anObject
| first ans |
first := true.
ans := OrderedCollection new.
aSequenceableCollection do: [:x |
first ifTrue: [first := false] ifFalse: [ans add: anObject].
ans add: x
].
^ ans! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/15/2020 03:25:24'!
form =
[self flag: #pvtOMetaForm:]
trans:x -> [{'(self pvtOMetaForm: ['. x. '])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/15/2020 03:23:26'!
idxconsby =
[self flag: #pvtOMetaIndexConsumedBy:]
trans:x -> [{'(self pvtOMetaIndexConsumedBy: ['. x. '])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/15/2020 03:24:42'!
lookahead =
[self flag: #pvtOMetaLookahead:]
trans:x -> [{'(self pvtOMetaLookahead: ['. x. '])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/15/2020 03:30:24'!
many =
[self flag: #pvtOMetaMany:]
trans:x -> [{'(self pvtOMetaMany: ['. x. '])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/15/2020 03:30:33'!
many1 =
[self flag: #pvtOMetaMany1:]
trans:x -> [{'(self pvtOMetaMany1: ['. x. '])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/15/2020 03:30:45'!
not =
[self flag: #pvtOMetaNot:]
trans:x -> [{'(self pvtOMetaNot: ['. x. '])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/15/2020 03:30:54'!
opt =
[self flag: #pvtOMetaOpt:]
trans:x -> [{'(self pvtOMetaOpt: ['. x. '])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/15/2020 03:31:10'!
or =
[self flag: #pvtOMetaOr:]
(trans:x -> [{'['. x. ']'}])*:xs -> [{'(self pvtOMetaOr: {'. self delim: xs with: '. '. '})'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 7/6/2021 00:09:29'!
rule =
symbol:name
anything:temps [temps select: [:t | (grammarClass instVarNames includes: t) not]]:temps
trans:body
-> [|oc tift|
tift := false.
oc := OrderedCollection new.
oc
add: name;
add: self class constOMetaGeneratorString;
add: ' |';
add: (self delim: temps asSortedCollection with: ' ');
add: ' | ^ '.
tift
ifTrue: [oc add: (self pvtGenerateProgn: body comment: 'from #rule')]
ifFalse: [oc add: body].
oc]
! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 5/22/2015 19:07'!
semact = string:x -> [{'('. x. ')'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 7/6/2021 00:10:10'!
semblkact = string:x -> [{self pvtGenerateProgn: x comment: nil}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 7/3/2021 14:00:00'!
semblkpred =
[self flag: #pvtOMetaPred:]
string:x -> [{'(['. x. '] value or: [self pvtOMetaSignalParseFailure])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 7/3/2021 14:00:00'!
sempred =
[self flag: #pvtOMetaPred:]
string:x -> [{'(('. x. ') or: [self pvtOMetaSignalParseFailure])'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 5/22/2015 19:12'!
set =
symbol:n trans:v -> [{'('. n asString. ' := '. v. ')'}]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 5/22/2015 19:12'!
squeak = string! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/30/2020 23:05:50'!
superapp =
[self flag: #pvtOMetaSuper:apply:withArgs:]
symbol:rule anything*:args
-> [args size = 1
ifTrue: [{'(self pvtOMetaSuper: '. grammarClass superclass name. ' apply: '. rule storeString. ' withArgument: '. args first. ')'}]
ifFalse: [{'(self pvtOMetaSuper: '. grammarClass superclass name. ' apply: '. rule storeString. ' withArgs: {'. (self delim: args with: '. '). '})'}]]
! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 5/5/2020 07:32:46'!
trans = {symbol:type applyRule(type asLowercase asSymbol):ans} -> [ans]! !
!OMeta2RuleTranslator methodsFor: 'private' stamp: 'pb 4/28/2020 23:52:51'!
trueiftrue =
[self flag: #ifTrue:]
trans*:xs -> [{'(true ifTrue: ['. xs. '])'}]
! !
!OMeta2RuleTranslator methodsFor: 'grammar root' stamp: 'pb 5/22/2015 19:13'!
translate :grammarClass = trans! !
!OMeta2RuleTranslator class methodsFor: 'constants' stamp: 'pb 5/23/2018 03:01:33'!
constOMetaGeneratorString
^ '"Generated by OMeta... edit the rule (i.e. show...->source), not this Smalltalk code"'.! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'aw 2/20/2009 00:43'!
arrayConstr =
"{" expr ("." expr)* ("." | empty) "}"
| "{" "}"! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:56:31'!
arrayLit =
"#" "(" (literal | arrayLit | arraySubLit | commentAndSpaces tsArraySymbol)* ")"! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:56:16'!
arraySubLit =
// A subarray in an array literal doesn't require the leading #
"(" (literal | arrayLit | arraySubLit | commentAndSpaces tsArraySymbol)* ")"! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:56:50'!
binary =
commentAndSpaces tsBinary! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'aw 2/20/2009 00:58'!
binaryExpr =
binaryExpr binaryMsg
| unaryExpr! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'aw 2/20/2009 00:58'!
binaryMsg =
binary unaryExpr! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:58:07'!
block =
"["
( (":" identifier)+ "|"
| empty
)
( "|" identifier* "|"
| empty
)
commentAndSpaces
( expr ("." expr)* ("." "^" expr | empty)
| "^" expr
| empty
)
commentAndSpaces
( "."
| empty
)
commentAndSpaces
"]"! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'aw 2/20/2009 00:58'!
cascade =
identifier
| binaryMsg
| keywordMsg! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:42:23'!
commentAndSpaces =
((spaces comment)+
| spaces comment?)
spaces
! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'aw 2/20/2009 00:59'!
expr =
identifier (":=" | "_") expr
| msgExpr! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:58:21'!
identifier =
commentAndSpaces tsIdentifier ~$:! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:58:29'!
keyword =
commentAndSpaces tsKeyword! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'aw 2/20/2009 01:19'!
keywordExpr =
binaryExpr keywordMsg! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'aw 2/20/2009 01:19'!
keywordMsg =
keywordMsg keywordMsgPart
| keywordMsgPart! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'aw 2/20/2009 01:20'!
keywordMsgPart =
keyword binaryExpr! !
!OMeta2SqueakSmalltalkRecognizer methodsFor: 'private' stamp: 'pb 5/2/2020 19:58:40'!
literal =
commentAndSpaces ( tsNumber
| tsCharacter
| tsString
| tsSymbol