Skip to content

Commit

Permalink
Support for C spec definitions
Browse files Browse the repository at this point in the history
Signed-off-by: worksofliam <[email protected]>
  • Loading branch information
worksofliam committed Dec 11, 2024
1 parent 006ea8f commit 8996af8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
17 changes: 10 additions & 7 deletions language/models/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ export default class Cache {
return (lines.length >= 1 ? lines[0] : 0);
}

/**
*
* @param {string} name
* @returns {Declaration}
*/
find(name) {
find(name: string, includeProcedure?: string): Declaration|undefined {
name = name.toUpperCase();

const fileStructs = this.files.flatMap(file => file.subItems);
Expand All @@ -127,6 +122,14 @@ export default class Cache {
if (found) return found;
}

if (includeProcedure) {
const procedureScope = this.procedures.find(proc => proc.name.toUpperCase() === includeProcedure);
if (procedureScope) {
const found = procedureScope.scope.find(name);
if (found) return found;
}
}

if (allStructs.length > 0) {
for (const def of allStructs) {
if (def.keyword[`QUALIFIED`] !== true) {
Expand All @@ -136,7 +139,7 @@ export default class Cache {
}
}

return null;
return;
}

findDefinition(lineNumber, word) {
Expand Down
7 changes: 7 additions & 0 deletions language/models/fixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export function parseCLine(lineNumber, lineIndex, content) {
const extended = content.substr(35);
const result = content.substr(49, 14);

const fieldLength = content.substr(63, 5);
const fieldDecimals = content.substr(68, 2);

const ind1 = content.substr(70, 2);
const ind2 = content.substr(72, 2);
const ind3 = content.substr(74, 2);
Expand All @@ -71,6 +74,10 @@ export function parseCLine(lineNumber, lineIndex, content) {
factor2: calculateToken(lineNumber, lineIndex+35, factor2),
result: calculateToken(lineNumber, lineIndex+49, result),
extended: calculateToken(lineNumber, lineIndex+35, extended),

fieldLength: calculateToken(lineNumber, lineIndex+63, fieldLength),
fieldDecimals: calculateToken(lineNumber, lineIndex+68, fieldDecimals),

ind1: calculateToken(lineNumber, lineIndex+70, ind1, `special-ind`),
ind2: calculateToken(lineNumber, lineIndex+72, ind2, `special-ind`),
ind3: calculateToken(lineNumber, lineIndex+74, ind3, `special-ind`)
Expand Down
25 changes: 25 additions & 0 deletions language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,31 @@ export default class Parser {
...fromToken(cSpec.factor2),
...fromToken(cSpec.result),
);

if (cSpec.result && cSpec.fieldLength) {
// This means we need to dynamically define this field
const fieldName = cSpec.result.value;
// Don't redefine this field.
if (!scopes[0].find(fieldName, currentProcName)) {
const fieldLength = parseInt(cSpec.fieldLength.value);
const decimals = cSpec.fieldDecimals ? parseInt(cSpec.fieldDecimals.value) : undefined;
const type = decimals !== undefined ? `PACKED`: `CHAR`;

currentItem = new Declaration(`variable`);
currentItem.name = fieldName;
currentItem.keyword = {[type]: `${fieldLength}${decimals !== undefined ? `:${decimals}` : ``}`};
currentItem.position = {
path: fileUri,
line: lineNumber
};
currentItem.range = {
start: lineNumber,
end: lineNumber
};

scope.variables.push(currentItem);
}
}
}

potentialName = cSpec.factor1 ? cSpec.factor1.value : ``;
Expand Down
11 changes: 10 additions & 1 deletion tests/suite/fixed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,16 @@ test('plist_test', async () => {

const cache = await parser.getDocs(uri, lines, { ignoreCache: true, withIncludes: true });

expect(cache.variables.length).to.equal(0);
expect(cache.variables.length).toBe(11);

const atPGMID = cache.find(`@PGMID`);
expect(atPGMID.keyword[`CHAR`]).toBe(`10`);

const atSON = cache.find(`@SQN`);
expect(atSON.keyword[`PACKED`]).toBe(`2:0`);

const atPVSELECTION = cache.find(`@PVSELECTION`);
expect(atPVSELECTION.keyword[`CHAR`]).toBe(`256`);
});

test(`range test 2`, async () => {
Expand Down
7 changes: 7 additions & 0 deletions tests/suite/references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,9 @@ test('references_15_fixed_4', async () => {
const r_transf = cache.find(`r_transf`);
expect(r_transf.references.length).toBe(3);
expect(r_transf.references.every(ref => lines.substring(ref.offset.position, ref.offset.end) === `r_transf`)).toBe(true);

const r_long_lue = cache.find(`r_long_lue`);
expect(r_long_lue.references.length).toBe(1);
});

test('references_16_fixed_5', async () => {
Expand Down Expand Up @@ -1117,6 +1120,10 @@ test('references_16_fixed_5', async () => {
const mntJtot = cache.find(`mntJtot`);
expect(mntJtot.references.length).toBe(2);
expect(mntJtot.references.every(ref => lines.substring(ref.offset.position, ref.offset.end) === `mntJtot`)).toBe(true);

const syde = cache.find(`syde`);
expect(syde.references.length).toBe(2);
expect(syde.references.every(ref => lines.substring(ref.offset.position, ref.offset.end) === `syde`)).toBe(true);
});

test('references_17_fixed_6', async () => {
Expand Down

0 comments on commit 8996af8

Please sign in to comment.