|
1 | | - |
2 | | -import * as vscode from 'vscode'; |
| 1 | +import * as vscode from "vscode"; |
3 | 2 |
|
4 | 3 | export interface Variable { |
5 | | - name: string; |
6 | | - type?: string; |
7 | | - lineNumber?: number; |
| 4 | + name: string; |
| 5 | + type?: string; |
| 6 | + lineNumber?: number; |
8 | 7 | } |
9 | 8 |
|
10 | | - |
11 | 9 | export interface Subroutine { |
12 | | - |
13 | | - name: string; |
14 | | - args: Variable[]; |
15 | | - docstr: string; |
16 | | - lineNumber: number |
| 10 | + name: string; |
| 11 | + args: Variable[]; |
| 12 | + docstr: string; |
| 13 | + lineNumber: number; |
17 | 14 | } |
18 | 15 |
|
19 | | -export interface Function extends Subroutine { |
20 | | - |
21 | | - return: Variable; // function is a subroutine with return type |
| 16 | +export interface Function extends Subroutine { |
| 17 | + return: Variable; // function is a subroutine with return type |
22 | 18 | } |
23 | 19 |
|
24 | 20 | export enum MethodType { |
25 | | - Subroutine, |
26 | | - Function |
27 | | -}; |
28 | | - |
29 | | - |
30 | | - |
31 | | -export function getDeclaredFunctions(document: vscode.TextDocument): Function[] { |
32 | | - |
33 | | - let lines = document.lineCount; |
34 | | - let funcs = []; |
| 21 | + Subroutine, |
| 22 | + Function |
| 23 | +} |
35 | 24 |
|
36 | | - for (let i = 0; i < lines; i++) { |
37 | | - let line: vscode.TextLine = document.lineAt(i); |
38 | | - if (line.isEmptyOrWhitespace) continue; |
39 | | - let newFunc = parseFunction(line.text) |
40 | | - if(newFunc){ |
41 | | - funcs.push({...newFunc, lineNumber: i }); |
42 | | - } |
| 25 | +export function getDeclaredFunctions( |
| 26 | + document: vscode.TextDocument |
| 27 | +): Function[] { |
| 28 | + let lines = document.lineCount; |
| 29 | + let funcs = []; |
| 30 | + |
| 31 | + for (let i = 0; i < lines; i++) { |
| 32 | + let line: vscode.TextLine = document.lineAt(i); |
| 33 | + if (line.isEmptyOrWhitespace) continue; |
| 34 | + let newFunc = parseFunction(line.text); |
| 35 | + if (newFunc) { |
| 36 | + funcs.push({ ...newFunc, lineNumber: i }); |
43 | 37 | } |
44 | | - return funcs; |
| 38 | + } |
| 39 | + return funcs; |
45 | 40 | } |
46 | 41 |
|
47 | | -export function getDeclaredSubroutines(document: vscode.TextDocument):Subroutine[]{ |
48 | | - |
49 | | - let lines = document.lineCount; |
50 | | - let subroutines = []; |
51 | | - |
52 | | - for (let i = 0; i < lines; i++) { |
53 | | - let line: vscode.TextLine = document.lineAt(i); |
54 | | - if (line.isEmptyOrWhitespace) continue; |
55 | | - let newSubroutine = parseSubroutine(line.text) |
56 | | - if(newSubroutine){ |
57 | | - subroutines.push({...newSubroutine, lineNumber: i }); |
58 | | - } |
| 42 | +export function getDeclaredSubroutines( |
| 43 | + document: vscode.TextDocument |
| 44 | +): Subroutine[] { |
| 45 | + let lines = document.lineCount; |
| 46 | + let subroutines = []; |
| 47 | + |
| 48 | + for (let i = 0; i < lines; i++) { |
| 49 | + let line: vscode.TextLine = document.lineAt(i); |
| 50 | + if (line.isEmptyOrWhitespace) continue; |
| 51 | + let newSubroutine = parseSubroutine(line.text); |
| 52 | + if (newSubroutine) { |
| 53 | + subroutines.push({ ...newSubroutine, lineNumber: i }); |
59 | 54 | } |
60 | | - return subroutines; |
| 55 | + } |
| 56 | + return subroutines; |
61 | 57 | } |
62 | 58 |
|
63 | | - |
64 | | - |
65 | 59 | export const parseFunction = (line: string) => { |
66 | | - |
67 | | - return _parse(line, MethodType.Function); |
68 | | -} |
| 60 | + return _parse(line, MethodType.Function); |
| 61 | +}; |
69 | 62 |
|
70 | 63 | export const parseSubroutine = (line: string) => { |
71 | | - |
72 | | - return _parse(line, MethodType.Subroutine); |
73 | | -} |
| 64 | + return _parse(line, MethodType.Subroutine); |
| 65 | +}; |
74 | 66 | export const _parse = (line: string, type: MethodType) => { |
75 | | - |
76 | | - 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; |
77 | | - const subroutineRegEx = /subroutine\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\((\s*[a-zA-Z][a-zA-z0-9_,\s]*)*\s*\)/g; |
78 | | - const regEx = (type === MethodType.Subroutine) ? subroutineRegEx : functionRegEx; |
79 | | - if (line.match(regEx) && type === MethodType.Function) { |
80 | | - let [attr, kind_descriptor, name, argsstr, result] = regEx.exec(line).slice(1, 5); |
81 | | - let args = (argsstr) ? parseArgs(argsstr) : []; |
82 | | - return { |
83 | | - name: name, |
84 | | - args: args |
85 | | - }; |
86 | | - } else if (line.match(regEx) && type === MethodType.Subroutine) { |
87 | | - let [name, argsstr] = regEx.exec(line).slice(1); |
88 | | - let args = (argsstr) ? parseArgs(argsstr) : []; |
89 | | - return { |
90 | | - name: name, |
91 | | - args: args |
92 | | - }; |
93 | | - } |
94 | | - |
95 | | -} |
96 | | - |
97 | | - |
98 | | - |
| 67 | + 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]*\))*/gi; |
| 68 | + const subroutineRegEx = /subroutine\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\((\s*[a-zA-Z][a-zA-z0-9_,\s]*)*\s*\)/gi; |
| 69 | + const regEx = |
| 70 | + type === MethodType.Subroutine ? subroutineRegEx : functionRegEx; |
| 71 | + if (line.match(regEx) && type === MethodType.Function) { |
| 72 | + let [attr, kind_descriptor, name, argsstr, result] = regEx |
| 73 | + .exec(line) |
| 74 | + .slice(1, 5); |
| 75 | + let args = argsstr ? parseArgs(argsstr) : []; |
| 76 | + return { |
| 77 | + name: name, |
| 78 | + args: args |
| 79 | + }; |
| 80 | + } else if (line.match(regEx) && type === MethodType.Subroutine) { |
| 81 | + let [name, argsstr] = regEx.exec(line).slice(1); |
| 82 | + let args = argsstr ? parseArgs(argsstr) : []; |
| 83 | + return { |
| 84 | + name: name, |
| 85 | + args: args |
| 86 | + }; |
| 87 | + } |
| 88 | +}; |
99 | 89 |
|
100 | 90 | export const parseArgs = (argsstr: string) => { |
101 | | - let args = argsstr.trim().split(','); |
102 | | - let variables: Variable[] = args.filter(name => validVariableName(name)) |
103 | | - .map(name => { |
104 | | - return { name: name }; |
105 | | - }); |
106 | | - return variables; |
107 | | -} |
| 91 | + let args = argsstr.trim().split(","); |
| 92 | + let variables: Variable[] = args |
| 93 | + .filter(name => validVariableName(name)) |
| 94 | + .map(name => { |
| 95 | + return { name: name }; |
| 96 | + }); |
| 97 | + return variables; |
| 98 | +}; |
108 | 99 |
|
109 | 100 | export const validVariableName = (name: string) => { |
110 | | - return name.trim().match(/^[a-zA-Z_][a-zA-Z0-9_]*$/) !== null; |
111 | | -} |
112 | | - |
| 101 | + return name.trim().match(/^[a-zA-Z_][a-zA-Z0-9_]*$/) !== null; |
| 102 | +}; |
0 commit comments