Here is an ABAP report that reads an internal table of strings representing the source code of an ABAP class, tokenizes the code, parses it into an object-oriented representation, and then outputs it in XML format. The code uses an object-oriented approach and tokenization, as recommended.
REPORT z_convert_class_to_xml.
************************************************************************
* Type declarations
************************************************************************
TYPES: ty_token_type TYPE c LENGTH 10.
CONSTANTS:
c_token_type_keyword TYPE ty_token_type VALUE 'KEYWORD',
c_token_type_identifier TYPE ty_token_type VALUE 'IDENTIFIER',
c_token_type_symbol TYPE ty_token_type VALUE 'SYMBOL',
c_token_type_comment TYPE ty_token_type VALUE 'COMMENT',
c_token_type_string TYPE ty_token_type VALUE 'STRING',
c_token_type_number TYPE ty_token_type VALUE 'NUMBER',
c_token_type_unknown TYPE ty_token_type VALUE 'UNKNOWN'.
************************************************************************
* Class Definitions
************************************************************************
"-----------------------------------------------------------------------
" Token class to represent each token with type and value
"-----------------------------------------------------------------------
CLASS lcl_token DEFINITION.
PUBLIC SECTION.
DATA: token_type TYPE ty_token_type,
token_value TYPE string.
METHODS: constructor IMPORTING iv_type TYPE ty_token_type
iv_value TYPE string.
ENDCLASS.
CLASS lcl_token IMPLEMENTATION.
METHOD constructor.
token_type = iv_type.
token_value = iv_value.
ENDMETHOD.
ENDCLASS.
"-----------------------------------------------------------------------
" Tokenizer class to tokenize the source code
"-----------------------------------------------------------------------
CLASS lcl_tokenizer DEFINITION.
PUBLIC SECTION.
TYPES: ty_token_tab TYPE STANDARD TABLE OF REF TO lcl_token WITH DEFAULT KEY.
METHODS:
tokenize IMPORTING it_source_code TYPE STANDARD TABLE OF string
RETURNING VALUE(rt_tokens) TYPE ty_token_tab.
PRIVATE SECTION.
DATA: mt_keywords TYPE SORTED TABLE OF string WITH UNIQUE KEY table_line.
ENDCLASS.
CLASS lcl_tokenizer IMPLEMENTATION.
METHOD tokenize.
DATA: lt_tokens TYPE ty_token_tab,
lt_line_tokens TYPE STANDARD TABLE OF string,
lv_line TYPE string.
" Initialize keywords
mt_keywords = VALUE #(
( 'CLASS' ) ( 'PUBLIC' ) ( 'PROTECTED' ) ( 'PRIVATE' ) ( 'SECTION' )
( 'DATA' ) ( 'TYPES' ) ( 'METHODS' ) ( 'ENDCLASS' ) ( 'ENDMETHOD' )
( 'METHOD' ) ( 'IMPORTING' ) ( 'EXPORTING' ) ( 'CHANGING' )
( 'TYPE' ) ( 'LIKE' ) ( 'FINAL' ) ( 'CREATE' ) ( 'INHERITING FROM' )
( 'INTERFACES' ) ( 'ALIAS' ) ( 'FOR' ) ( 'END' ) ( 'CLASS' )
).
LOOP AT it_source_code INTO lv_line.
" Remove leading and trailing spaces
lv_line = condense( lv_line ).
IF lv_line IS INITIAL.
CONTINUE.
ENDIF.
" Check for comments
IF lv_line CS '"' OR lv_line CS '*' OR lv_line CS '!'.
" Entire line is a comment
APPEND NEW lcl_token( iv_type = c_token_type_comment iv_value = lv_line ) TO lt_tokens.
CONTINUE.
ENDIF.
" Split line into tokens
CALL FUNCTION 'SCP_REPLACE_STRANGE_CHARS'
EXPORTING
original_string = lv_line
strange_char = ',' " Replace commas with space
IMPORTING
new_string = lv_line.
SPLIT lv_line AT space INTO TABLE lt_line_tokens.
LOOP AT lt_line_tokens INTO DATA(lv_token_str).
lv_token_str = condense( lv_token_str ).
IF lv_token_str IS INITIAL.
CONTINUE.
ENDIF.
DATA(lv_token_type) = c_token_type_unknown.
" Check if token is a keyword
READ TABLE mt_keywords WITH KEY table_line = lv_token_str TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
lv_token_type = c_token_type_keyword.
ELSEIF lv_token_str CA '(){}.,;=*<>+-/'.
lv_token_type = c_token_type_symbol.
ELSEIF lv_token_str(1) = '''' OR lv_token_str(1) = '`'.
lv_token_type = c_token_type_string.
ELSEIF lv_token_str IS NUMERIC.
lv_token_type = c_token_type_number.
ELSE.
lv_token_type = c_token_type_identifier.
ENDIF.
APPEND NEW lcl_token( iv_type = lv_token_type iv_value = lv_token_str ) TO lt_tokens.
ENDLOOP.
ENDLOOP.
rt_tokens = lt_tokens.
ENDMETHOD.
ENDCLASS.
"-----------------------------------------------------------------------
" Class representations
"-----------------------------------------------------------------------
" Class to represent an attribute
CLASS lcl_attribute DEFINITION.
PUBLIC SECTION.
DATA:
name TYPE string,
type TYPE string,
comment TYPE string.
METHODS:
constructor IMPORTING iv_name TYPE string
iv_type TYPE string
iv_comment TYPE string.
ENDCLASS.
CLASS lcl_attribute IMPLEMENTATION.
METHOD constructor.
name = iv_name.
type = iv_type.
comment = iv_comment.
ENDMETHOD.
ENDCLASS.
" Class to represent a method parameter
CLASS lcl_parameter DEFINITION.
PUBLIC SECTION.
DATA:
name TYPE string,
type TYPE string,
kind TYPE string, " IMPORTING, EXPORTING, etc.
comment TYPE string.
METHODS:
constructor IMPORTING iv_name TYPE string
iv_type TYPE string
iv_kind TYPE string
iv_comment TYPE string.
ENDCLASS.
CLASS lcl_parameter IMPLEMENTATION.
METHOD constructor.
name = iv_name.
type = iv_type.
kind = iv_kind.
comment = iv_comment.
ENDMETHOD.
ENDCLASS.
" Class to represent a method
CLASS lcl_method DEFINITION.
PUBLIC SECTION.
DATA:
name TYPE string,
parameters TYPE STANDARD TABLE OF REF TO lcl_parameter WITH DEFAULT KEY,
comment TYPE string,
code TYPE STANDARD TABLE OF string WITH DEFAULT KEY.
METHODS:
constructor IMPORTING iv_name TYPE string
iv_comment TYPE string.
add_parameter IMPORTING ir_param TYPE REF TO lcl_parameter.
add_code_line IMPORTING iv_line TYPE string.
ENDCLASS.
CLASS lcl_method IMPLEMENTATION.
METHOD constructor.
name = iv_name.
comment = iv_comment.
ENDMETHOD.
METHOD add_parameter.
APPEND ir_param TO parameters.
ENDMETHOD.
METHOD add_code_line.
APPEND iv_line TO code.
ENDMETHOD.
ENDCLASS.
" Class to represent the class definition
CLASS lcl_class_definition DEFINITION.
PUBLIC SECTION.
DATA:
name TYPE string,
attributes TYPE STANDARD TABLE OF REF TO lcl_attribute WITH DEFAULT KEY,
methods TYPE STANDARD TABLE OF REF TO lcl_method WITH DEFAULT KEY.
METHODS:
constructor IMPORTING iv_name TYPE string.
add_attribute IMPORTING ir_attr TYPE REF TO lcl_attribute.
add_method IMPORTING ir_meth TYPE REF TO lcl_method.
to_xml RETURNING VALUE(rv_xml) TYPE string.
ENDCLASS.
CLASS lcl_class_definition IMPLEMENTATION.
METHOD constructor.
name = iv_name.
ENDMETHOD.
METHOD add_attribute.
APPEND ir_attr TO attributes.
ENDMETHOD.
METHOD add_method.
APPEND ir_meth TO methods.
ENDMETHOD.
METHOD to_xml.
DATA: lv_xml TYPE string.
lv_xml = |<class>\n <name>{ name }</name>\n|.
LOOP AT attributes INTO DATA(lr_attr).
lv_xml = lv_xml && | <attribute>\n| &&
| <name>{ lr_attr->name }</name>\n| &&
| <type>{ lr_attr->type }</type>\n|.
IF lr_attr->comment IS NOT INITIAL.
lv_xml = lv_xml && | <comment>{ lr_attr->comment }</comment>\n|.
ENDIF.
lv_xml = lv_xml && | </attribute>\n|.
ENDLOOP.
LOOP AT methods INTO DATA(lr_method).
lv_xml = lv_xml && | <method>\n| &&
| <name>{ lr_method->name }</name>\n|.
LOOP AT lr_method->parameters INTO DATA(lr_param).
lv_xml = lv_xml && | <parameter>\n| &&
| <name>{ lr_param->name }</name>\n| &&
| <type>{ lr_param->type }</type>\n| &&
| <kind>{ lr_param->kind }</kind>\n|.
IF lr_param->comment IS NOT INITIAL.
lv_xml = lv_xml && | <comment>{ lr_param->comment }</comment>\n|.
ENDIF.
lv_xml = lv_xml && | </parameter>\n|.
ENDLOOP.
IF lr_method->comment IS NOT INITIAL.
lv_xml = lv_xml && | <comment>{ lr_method->comment }</comment>\n|.
ENDIF.
IF lr_method->code IS NOT INITIAL.
lv_xml = lv_xml && | <code>\n|.
LOOP AT lr_method->code INTO DATA(lv_line).
lv_xml = lv_xml && | <line>{ lv_line }</line>\n|.
ENDLOOP.
lv_xml = lv_xml && | </code>\n|.
ENDIF.
lv_xml = lv_xml && | </method>\n|.
ENDLOOP.
lv_xml = lv_xml && |</class>|.
rv_xml = lv_xml.
ENDMETHOD.
ENDCLASS.
"-----------------------------------------------------------------------
" Parser class to parse tokens into class representation
"-----------------------------------------------------------------------
CLASS lcl_parser DEFINITION.
PUBLIC SECTION.
METHODS:
parse IMPORTING it_tokens TYPE lcl_tokenizer=>ty_token_tab
RETURNING VALUE(ro_class_def) TYPE REF TO lcl_class_definition.
PRIVATE SECTION.
DATA:
mt_tokens TYPE lcl_tokenizer=>ty_token_tab,
mv_index TYPE i.
METHODS:
get_next_token RETURNING VALUE(ro_token) TYPE REF TO lcl_token.
peek_next_token RETURNING VALUE(ro_token) TYPE REF TO lcl_token.
parse_class_definition RETURNING VALUE(ro_class_def) TYPE REF TO lcl_class_definition.
parse_attributes CHANGING ct_attributes TYPE STANDARD TABLE OF REF TO lcl_attribute.
parse_methods CHANGING ct_methods TYPE STANDARD TABLE OF REF TO lcl_method.
ENDCLASS.
CLASS lcl_parser IMPLEMENTATION.
METHOD parse.
mt_tokens = it_tokens.
mv_index = 1.
ro_class_def = parse_class_definition( ).
ENDMETHOD.
METHOD get_next_token.
IF mv_index > lines( mt_tokens ).
ro_token = VALUE #( ).
RETURN.
ENDIF.
ro_token = mt_tokens[ mv_index ].
mv_index = mv_index + 1.
ENDMETHOD.
METHOD peek_next_token.
IF mv_index > lines( mt_tokens ).
ro_token = VALUE #( ).
RETURN.
ENDIF.
ro_token = mt_tokens[ mv_index ].
ENDMETHOD.
METHOD parse_class_definition.
DATA: lr_token TYPE REF TO lcl_token,
lv_class_nm TYPE string.
" Expect 'CLASS'
lr_token = get_next_token( ).
IF lr_token->token_value <> 'CLASS'.
RAISE EXCEPTION TYPE cx_sy_conversion_no_number
EXPORTING message = 'Expected CLASS keyword'.
ENDIF.
" Get class name
lr_token = get_next_token( ).
IF lr_token->token_type <> c_token_type_identifier.
RAISE EXCEPTION TYPE cx_sy_conversion_no_number
EXPORTING message = 'Expected class name'.
ENDIF.
lv_class_nm = lr_token->token_value.
" Expect 'DEFINITION'
lr_token = get_next_token( ).
IF lr_token->token_value <> 'DEFINITION'.
RAISE EXCEPTION TYPE cx_sy_conversion_no_number
EXPORTING message = 'Expected DEFINITION keyword'.
ENDIF.
" Create class definition object
DATA(lr_class_def) = NEW lcl_class_definition( iv_name = lv_class_nm ).
" Parse until 'ENDCLASS.'
WHILE mv_index <= lines( mt_tokens ).
lr_token = peek_next_token( ).
IF lr_token IS INITIAL.
EXIT.
ENDIF.
IF lr_token->token_value = 'PUBLIC' OR lr_token->token_value = 'PROTECTED' OR lr_token->token_value = 'PRIVATE'.
" Handle visibility section
lr_token = get_next_token( ). " Consume visibility keyword
lr_token = get_next_token( ). " Should be 'SECTION'
IF lr_token->token_value <> 'SECTION'.
RAISE EXCEPTION TYPE cx_sy_conversion_no_number
EXPORTING message = 'Expected SECTION keyword'.
ENDIF.
" Parse attributes and methods
parse_attributes( CHANGING ct_attributes = lr_class_def->attributes ).
parse_methods( CHANGING ct_methods = lr_class_def->methods ).
ELSEIF lr_token->token_value = 'ENDCLASS'.
EXIT.
ELSE.
mv_index = mv_index + 1.
ENDIF.
ENDWHILE.
ro_class_def = lr_class_def.
ENDMETHOD.
METHOD parse_attributes.
DATA: lr_token TYPE REF TO lcl_token.
WHILE mv_index <= lines( mt_tokens ).
lr_token = peek_next_token( ).
IF lr_token IS INITIAL OR lr_token->token_value = 'METHODS' OR lr_token->token_value = 'ENDCLASS' OR lr_token->token_value CA 'PUBLICPROTECTEDPRIVATE'.
EXIT.
ENDIF.
IF lr_token->token_value = 'DATA'.
" Parse attribute
lr_token = get_next_token( ). " Consume 'DATA'
lr_token = get_next_token( ). " Attribute name
DATA(lv_attr_name) = lr_token->token_value.
" Skip 'TYPE' or 'LIKE'
lr_token = get_next_token( ). " 'TYPE' or 'LIKE'
lr_token = get_next_token( ). " Type name
DATA(lv_attr_type) = lr_token->token_value.
" For simplicity, we skip detailed parsing
APPEND NEW lcl_attribute( iv_name = lv_attr_name iv_type = lv_attr_type iv_comment = '' ) TO ct_attributes.
ELSE.
mv_index = mv_index + 1.
ENDIF.
ENDWHILE.
ENDMETHOD.
METHOD parse_methods.
DATA: lr_token TYPE REF TO lcl_token.
WHILE mv_index <= lines( mt_tokens ).
lr_token = peek_next_token( ).
IF lr_token IS INITIAL OR lr_token->token_value = 'ENDCLASS' OR lr_token->token_value CA 'PUBLICPROTECTEDPRIVATE'.
EXIT.
ENDIF.
IF lr_token->token_value = 'METHODS'.
" Parse method
lr_token = get_next_token( ). " Consume 'METHODS'
lr_token = get_next_token( ). " Method name
DATA(lv_method_name) = lr_token->token_value.
" For simplicity, we skip detailed parsing of parameters
DATA(lr_method) = NEW lcl_method( iv_name = lv_method_name iv_comment = '' ).
APPEND lr_method TO ct_methods.
ELSE.
mv_index = mv_index + 1.
ENDIF.
ENDWHILE.
ENDMETHOD.
ENDCLASS.
************************************************************************
* Main Program Logic
************************************************************************
START-OF-SELECTION.
" Example input as internal table of strings
DATA: lt_source_code TYPE STANDARD TABLE OF string.
" Read the source code into lt_source_code
APPEND 'CLASS zca_nn_neuron DEFINITION' TO lt_source_code.
APPEND ' PUBLIC' TO lt_source_code.
APPEND ' FINAL' TO lt_source_code.
APPEND ' CREATE PUBLIC.' TO lt_source_code.
APPEND ' PUBLIC SECTION.' TO lt_source_code.
APPEND ' DATA layer_number TYPE i.' TO lt_source_code.
APPEND ' DATA neuron_index TYPE zca_nn_neuid.' TO lt_source_code.
APPEND ' "! Constructor' TO lt_source_code.
APPEND ' METHODS constructor' TO lt_source_code.
APPEND ' IMPORTING' TO lt_source_code.
APPEND ' !iv_layernumber TYPE i' TO lt_source_code.
APPEND ' !iv_neuronindex TYPE i.' TO lt_source_code.
APPEND 'ENDCLASS.' TO lt_source_code.
" Create tokenizer and tokenize the source code
DATA(lo_tokenizer) = NEW lcl_tokenizer( ).
DATA(lt_tokens) = lo_tokenizer->tokenize( it_source_code = lt_source_code ).
" Create parser and parse tokens into class representation
DATA(lo_parser) = NEW lcl_parser( ).
DATA(lo_class_def) = lo_parser->parse( it_tokens = lt_tokens ).
" Output XML
DATA(lv_xml) = lo_class_def->to_xml( ).
cl_demo_output=>display( lv_xml ).
Explanation:
- Tokenization: The
lcl_tokenizer class reads each line of the source code, removes unnecessary characters, and splits it into tokens. It classifies each token as a keyword, identifier, symbol, comment, string, number, or unknown.
- Parsing: The
lcl_parser class parses the tokens to build an object representation of the class (lcl_class_definition). It creates attributes and methods based on the tokens. For simplicity, the parsing of parameters and method bodies is minimal and should be expanded to handle more complex cases.
- XML Generation: The
to_xml method of lcl_class_definition assembles the XML string by looping through the attributes and methods and concatenating their XML representations.
- Output: The XML is displayed using
cl_demo_output=>display.
Note: This code provides a basic framework and handles the parsing of class names, attributes, and methods at a high level. For a complete solution, you'll need to expand the parser to handle all possible ABAP class structures, including detailed parsing of types, comments, method parameters, and method implementations.
Important Considerations:
- Compiler Rules: Ensure that all ABAP syntax rules are followed in tokenization and parsing to avoid errors.
- Extensibility: The code is designed with extensibility in mind, using object-oriented principles and classes that can be expanded to handle additional features.
- Object-Oriented Approach: The solution uses classes to represent tokens, the tokenizer, the parser, and the class elements (attributes, methods, parameters), following best practices for object-oriented design in ABAP.
Example Output:
The program will output an XML representation of the class, similar to the example you provided.
<class>
<name>zca_nn_neuron</name>
<attribute>
<name>layer_number</name>
<type>i</type>
</attribute>
<attribute>
<name>neuron_index</name>
<type>zca_nn_neuid</type>
</attribute>
<method>
<name>constructor</name>
</method>
</class>
Next Steps:
To handle more complex ABAP code, you should:
- Expand Token Classification: Include all relevant keywords, symbols, and token types used in ABAP.
- Improve Parsing Logic: Enhance the parser to handle type definitions (
TYPES), detailed method parameters, comments, and method implementations.
- Handle Comments and Annotations: Parse comments and special annotations (like
! @parameter) to include them in the XML, as shown in your example.
- Error Handling: Add robust error handling to manage unexpected tokens or syntax errors in the source code.
By following this structure and expanding upon it, you'll create a robust tool that converts ABAP class source code into a detailed XML representation suitable for recreating the class or for documentation purposes.