Skip to content

Commit 1042ad7

Browse files
committed
update function regex
1 parent 8584521 commit 1042ad7

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

src/lib/functions.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,78 @@ export interface Variable {
66
type?: string;
77
}
88

9-
export interface Function {
9+
10+
export interface Subroutine {
11+
1012
name: string;
1113
args: Variable[];
12-
return: Variable;
1314
docstr: string;
1415
lineNumber: number
1516
}
1617

18+
export interface Function extends Subroutine {
19+
20+
return: Variable; // function is a subroutine with return type
21+
}
22+
23+
export enum MethodType {
24+
Subroutine,
25+
Function
26+
};
27+
1728

1829

1930
export function getDeclaredFunctions(document: vscode.TextDocument): Function[] {
2031

2132
let lines = document.lineCount;
22-
let funs = [];
33+
let funcs = [];
2334

2435
for (let i = 0; i < lines; i++) {
2536
let line: vscode.TextLine = document.lineAt(i);
2637
if (line.isEmptyOrWhitespace) continue;
2738
let newFunc = parseFunction(line.text)
2839
if(newFunc){
29-
funs.push({...newFunc, lineNumber: i });
40+
funcs.push({...newFunc, lineNumber: i });
3041
}
3142
}
32-
return funs;
43+
return funcs;
3344
}
3445

46+
export function getDeclaredSubroutines(document: vscode.TextDocument):Subroutine[]{
47+
48+
return [];
49+
}
50+
51+
52+
3553
export const parseFunction = (line: string) => {
3654

37-
const functionRegEx = /function\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\((\s*[a-zA-z][a-zA-z0-9_,\s]*)*\s*\)/g
38-
if (line.match(functionRegEx)) {
55+
return _parse(line, MethodType.Function);
56+
}
57+
58+
export const parseSubroutine = (line: string) => {
59+
60+
return _parse(line, MethodType.Subroutine);
61+
}
62+
export const _parse = (line: string, type: MethodType) => {
3963

64+
const functionRegEx = /([a-zA-Z]+(\([\w.=]+\))*)*\s*function\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\((\s*[a-zA-z][a-zA-z0-9_,\s]*)*\s*\)\s*(result\([a-zA-z_][\w]*\))*/g;
65+
const subroutineRegEx = /subroutine\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\((\s*[a-zA-z][a-zA-z0-9_,\s]*)*\s*\)/g
66+
const regEx = (type === MethodType.Subroutine)?subroutineRegEx: functionRegEx;
67+
if (line.match(regEx)) {
4068
let [name, argsstr] = functionRegEx.exec(line).slice(1, 3);
4169
let args = (argsstr)? parseArgs(argsstr): [];
4270
return {
4371
name: name,
44-
args: args,
45-
return: null
72+
args: args
4673
};
4774
}
75+
4876
}
4977

78+
79+
80+
5081
export const parseArgs = (argsstr: string) => {
5182
let args = argsstr.trim().split(',');
5283
let variables: Variable[] = args.filter(name => validVariableName(name))

0 commit comments

Comments
 (0)