-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathProtBufParse.pas
1658 lines (1576 loc) · 56.5 KB
/
ProtBufParse.pas
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
{
DelphiProtocolBuffer: ProtBufParse.pas
Copyright 2014-2016 Stijn Sanders
Made available under terms described in file "LICENSE"
https://github.com/stijnsanders/DelphiProtocolBuffer
}
unit ProtBufParse;
interface
uses SysUtils, Classes;
type
TProtocolBufferParserValue=(
pbpvUnitName,
pbpvTypePrefix,
pbpvImportPath,
pbpvAddPrefix,
//add new here above
pbpv_Unknown);
TProtocolBufferParserFlag=(
pbpfPrependNameParent,
pbpfPrependEnumName,
pbpfPrependEnumFields,
pbpfDebugData,
//add new here above
pbpf_Unknown);
TProtocolBufferParserValues=array[TProtocolBufferParserValue] of string;
const
ProtocolBufferParserValueName:TProtocolBufferParserValues=(
'u<UnitName>',
'p<TypePrefix>',
'i<ImportPath>',
'a<AddPrefix>',
//add new here above
'');
ProtocolBufferParserValueDefaults:TProtocolBufferParserValues=(
'',
'T',
'',
'Add_',
//add new here above
'');
ProtocolBufferParserFlagName:array[TProtocolBufferParserFlag] of string=(
'Pn: prepend with parent name',
'En: prepend enumeration name with parent',
'Ef: prepend enumeration field with name',
'Dd: include debug data',
//add new here above
'');
type
EProtocolBufferParseError=class(Exception);
TProtocolBufferParser=class;//forward
TProtocolBufferParserFlags=set of TProtocolBufferParserFlag;
TProtBufMessageDescriptor=class(TObject)
private
FName,FPasName:string;
FMembers:array of record
Key,Quant,TypeNr:integer;
Name,TypeName,DefaultValue,PascalType,OneOfName:string;
end;
FMembersIndex,FMembersCount,FHighKey:integer;
FWireFlags:cardinal;
protected
function GenerateInterface(p:TProtocolBufferParser;
Flags:TProtocolBufferParserFlags):string; virtual;
function GenerateImplementation(p:TProtocolBufferParser;
Flags:TProtocolBufferParserFlags):string; virtual;
public
Parent:TProtBufMessageDescriptor;
NextKey,ExtensionsLo,ExtensionsHi:integer;
Forwarded,Extending:boolean;
OneOfName:string;
constructor Create(const Name:string);
procedure AddMember(Quant,TypeNr:integer;
const Name,TypeName,DefaultValue:string);
function MemberByKey(Key: integer; var FieldName, FieldType: string;
var Quant, TypeNr: integer): boolean;
property Name:string read FName;
property PasName:string read FPasName;
end;
TProtBufEnumDescriptor=class(TProtBufMessageDescriptor)
protected
function GenerateInterface(p:TProtocolBufferParser;
Flags:TProtocolBufferParserFlags):string; override;
function GenerateImplementation(p:TProtocolBufferParser;
Flags:TProtocolBufferParserFlags):string; override;
end;
TProtocolBufferParser=class(TObject)
private
FPackageName,FUnitName:string;
FMsgDesc:array of TProtBufMessageDescriptor;
FMsgDescIndex,FMsgDescSize:integer;
procedure AddMsgDesc(x:TProtBufMessageDescriptor);
procedure InsertMsgDesc(x,before:TProtBufMessageDescriptor);
public
Values:TProtocolBufferParserValues;
Options:TStringList;
constructor Create;
destructor Destroy; override;
procedure Parse(const FilePath:string);
function MsgDescByName(OptParent: TProtBufMessageDescriptor;
const Name:string):TProtBufMessageDescriptor;
procedure ListDescriptors(const List:TStrings);
function GenerateUnit(Flags:TProtocolBufferParserFlags):string;
property DescriptorCount: integer read FMsgDescIndex;
end;
const
Quant_Required=1;
Quant_Optional=2;
Quant_Repeated=3;
Quant_Repeated_Packed=4;
//varint
TypeNr_int32=$10;
TypeNr_int64=$11;
TypeNr_uint32=$12;
TypeNr_uint64=$13;
TypeNr_sint32=$14;
TypeNr_sint64=$15;
TypeNr_bool=$16;
TypeNr_enum=$17;
//length delimited
TypeNr_string=$20;
TypeNr_bytes=$21;
TypeNr_msg=$22;
//fixed
TypeNr_fixed32=$30;
TypeNr_fixed64=$40;
TypeNr_sfixed32=$31;
TypeNr_sfixed64=$41;
TypeNr_float=$32;
TypeNr_double=$42;
//depends:enum/message/map
TypeNr__typeByName=$01;
TypeNr__map=$02;
WireFlag_VarInt = $002;// shl 1
WireFlag_Len = $004;// shl 2
WireFlag_32 = $008;// shl 3
WireFlag_64 = $010;// shl 4
WireFlag_Msg = $040;// shl 6
WireFlag_Default = $080;// shl 7
WireFlag_RepeatBase = $100;// shl 8
kFirstReservedNumber = 19000;
kLastReservedNumber = 19999;
implementation
uses SelfVersion;
{ TProtocolBufferParser }
constructor TProtocolBufferParser.Create;
begin
inherited Create;
FPackageName:='';
FUnitName:='';
FMsgDescIndex:=0;
FMsgDescSize:=0;
Values:=ProtocolBufferParserValueDefaults;
Options:=TStringList.Create;
end;
destructor TProtocolBufferParser.Destroy;
begin
while FMsgDescIndex<>0 do
begin
dec(FMsgDescIndex);
FreeAndNil(FMsgDesc[FMsgDescIndex]);
end;
Options.Free;
inherited;
end;
procedure TProtocolBufferParser.AddMsgDesc(x: TProtBufMessageDescriptor);
begin
//TODO: check unique name, (auto-enable pbpfPrependNameParent?)
if FMsgDescIndex=FMsgDescSize then
begin
inc(FMsgDescSize,32);//Grow
SetLength(FMsgDesc,FMsgDescSize);
end;
FMsgDesc[FMsgDescIndex]:=x;
inc(FMsgDescIndex);
end;
procedure TProtocolBufferParser.InsertMsgDesc(x,
before: TProtBufMessageDescriptor);
var
i,j:integer;
begin
if FMsgDescIndex=FMsgDescSize then
begin
inc(FMsgDescSize,32);//Grow
SetLength(FMsgDesc,FMsgDescSize);
end;
i:=FMsgDescIndex;
if i<>0 then
begin
dec(i);
while (i<>0) and (FMsgDesc[i]<>before) do dec(i);
end;
j:=FMsgDescIndex;
while (j<>i) do
begin
FMsgDesc[j]:=FMsgDesc[j-1];
dec(j);
end;
FMsgDesc[i]:=x;
inc(FMsgDescIndex);
end;
function pd(const x:string):string;
begin
if x='' then Result:='' else Result:=IncludeTrailingPathDelimiter(x);
end;
procedure TProtocolBufferParser.Parse(const FilePath: string);
var
Line,CodeL,CodeI,CodeJ,CodeI_EOL:integer;
Code,Keyword:string;
procedure LoadCode;
var
f:TFileStream;
begin
f:=TFileStream.Create(FilePath,fmOpenRead or fmShareDenyWrite);
try
//TODO: UTF-8? UTF-16?
CodeL:=f.Size;
SetLength(Code,CodeL);
if f.Read(Code[1],CodeL)<>CodeL then RaiseLastOSError;
finally
f.Free;
end;
end;
procedure SkipWhiteSpace;
var
b:boolean;
begin
b:=true;
while b or ((CodeI<=CodeL) and (Code[CodeI]<=' ')) do
begin
b:=false;
while (CodeI<=CodeL) and (Code[CodeI]<=' ') do
begin
if (Code[CodeI]=#10) then
begin
inc(Line);
CodeI_EOL:=CodeI;
end
else
if (Code[CodeI]=#13) then
begin
inc(Line);
if (CodeI<CodeL) and (Code[CodeI+1]=#10) then inc(CodeI);
CodeI_EOL:=CodeI;
end;
inc(CodeI);
end;
//TODO: support /* */ ?
if (CodeI<CodeL) and (Code[CodeI]='/') and (Code[CodeI+1]='/') then
begin
//skip comment to EOL
while (CodeI<=CodeL)
and (Code[CodeI]<>#13) and (Code[CodeI]<>#10) do
inc(CodeI);
end;
end;
end;
procedure R(const Msg:string);
function ReturnAddr: pointer;
asm
mov eax,[ebp+4]
end;
begin
raise EProtocolBufferParseError.CreateFmt(
'%s, line %d pos %d',[Msg,Line,CodeI-CodeI_EOL]) at ReturnAddr;
end;
procedure Expect(x:char);
begin
SkipWhiteSpace;
if (CodeI<=CodeL) and (Code[CodeI]=x) then
inc(CodeI)
else
R('Expected "'+x+'"');
end;
function IsNext(x:char):boolean;
begin
SkipWhiteSpace;
if (CodeI<=CodeL) and (Code[CodeI]=x) then
begin
inc(CodeI);
Result:=true;
end
else
Result:=false;
end;
function NextKeyword:boolean;
begin
SkipWhiteSpace;
CodeJ:=CodeI;
//while (CodeJ<=CodeL) and (Code[CodeJ]>' ') do inc(CodeJ);
while (CodeJ<=CodeL)
and (Code[CodeJ] in ['A'..'Z','a'..'z','0'..'9','_','.']) do
inc(CodeJ);
Keyword:=Copy(Code,CodeI,CodeJ-CodeI);
Result:=CodeJ>CodeI;
CodeI:=CodeJ;
end;
function NextInt:integer;
begin
SkipWhiteSpace;
//TODO: support '-'?
Result:=0;
if (CodeI<CodeL) and (Code[CodeI]='0') and (Code[CodeI+1]='x') then
begin
inc(CodeI,2);
while (CodeI<=CodeL) and (Code[CodeI] in ['0'..'9','A'..'F','a'..'f']) do
begin
Result:=(Result shl 4) or ((byte(Code[CodeI]) and $F)+
9*((byte(Code[CodeI]) shr 6) and 1));
inc(CodeI);
end;
end
else
begin
while (CodeI<=CodeL) and (Code[CodeI] in ['0'..'9']) do
begin
Result:=Result*10+(byte(Code[CodeI]) and $F);
inc(CodeI);
end;
end;
end;
function NextStr:string;
begin
Expect('"');
Result:='';
while (CodeI<=CodeL) and (Code[CodeI]<>'"') do
begin
if (Code[CodeI]='\') and (CodeI<CodeL) then
begin
inc(CodeI);
Result:=Result+Code[CodeI];
end
else
Result:=Result+Code[CodeI];//TODO: more Copy's!
inc(CodeI);
end;
//Expect('"');
if (CodeI<=CodeL) and (Code[CodeI]='"') then
inc(CodeI)
else
R('Expected end of string quotes');
end;
var
FieldName,TypeName,DefaultValue,OptionName:string;
Quant,TypeNr:integer;
First:boolean;
Msg,Msg1,MsgEnum:TProtBufMessageDescriptor;
begin
FUnitName:=ChangeFileExt(ExtractFileName(FilePath),'');
LoadCode;
CodeL:=Length(Code);
CodeI:=1;
CodeI_EOL:=0;
Line:=1;
Keyword:='';
Msg:=nil;
MsgEnum:=nil;
while (CodeI<=CodeL) do
if (Msg=nil) and (MsgEnum=nil) then
begin
if NextKeyword then
if Keyword='syntax' then
begin
Expect('=');
NextStr;//expect 'proto3'? flag 'proto2'?
Expect(';');
end
else
if Keyword='package' then
begin
if FPackageName<>'' then R('Package name was already set');
if NextKeyword then FPackageName:=Keyword else R('Package name expected');
Expect(';');
end
else
if Keyword='import' then
begin
if NextKeyword then
if Keyword='public' then
//TODO
else
if Keyword='weak' then
//TODO
else
R('Unsupported import moidifier');
Parse(pd(Values[pbpvImportPath])+StringReplace(NextStr,'/','\',[rfReplaceAll]));
Expect(';');
end
else
if Keyword='option' then
begin
if IsNext('(') then
begin
if not NextKeyword then R('Option name expected');
OptionName:=Keyword;
while IsNext('.') do
begin
if not NextKeyword then R('Option name expected');
OptionName:=OptionName+'.'+Keyword;
end;
Expect(')');
end
else
begin
if not NextKeyword then R('Option name expected');
OptionName:=Keyword;
end;
while IsNext('.') do
begin
if not NextKeyword then R('Option name expected');
OptionName:=OptionName+'.'+Keyword;
end;
Expect('=');
if not NextKeyword then Keyword:=NextStr;
Options.Add(OptionName+'='+Keyword);
Expect(';');
end
else
//TODO: 'service'
if Keyword='message' then
begin
if not NextKeyword then R('Message identifier expected');
Expect('{');
Msg:=TProtBufMessageDescriptor.Create(Keyword);
AddMsgDesc(Msg);
end
else
if Keyword='extend' then
begin
if not NextKeyword then R('Extend identifier expected');
Expect('{');
Msg:=MsgDescByName(nil,Keyword);
if Msg=nil then
raise Exception.Create('Extend descriptor "'+Keyword+'" not found');
Msg.Extending:=true;
Msg.NextKey:=Msg.ExtensionsLo;//assert<>0
end
else
if Keyword='enum' then
begin
if not NextKeyword then R('Enum identifier expected');
Expect('{');
MsgEnum:=TProtBufEnumDescriptor.Create(Keyword);
InsertMsgDesc(MsgEnum,nil);
//MsgEnum.Parent:=Msg;
MsgEnum.NextKey:=0;
end
else
R('Unexpected keyword "'+Keyword+'"')
else
if CodeI<=CodeL then
R('Unexpected non-keyword');
//else EOF
end
else
if MsgEnum=nil then //Msg<>nil
begin
if NextKeyword then
if Keyword='enum' then
begin
if not NextKeyword then R('Enum identifier expected');
Expect('{');
MsgEnum:=TProtBufEnumDescriptor.Create(Keyword);
InsertMsgDesc(MsgEnum,Msg);
MsgEnum.Parent:=Msg;
MsgEnum.NextKey:=0;
end
else
if Keyword='message' then
begin
//nested message
if not NextKeyword then R('Message identifier expected');
Expect('{');
//push message
Msg1:=Msg;
Msg:=TProtBufMessageDescriptor.Create(Keyword);
AddMsgDesc(Msg);
Msg.Parent:=Msg1;
end
else
if Keyword='extensions' then //TODO: proto2 only
begin
//extensions
if (Msg.ExtensionsLo<>0) then R('Extensions range already set');
if Msg.Extending then R('Can''t set extensions range when already extending');
Msg.ExtensionsLo:=NextInt;
if not(NextKeyword) or (Keyword<>'to') then R('Expected "to"');
if NextKeyword then
if Keyword='max' then
Msg.ExtensionsHi:=$1FFFFFFF//int23
else
if TryStrToInt(Keyword,Msg.ExtensionsHi) then
else
R('Unsupported range syntax')
else
Msg.ExtensionsHi:=NextInt;
if (Msg.ExtensionsLo=0) or (Msg.ExtensionsHi=0)
or (Msg.ExtensionsHi<Msg.ExtensionsLo) then
R('Invalid extensions range');
Expect(';');
end
else
if Keyword='option' then
begin
if IsNext('(') then
begin
if not NextKeyword then R('Option name expected');
OptionName:=Keyword;
while IsNext('.') do
begin
if not NextKeyword then R('Option name expected');
OptionName:=OptionName+'.'+Keyword;
end;
Expect(')');
end
else
begin
if not NextKeyword then R('Option name expected');
OptionName:=Keyword;
end;
while IsNext('.') do
begin
if not NextKeyword then R('Option name expected');
OptionName:=OptionName+'.'+Keyword;
end;
Expect('=');
if not NextKeyword then Keyword:=NextStr;
//TODO: Msg.OptionAdd(OptionName,Keyword);
Expect(';');
end
else
if Keyword='reserved' then
begin
//TODO:
if NextKeyword then //if (CodeI<=CodeL) and (Code[CodeI] in ['0'..'9']) then
begin
First:=true;
while First or IsNext(',') do
begin
if First then First:=false;
if NextKeyword then
begin
if Keyword='to' then NextInt//TODO:
else ;//
end;
//Keyword StrToInt?
end;
end
else
begin
NextStr;
while IsNext(',') do NextStr;
end;
Expect(';');
end
else
if Keyword='oneof' then
begin
if Msg.OneOfName<>'' then R('Nested oneof not allowed');
if not NextKeyword then R('Message identifier expected');
Expect('{');
Msg.OneOfName:=Keyword;//see below
end
else
begin
//field
if Keyword='required' then Quant:=Quant_Required else //TODO: proto2 only
if Keyword='optional' then Quant:=Quant_Optional else //TODO: proto2 only
if Keyword='repeated' then Quant:=Quant_Repeated else
Quant:=0;
if Quant<>0 then
if not NextKeyword then R('Type identifier expected');
DefaultValue:='';
if Keyword='map' then
begin
if Quant<>0 then R('Quantifiers not allowed on maps');
Expect('<');
if not NextKeyword then R('Map key type expected');
TypeNr:=0;
case Keyword[1] of
'b':
if Keyword='bool' then TypeNr:=TypeNr_bool
else
;
'f':
if Keyword='fixed32' then TypeNr:=TypeNr_fixed32
else
if Keyword='fixed64' then TypeNr:=TypeNr_fixed64
else
;
'i':
if Keyword='int32' then TypeNr:=TypeNr_int32
else
if Keyword='int64' then TypeNr:=TypeNr_int64
;
's':
if Keyword='string' then TypeNr:=TypeNr_string
else
if Keyword='sint32' then TypeNr:=TypeNr_sint32
else
if Keyword='sint64' then TypeNr:=TypeNr_sint64
else
if Keyword='sfixed32' then TypeNr:=TypeNr_sfixed32
else
if Keyword='sfixed64' then TypeNr:=TypeNr_sfixed64
else
;
'u':
if Keyword='uint32' then TypeNr:=TypeNr_uint32
else
if Keyword='uint64' then TypeNr:=TypeNr_uint64
else
;
//else ;
end;
if TypeNr=0 then R('Invalud map key value type');
Expect(',');
if not NextKeyword then R('Map value type expected');
TypeName:=IntToStr(TypeNr)+':'+Keyword;
Expect('>');
end
else
begin
TypeName:='';
TypeNr:=0;
case Keyword[1] of
'b':
if Keyword='bool' then TypeNr:=TypeNr_bool
else
if Keyword='bytes' then TypeNr:=TypeNr_bytes
else
;
'd':
if Keyword='double' then TypeNr:=TypeNr_double
else
;
'f':
if Keyword='float' then TypeNr:=TypeNr_float
else
if Keyword='fixed32' then TypeNr:=TypeNr_fixed32
else
if Keyword='fixed64' then TypeNr:=TypeNr_fixed64
else
;
'i':
if Keyword='int32' then TypeNr:=TypeNr_int32
else
if Keyword='int64' then TypeNr:=TypeNr_int64
;
's':
if Keyword='string' then TypeNr:=TypeNr_string
else
if Keyword='sint32' then TypeNr:=TypeNr_sint32
else
if Keyword='sint64' then TypeNr:=TypeNr_sint64
else
if Keyword='sfixed32' then TypeNr:=TypeNr_sfixed32
else
if Keyword='sfixed64' then TypeNr:=TypeNr_sfixed64
else
if Keyword='single' then TypeNr:=TypeNr_float
else
;
'u':
if Keyword='uint32' then TypeNr:=TypeNr_uint32
else
if Keyword='uint64' then TypeNr:=TypeNr_uint64
else
;
//else ;
end;
if TypeNr=0 then
begin
TypeName:=Keyword;
TypeNr:=TypeNr__typeByName;
//lookup here? see build output script
end;
if (TypeNr=TypeNr_bytes) and (Quant>=Quant_Repeated) then
R('"repeated bytes" not supported');
end;
if NextKeyword then FieldName:=Keyword else R('Identifier expected');
while TypeNr<>0 do
begin
SkipWhiteSpace;
if CodeI<=CodeL then
begin
inc(CodeI);
case Code[CodeI-1] of
';':
begin
if (Msg.NextKey>=kFirstReservedNumber)
and (Msg.NextKey<=kLastReservedNumber) then
R('Reserved key value '+IntToStr(Msg.NextKey));
if Msg.Extending and ((Msg.NextKey<Msg.ExtensionsLo) or
(Msg.NextKey>Msg.ExtensionsHi)) then
R('Key value outside of extensions range '+IntToStr(Msg.NextKey));
Msg.AddMember(Quant,TypeNr,FieldName,TypeName,DefaultValue);
TypeNr:=0;
end;
'=':
Msg.NextKey:=NextInt;
'[':
begin
NextKeyword;
Expect('=');
if (Keyword='default') and (Quant=Quant_Optional) then
if NextKeyword then
DefaultValue:=Keyword
else
R('Default value expected')
else
if (Keyword='packed')
and (Quant in [Quant_Repeated,Quant_Repeated_Packed]) then
if NextKeyword then
if Keyword='true' then Quant:=Quant_Repeated_Packed else
if Keyword='false' then Quant:=Quant_Repeated else
R('Unknown packed value "'+Keyword+'"')
else R('Packed value expected')
else
R('Unknown modifier "'+Keyword+'"');
Expect(']');
end;
else R('Expected ";" or "=" or "["');
end;
end;
end;
end
else
begin
Expect('}');
if Msg.OneOfName<>'' then
Msg.OneOfName:='' //continue with message
else
Msg:=Msg.Parent;//pop message
end;
end
else //MsgEnum<>nil
begin
if NextKeyword then
begin
if Keyword='option' then
begin
if not NextKeyword then R('Enum option identifier expected');
if Keyword='allow_alias' then
begin
SkipWhiteSpace;
if (CodeI<=CodeL) and (Code[CodeI]='=') then
begin
inc(CodeI);
if not NextKeyword then R('Enum option value expected');
if Keyword='true' then //TODO: allow_alias=true
else
if Keyword='false' then //TODO: allow_alias=false
else
R('Unknown enum option value "'+Keyword+'"');
end
else
R('Assignment to "allow_alias" expected');
end
else
R('Unknown enum option "'+Keyword+'"');
end
else
begin
SkipWhiteSpace;
if (CodeI<=CodeL) and (Code[CodeI]='=') then
begin
inc(CodeI);
MsgEnum.NextKey:=NextInt;
end;
MsgEnum.AddMember(0,0,Keyword,'','');
end;
if IsNext('[') then
begin
//option(s)
First:=true;
while First or IsNext(',') do
begin
if First then First:=false;
if IsNext('(') then
begin
if not NextKeyword then R('Option name expected');
OptionName:=Keyword;
while IsNext('.') do
begin
if not NextKeyword then R('Option name expected');
OptionName:=OptionName+'.'+Keyword;
end;
Expect(')');
end
else
begin
if not NextKeyword then R('Option name expected');
OptionName:=Keyword;
end;
while IsNext('.') do
begin
if not NextKeyword then R('Option name expected');
OptionName:=OptionName+'.'+Keyword;
end;
Expect('=');
//TODO:OptionName+'='+
NextStr;
end;
Expect(']');
end;
Expect(';');
end
else
if IsNext('}') then
begin
MsgEnum:=nil; //continue
if Msg<>nil then IsNext(';');
end
else
R('Unexpected syntax');
end;
end;
function TProtocolBufferParser.GenerateUnit(
Flags:TProtocolBufferParserFlags): string;
var
MsgI:integer;
v:TProtocolBufferParserValue;
f:TProtocolBufferParserFlag;
begin
//TODO: Flags from options?
if Values[pbpvUnitName]<>'' then FUnitName:=Values[pbpvUnitName];
Result:='unit '+FUnitName+';'#13#10#13#10+
'// ATTENTION:'#13#10+
'// This file was auto generated by dpbp '+GetSelfVersion+#13#10+
'// https://github.com/stijnsanders/DelphiProtocolBuffer'#13#10+
'//'#13#10;
v:=TProtocolBufferParserValue(0);
while v<>pbpv_Unknown do
begin
if Values[v]<>ProtocolBufferParserValueDefaults[v] then
Result:=Result+'// VALUE: -'+
ProtocolBufferParserValueName[v][1]+'"'+Values[v]+'"'#13#10;
inc(v);
end;
f:=TProtocolBufferParserFlag(0);
while f<>pbpf_Unknown do
begin
if f in Flags then Result:=Result+'// FLAG: '+
ProtocolBufferParserFlagName[f]+#13#10;
inc(f);
end;
if not(pbpfDebugData in Flags) then
Result:=Result+#13#10'{$D-}'#13#10'{$L-}'#13#10;//'{$Y-}'#13#10;?
//first pass
for MsgI:=0 to FMsgDescIndex-1 do
begin
//TODO: determine dependancy-safe order?
if FMsgDesc[MsgI] is TProtBufEnumDescriptor then
begin
if (pbpfPrependEnumName in Flags)
and (FMsgDesc[MsgI].Parent<>nil) then
FMsgDesc[MsgI].FPasName:=
FMsgDesc[MsgI].Parent.Name+'_'+FMsgDesc[MsgI].FPasName;
end
else
begin
if (pbpfPrependNameParent in Flags)
and (FMsgDesc[MsgI].Parent<>nil) then
FMsgDesc[MsgI].FPasName:=
FMsgDesc[MsgI].Parent.Name+'_'+FMsgDesc[MsgI].FPasName;
end;
end;
//interface
Result:=Result+#13#10+
'interface'#13#10#13#10+
'uses Classes, ProtBuf;'#13#10#13#10+
'type'#13#10;
for MsgI:=0 to FMsgDescIndex-1 do
Result:=Result+FMsgDesc[MsgI].GenerateInterface(Self,Flags);
//implementation
Result:=Result+'implementation'#13#10#13#10'uses SysUtils;'#13#10#13#10;
for MsgI:=0 to FMsgDescIndex-1 do
Result:=Result+FMsgDesc[MsgI].GenerateImplementation(Self,Flags);
Result:=Result+'end.'#13#10;
end;
function TProtocolBufferParser.MsgDescByName(
OptParent: TProtBufMessageDescriptor;
const Name:string):TProtBufMessageDescriptor;
var
i:integer;
begin
if OptParent=nil then i:=FMsgDescIndex else
begin
//search with Parent set
i:=0;
//TODO: ascend over .Parent(s)?
while (i<FMsgDescIndex) and ((FMsgDesc[i].Parent<>OptParent)
or (FMsgDesc[i].Name<>Name)) do inc(i);
end;
if i=FMsgDescIndex then
begin
//not found, search disregarding parent
i:=0;
while (i<FMsgDescIndex) and (FMsgDesc[i].Name<>Name) do inc(i);
end;
if i<FMsgDescIndex then Result:=FMsgDesc[i] else Result:=nil;
end;
function IsReservedWord(x:string):boolean;
const
ResWordsCount=65;
ResWords:array[0..ResWordsCount-1] of string=(
'and', 'array', 'as', 'asm',
'begin', 'case', 'class', 'const',
'constructor', 'destructor', 'dispinterface', 'div',
'do', 'downto', 'else', 'end',
'except', 'exports', 'file', 'finalization',
'finally', 'for', 'function', 'goto',
'if', 'implementation', 'in', 'inherited',
'initialization', 'inline', 'interface', 'is',
'label', 'library', 'mod', 'nil',
'not', 'object', 'of', 'or',
'out', 'packed', 'procedure', 'program',
'property', 'raise', 'record', 'repeat',
'resourcestring', 'set', 'shl', 'shr',
'string', 'then', 'threadvar', 'to',
'try', 'type', 'unit', 'until',
'uses', 'var', 'while', 'with',
'xor'
);
ResWordMaxLength:array['A'..'Z'] of byte=(5,5,11,13,7,12,4,0,14,0,0,7,3,3,6,9,0,14,6,9,5,3,5,3,0,0);
var
c:char;
y:string;
i:integer;
begin
//assert x<>''
c:=char(UpCase(x[1]));
//skip anything longer than longest word
if not(c in ['A'..'Z']) or (Length(x)>ResWordMaxLength[c]) then
Result:=false
else
begin
y:=LowerCase(x);
i:=0;
while (i<ResWordsCount) and (y<>ResWords[i]) do inc(i);