Skip to content

Commit 40c727f

Browse files
committed
fix(parser+grammar): handle quoted ) in function defaults (fixes #243)
1 parent 19be969 commit 40c727f

File tree

9 files changed

+460
-42
lines changed

9 files changed

+460
-42
lines changed

src/parsers/MapParser.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/**
22
* Parses AutoIt source code to extract Map variable information
33
*/
4+
import {
5+
parseFunctionDeclarationLine,
6+
parseParameterNames,
7+
splitTopLevel,
8+
} from '../utils/functionSignatureParsing.js';
9+
410
export default class MapParser {
511
constructor(source) {
612
this.source = source;
@@ -117,22 +123,18 @@ export default class MapParser {
117123
*/
118124
parseFunctionBoundaries() {
119125
this.functions = [];
120-
const funcStartPattern = /^\s*Func\s+(\w+)\s*\((.*?)\)/i;
121126
const funcEndPattern = /^\s*EndFunc/i;
122127

123128
let currentFunc = null;
124129

125130
this.lines.forEach((line, index) => {
126-
const funcStart = line.match(funcStartPattern);
127-
if (funcStart) {
131+
const funcDeclaration = parseFunctionDeclarationLine(line);
132+
if (funcDeclaration) {
128133
// Parse parameters
129-
const paramsStr = funcStart[2].trim();
130-
const parameters = paramsStr
131-
? paramsStr.split(',').map(p => p.trim().split('=')[0].trim())
132-
: [];
134+
const parameters = parseParameterNames(funcDeclaration.paramsText);
133135

134136
currentFunc = {
135-
name: funcStart[1],
137+
name: funcDeclaration.functionName,
136138
startLine: index,
137139
endLine: -1,
138140
parameters,
@@ -305,7 +307,7 @@ export default class MapParser {
305307
if (!match) return;
306308

307309
const funcName = match[1];
308-
const args = match[2].split(',').map(a => a.trim());
310+
const args = splitTopLevel(match[2], ',').map(a => a.trim());
309311

310312
// Check if our Map is passed as an argument
311313
const argIndex = args.indexOf(mapName);

src/parsers/VariableParser.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
*/
55

66
import VariablePatterns from '../utils/VariablePatterns.js';
7+
import {
8+
parseFunctionDeclarationLine,
9+
parseParameterNames,
10+
} from '../utils/functionSignatureParsing.js';
711

812
class VariableParser {
913
constructor(source, filePath = '') {
@@ -22,26 +26,18 @@ class VariableParser {
2226
*/
2327
parseFunctionBoundaries() {
2428
this.functions = [];
25-
const funcStartPattern = /^\s*(?:Volatile\s+)?Func\s+(\w+)\s*\((.*?)\)/i;
2629
const funcEndPattern = /^\s*EndFunc/i;
2730

2831
let currentFunc = null;
2932

3033
this.lines.forEach((line, index) => {
31-
const funcStart = line.match(funcStartPattern);
32-
if (funcStart) {
34+
const funcDeclaration = parseFunctionDeclarationLine(line);
35+
if (funcDeclaration) {
3336
// Parse parameters from function signature
34-
const paramsStr = funcStart[2].trim();
35-
const parameters = paramsStr
36-
? paramsStr.split(',').map(p => {
37-
const param = p.trim().split('=')[0].trim();
38-
// Parameters should start with $
39-
return param.startsWith('$') ? param : '$' + param;
40-
})
41-
: [];
37+
const parameters = parseParameterNames(funcDeclaration.paramsText, true);
4238

4339
currentFunc = {
44-
name: funcStart[1],
40+
name: funcDeclaration.functionName,
4541
startLine: index,
4642
endLine: -1,
4743
parameters,
@@ -192,31 +188,22 @@ class VariableParser {
192188
* @private
193189
*/
194190
parseFunctionParameters(line, lineIndex) {
195-
const funcMatch = line.match(/^\s*(?:Volatile\s+)?Func\s+(\w+)\s*\((.*?)\)/i);
196-
197-
if (!funcMatch) {
191+
const funcDeclaration = parseFunctionDeclarationLine(line);
192+
if (!funcDeclaration) {
198193
return;
199194
}
200195

201-
const functionName = funcMatch[1];
202-
const paramsStr = funcMatch[2].trim();
196+
const { functionName, paramsText: paramsStr, paramsStartIndex } = funcDeclaration;
203197

204198
if (!paramsStr) {
205199
return; // No parameters
206200
}
207201

208202
// Parse parameter list
209-
const parameters = paramsStr
210-
.split(',')
211-
.map(p => {
212-
const param = p.trim().split('=')[0].trim();
213-
return param.startsWith('$') ? param : '$' + param;
214-
})
215-
.filter(p => p.length > 0);
203+
const parameters = parseParameterNames(paramsStr, true);
216204

217205
// Find opening parenthesis to search from there
218-
const parenIndex = line.indexOf('(');
219-
let searchFrom = parenIndex >= 0 ? parenIndex : 0;
206+
let searchFrom = paramsStartIndex;
220207

221208
// Add each parameter as a variable
222209
parameters.forEach(param => {

src/util.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs';
33
import path from 'path';
44
import { CompletionItemKind, MarkdownString, window, workspace } from 'vscode';
55
import aiConfig from './ai_config';
6+
import { splitTopLevel } from './utils/functionSignatureParsing';
67

78
// ============================================================================
89
// CONSTANTS AND CONFIGURATION
@@ -702,14 +703,12 @@ const getParams = (paramText, text, headerIndex) => {
702703
return params;
703704
}
704705

705-
const paramList = paramText
706-
.split(',')
706+
const paramList = splitTopLevel(paramText, ',')
707707
.map(param => param.trim())
708708
.filter(param => param.length > 0);
709709

710710
paramList.forEach(param => {
711-
const paramEntry = param
712-
.split('=')[0]
711+
const paramEntry = splitTopLevel(param, '=')[0]
713712
.trim()
714713
.replace(/^ByRef\s*/, '');
715714

0 commit comments

Comments
 (0)