Skip to content

Commit

Permalink
Support for fixed-format SQL references
Browse files Browse the repository at this point in the history
Signed-off-by: worksofliam <[email protected]>
  • Loading branch information
worksofliam committed Dec 9, 2024
1 parent 54dc3e5 commit f0d46ff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
25 changes: 23 additions & 2 deletions language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ export default class Parser {
}
};

let fixedExec = false;

for (let li = 0; li < lines.length; li++) {
if (li >= 1) {
lineIndex += lines[li-1].length + (eol === `\r\n` ? 2 : 1);
Expand All @@ -427,7 +429,7 @@ export default class Parser {
const scope = scopes[scopes.length - 1];

let baseLine = lines[li];
let spec;
let spec: string|undefined;

lineIsFree = false;
lineNumber += 1;
Expand All @@ -443,14 +445,19 @@ export default class Parser {
const comment = baseLine[6];
spec = baseLine[5].toUpperCase();

if ([spec, comment].includes(`*`) || [`*`, `+`].includes(comment)) {
if ([spec, comment].includes(`*`)) {
continue;
}

if (comment === `/`) {
// Directives can be parsed by the free format parser
baseLine = ``.padEnd(6) + baseLine.substring(6);
lineIsFree = true;
} else if (comment === `+` && fixedExec && currentStmtStart.content) {
// Fixed format EXEC SQL
baseLine = ``.padEnd(7) + baseLine.substring(7);
currentStmtStart.content += baseLine + ''.padEnd(eol.length);
continue;
} else {
if (spec === ` `) {
//Clear out stupid comments
Expand Down Expand Up @@ -498,6 +505,20 @@ export default class Parser {
return;
} else {
switch (parts[0]) {
case '/EXEC':
fixedExec = true;
baseLine = ``.padEnd(7) + baseLine.substring(7);
currentStmtStart = {
line: lineNumber,
index: lineIndex,
content: baseLine + ''.padEnd(eol.length)
}
continue;
case '/END':
line = `;`;
baseLine = ``.padEnd(baseLine.length) + `;`;
fixedExec = false;
break;
case `/COPY`:
case `/INCLUDE`:
if (options.withIncludes && this.includeFileFetch && lineCanRun()) {
Expand Down
2 changes: 1 addition & 1 deletion language/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const commonMatchers: Matcher[] = [
{ type: `divide` },
{
type: `word`,
match: (word) => [`TITLE`, `EJECT`, `SPACE`, `COPY`, `INCLUDE`, `SET`, `RESTORE`, `OVERLOAD`, `DEFINE`, `UNDEFINE`, `IF`, `ELSE`, `ELSEIF`, `ENDIF`, `EOF`, `CHARCOUNT`].includes(word.toUpperCase())
match: (word) => [`TITLE`, `EJECT`, `SPACE`, `COPY`, `INCLUDE`, `SET`, `RESTORE`, `OVERLOAD`, `DEFINE`, `UNDEFINE`, `IF`, `ELSE`, `ELSEIF`, `ENDIF`, `EOF`, `CHARCOUNT`, `EXEC`, `END`].includes(word.toUpperCase())
},
],
becomes: {
Expand Down
11 changes: 10 additions & 1 deletion tests/suite/references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,16 @@ test('references_14_fixed_3', async () => {
const cache = await parser.getDocs(uri, lines, { ignoreCache: true, withIncludes: true, collectReferences: true });

const per = cache.find(`per`);
expect(per.references.length).toBe(6);
expect(per.references.length).toBe(7);

// for (const ref of per.references) {
// console.log({
// ref,
// text: lines.substring(ref.offset.position, ref.offset.end),
// about: lines.substring(ref.offset.position - 10, ref.offset.end + 10)
// })
// }

expect(per.references.every(ref => lines.substring(ref.offset.position, ref.offset.end) === `per`)).toBe(true);

const pmyy = cache.find(`pmyy`);
Expand Down

0 comments on commit f0d46ff

Please sign in to comment.