1
- import * as abaplint from "@abaplint/core" ;
2
-
3
1
// https://www.w3schools.com/js/js_reserved.asp
4
2
export const DEFAULT_KEYWORDS : string [ ] = [
5
3
"abstract" , "arguments" , "await" ,
@@ -21,99 +19,3 @@ export const DEFAULT_KEYWORDS: string[] = [
21
19
"delete" ,
22
20
"volatile" , "while" , "yield" ] ;
23
21
// "with"
24
-
25
- /** Replaces javascript keywords in ABAP source code, in-memory only */
26
- export class Keywords {
27
- private readonly keywords : string [ ] = [ ] ;
28
-
29
- public constructor ( keywords ?: string [ ] ) {
30
- if ( keywords !== undefined ) {
31
- this . keywords = keywords ;
32
- } else {
33
- this . keywords = DEFAULT_KEYWORDS ;
34
- }
35
- }
36
-
37
- public handle ( reg : abaplint . IRegistry ) {
38
- if ( this . keywords . length === 0 ) {
39
- return ;
40
- }
41
- reg . parse ( ) ;
42
-
43
- for ( const o of reg . getObjects ( ) ) {
44
- if ( ! ( o instanceof abaplint . ABAPObject ) ) {
45
- continue ;
46
- }
47
- for ( const f of o . getABAPFiles ( ) ) {
48
- const tokens : abaplint . Token [ ] = [ ] ;
49
- for ( const s of f . getStatements ( ) ) {
50
- if ( s . get ( ) instanceof abaplint . MacroCall ) {
51
- tokens . push ( ...this . handleMacro ( s ) ) ;
52
- } else {
53
- tokens . push ( ...this . traverse ( s , f ) ) ;
54
- }
55
- }
56
- if ( tokens . length === 0 ) {
57
- continue ;
58
- }
59
- const rows = f . getRawRows ( ) ;
60
- for ( const t of tokens . reverse ( ) ) {
61
- const original = rows [ t . getRow ( ) - 1 ] ;
62
- const index = t . getEnd ( ) . getCol ( ) - 1 ;
63
- rows [ t . getRow ( ) - 1 ] = original . substring ( 0 , index ) + "_" + original . substring ( index ) ;
64
- }
65
- reg . updateFile ( new abaplint . MemoryFile ( f . getFilename ( ) , rows . join ( "\n" ) ) ) ;
66
- }
67
- }
68
-
69
- reg . parse ( ) ;
70
- }
71
-
72
- private handleMacro ( node : abaplint . Nodes . StatementNode ) : abaplint . Token [ ] {
73
- const tokens : abaplint . Token [ ] = [ ] ;
74
-
75
- for ( const token of node . getTokens ( ) ) {
76
- for ( const k of this . keywords ) {
77
- const lower = token . getStr ( ) . toLowerCase ( ) ;
78
- if ( k === lower
79
- || "!" + k === lower
80
- || lower . endsWith ( "~" + k ) ) {
81
- tokens . push ( token ) ;
82
- }
83
- }
84
- }
85
-
86
- return tokens ;
87
- }
88
-
89
- private traverse ( node : abaplint . INode , file : abaplint . ABAPFile ) : abaplint . Token [ ] {
90
-
91
- const ret : abaplint . Token [ ] = [ ] ;
92
- for ( const c of node . getChildren ( ) ) {
93
- if ( c instanceof abaplint . Nodes . TokenNodeRegex ) {
94
- const token = c . getFirstToken ( ) ;
95
-
96
- const start = token . getStart ( ) ;
97
- if ( start instanceof abaplint . VirtualPosition ) {
98
- continue ;
99
- }
100
- for ( const k of this . keywords ) {
101
- const lower = token . getStr ( ) . toLowerCase ( ) ;
102
- if ( k === lower
103
- || "!" + k === lower
104
- || lower . endsWith ( "~" + k ) ) {
105
- ret . push ( token ) ;
106
- break ;
107
- }
108
- }
109
- } else if ( c instanceof abaplint . Nodes . TokenNode ) {
110
- continue ;
111
- } else {
112
- ret . push ( ...this . traverse ( c , file ) ) ;
113
- }
114
- }
115
-
116
- return ret ;
117
- }
118
-
119
- }
0 commit comments