From 9f25251db4059f254906058734c3c1201e6aaafb Mon Sep 17 00:00:00 2001 From: worksofliam Date: Mon, 9 Dec 2024 14:17:59 -0500 Subject: [PATCH] Fix to parser circular include Signed-off-by: worksofliam --- language/parser.ts | 26 +++++++++++++++----------- tests/parserSetup.ts | 4 ++-- tests/sources/rpgle-repl | 1 + 3 files changed, 18 insertions(+), 13 deletions(-) create mode 160000 tests/sources/rpgle-repl diff --git a/language/parser.ts b/language/parser.ts index 4217702a..664c426a 100644 --- a/language/parser.ts +++ b/language/parser.ts @@ -557,17 +557,21 @@ export default class Parser { if (includePath) { const include = await this.includeFileFetch(workingUri, includePath); - if (include.found) { - scopes[0].includes.push({ - toPath: include.uri, - line: lineNumber - }); - - try { - await parseContent(include.uri, include.content); - } catch (e) { - console.log(`Error parsing include: ${include.uri}`); - console.log(e); + if (include.found && include.uri) { + if (!scopes[0].includes.some(inc => inc.toPath === include.uri)) { + scopes[0].includes.push({ + toPath: include.uri, + line: lineNumber + }); + + try { + await parseContent(include.uri, include.content); + } catch (e) { + console.log(`Error parsing include: ${include.uri}`); + console.log(e); + } + } else { + console.log(`Circular include detected: ${includePath}`); } } } diff --git a/tests/parserSetup.ts b/tests/parserSetup.ts index f5c5dd88..55d13c4a 100644 --- a/tests/parserSetup.ts +++ b/tests/parserSetup.ts @@ -24,14 +24,14 @@ export default function setupParser(projectRoot = TEST_INCLUDE_DIR): Parser { } const globPath = path.join(`**`, includeFile); - const files = glob.sync(globPath, { + const files: string[] = glob.sync(globPath, { cwd: projectRoot, absolute: true, nocase: true, }); if (files.length >= 1) { - const file = files[0]; + const file = files.find(f => f.toLowerCase().endsWith(`rpgleinc`)) || files[0]; const content = await readFile(file, { encoding: `utf-8` }); diff --git a/tests/sources/rpgle-repl b/tests/sources/rpgle-repl new file mode 160000 index 00000000..b4240b03 --- /dev/null +++ b/tests/sources/rpgle-repl @@ -0,0 +1 @@ +Subproject commit b4240b0314ce9d90dc59c48858e3809ba0924815