SCAN ( SAP ABAP Keyword )

SCAN is a keyword used in SAP ABAP programming.This tutorial covers its introduction & syntax details.

SCAN

Basic form
SCAN ABAP-SOURCE itab1 TOKENS INTO itab2
STATEMENTS INTO itab3.

Additions
1. … FROM n1
2. … TO n2
3. … KEYWORDS FROM itab4
4. … LEVELS INTO itab5
5. … OVERFLOW INTO c1
6. … WITH ANALYSIS
7. … WITH COMMENTS
8. … WITH INCLUDES
9. … WITHOUT TRMAC
10. … PROGRAM FROM c2
11. … INCLUDE INTO c3
12. … MESSAGE INTO c4
13. … WORD INTO c5
14. … LINE INTO n3
15. … OFFSET INTO n4

Effect
Breaks
down the ABAP/4 source code in the source code table itab1 into tokens
according to the rules of the ABAP/4 scanner. The tokens are written –
one per line – to the token table itab2 .

The token table itab2
must have the structure STOKEN . (If you specify the addition WITH
ANALYSIS , the token table must have the extended structure STOKEX .)

Normally,
comments are filtered out and subordinate source code units (included
programs, called macros) are ignored. If you want to include these
items, use the additions WITH COMMENTS and WITH ANALYSIS .

In
addition to classifying the source code by token, the scanner organizes
the tokens themselves into statements – using the colon-comma logic to
form chain records – and the statement table itab3 contains a statement
description on each line. Here, a three-part chain record “a: b, c1 c2,
d.” results in three entries “a b,”, “a c1 c2,” and “a d.” in the
statement table itab3 .

The statement table itab3 must have the structure SSTMNT .

The
statement classification characters colon, comma and period are not
written to the token table itab2 . Instead, the table itab3 contains
details about the position of a colon or the type (comma or period and
position of the end marker in the statement description.

The return code value is set as follows:

SY-SUBRC = 0 Source code table is not empty, contains no errors and is broken down into tokens.
SY-SUBRC
= 1 Source code table is not empty and is broken down into tokens, but
at least one include program does not exist (can occur only in
connection with the addition WITH INCLUDES ).
SY-SUBRC = 2 Source code table itab1 is empty or a blank line range was selected (applies to the additions FROM and TO ).
SY_SUBRC = 4 Scanner detects error in source code.
SY-SUBRC = 8 Other error or RABAX in scanner.
The fields of the structure STOKEN , and thus the columns of the token table itab2 , have the following meaning:
TYPE Type of token with possible values:

I (Identifier)
S (String, i.e. character literal)
L (List, enclosed in parentheses)
C (Comment)
ROW Number of line where token occurs or where it begins (>= 1)
COL Offset of first character of token relative to start of line (>= 0)
LEN Length of token
STR Character string forming the token (or just first part)
OVFL Overflow flag for field STR with the following possible values:

SPACE (no overflow, token fits completely in field STR )
X (overflow, either not resolved (no overflow are specified) or token fits in overflow area c1 ))
O (overflow of token and overflow of overflow area c1 )
OFF1 Offset in overflow area, if

token does not fit completely in field STR and
an overlfow area c1 is specified and
token fits completely in overflow area c1 .
The fields of the structure SSTMNT , and thus the columns of the statement table itab3 , have the following meaning:
TYPE Type of statement with the following possible values:

E (Native SQL statement between EXEC SQL and
ENDEXEC )
I ( INCLUDE prog )
J ( INCLUDE prog , prog does not exist, can
occur only in connection with the addition
WITH INCLUDES )
R (Call a macro from table TRMAC )
D (Call an internally defined macro with DEFINE )
M (Macro definition between DEFINE and
END-OF-DEFINITION )
C ( COMPUTE statement, sometimes without
COMPUTE as first token)
K (Other ABAP/4 key word)
N (Blank statement)
U (Unknown, non-blank statement)
LEVEL Index of source code unit in the level table itab5 (>= 1, if level table specified, otherwise 0)
FROM Index of first token of statement in the token table itab2
TO
Index of last token of statement in the token table itab2 (the end
marker of the statement – comma or period – counts as no more then a
token)
NUMBER Statement counter in a source code unit. Covers all
statements, regardless of how many are actually selected – in cases
where a key word table itab4 is specified
PREFIXLEN Number of tokens before the colon (with chain statements >= 1, otherwise 0)
COLONROW Line number of colon (with chain statements >= 1, otherwise 0)
COLONCOL Column position of colon (with chain statements >= 0, otheriwse 0)
TERMINATOR
End marker of a statement (normally a period or a comma, but SPACE in
the case of native SQL statements and internal macro definitions)
TROW Line number of end marker (>= 1, if TERMINATOR <> SPACE , otherwise 0)
TCOL Column position of end marker (>= 0, if TERMINATOR <> SPACE , otherwise 0)

Notes
When
expanding macro calls, no position specifications are available. The
relevant fields in the token table itab2 and in the statement table
itab3 are then set to 0.
Unlike the usual syntax check, the following are not treated as errors:
Comma without preceding colon (the comma then acts as an end marker),
Open chain statement sequence at end of source code, i.e. the last statement is closed by a comma, not by a period,
Open statement at end of source code, i.e. the last statement is closed neither by a period nor by a comma.

To
be able to analyze errors without modifying programs, use the additions
INCLUDE , MESSAGE , WORD , LINE and OFFSET . These provide information
about the errors which have occurred.

Addition 1
… FROM n1
Addition 2
… TO n2

Effect
Breaks down the source code table itab1 into tokens not from start to finish, but only from line n1 to line n2 .

The additions FROM n1 and TO n2 must follow specification of the source code table itab1 – in this order.

Notes
When
using the start specification n1 , use the addition WITHOUT TRMAC to
ensure that there are no unnecessary database accesses to the table
TRMAC .
The end specification n2 is treated as “soft”, i.e. a
statement that begins on a line <= n2 , but ends only on a line >
n2 , is not returned completely.
If the end specification n2 is
split in a chain statment, only the split part up to the next comma is
returned completely, not the entire chain statement up to the next
period.
Negative line specifications are not allowed and result in a runtime error.
A line specification of 0 amounts essentially to no specification.
If n1 number of lines in source code table, the scanner is not called ( SY-SUBRC = 2).
If n1 > n2 and n2 > 0, the scanner is not called ( SY-SUBRC = 2).

Addition 3
… KEYWORDS FROM itab4

Effect
Does not return all statements, only those specified in the key word table itab4 .

If the key word table is empty (i.e. it contains 0 lines), all the statements are selected.

The lines of the key word table are treated as a character field.

To
select a native SQL statement or a macro definition, you can specify
the pseudo key words EXEC_SQL or DEFINE_MACRO . It makes no difference
whether these are intercepted. Native SQL statements and macro
definitions are returned as statements (of type E or M whenever the
expansion of a macro definition results in more than one statement.

If the key word contains a blank line, blank statements are also selected.

Addition 4
… LEVELS INTO itab5

Effect
Stores
details about each edited source code unit (source code table itab1
itself, expanded include programs, expanded macro definitions) in the
level table itab5 .

Specification of a level table makes sense only with the addition WITH INCLUDES .

The level table itab5 must have the structure SLEVEL .
The fields of the structure SLEVEL – and consequently the columns of the level table itab5 have the following meaning:
TYPE Type of source code unit with the following possible values:

P (Program)
D (Internal DEFINE macro)
R (Macro from table TRMAC )
NAME Name of source code unit (name of include program, macro name)
DEPTH Current nesting depth of source code unit (>= 1)
LEVEL Index of superior (i.e. included or called) source code unit in the level table (>= 1, if DEPTH >= 2, otherwise 0)
STMNT Index of superior (d.h. included or called) statement in the statement table (>= 1, if DEPTH >= 2, otherwise 0)
FROM Index of first statement of source code unit in the statement table (>= 1)
TO Index of last statement of source code unit in the statement table (>= 1)
If
the source code unit contains include programs or macro calls, the line
range [ FROM, TO ] in the statement table also covers the statements in
subordinate source code units.

Addition 5
… OVERFLOW INTO c1

Effect
If
a token is too large to be stored in the token table in the field STR ,
it is placed in the overflow area c1 . The offset of the token in the
overflow area then lies in the token table in the field OFF1 .

Addition 6
… WITH ANALYSIS

Effect
Breaks down each token t = a+b(c) according to the logic of the RSYN key word >ANALY into its three components a, b and c.

Offset
and length of components a, b and c are stored in the fields OFF1 ,
LEN1 , OFF2 , LEN2 , OFF3 and LEN3 in the token table.

If you
specify the addition WITH ANALYSIS , the token table itab2 must have
the structure STOKEX , so that the fields LEN1 , OFF2 , LEN2 , OFF3 and
LEN3 are available.

If the whole token exists in the token
table, the offset specifications are relative to the token start. If
the token is in the overflow area c1 , the offset specifications are
relative to the start of the overflow area.

Addition 7
… WITH COMMENTS

Effect
Returns comments also, with each individual comment representing a token.

Note
The addition … WITH COMMENTS is unfortunately not supported at present!

Addition 8
… WITH INCLUDES

Effect
Also breaks down subordinate source code units (included programs, called macros) into tokens.

You should normally combine the addition WITH INCLUDES with the addition LEVELS INTO itab5 .

Note
If
(at least) one included program does not exist, SY-SUBRC is set to 1
and the relevant INCLUDE statement is flagged in the statement table
itab3 by the statement type J (instead of I). Otherwise, the breakdown
process continues. The level table itab5 contains no entry for include
programs that do not exist.
If you combine WITH INCLUDES with
WITHOUT TRMAC , TRMAC macros are not expanded because the system does
not recognize them as subordinate source code units.
When macro
calls are expanded, no position specifications are available. The
fields in the token table itab2 and the statement table itab3 are then
set to 0.

Addition 9
… WITHOUT TRMAC

Effect
If a
statement begins neither with an ABAP/4 key word nor with a DEFINE
macro, the system does not check whether this is a TRMAC macro, but
assumes an unknown statement. (Unknown statements are flagged in the
statement table itab3 with a U in the field TYPE .)

To avoid
unnecessary database accesses to the table TRMAC , you should use the
addition WITHOUT TRMAC whenever you want to assume that the source code
to be scanned contains unknown statements. Unknown statements are
particularly likely to occur if you use the addition FROM n1 . In this
case, the scanner does not start at the beginning of the source code,
but from a specified point.

Note
If you use WITHOUT TRMAC
with WITH INCLUDES , TRMAC macros are not expanded because the system
does not recognize them as subordinate source code units.

Addition 10
… PROGRAM FROM c2
Addition 11
… INCLUDE INTO c3
Addition 12
… MESSAGE INTO c4
Addition 13
… WORD INTO c5
Addition 14
… LINE INTO n3
Addition 15
… OFFSET INTO n4

Effect
The
above additions have the same meaning as the those for the SYNTAX-CHECK
statement: c2 is an input field for a program name to be assigned to
the source code, while the fields c3, c4, c5, n3 and n4 are output
fields in case an error occurs.

To be able to analyze errors
without modifying programs, use the additions INCLUDE , MESSAGE , WORD
, LINE and OFFSET . These provide information about the errors which
have occurred.