Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
larshp committed Aug 17, 2023
1 parent 51a542e commit ee9e6dc
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/model/zcl_protobuf2_field.clas.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CLASS zcl_protobuf2_field DEFINITION PUBLIC.
PUBLIC SECTION.
DATA mv_label TYPE string.
DATA mv_type TYPE string.
DATA mv_field_name TYPE string.
DATA mv_field_number TYPE i.
ENDCLASS.

CLASS zcl_protobuf2_field IMPLEMENTATION.

ENDCLASS.
16 changes: 16 additions & 0 deletions src/model/zcl_protobuf2_field.clas.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VSEOCLASS>
<CLSNAME>ZCL_PROTOBUF2_FIELD</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>ZCL_PROTOBUF2_FIELD</DESCRIPT>
<STATE>1</STATE>
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
<UNICODE>X</UNICODE>
</VSEOCLASS>
</asx:values>
</asx:abap>
</abapGit>
1 change: 1 addition & 0 deletions src/model/zcl_protobuf2_message.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CLASS zcl_protobuf2_message DEFINITION PUBLIC.
* https://protobuf.dev/reference/protobuf/proto2-spec/#message_definition
METHODS constructor IMPORTING iv_name TYPE string.
DATA mv_name TYPE string.
DATA mt_fields TYPE STANDARD TABLE OF REF TO zcl_protobuf2_field WITH EMPTY KEY.
ENDCLASS.

CLASS zcl_protobuf2_message IMPLEMENTATION.
Expand Down
3 changes: 3 additions & 0 deletions src/model/zif_protobuf2_serializable.intf.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INTERFACE zif_protobuf2_serializable PUBLIC.
METHODS serialize RETURNING VALUE(rv_string) TYPE string.
ENDINTERFACE.
15 changes: 15 additions & 0 deletions src/model/zif_protobuf2_serializable.intf.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_INTF" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VSEOINTERF>
<CLSNAME>ZIF_PROTOBUF2_SERIALIZABLE</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>ZIF_PROTOBUF2_SERIALIZABLE</DESCRIPT>
<EXPOSURE>2</EXPOSURE>
<STATE>1</STATE>
<UNICODE>X</UNICODE>
</VSEOINTERF>
</asx:values>
</asx:abap>
</abapGit>
55 changes: 43 additions & 12 deletions src/parser/zcl_protobuf2_parser.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,58 @@ CLASS zcl_protobuf2_parser DEFINITION PUBLIC.
IMPORTING
io_file TYPE REF TO zcl_protobuf2_file
io_stream TYPE REF TO lcl_stream.
CLASS-METHODS message_body

CLASS-METHODS message
IMPORTING
io_stream TYPE REF TO lcl_stream.
io_stream TYPE REF TO lcl_stream
RETURNING
VALUE(ro_message) TYPE REF TO zcl_protobuf2_message.

CLASS-METHODS field
IMPORTING
io_stream TYPE REF TO lcl_stream
RETURNING
VALUE(ro_field) TYPE REF TO zcl_protobuf2_field.
ENDCLASS.



CLASS zcl_protobuf2_parser IMPLEMENTATION.

METHOD field.
* https://protobuf.dev/reference/protobuf/proto2-spec/#fields
* WRITE / io_stream->get( ).

ro_field = NEW #( ).
ro_field->mv_label = io_stream->take_token( ).
ro_field->mv_type = io_stream->take_token( ).
ro_field->mv_field_name = io_stream->take_token( ).
ASSERT io_stream->take_token( ) = '='.
ro_field->mv_field_number = io_stream->take_token( ).
ENDMETHOD.

METHOD message_body.
METHOD message.
* https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#message_definition
ASSERT io_stream IS NOT INITIAL.

WRITE / io_stream->get( ).
WHILE io_stream->is_empty( ) = abap_false.
DATA(lv_token) = io_stream->take_token( ).
WRITE / lv_token.
ro_message = NEW #( io_stream->take_token( ) ).

DATA(lo_stream) = io_stream->take_matching_squiggly( ).

* WRITE / io_stream->get( ).
WHILE lo_stream->is_empty( ) = abap_false.
DATA(lv_token) = lo_stream->peek_token( ).
CASE lv_token.
WHEN 'message'.
ASSERT 1 = 'todo'.
WHEN 'enum'.
ASSERT 1 = 'todo'.
WHEN OTHERS. " then its a field
APPEND field( lo_stream->take_statement( ) ) TO ro_message->mt_fields.
ENDCASE.

" DATA(lv_token) = io_stream->take_token( ).
" WRITE / lv_token.
ENDWHILE.
ENDMETHOD.

Expand Down Expand Up @@ -56,13 +90,10 @@ CLASS zcl_protobuf2_parser IMPLEMENTATION.
DATA(lv_token) = io_stream->take_token( ).
CASE lv_token.
WHEN 'message'.
DATA(lo_message) = NEW zcl_protobuf2_message( io_stream->take_token( ) ).
APPEND lo_message TO io_file->mt_messages.
* WRITE: / 'Message:', lo_message->mv_name.
message_body( io_stream->take_matching( ) ).
APPEND message( io_stream ) TO io_file->mt_messages.
WHEN OTHERS.
WRITE: / 'todo, handle token:', lv_token.
ASSERT 1 = 2.
ASSERT 1 = 'todo'.
ENDCASE.
ENDWHILE.
ENDMETHOD.
Expand Down
3 changes: 2 additions & 1 deletion src/parser/zcl_protobuf2_parser.clas.locals_def.abap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ CLASS lcl_stream DEFINITION.
METHODS peek_token RETURNING VALUE(rv_token) TYPE string.
METHODS is_empty RETURNING VALUE(rv_empty) TYPE abap_bool.
METHODS get RETURNING VALUE(rv_str) TYPE string.
METHODS take_matching RETURNING VALUE(ro_stream) TYPE REF TO lcl_stream.
METHODS take_statement RETURNING VALUE(ro_stream) TYPE REF TO lcl_stream.
METHODS take_matching_squiggly RETURNING VALUE(ro_stream) TYPE REF TO lcl_stream.
PRIVATE SECTION.
DATA mv_str TYPE string.
ENDCLASS.
20 changes: 19 additions & 1 deletion src/parser/zcl_protobuf2_parser.clas.locals_imp.abap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ CLASS lcl_stream IMPLEMENTATION.
rv_token = mv_str(lv_offset).
ENDMETHOD.

METHOD take_matching.
METHOD take_statement.
DATA lv_offset TYPE i.

FIND FIRST OCCURRENCE OF |;| IN mv_str MATCH OFFSET lv_offset.
ASSERT sy-subrc = 0.

ro_stream = NEW #( mv_str(lv_offset) ).

lv_offset = lv_offset + 1.
mv_str = mv_str+lv_offset.
CONDENSE mv_str.
ENDMETHOD.

METHOD take_matching_squiggly.
DATA lt_open TYPE match_result_tab.
DATA lt_close TYPE match_result_tab.
DATA lt_all TYPE match_result_tab.
Expand Down Expand Up @@ -65,6 +78,11 @@ CLASS lcl_stream IMPLEMENTATION.
ENDLOOP.

DATA(lv_tmp) = mv_str(ls_all-offset).
" remove the squirly brackets,
lv_tmp = lv_tmp+1.
lv_count = strlen( lv_tmp ) - 1.
lv_tmp = lv_tmp(lv_count).

ro_stream = NEW #( lv_tmp ).
mv_str = mv_str+ls_all-offset.
CONDENSE mv_str.
Expand Down

0 comments on commit ee9e6dc

Please sign in to comment.