Skip to content

Commit b1c1ef9

Browse files
New property for variables to update them during post-processing
1 parent 80f0138 commit b1c1ef9

5 files changed

Lines changed: 61 additions & 0 deletions

File tree

libs/parsing/index/src/lib/post-process/post-process-parsed.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { GetSemanticTokens } from '@idl/parsing/semantic-tokens';
33
import {
44
IParsed,
55
PopulateScopeDetailAndResetTokenCache,
6+
ResetVariables,
67
} from '@idl/parsing/syntax-tree';
78

89
import { GetSyntaxProblems } from '../helpers/get-syntax-problems';
@@ -26,6 +27,11 @@ export function PostProcessParsed(
2627
*/
2728
PopulateScopeDetailAndResetTokenCache(parsed, cancel);
2829

30+
/**
31+
* Reset variables so we can update types
32+
*/
33+
ResetVariables(parsed);
34+
2935
/**
3036
* Populate types of local variables and validate them
3137
*/

libs/parsing/syntax-tree/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export * from './lib/recursion-and-callbacks/tree-recurser-basic';
5959
export * from './lib/remove-scope-detail';
6060
export * from './lib/remove-scope-detail-and-reset-token-cache';
6161
export * from './lib/reset-token-cache';
62+
export * from './lib/reset-variables';
6263
export * from './lib/set-token-cache';
6364
export * from './lib/syntax-problem-with';
6465
export * from './lib/validator.class.interface';

libs/parsing/syntax-tree/src/lib/populators/populate-global.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ export function PopulateGlobalLocalCompileOpts(
203203
meta: {
204204
display: 'self',
205205
isDefined: true,
206+
canReset: false,
206207
docs: IDL_TRANSLATION.docs.hover.params.self,
207208
source: GLOBAL_TOKEN_SOURCE_LOOKUP.USER,
208209
type: ParseIDLType(split[0]),
@@ -319,6 +320,7 @@ export function PopulateGlobalLocalCompileOpts(
319320
meta: {
320321
display: 'self',
321322
isDefined: true,
323+
canReset: false,
322324
docs: IDL_TRANSLATION.docs.hover.params.self,
323325
source: GLOBAL_TOKEN_SOURCE_LOOKUP.USER,
324326
type: ParseIDLType(split[0]),

libs/parsing/syntax-tree/src/lib/populators/populate-local.interface.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ export const LOCAL_TOKEN_LOOKUP: ILocalTokenNameLookup = {
3333
export interface ILocalVariableTokenMetadata extends IBaseValueDetails {
3434
/** A flag indicating if our variable has been defined or not */
3535
isDefined: boolean;
36+
/**
37+
* For variables that we detect, can we reset them during our post-processing?
38+
*
39+
* This is needed because we have variables from arguments and keywords which can't reset as
40+
* they are static, but we have variables inside code from things like `a = plot()` where, if
41+
* the function "plot()" changes, we have to reset the variable and re-determine the type for
42+
* said variable.
43+
*
44+
* We don't do this for args/keywords though because they are static.
45+
*/
46+
canReset: boolean;
3647
/** Flag indicating if our variable represents a static class or not */
3748
isStaticClass?: boolean;
3849
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { IParsed } from './parsed.interface';
2+
import { LocalToken } from './populators/populate-local.interface';
3+
4+
/** Resets array of tokens */
5+
function _ResetVariables(vars: LocalToken[]) {
6+
for (let i = 0; i < vars.length; i++) {
7+
if (vars[i].meta.canReset) {
8+
vars[i].meta.isDefined = false;
9+
}
10+
}
11+
}
12+
13+
/**
14+
* Resets variables that can be reset so we can update types when we
15+
* just post-process code and don't do a full parse.
16+
*
17+
* This is needed because some variables are dynamic and some are static (i.e.
18+
* arguments and keywords)
19+
*/
20+
export function ResetVariables(parsed: IParsed): void {
21+
/**
22+
* Reset functions
23+
*/
24+
const funcs = Object.keys(parsed.local.func);
25+
for (let i = 0; i < funcs.length; i++) {
26+
_ResetVariables(Object.values(parsed.local.func[funcs[i]]));
27+
}
28+
29+
/**
30+
* Reset functions
31+
*/
32+
const pros = Object.keys(parsed.local.pro);
33+
for (let i = 0; i < pros.length; i++) {
34+
_ResetVariables(Object.values(parsed.local.pro[pros[i]]));
35+
}
36+
37+
/**
38+
* Reset main
39+
*/
40+
_ResetVariables(Object.values(parsed.local.main));
41+
}

0 commit comments

Comments
 (0)