Skip to content

Commit 56308f1

Browse files
EastLordkaby76
andauthored
[PlSql] Add support for CREATE SCHEMA (#4436)
* Update to version 0.23.15 of Trash Toolkit. * Update dotnet-tools.json * Update to 0.23.16, add trapc. * Remaster due to changed grammar. * Remove all broken ports for antlr/antlr4. All ports need to be rewritten in target agnostic format. * Remove testing of PHP for trapc--PHP is not a reliable runtime. Add antlr-ng generator for Antlr4ng testing. * Update grammar to be target agnostic. Antlr4 "@Header" blocks are target specific. But, we need this to work for any target. So, we use a comment in the grammar and transformGrammar.py to fix up the problem. _insideOptionsBlock is removed because it's not used anywhere. Untabify source code--tabs are presentation specific and thus depend on the editor used to open it. We don't use the same editors people. * Create LexerAdaptor.ts * Create transformGrammar.py * Update LexerAdaptor.ts * Fix missing if-then-else. * Add Antlr4ng port. * Add Cpp target. * Update to latest of Trash for Cpp testing. * Initial version of Dart port--does not work yet. * Fix Dart port. * [PlSql] Add support for CREATE SCHEMA * Add TypeScript port. * Add Python3 port. --------- Co-authored-by: Ken Domino <[email protected]>
1 parent 2336436 commit 56308f1

File tree

17 files changed

+1036
-139
lines changed

17 files changed

+1036
-139
lines changed

.config/dotnet-tools.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,189 +3,189 @@
33
"isRoot": true,
44
"tools": {
55
"trcaret": {
6-
"version": "0.23.16",
6+
"version": "0.23.17",
77
"commands": [
88
"trcaret"
99
],
1010
"rollForward": false
1111
},
1212
"trcover": {
13-
"version": "0.23.16",
13+
"version": "0.23.17",
1414
"commands": [
1515
"trcover"
1616
],
1717
"rollForward": false
1818
},
1919
"trgen": {
20-
"version": "0.23.16",
20+
"version": "0.23.17",
2121
"commands": [
2222
"trgen"
2323
],
2424
"rollForward": false
2525
},
2626
"trglob": {
27-
"version": "0.23.16",
27+
"version": "0.23.17",
2828
"commands": [
2929
"trglob"
3030
],
3131
"rollForward": false
3232
},
3333
"triconv": {
34-
"version": "0.23.16",
34+
"version": "0.23.17",
3535
"commands": [
3636
"triconv"
3737
],
3838
"rollForward": false
3939
},
4040
"trparse": {
41-
"version": "0.23.16",
41+
"version": "0.23.17",
4242
"commands": [
4343
"trparse"
4444
],
4545
"rollForward": false
4646
},
4747
"trquery": {
48-
"version": "0.23.16",
48+
"version": "0.23.17",
4949
"commands": [
5050
"trquery"
5151
],
5252
"rollForward": false
5353
},
5454
"trtext": {
55-
"version": "0.23.16",
55+
"version": "0.23.17",
5656
"commands": [
5757
"trtext"
5858
],
5959
"rollForward": false
6060
},
6161
"trwdog": {
62-
"version": "0.23.16",
62+
"version": "0.23.17",
6363
"commands": [
6464
"trwdog"
6565
],
6666
"rollForward": false
6767
},
6868
"trxgrep": {
69-
"version": "0.23.16",
69+
"version": "0.23.17",
7070
"commands": [
7171
"trxgrep"
7272
],
7373
"rollForward": false
7474
},
7575
"trxml": {
76-
"version": "0.23.16",
76+
"version": "0.23.17",
7777
"commands": [
7878
"trxml"
7979
],
8080
"rollForward": false
8181
},
8282
"trxml2": {
83-
"version": "0.23.16",
83+
"version": "0.23.17",
8484
"commands": [
8585
"trxml2"
8686
],
8787
"rollForward": false
8888
},
8989
"trclonereplace": {
90-
"version": "0.23.16",
90+
"version": "0.23.17",
9191
"commands": [
9292
"trclonereplace"
9393
],
9494
"rollForward": false
9595
},
9696
"trcombine": {
97-
"version": "0.23.16",
97+
"version": "0.23.17",
9898
"commands": [
9999
"trcombine"
100100
],
101101
"rollForward": false
102102
},
103103
"trconvert": {
104-
"version": "0.23.16",
104+
"version": "0.23.17",
105105
"commands": [
106106
"trconvert"
107107
],
108108
"rollForward": false
109109
},
110110
"trfoldlit": {
111-
"version": "0.23.16",
111+
"version": "0.23.17",
112112
"commands": [
113113
"trfoldlit"
114114
],
115115
"rollForward": false
116116
},
117117
"trgenvsc": {
118-
"version": "0.23.16",
118+
"version": "0.23.17",
119119
"commands": [
120120
"trgenvsc"
121121
],
122122
"rollForward": false
123123
},
124124
"tritext": {
125-
"version": "0.23.16",
125+
"version": "0.23.17",
126126
"commands": [
127127
"tritext"
128128
],
129129
"rollForward": false
130130
},
131131
"trjson": {
132-
"version": "0.23.16",
132+
"version": "0.23.17",
133133
"commands": [
134134
"trjson"
135135
],
136136
"rollForward": false
137137
},
138138
"trperf": {
139-
"version": "0.23.16",
139+
"version": "0.23.17",
140140
"commands": [
141141
"trperf"
142142
],
143143
"rollForward": false
144144
},
145145
"trrename": {
146-
"version": "0.23.16",
146+
"version": "0.23.17",
147147
"commands": [
148148
"trrename"
149149
],
150150
"rollForward": false
151151
},
152152
"trsort": {
153-
"version": "0.23.16",
153+
"version": "0.23.17",
154154
"commands": [
155155
"trsort"
156156
],
157157
"rollForward": false
158158
},
159159
"trsplit": {
160-
"version": "0.23.16",
160+
"version": "0.23.17",
161161
"commands": [
162162
"trsplit"
163163
],
164164
"rollForward": false
165165
},
166166
"trsponge": {
167-
"version": "0.23.16",
167+
"version": "0.23.17",
168168
"commands": [
169169
"trsponge"
170170
],
171171
"rollForward": false
172172
},
173173
"trtokens": {
174-
"version": "0.23.16",
174+
"version": "0.23.17",
175175
"commands": [
176176
"trtokens"
177177
],
178178
"rollForward": false
179179
},
180180
"trtree": {
181-
"version": "0.23.16",
181+
"version": "0.23.17",
182182
"commands": [
183183
"trtree"
184184
],
185185
"rollForward": false
186186
},
187187
"trunfold": {
188-
"version": "0.23.16",
188+
"version": "0.23.17",
189189
"commands": [
190190
"trunfold"
191191
],

antlr/antlr4/ANTLRv4Lexer.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ options {
5454
tokenVocab = predefined;
5555
}
5656

57+
// Insert here @header for lexer.
5758

5859
// Standard set of fragments
5960
tokens {
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) Mike Lischke. All rights reserved.
3+
* Licensed under the BSD 3-clause License. See License.txt in the project root for license information.
4+
*/
5+
6+
import { CharStream, Lexer, Token } from "antlr4ng";
7+
8+
import { ANTLRv4Lexer } from "./ANTLRv4Lexer.js";
9+
10+
export abstract class LexerAdaptor extends Lexer {
11+
/** Generic type for OPTIONS, TOKENS and CHANNELS */
12+
private static PREQUEL_CONSTRUCT = -10;
13+
private static OPTIONS_CONSTRUCT = -11;
14+
15+
private currentRuleType: number = Token.INVALID_TYPE;
16+
17+
public constructor(input: CharStream) {
18+
super(input);
19+
20+
/**
21+
* Track whether we are inside of a rule and whether it is lexical parser. _currentRuleType==Token.INVALID_TYPE
22+
* means that we are outside of a rule. At the first sign of a rule name reference and _currentRuleType
23+
* ==invalid, we can assume that we are starting a parser rule. Similarly, seeing a token reference when not
24+
* already in rule means starting a token rule. The terminating ';' of a rule, flips this back to invalid type.
25+
*
26+
* This is not perfect logic but works. For example, "grammar T;" means that we start and stop a lexical rule
27+
* for the "T;". Dangerous but works.
28+
*
29+
* The whole point of this state information is to distinguish between [..arg actions..] and [char sets].
30+
* Char sets can only occur in lexical rules and arg actions cannot occur.
31+
*/
32+
this.currentRuleType = Token.INVALID_TYPE;
33+
}
34+
35+
public override reset(): void {
36+
this.currentRuleType = Token.INVALID_TYPE;
37+
super.reset();
38+
}
39+
40+
public override emit(): Token {
41+
if ((this.type === ANTLRv4Lexer.OPTIONS || this.type === ANTLRv4Lexer.TOKENS
42+
|| this.type === ANTLRv4Lexer.CHANNELS)
43+
&& this.currentRuleType === Token.INVALID_TYPE) {
44+
// Enter prequel construct ending with an RBRACE.
45+
this.currentRuleType = LexerAdaptor.PREQUEL_CONSTRUCT;
46+
} else if (this.type === ANTLRv4Lexer.OPTIONS && this.currentRuleType === ANTLRv4Lexer.TOKEN_REF) {
47+
this.currentRuleType = LexerAdaptor.OPTIONS_CONSTRUCT;
48+
} else if (this.type === ANTLRv4Lexer.RBRACE
49+
&& this.currentRuleType === LexerAdaptor.PREQUEL_CONSTRUCT) {
50+
// Exit prequel construct.
51+
this.currentRuleType = Token.INVALID_TYPE;
52+
} else if (this.type === ANTLRv4Lexer.RBRACE
53+
&& this.currentRuleType === LexerAdaptor.OPTIONS_CONSTRUCT) {
54+
// Exit options.
55+
this.currentRuleType = ANTLRv4Lexer.TOKEN_REF;
56+
} else if (this.type === ANTLRv4Lexer.AT && this.currentRuleType === Token.INVALID_TYPE) {
57+
// Enter action.
58+
this.currentRuleType = ANTLRv4Lexer.AT;
59+
} else if (this.type === ANTLRv4Lexer.SEMI
60+
&& this.currentRuleType === LexerAdaptor.OPTIONS_CONSTRUCT) {
61+
// ';' in options { .... }. Don't change anything.
62+
} else if (this.type == ANTLRv4Lexer.END_ACTION && this.currentRuleType == ANTLRv4Lexer.AT) {
63+
// exit action
64+
this.currentRuleType = Token.INVALID_TYPE;
65+
} else if (this.type === ANTLRv4Lexer.ID) {
66+
const firstChar = this.inputStream.getTextFromRange(this.tokenStartCharIndex, this.tokenStartCharIndex);
67+
const c = firstChar.charAt(0);
68+
if (c === c.toUpperCase()) {
69+
this.type = ANTLRv4Lexer.TOKEN_REF;
70+
} else {
71+
this.type = ANTLRv4Lexer.RULE_REF;
72+
}
73+
74+
// If outside of rule def.
75+
if (this.currentRuleType === Token.INVALID_TYPE) {
76+
// Set to inside lexer or parser rule.
77+
this.currentRuleType = this.type;
78+
}
79+
} else if (this.type === ANTLRv4Lexer.SEMI) {
80+
// Exit rule def.
81+
this.currentRuleType = Token.INVALID_TYPE;
82+
}
83+
84+
return super.emit();
85+
}
86+
87+
protected handleBeginArgument(): void {
88+
if (this.currentRuleType === ANTLRv4Lexer.TOKEN_REF) {
89+
this.pushMode(ANTLRv4Lexer.LexerCharSet);
90+
this.more();
91+
} else {
92+
this.pushMode(ANTLRv4Lexer.Argument);
93+
}
94+
}
95+
96+
protected handleEndArgument(): void {
97+
this.popMode();
98+
if (this.modeStack.length > 0) {
99+
this.type = ANTLRv4Lexer.ARGUMENT_CONTENT;
100+
}
101+
}
102+
103+
protected handleEndAction(): void
104+
{
105+
var oldMode = this.mode;
106+
var newMode = this.popMode();
107+
var isActionWithinAction = this.modeStack.length > 0
108+
&& newMode == ANTLRv4Lexer.TargetLanguageAction
109+
&& oldMode == newMode;
110+
111+
if (isActionWithinAction) {
112+
this.type = ANTLRv4Lexer.ACTION_CONTENT;
113+
}
114+
}
115+
}
116+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""The script transforms the grammar to fit for the c++ target """
2+
import sys
3+
import re
4+
import shutil
5+
from glob import glob
6+
from pathlib import Path
7+
8+
def main():
9+
"""Executes the script."""
10+
for file in glob("./*.g4"):
11+
transform_grammar(file)
12+
13+
def transform_grammar(file_path):
14+
"""Transforms the grammar to fit for the target"""
15+
print("Altering " + file_path)
16+
if not Path(file_path).is_file:
17+
print(f"Could not find file: {file_path}")
18+
sys.exit(1)
19+
20+
shutil.move(file_path, file_path + ".bak")
21+
with open(file_path + ".bak",'r', encoding="utf-8") as input_file:
22+
with open(file_path, 'w', encoding="utf-8") as output_file:
23+
for line in input_file:
24+
line = re.sub(r"(\/\/ Insert here @header for lexer\.)",\
25+
'@header {import { LexerAdaptor } from "./LexerAdaptor.js"}', line)
26+
output_file.write(line)
27+
28+
print("Writing ...")
29+
30+
if __name__ == '__main__':
31+
main()

0 commit comments

Comments
 (0)