Skip to content

Instantly share code, notes, and snippets.

@furlan
Created May 10, 2011 12:55
Show Gist options
  • Save furlan/964413 to your computer and use it in GitHub Desktop.
Save furlan/964413 to your computer and use it in GitHub Desktop.
SCAN ABAP SOURCE
form fetch_source_code.
data:
lc_table(10) type c, " table name
lc_program(30) type c, " program name
ln_statement(05) type n, " statement counter
lc_field(10) type c, " field name
li_discard type i, " discard analysis
li_sourcestart type i, " source statement start line
li_sourceend type i, " source statement end line
li_table type i, " TABLE token line
li_tokenstart type i, " token statement start line
li_tokenend type i, " token statement end line
* tabelas para análise do código fonte
lh_source like sy-entry occurs 0 with header line,
lh_tokens like stoken occurs 0 with header line,
lh_statements like sstmnt occurs 0 with header line.
select * from d010sinf into table t_prog where prog in p_prog.
loop at t_prog.
* carrega o código fonte do programa
read report t_prog-prog into lh_source.
* quebra o código fonte em tokens utilizando o analisador léxico do ABAP
scan abap-source lh_source
tokens into lh_tokens
statements into lh_statements.
* se o programa não contém erros de sintaxe
if sy-subrc = 0.
* para todos os comandos
loop at lh_tokens.
* tratamento LIKE ou FOR
if lh_tokens-str = cc_like or lh_tokens-str = cc_for.
* determina a posição da provável tabela/campo
li_tokenstart = sy-tabix + 1.
read table lh_tokens index li_tokenstart.
if lh_tokens-str ca '-'.
move: t_prog-prog to t_analise-progname,
lh_tokens-row to t_analise-linenr.
split lh_tokens-str at '-' into t_analise-tabname
t_analise-fieldname.
append t_analise.
endif.
endif.
* tratamento para INCLUDE STRUCTURE
if lh_tokens-str = cc_struct.
* determina a posição do nome da tabela
li_tokenstart = sy-tabix + 1.
read table lh_tokens index li_tokenstart.
move: t_prog-prog to t_stables-progname,
lh_tokens-row to t_stables-linenr,
lh_tokens-str to t_stables-tabname,
lh_tokens-str to t_struct-tabname.
append: t_stables, t_struct.
endif.
* se for um comando EXEC SQL desliga a análise
if lh_tokens-str = cc_exec.
li_discard = 1.
endif.
* se for um comando ENDEXEC liga a análise
if lh_tokens-str = cc_endexec.
li_discard = 0.
endif.
* se for um comando SELECT e a análise estiver ligada
if lh_tokens-str = cc_select and li_discard = 0.
* incrementa o contador de statements
ln_statement = ln_statement + 1.
* determina a linha inicial do statement corrente na tabela de TOKENS
li_tokenstart = sy-tabix.
* determina a linha inicial do statement corrente no código fonte
li_sourcestart = lh_tokens-row.
* determina a linha final do statement corrente no código fonte
read table lh_statements
with key from = li_tokenstart terminator = '.'.
if sy-subrc <> 0.
read table lh_statements
with key from = li_tokenstart terminator = space.
endif.
read table lh_tokens index lh_statements-to.
li_sourceend = lh_tokens-row.
* determina a linha final do statement corrente na tabela de TOKENS
li_tokenend = sy-tabix.
* determina o nome da tabela acessada após o FROM ou o JOIN
loop at lh_tokens from li_tokenstart to li_tokenend
where str = cc_from
or str = cc_join.
li_table = sy-tabix + 1.
read table lh_tokens index li_table.
if lh_tokens-str+0(1) = '*'.
lc_table = lh_tokens-str+1.
else.
lc_table = lh_tokens-str.
endif.
* armazena informações da tabela na instrução SELECT em t_table.
move: t_prog-prog to t_tables-progname,
lh_tokens-row to t_tables-linenr,
lc_table to t_tables-tabname.
append t_tables.
endloop.
endif.
endloop.
endif.
endloop.
sort: t_analise, t_tables.
delete adjacent duplicates from t_analise.
delete adjacent duplicates from t_tables.
endform. " FETCH_SOURCE_CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment