source upload
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,64 @@
|
||||
{ Unit pbTestImport1Messages.pas }
|
||||
{ Generated from TestImport1.proto }
|
||||
{ Package TestImport1 }
|
||||
|
||||
unit pbTestImport1Messages;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
flcUtils,
|
||||
flcStrings,
|
||||
flcProtoBufUtils;
|
||||
|
||||
|
||||
|
||||
{ TEnumGlobal }
|
||||
|
||||
type
|
||||
TEnumGlobal = (
|
||||
enumglobalGVal1 = 1,
|
||||
enumglobalGVal2 = 2
|
||||
);
|
||||
|
||||
function pbEncodeValueEnumGlobal(var Buf; const BufSize: Integer; const Value: TEnumGlobal): Integer;
|
||||
function pbEncodeFieldEnumGlobal(var Buf; const BufSize: Integer; const FieldNum: Integer; const Value: TEnumGlobal): Integer;
|
||||
function pbDecodeValueEnumGlobal(const Buf; const BufSize: Integer; var Value: TEnumGlobal): Integer;
|
||||
procedure pbDecodeFieldEnumGlobal(const Field: TpbProtoBufDecodeField; var Value: TEnumGlobal);
|
||||
|
||||
|
||||
|
||||
implementation
|
||||
|
||||
|
||||
|
||||
{ TEnumGlobal }
|
||||
|
||||
function pbEncodeValueEnumGlobal(var Buf; const BufSize: Integer; const Value: TEnumGlobal): Integer;
|
||||
begin
|
||||
Result := pbEncodeValueInt32(Buf, BufSize, Ord(Value));
|
||||
end;
|
||||
|
||||
function pbEncodeFieldEnumGlobal(var Buf; const BufSize: Integer; const FieldNum: Integer; const Value: TEnumGlobal): Integer;
|
||||
begin
|
||||
Result := pbEncodeFieldInt32(Buf, BufSize, FieldNum, Ord(Value));
|
||||
end;
|
||||
|
||||
function pbDecodeValueEnumGlobal(const Buf; const BufSize: Integer; var Value: TEnumGlobal): Integer;
|
||||
var I : LongInt;
|
||||
begin
|
||||
Result := pbDecodeValueInt32(Buf, BufSize, I);
|
||||
Value := TEnumGlobal(I);
|
||||
end;
|
||||
|
||||
procedure pbDecodeFieldEnumGlobal(const Field: TpbProtoBufDecodeField; var Value: TEnumGlobal);
|
||||
var I : LongInt;
|
||||
begin
|
||||
pbDecodeFieldInt32(Field, I);
|
||||
Value := TEnumGlobal(I);
|
||||
end;
|
||||
|
||||
|
||||
|
||||
end.
|
||||
|
86
contrib/fundamentals/ProtocolBuffers/Examples/Test1.proto
Normal file
86
contrib/fundamentals/ProtocolBuffers/Examples/Test1.proto
Normal file
@@ -0,0 +1,86 @@
|
||||
//
|
||||
// Test proto file for ProtoCodeGen
|
||||
//
|
||||
|
||||
// TODO
|
||||
// * CodeGen: optional fields in records: indicate which fields have been populated
|
||||
// * CodeGen: unknown fields in records
|
||||
// * services
|
||||
// * options
|
||||
// * extensions
|
||||
// * CodeGen: generate objects
|
||||
// * Dots (.) in package identifier, e.g. package abc.def;
|
||||
// * Comments from proto file in generated file
|
||||
// * Bytes type e.g. optional bytes DefField10 = 10 [default = "123"];
|
||||
|
||||
package test1;
|
||||
|
||||
import "TestImport1.proto";
|
||||
|
||||
// enumeration
|
||||
enum EnumG0 {
|
||||
g1 = 1;
|
||||
g2 = 2;
|
||||
}
|
||||
|
||||
// simple message
|
||||
message TestMsg0 {
|
||||
required int32 Field1 = 1;
|
||||
required int64 Field2 = 2;
|
||||
}
|
||||
|
||||
message TestMsg1 {
|
||||
|
||||
// fields with defaults
|
||||
optional int32 DefField1 = 1 [default = 2];
|
||||
optional int64 DefField2 = 2 [default = -1];
|
||||
optional string DefField3 = 3 [default = "yes"];
|
||||
optional double DefField4 = 4 [default = 1.1];
|
||||
optional bool DefField5 = 5 [default = true];
|
||||
optional EnumG0 DefField6 = 6 [default = g2];
|
||||
optional sint64 DefField7 = 7 [default = 100];
|
||||
optional fixed32 DefField8 = 8 [default = 1];
|
||||
optional float DefField9 = 9 [default = 1.23e1];
|
||||
|
||||
// field of message type
|
||||
optional TestMsg0 FieldMsg1 = 20;
|
||||
|
||||
// nested enumeration
|
||||
enum Enum1 {
|
||||
Val1 = 1;
|
||||
Val2 = 2;
|
||||
}
|
||||
|
||||
// fields of nested enumeration type
|
||||
optional Enum1 FieldE1 = 21;
|
||||
optional TestMsg1.Enum1 FieldE2 = 22 [default = Val2, packed = true];
|
||||
|
||||
// nested message
|
||||
message TestNested1 {
|
||||
optional int32 Field1 = 1;
|
||||
}
|
||||
|
||||
// fields of nested message type
|
||||
optional TestNested1 FieldNested1 = 30;
|
||||
optional TestMsg1.TestNested1 FieldNested2 = 31;
|
||||
|
||||
// repeated fields
|
||||
repeated int32 FieldArr1 = 40;
|
||||
repeated int32 FieldArr2 = 41 [packed = true];
|
||||
repeated string FieldArr3 = 42;
|
||||
repeated Enum1 FieldArrE1 = 43;
|
||||
repeated TestMsg0 FieldMArr2 = 44;
|
||||
|
||||
// fields of imported types
|
||||
optional TestImport1.EnumGlobal FieldImp1 = 50;
|
||||
optional EnumGlobal FieldImp2 = 51;
|
||||
|
||||
// extensions 1000 to 1999;
|
||||
}
|
||||
|
||||
// test proto identifier name conversion
|
||||
message test_iden1 {
|
||||
optional int32 field_name_test_1 = 1;
|
||||
optional int32 field_Name_test_2 = 2;
|
||||
}
|
||||
|
@@ -0,0 +1,6 @@
|
||||
package TestImport1;
|
||||
|
||||
enum EnumGlobal {
|
||||
gVal1 = 1;
|
||||
gVal2 = 2;
|
||||
}
|
@@ -0,0 +1,190 @@
|
||||
program ProtoBufCodeGen;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils,
|
||||
flcDynArrays in '..\..\Utils\flcDynArrays.pas',
|
||||
flcStrings in '..\..\Utils\flcStrings.pas',
|
||||
flcProtoBufUtils in '..\flcProtoBufUtils.pas',
|
||||
flcProtoBufProtoNodes in '..\flcProtoBufProtoNodes.pas',
|
||||
flcProtoBufProtoParser in '..\flcProtoBufProtoParser.pas',
|
||||
flcProtoBufProtoParserTests in '..\flcProtoBufProtoParserTests.pas',
|
||||
flcProtoBufProtoCodeGenPascal in '..\flcProtoBufProtoCodeGenPascal.pas',
|
||||
flcUtils in '..\..\Utils\flcUtils.pas',
|
||||
flcStdTypes in '..\..\Utils\flcStdTypes.pas',
|
||||
flcASCII in '..\..\Utils\flcASCII.pas',
|
||||
flcFloats in '..\..\Utils\flcFloats.pas',
|
||||
flcStringBuilder in '..\..\Utils\flcStringBuilder.pas';
|
||||
|
||||
const
|
||||
AppVersion = '1.0.1';
|
||||
|
||||
procedure PrintTitle;
|
||||
begin
|
||||
Writeln('Proto code generator ', AppVersion);
|
||||
end;
|
||||
|
||||
procedure PrintHelp;
|
||||
begin
|
||||
Writeln('Usage:');
|
||||
Writeln('ProtoCodeGen <input .proto file> [ <options> ]');
|
||||
Writeln('<options>');
|
||||
Writeln(' --proto_path=<import path for .proto files>');
|
||||
Writeln(' -I same as as --proto_path');
|
||||
Writeln(' --pas_out=<output path for .pas files>');
|
||||
Writeln(' -O same as --pas_out');
|
||||
Writeln(' --pas_ver=<output delphi version for .pas files; 0: less delpi XE, 1: only delphi XE, 2: all>');
|
||||
Writeln(' -V= same as --pas_ver');
|
||||
|
||||
Writeln(' --help');
|
||||
end;
|
||||
|
||||
procedure PrintError(const ErrorStr: String);
|
||||
begin
|
||||
Writeln('Error: ', ErrorStr);
|
||||
end;
|
||||
|
||||
var
|
||||
// command line paramaters
|
||||
ParamInputFile : String;
|
||||
ParamOutputPath : String;
|
||||
ParamProtoPath : String;
|
||||
|
||||
ParamSupportVersion: TCodeGenSupportVersion = cgsvLessXE;
|
||||
|
||||
// app
|
||||
InputFileFull : String;
|
||||
InputFilePath : String;
|
||||
InputFileName : String;
|
||||
OutputPath : String;
|
||||
|
||||
procedure ProcessParameters;
|
||||
var L, I, N: Integer;
|
||||
S : String;
|
||||
begin
|
||||
L := ParamCount;
|
||||
if L = 0 then
|
||||
begin
|
||||
PrintHelp;
|
||||
Halt(1);
|
||||
end;
|
||||
for I := 1 to L do
|
||||
begin
|
||||
S := ParamStr(I);
|
||||
|
||||
if StrMatchLeft(S, '--pas_ver=', False) then
|
||||
begin
|
||||
Delete(S, 1, 10);
|
||||
N := StrToIntDef(S, 0);
|
||||
if (N >= Integer(Low(TCodeGenSupportVersion))) and (N <= Integer(High(TCodeGenSupportVersion))) then
|
||||
ParamSupportVersion := TCodeGenSupportVersion(N);
|
||||
end
|
||||
else if StrMatchLeft(S, '-V=', False) then
|
||||
begin
|
||||
Delete(S, 1, 3);
|
||||
N := StrToIntDef(S, 0);
|
||||
if (N >= Integer(Low(TCodeGenSupportVersion))) and (N <= Integer(High(TCodeGenSupportVersion))) then
|
||||
ParamSupportVersion := TCodeGenSupportVersion(N);
|
||||
end
|
||||
else if StrMatchLeft(S, '--pas_out=', False) then
|
||||
begin
|
||||
Delete(S, 1, 10);
|
||||
ParamOutputPath := S;
|
||||
end
|
||||
else if StrMatchLeft(S, '-O=', False) then
|
||||
begin
|
||||
Delete(S, 1, 3);
|
||||
ParamOutputPath := S;
|
||||
end
|
||||
else if StrMatchLeft(S, '--proto_path=', False) then
|
||||
begin
|
||||
Delete(S, 1, 13);
|
||||
ParamProtoPath := S;
|
||||
end
|
||||
else if StrMatchLeft(S, '-I=', False) then
|
||||
begin
|
||||
Delete(S, 1, 3);
|
||||
ParamProtoPath := S;
|
||||
end
|
||||
else if StrEqualNoAsciiCase(S, '--help') then
|
||||
begin
|
||||
PrintHelp;
|
||||
Halt(1);
|
||||
end
|
||||
else
|
||||
begin
|
||||
ParamInputFile := S;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure InitialiseApp;
|
||||
begin
|
||||
if ParamInputFile = '' then
|
||||
begin
|
||||
PrintError('No input file specified');
|
||||
PrintHelp;
|
||||
Halt(1);
|
||||
end;
|
||||
InputFileFull := ExpandFileName(ParamInputFile);
|
||||
InputFilePath := ExtractFilePath(InputFileFull);
|
||||
InputFileName := ExtractFileName(InputFileFull);
|
||||
if ParamOutputPath <> '' then
|
||||
OutputPath := ParamOutputPath
|
||||
else
|
||||
OutputPath := InputFilePath;
|
||||
end;
|
||||
|
||||
var
|
||||
Package : TpbProtoPackage;
|
||||
|
||||
procedure ParseInputFile;
|
||||
var
|
||||
Parser : TpbProtoParser;
|
||||
begin
|
||||
Parser := TpbProtoParser.Create;
|
||||
try
|
||||
Parser.ProtoPath := ParamProtoPath;
|
||||
Parser.SetFileName(ParamInputFile);
|
||||
Package := Parser.Parse(GetPascalProtoNodeFactory);
|
||||
finally
|
||||
Parser.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure ProduceOutputFiles;
|
||||
var
|
||||
CodeGen : TpbProtoCodeGenPascal;
|
||||
begin
|
||||
CodeGen := TpbProtoCodeGenPascal.Create;
|
||||
try
|
||||
CodeGen.OutputPath := OutputPath;
|
||||
CodeGen.GenerateCode(Package, ParamSupportVersion);
|
||||
finally
|
||||
FreeAndNil(CodeGen);
|
||||
end;
|
||||
end;
|
||||
|
||||
begin
|
||||
{$IFDEF TEST}
|
||||
flcProtoBufUtils.Test;
|
||||
flcProtoBufProtoParserTests.Test;
|
||||
{$ENDIF}
|
||||
|
||||
PrintTitle;
|
||||
try
|
||||
ProcessParameters;
|
||||
InitialiseApp;
|
||||
Writeln(InputFileName);
|
||||
ParseInputFile;
|
||||
try
|
||||
ProduceOutputFiles;
|
||||
finally
|
||||
FreeAndNil(Package);
|
||||
end;
|
||||
except
|
||||
on E: Exception do
|
||||
PrintError(E.Message);
|
||||
end;
|
||||
end.
|
12
contrib/fundamentals/ProtocolBuffers/flcProtoBuf.inc
Normal file
12
contrib/fundamentals/ProtocolBuffers/flcProtoBuf.inc
Normal file
@@ -0,0 +1,12 @@
|
||||
{$INCLUDE ..\flcInclude.inc}
|
||||
|
||||
{$IFDEF DEBUG}
|
||||
{$DEFINE PROTOBUF_DEBUG}
|
||||
{$ENDIF}
|
||||
|
||||
{$IFDEF DEBUG}
|
||||
{$IFDEF TEST}
|
||||
{$DEFINE PROTOBUF_TEST}
|
||||
{$ENDIF}
|
||||
{$ENDIF}
|
||||
|
File diff suppressed because it is too large
Load Diff
1285
contrib/fundamentals/ProtocolBuffers/flcProtoBufProtoNodes.pas
Normal file
1285
contrib/fundamentals/ProtocolBuffers/flcProtoBufProtoNodes.pas
Normal file
File diff suppressed because it is too large
Load Diff
1277
contrib/fundamentals/ProtocolBuffers/flcProtoBufProtoParser.pas
Normal file
1277
contrib/fundamentals/ProtocolBuffers/flcProtoBufProtoParser.pas
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,53 @@
|
||||
unit flcProtoBufProtoParserTests;
|
||||
|
||||
interface
|
||||
|
||||
|
||||
|
||||
{$INCLUDE flcProtoBuf.inc}
|
||||
|
||||
{$IFDEF PROTOBUF_TEST}
|
||||
procedure Test;
|
||||
{$ENDIF}
|
||||
|
||||
|
||||
|
||||
implementation
|
||||
|
||||
{$IFDEF PROTOBUF_TEST}
|
||||
uses
|
||||
flcProtoBufProtoNodes,
|
||||
flcProtoBufProtoParser;
|
||||
|
||||
|
||||
|
||||
const
|
||||
CRLF = #$D#$A;
|
||||
|
||||
ProtoText1 =
|
||||
'message test { ' +
|
||||
' required int32 field1 = 1; ' +
|
||||
'} ';
|
||||
ProtoText1_ProtoString =
|
||||
'message test {' + CRLF +
|
||||
'required int32 field1 = 1;' +
|
||||
'}' + CRLF;
|
||||
|
||||
procedure Test;
|
||||
var
|
||||
P : TpbProtoParser;
|
||||
A : TpbProtoPackage;
|
||||
begin
|
||||
P := TpbProtoParser.Create;
|
||||
P.SetTextStr(ProtoText1);
|
||||
A := P.Parse(GetDefaultProtoNodeFactory);
|
||||
Assert(Assigned(A));
|
||||
Assert(A.GetAsProtoString = ProtoText1_ProtoString);
|
||||
P.Free;
|
||||
end;
|
||||
{$ENDIF}
|
||||
|
||||
|
||||
|
||||
end.
|
||||
|
1993
contrib/fundamentals/ProtocolBuffers/flcProtoBufUtils.pas
Normal file
1993
contrib/fundamentals/ProtocolBuffers/flcProtoBufUtils.pas
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user