Skip to content

Commit

Permalink
Fixes for lines with semi-colon
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 9f25251 commit 9655dc7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
17 changes: 15 additions & 2 deletions language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,14 @@ export default class Parser {
}

const getValidStatement = (inputLine: string, withSep?: boolean) => {
const sep = inputLine.indexOf(`;`);
const comment = inputLine.indexOf(`//`);
const quote = inputLine.lastIndexOf(`'`);
const sep = inputLine.indexOf(`;`, quote >= 0 ? quote : 0);

if (comment >= 0 && comment < sep) {
return inputLine;
}

return (sep >= 0 ? inputLine.substring(0, sep + (withSep ? 1 : 0)) : inputLine);
}

Expand Down Expand Up @@ -633,7 +640,13 @@ export default class Parser {
currentStmtStart = {line: lineNumber, index: lineIndex};
}

if (!lineIsComment) {
if (lineIsComment) {
// This happens when we put a comment on a line which is part of one long statement.
// See references_24_comment_in_statement
if (currentStmtStart.content) {
currentStmtStart.content += ``.padEnd(baseLine.length) + LINEEND;
}
} else {
if (stripComment(line).endsWith(`;`)) {

if (currentStmtStart.content) {
Expand Down
91 changes: 91 additions & 0 deletions tests/suite/references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1477,4 +1477,95 @@ test('references_23_before_spaces', async () => {
const id = cache.find(`id`);
expect(id.references.length).toBe(2);
expect(id.references.every(ref => lines.substring(ref.offset.position, ref.offset.end) === `id`)).toBe(true);
});

// Test case is from rpgle-repl
test('references_24_comment_in_statement', async () => {
const lines = [
`**free`,
``,
`dcl-proc freeFormatEvaluationFound export;`,
` dcl-pi *n ind;`,
` code like(t_longLineOfCode) const;`,
` triggerType like(t_triggerType);`,
` end-pi;`,
``,
` if %len(%trim(code)) >= 10 `,
` and %lower(%subst(%trim(code): 1: 10)) = 'replprint(';`,
` triggerType = c_replPrintStatement;`,
` return *on;`,
` endif;`,
``,
` if %len(%trim(code)) >= 11`,
` and %lower(%subst(%trim(code): 1: 11)) = 'replequals(';`,
` triggerType = c_replEqualsStatement;`,
` return *on;`,
` endif;`,
``,
` if %scan('IF ': %trim(toUpperCase(code))) = 1`,
` or %scan('IF%': %trim(toUpperCase(code))) = 1`,
` or %scan('ELSE ': %trim(toUpperCase(code))) = 1`,
` // why do we need the next case?`,
` //or %scan('ELSE%': %trim(toUpperCase(code))) = 1`,
` or %scan('ELSE;': %trim(toUpperCase(code))) = 1`,
` or %scan('ELSEIF ': %trim(toUpperCase(code))) = 1`,
` or %scan('ELSEIF%': %trim(toUpperCase(code))) = 1`,
` or %scan('WHEN ': %trim(toUpperCase(code))) = 1`,
` or %scan('WHEN%': %trim(toUpperCase(code))) = 1`,
` or %scan('OTHER ': %trim(toUpperCase(code))) = 1`,
` or %scan('OTHER;': %trim(toUpperCase(code))) = 1`,
` or %scan('ON-ERROR ': %trim(toUpperCase(code))) = 1`,
` or %scan('ON-ERROR;': %trim(toUpperCase(code))) = 1;`,
``,
` triggerType = c_conditionalStatement;`,
` return *on;`,
``,
` endif;`,
``,
` if %scan('EXEC SQL': %trim(toUpperCase(code))) = 1;`,
``,
` // DECLARE statement are non-executable, so don't`,
` // evaluate them - check by removing blanks, uppercasing,`,
` // and trimming.`,
` if %scan('EXECSQLDECLARE':`,
` %trim(toUpperCase(%scanrpl(' ':'':code)))`,
` ) <> 1;`,
``,
` triggerType = c_sqlStatement;`,
` return *on;`,
``,
` endif;`,
``,
` endif;`,
``,
` if %scan('DOW ': %trim(toUpperCase(code))) = 1`,
` or %scan('DOW%': %trim(toUpperCase(code))) = 1`,
` or %scan('DOU ': %trim(toUpperCase(code))) = 1`,
` or %scan('DOU%': %trim(toUpperCase(code))) = 1`,
` or %scan('FOR ': %trim(toUpperCase(code))) = 1;`,
` // why do we need the next case?`,
` //or %scan('FOR%': %trim(toUpperCase(code))) = 1;`,
``,
` triggerType = c_loopStatement;`,
` return *on;`,
``,
` endif;`,
``,
` if %scan('=': %trim(code)) <> 0;`,
``,
` triggerType = c_setValueStatement;`,
` return *on;`,
``,
` endif;`,
``,
` return *off;`,
``,
`end-proc;`,
].join(`\n`);

const cache = await parser.getDocs(uri, lines, { ignoreCache: true, withIncludes: true, collectReferences: true });
const freeFormatEvaluationFound = cache.find(`freeFormatEvaluationFound`);
const code = freeFormatEvaluationFound.scope.find(`code`);
expect(code.references.length).toBe(25);
expect(code.references.every(ref => lines.substring(ref.offset.position, ref.offset.end) === `code`)).toBe(true);
});

0 comments on commit 9655dc7

Please sign in to comment.