Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 | 14x 14x 14x 14x 14x 14x 14x 160x 160x 4x 4x 156x 214x 214x 214x 214x 126x 126x 126x 126x 126x 214x 34x 214x 214x 214x 156x 4x 4x 4x 4x 214x 214x 214x 123x 123x 123x 214x 208x 208x 208x 208x 30x 30x 30x 16x 16x 14x 30x 2x 30x 30x 30x 16x 16x 10x 6x 30x 264x 4x 4x 14x 1x 1x 4x 3x 27x 205x 205x 205x 205x 205x 1x 204x 6x 205x 205x 205x 205x 13x 205x 849x 849x 849x 64x 64x 64x 56x 56x 209x 209x 209x 209x 418x 418x 419x 419x 209x 209x 209x 13x 13x 209x 159x 11x 11x 209x 879x 23x 23x 856x 856x 213x 213x 213x 6x 213x 2x 213x 213x 213x 213x 213x 3x 3x 3x 213x 426x 426x 413x 413x 413x 413x 413x 413x 3x 213x 413x 413x 413x 27047x 423x 423x 26624x 423x 423x 14x 14x 14x 14x 208x 208x 205x 205x 24x 48x 24x 15x 15x 15x 654x 15x 15x 9x 9x 24x 24x 24x 18x 24x 642x 642x 1284x 1284x 24x 24x 24x 24x 24x 24x 24x 24x 24x 18x 102x 642x 214x 214x 34x 214x 214x 214x 214x 214x 9x 9x 9x 3x 214x 214x 214x 214x 214x 284x 284x 282x 282x 30x 30x 214x 214x 214x 214x 214x 11x 214x 214x 214x 284x 284x 2x 1x 1x 214x 214x 89x 214x 214x 214x 214x 214x 214x 214x 214x 126x 214x 214x 214x 214x 214x 214x 156x 156x 214x 34x 214x 214x 209x 209x 213x 213x 213x 200x | import type { ParserMessage } from "./schema";
import type { PinMode } from "@shared/types/arduino.types";
import { randomUUID } from "node:crypto";
/**
* Centralized patterns and constants for Arduino code parsing
* Extracted to reduce cognitive complexity and enable reuse
*/
// Two separate for-loop regexes to avoid super-linear backtracking (S5843):
const FOR_LOOP_TYPED = /for\s*\(\s*\w+\s+(\w+)\s*=\s*(\d+)\s*;\s*\w+\s*(<=?)\s*(\d+)\s*;[^)]*\)/g;
const FOR_LOOP_BARE = /for\s*\(\s*(\w+)\s*=\s*(\d+)\s*;\s*\w+\s*(<=?)\s*(\d+)\s*;[^)]*\)/g;
// Two separate function-def regexes to reduce alternation complexity (S5843):
const FUNCTION_DEF_BASIC = /(?:void|int|bool|byte|long|float|double|char|String)\s+(\w+)\s*\([^)]*\)\s*\{/g;
const FUNCTION_DEF_UNSIGNED = /unsigned\s+(?:int|long)\s+(\w+)\s*\([^)]*\)\s*\{/g;
const PARSER_PATTERNS = {
// Serial configuration patterns
SERIAL_USAGE: /Serial\s*\.\s*(print|println|write|read|available|peek|readString|readBytes|parseInt|parseFloat|find|findUntil)/,
SERIAL_BEGIN: /Serial\s*\.\s*begin\s*\(\s*\d+\s*\)/,
SERIAL_BEGIN_EXTRACT: /Serial\s*\.\s*begin\s*\(\s*(\d+)\s*\)/,
SERIAL_WHILE_NOT: /while\s*\(\s*!\s*Serial\s*\)/,
SERIAL_READ: /Serial\s*\.\s*read\s*\(\s*\)/,
SERIAL_AVAILABLE: /Serial\s*\.\s*available\s*\(\s*\)/,
// Structure patterns
SETUP_FUNCTION: /void\s+setup\s*\(\s*\)/,
SETUP_ANY: /void\s+setup\s*\([^)]*\)/,
LOOP_FUNCTION: /void\s+loop\s*\(\s*\)/,
LOOP_ANY: /void\s+loop\s*\([^)]*\)/,
// Pin-related patterns
FOR_LOOP_TYPED,
FOR_LOOP_BARE,
PIN_MODE: /pinMode\s*\(\s*(\d+|A\d+)\s*,/g,
PIN_MODE_WITH_MODE: /pinMode\s*\(\s*(\d+|A\d+)\s*,\s*(INPUT_PULLUP|INPUT|OUTPUT)\s*\)/g,
PIN_MODE_VAR: /pinMode\s*\(\s*([a-zA-Z_]\w*)\s*,/g,
ANALOG_WRITE: /analogWrite\s*\(\s*(\d+|A\d+)\s*,/g,
DIGITAL_READ_WRITE: /digital(?:Read|Write)\s*\(\s*(\d+|A\d+|[a-zA-Z_]\w*)/g,
DIGITAL_READ_LITERAL: /\bdigitalRead\s*\(\s*(\d+|A\d+)\s*\)/g,
DIGITAL_WRITE_READ_PIN: /pinMode\s*\(\s*(\d+|A\d+)/gi,
DIGITAL_WRITE_READ_DIO: /digital(?:Write|Read)\s*\(\s*(\d+|A\d+)/gi,
ANALOG_READ_WRITE: /analog(?:Read|Write)\s*\(\s*(\d+|A\d+)/gi,
// Performance patterns
WHILE_TRUE: /while\s*\(\s*true\s*\)/,
FOR_NO_EXIT: /for *\( *[^;\n]+; *; *[^)\n]+\)/, // NOSONAR S5843
LARGE_ARRAY: /\[\s*(\d{4,})\s*\]/,
FUNCTION_DEF_BASIC,
FUNCTION_DEF_UNSIGNED,
// Comment patterns (consolidated from inline)
COMMENT_SINGLE_LINE: /\/\/[^\n]*$/gm, // NOSONAR S5843
COMMENT_MULTI_LINE: /\/\*[^*]*(?:\*+[^*/][^*]*)*\*\//g,
// Additional pin patterns (consolidated from inline)
PIN_MODE_ANY: /pinMode *\( *[^,)\n]+,/, // NOSONAR S5843
DIGITAL_DYNAMIC_PIN_READ: /digitalRead\s*\(\s*[^0-9A\s][^,)]*/,
DIGITAL_DYNAMIC_PIN_WRITE: /digitalWrite\s*\(\s*[^0-9A\s][^,)]*/,
// Utility patterns
ANALOG_PIN_FORMAT: /^A\d+$/,
} as const;
interface PinModeCall {
pin: number;
mode: PinMode;
line: number;
}
interface PinModeEntry {
modes: Array<PinMode>;
lines: number[];
}
const VALID_PIN_MODES = new Set<string>(["INPUT", "OUTPUT", "INPUT_PULLUP"]);
function addPinModeEntry(result: Map<string, PinModeEntry>, pin: string, mode: PinMode, line: number): void {
const existing = result.get(pin);
if (existing) {
existing.modes.push(mode);
existing.lines.push(line);
} else {
result.set(pin, { modes: [mode], lines: [line] });
}
}
/**
* Specialized analyzer for pin mode conflicts and hardware compatibility
*/
class PinCompatibilityChecker {
constructor(private readonly uncommentedCode: string) {}
/**
* Extract all pins configured with pinMode calls (direct and loop-based)
*/
getPinModeInfo(getLoopPinModeCalls: (code: string) => PinModeCall[]): Map<string, PinModeEntry> {
const result = new Map<string, PinModeEntry>();
// Direct pinMode() calls
const pinModeWithModeRegex = PARSER_PATTERNS.PIN_MODE_WITH_MODE;
let match;
while ((match = pinModeWithModeRegex.exec(this.uncommentedCode)) !== null) {
const pin = match[1];
const rawMode = match[2];
const mode: PinMode = VALID_PIN_MODES.has(rawMode) ? (rawMode as PinMode) : "INPUT";
const line = this.uncommentedCode.slice(0, Math.max(0, match.index)).split("\n").length;
addPinModeEntry(result, pin, mode, line);
}
// Loop-based pinMode() calls
for (const { pin, mode, line } of getLoopPinModeCalls(this.uncommentedCode)) {
addPinModeEntry(result, String(pin), mode, line);
}
return result;
}
/**
* Check for conflicting pin mode declarations
*/
checkPinModeConflicts(
pinModeCalls: Map<string, PinModeEntry>,
): ParserMessage[] {
const messages: ParserMessage[] = [];
for (const [pin, entry] of pinModeCalls.entries()) {
if (entry.modes.length < 2) continue;
const uniqueModes = Array.from(new Set(entry.modes));
const line = entry.lines[1];
if (uniqueModes.length > 1) {
messages.push({
id: randomUUID(),
type: "warning",
category: "pins",
severity: 2,
message: `Pin ${pin} has multiple pinMode() calls with different modes: ${[...uniqueModes].join(", ")}.`,
suggestion: `Use a single pinMode(${pin}, <MODE>) call in setup().`,
line,
});
} else E{
messages.push({
id: randomUUID(),
type: "warning",
category: "pins",
severity: 2,
message: `Pin ${pin} has pinMode() called multiple times (${entry.modes.length}x).`,
suggestion: `Remove duplicate pinMode(${pin}, ${uniqueModes[0]}) calls.`,
line,
});
}
}
return messages;
}
/**
* Check for OUTPUT pins being read with digitalRead()
*/
checkOutputPinsReadAsInput(
uncommentedCode: string,
outputPins: Set<number>,
parsePinNumber: (pin: string) => number | undefined,
): ParserMessage[] {
const messages: ParserMessage[] = [];
if (outputPins.size > 0) {
const digitalReadLiteralRe = PARSER_PATTERNS.DIGITAL_READ_LITERAL;
const outputReadWarnedPins = new Set<number>();
let match;
while ((match = digitalReadLiteralRe.exec(uncommentedCode)) !== null) {
const pinNum = parsePinNumber(match[1]);
if (
pinNum !== undefined &&
outputPins.has(pinNum) &&
!outputReadWarnedPins.has(pinNum)
) {
outputReadWarnedPins.add(pinNum);
const pinStr = pinNum >= 14 ? `A${pinNum - 14}` : `${pinNum}`;
const line = uncommentedCode.slice(0, Math.max(0, match.index)).split("\n").length;
messages.push({
id: randomUUID(),
type: "warning",
category: "pins",
severity: 2,
message: `Pin ${pinStr} is configured as OUTPUT but read with digitalRead(). Reading an OUTPUT pin may return unexpected values.`,
suggestion: `If you need to read the pin, use pinMode(${pinStr}, INPUT) or INPUT_PULLUP instead.`,
line,
});
}
}
}
return messages;
}
}
/**
* Specialized analyzer for Serial configuration issues
*/
class SerialConfigurationParser {
constructor(private readonly code: string) {}
parse(): ParserMessage[] {
const messages: ParserMessage[] = [];
const uncommentedCode = removeCommentsHelper(this.code);
// Check if Serial is used
if (!PARSER_PATTERNS.SERIAL_USAGE.test(uncommentedCode)) return messages;
// Check Serial.begin
const serialBeginExists = PARSER_PATTERNS.SERIAL_BEGIN.test(this.code);
const serialBeginActive = PARSER_PATTERNS.SERIAL_BEGIN.test(uncommentedCode);
if (serialBeginActive) {
const baudMsg = this._detectBaudRateMismatch(uncommentedCode);
if (baudMsg) messages.push(baudMsg);
} else {
messages.push({
id: randomUUID(),
type: "warning",
category: "serial",
severity: 2,
message: serialBeginExists
? "Serial.begin() is commented out! Serial output may not work correctly."
: "Serial.begin(115200) is missing in setup(). Serial output may not work correctly.",
suggestion: "Serial.begin(115200);",
line: findLineNumberHelper(this.code, /Serial\s*\.\s*begin/),
});
}
// Check for while (!Serial) antipattern
if (PARSER_PATTERNS.SERIAL_WHILE_NOT.test(uncommentedCode)) {
messages.push({
id: randomUUID(),
type: "warning",
category: "serial",
severity: 2,
message: "while (!Serial) loop detected. This blocks the simulator - not recommended.",
suggestion: "// while (!Serial) { }",
line: findLineNumberHelper(this.code, PARSER_PATTERNS.SERIAL_WHILE_NOT),
});
}
// Check for Serial.read() without Serial.available() check
const readMsg = this._detectSerialReadWithoutAvailable(uncommentedCode.split("\n"));
if (readMsg) messages.push(readMsg);
return messages;
}
private _detectBaudRateMismatch(uncommentedCode: string): ParserMessage | null {
const baudRateMatch = PARSER_PATTERNS.SERIAL_BEGIN_EXTRACT.exec(uncommentedCode);
if (baudRateMatch && baudRateMatch[1] !== "115200") {
return {
id: randomUUID(),
type: "warning",
category: "serial",
severity: 2,
message: `Serial.begin(${baudRateMatch[1]}) uses wrong baud rate. This simulator expects Serial.begin(115200).`,
suggestion: "Serial.begin(115200);",
line: findLineNumberHelper(
this.code,
new RegExp(String.raw`Serial\s*\.\s*begin\s*\(\s*${baudRateMatch[1]}`),
),
};
}
return null;
}
private _detectSerialReadWithoutAvailable(lines: string[]): ParserMessage | null {
for (let i = 0; i < lines.length; i++) {
if (!PARSER_PATTERNS.SERIAL_READ.test(lines[i])) continue;
let hasAvailableCheck = false;
for (let j = Math.max(0, i - 3); j <= i; j++) {
if (PARSER_PATTERNS.SERIAL_AVAILABLE.test(lines[j])) {
hasAvailableCheck = true;
break;
}
}
if (!hasAvailableCheck) {
return {
id: randomUUID(),
type: "warning",
category: "serial",
severity: 2,
message: "Serial.read() used without checking Serial.available(). This may return -1 when no data is available.",
suggestion: "if (Serial.available()) { }",
line: findLineNumberHelper(this.code, PARSER_PATTERNS.SERIAL_READ),
};
}
}
return null;
}
}
/**
* Specialized analyzer for structure (setup/loop) issues
*/
class StructureParser {
constructor(private readonly code: string) {}
parse(): ParserMessage[] {
const messages: ParserMessage[] = [];
const setupMatch = PARSER_PATTERNS.SETUP_FUNCTION.test(this.code);
const anySetup = PARSER_PATTERNS.SETUP_ANY.test(this.code);
if (!setupMatch && anySetup) {
messages.push({
id: randomUUID(),
type: "warning",
category: "structure",
severity: 2,
message: "setup() has parameters, but Arduino setup() should have no parameters.",
suggestion: "void setup()",
line: findLineNumberHelper(this.code, PARSER_PATTERNS.SETUP_ANY),
});
} else if (!setupMatch) {
messages.push({
id: randomUUID(),
type: "error",
category: "structure",
severity: 3,
message: "Missing void setup() function. Every Arduino program needs setup().",
suggestion: "void setup() { }",
});
}
const loopMatch = PARSER_PATTERNS.LOOP_FUNCTION.test(this.code);
const anyLoop = PARSER_PATTERNS.LOOP_ANY.test(this.code);
Iif (!loopMatch && anyLoop) {
messages.push({
id: randomUUID(),
type: "warning",
category: "structure",
severity: 2,
message: "loop() has parameters, but Arduino loop() should have no parameters.",
suggestion: "void loop()",
line: findLineNumberHelper(this.code, PARSER_PATTERNS.LOOP_ANY),
});
} else if (!loopMatch) {
messages.push({
id: randomUUID(),
type: "error",
category: "structure",
severity: 3,
message: "Missing void loop() function. Every Arduino program needs loop().",
suggestion: "void loop() { }",
});
}
return messages;
}
}
/**
* Helper function to remove comments (used by sub-parsers)
*/
function removeCommentsHelper(code: string): string {
let result = code.replaceAll(PARSER_PATTERNS.COMMENT_SINGLE_LINE, "");
result = result.replaceAll(PARSER_PATTERNS.COMMENT_MULTI_LINE, "");
return result;
}
/**
* Helper function to find line numbers (used by sub-parsers)
*/
function findLineNumberHelper(
code: string,
pattern: RegExp | string,
): number | undefined {
const regex = typeof pattern === "string" ? new RegExp(pattern) : pattern;
const match = regex.exec(code);
if (!match) return undefined;
const upToMatch = code.slice(0, Math.max(0, match.index));
return upToMatch.split("\n").length;
}
/**
* Analyzer for pin conflicts (same pin used as digital and analog)
*/
class PinConflictAnalyzer {
constructor(private readonly code: string) {}
analyze(): ParserMessage[] {
const messages: ParserMessage[] = [];
const digitalPins = new Set<number>();
let match;
for (const re of [PARSER_PATTERNS.DIGITAL_WRITE_READ_PIN, PARSER_PATTERNS.DIGITAL_WRITE_READ_DIO]) {
re.lastIndex = 0;
while ((match = re.exec(this.code)) !== null) {
const pin = parsePinNumberHelper(match[1]);
Eif (pin !== undefined) digitalPins.add(pin);
}
}
const analogPins = new Set<number>();
const analogRegex = PARSER_PATTERNS.ANALOG_READ_WRITE;
while ((match = analogRegex.exec(this.code)) !== null) {
const pin = parsePinNumberHelper(match[1]);
Eif (pin !== undefined) analogPins.add(pin);
}
for (const pin of digitalPins) {
if (analogPins.has(pin)) {
const pinStr = pin >= 14 ? `A${pin - 14}` : `${pin}`;
messages.push({
id: randomUUID(),
type: "warning",
category: "hardware",
severity: 2,
message: `Pin ${pinStr} used as both digital and analog. This may be unintended.`,
suggestion: `// Use separate pins for digital and analog`,
});
}
}
return messages;
}
}
/**
* Helper function for parsing pin numbers (used by sub-parsers)
*/
function parsePinNumberHelper(pinStr: string): number | undefined {
if (PARSER_PATTERNS.ANALOG_PIN_FORMAT.test(pinStr)) {
const analogNum = Number.parseInt(pinStr.slice(1));
Eif (analogNum >= 0 && analogNum <= 5) return 14 + analogNum;
} else {
const digitalNum = Number.parseInt(pinStr);
Eif (!Number.isNaN(digitalNum) && digitalNum >= 0 && digitalNum <= 19) return digitalNum;
}
return undefined;
}
/**
* Specialized analyzer for performance issues
*/
class PerformanceAnalyzer {
constructor(private readonly uncommentedCode: string, private readonly fullCode: string) {}
/**
* Check for infinite loops and recursion
*/
analyzeComplexity(): ParserMessage[] {
const messages: ParserMessage[] = [];
// Check for while (true)
if (PARSER_PATTERNS.WHILE_TRUE.test(this.fullCode)) {
messages.push({
id: randomUUID(),
type: "warning",
category: "performance",
severity: 2,
message:
"Infinite while(true) loop detected. This may freeze the simulator.",
suggestion: "delay(100);",
line: this.findLineInFull(PARSER_PATTERNS.WHILE_TRUE),
});
}
// Check for for loops without exit condition
if (PARSER_PATTERNS.FOR_NO_EXIT.test(this.fullCode)) {
messages.push({
id: randomUUID(),
type: "warning",
category: "performance",
severity: 2,
message:
"for loop without exit condition detected. This creates an infinite loop.",
suggestion: "for (int i = 0; i < 10; i++) { }",
line: this.findLineInFull(PARSER_PATTERNS.FOR_NO_EXIT),
});
}
return messages;
}
/**
* Check for large arrays and recursion
*/
analyzeLargeArraysAndRecursion(): ParserMessage[] {
const messages: ParserMessage[] = [];
// Check for large arrays
const arrayRegex = PARSER_PATTERNS.LARGE_ARRAY;
const arrayMatch = arrayRegex.exec(this.fullCode);
if (arrayMatch) {
const arraySize = Number.parseInt(arrayMatch[1], 10);
Eif (arraySize > 1000) {
messages.push({
id: randomUUID(),
type: "warning",
category: "performance",
severity: 2,
message: `Large array of ${arraySize} elements detected. This may cause memory issues on Arduino.`,
suggestion: `// Use smaller array size: int array[100];`,
line: this.findLineInFull(arrayRegex),
});
}
}
// Check for recursion
let match;
for (const functionDefinitionRegex of [PARSER_PATTERNS.FUNCTION_DEF_BASIC, PARSER_PATTERNS.FUNCTION_DEF_UNSIGNED]) {
functionDefinitionRegex.lastIndex = 0;
while ((match = functionDefinitionRegex.exec(this.uncommentedCode)) !== null) {
const functionName = match[1];
const functionEnd = this._findFunctionBodyEnd(match.index);
// Extract function body
const functionBody = this.uncommentedCode.slice(match.index, functionEnd + 1);
// Check if function calls itself (recursive)
const functionCallRegex = new RegExp(String.raw`\b${functionName}\s*\(`, "g");
const calls = functionBody.match(functionCallRegex);
if (calls && calls.length > 1) {
messages.push({
id: randomUUID(),
type: "warning",
category: "performance",
severity: 2,
message: `Recursive function '${functionName}' detected. Deep recursion may cause stack overflow on Arduino.`,
suggestion: "// Use iterative approach instead",
line: this.findLineInFull(new RegExp(String.raw`\b${functionName}\s*\(`)),
});
}
}
}
return messages;
}
private _findFunctionBodyEnd(functionStart: number): number {
let braceCount = 0;
let foundOpenBrace = false;
for (let i = functionStart; i < this.uncommentedCode.length; i++) {
if (this.uncommentedCode[i] === "{") {
braceCount++;
foundOpenBrace = true;
} else if (this.uncommentedCode[i] === "}") {
braceCount--;
if (foundOpenBrace && braceCount === 0) return i;
}
}
return functionStart;
}
private findLineInFull(pattern: RegExp): number | undefined {
const match = pattern.exec(this.fullCode);
Iif (!match) return undefined;
const upToMatch = this.fullCode.slice(0, Math.max(0, match.index));
return upToMatch.split("\n").length;
}
}
export class CodeParser {
/**
* Parse Serial configuration issues
*/
parseSerialConfiguration(code: string): ParserMessage[] {
const parser = new SerialConfigurationParser(code);
return parser.parse();
}
/**
* Parse structure issues (setup/loop)
*/
parseStructure(code: string): ParserMessage[] {
const parser = new StructureParser(code);
return parser.parse();
}
/**
* Extract all (pin, mode) pairs produced by for-loops containing
* `pinMode(loopVar, MODE)`. Handles:
* - Both braced bodies `{ ... }` and braceless single-statement bodies
* - Both `<` and `<=` comparisons
* - Type keywords: int, byte, uint8_t, unsigned int, unsigned, var, or none
*/
private extractLoopBodyFromCode(code: string, forMatch: RegExpExecArray): string {
let pos = forMatch.index + forMatch[0].length;
while (pos < code.length && /[ \t\r\n]/.test(code[pos])) pos++;
if (code[pos] === "{") {
let depth = 0;
let bodyStart = pos + 1;
for (let i = pos; i < code.length; i++) {
if (code[i] === "{") depth++;
else if (code[i] === "}") {
depth--;
Eif (depth === 0) return code.slice(bodyStart, i);
}
}
return "";
}
const semiIdx = code.indexOf(";", pos);
return semiIdx >= 0 ? code.slice(pos, semiIdx) : "";
}
private findPinModesInLoopBody(body: string, varName: string): Array<{ mode: PinMode }> {
const pinModeRe = new RegExp(
String.raw`\bpinMode\s*\(\s*${varName}\s*,\s*(INPUT_PULLUP|INPUT|OUTPUT)\s*\)`,
"g",
);
const modes: Array<{ mode: PinMode }> = [];
let pmMatch: RegExpExecArray | null;
while ((pmMatch = pinModeRe.exec(body)) !== null) {
modes.push({ mode: pmMatch[1] as PinMode });
}
return modes;
}
private getLoopPinModeCalls(code: string): PinModeCall[] {
const results: PinModeCall[] = [];
// Use both typed and bare for-loop regexes to avoid S5843
for (const forHeaderRe of [PARSER_PATTERNS.FOR_LOOP_TYPED, PARSER_PATTERNS.FOR_LOOP_BARE]) {
forHeaderRe.lastIndex = 0;
let forMatch: RegExpExecArray | null;
while ((forMatch = forHeaderRe.exec(code)) !== null) {
const varName = forMatch[1];
const startVal = Number.parseInt(forMatch[2], 10);
const op = forMatch[3];
const endVal = Number.parseInt(forMatch[4], 10);
const lastVal = op === "<=" ? endVal : endVal - 1;
const forLine = code.slice(0, Math.max(0, forMatch.index)).split("\n").length;
const body = this.extractLoopBodyFromCode(code, forMatch);
const modes = this.findPinModesInLoopBody(body, varName);
for (const { mode } of modes) {
for (let pin = startVal; pin <= lastVal; pin++) {
results.push({ pin, mode, line: forLine });
}
}
}
}
return results;
}
/**
* Detect pins configured in loops (e.g., for i=0; i<7 with pinMode(i, ...)
* Returns Set of numeric pin numbers that are likely configured by loop.
* Delegates to getLoopPinModeCalls so braceless and <= loops are covered.
*/
private getLoopConfiguredPins(code: string): Set<number> {
const configuredPins = new Set<number>();
for (const { pin } of this.getLoopPinModeCalls(removeCommentsHelper(code))) {
configuredPins.add(pin);
}
return configuredPins;
}
private checkAnalogWritePWM(code: string): ParserMessage[] {
const messages: ParserMessage[] = [];
const PWM_PINS = new Set([3, 5, 6, 9, 10, 11]);
const analogWriteRegex = PARSER_PATTERNS.ANALOG_WRITE;
let match;
while ((match = analogWriteRegex.exec(code)) !== null) {
const pinStr = match[1];
const pin = parsePinNumberHelper(pinStr);
if (pin !== undefined && !PWM_PINS.has(pin)) {
messages.push({
id: randomUUID(),
type: "warning",
category: "hardware",
severity: 2,
message: `analogWrite(${pinStr}, ...) used on pin ${pin}, which doesn't support PWM on Arduino UNO. PWM pins: 3, 5, 6, 9, 10, 11.`,
suggestion: `// Use PWM pin instead: analogWrite(3, value);`,
line: findLineNumberHelper(code, new RegExp(String.raw`analogWrite\s*\(\s*${pinStr}`)),
});
}
}
return messages;
}
private checkDigitalIOSetup(code: string, pinModeSet: Set<string>, loopConfiguredPins: Set<number>): ParserMessage[] {
const messages: ParserMessage[] = [];
const digitalReadWriteRegex = PARSER_PATTERNS.DIGITAL_READ_WRITE;
const warnedPins = new Set<string>();
let match;
while ((match = digitalReadWriteRegex.exec(code)) !== null) {
const pinStr = match[1];
if (/^\d+/.test(pinStr) || /^A\d+/.test(pinStr)) {
const pin = parsePinNumberHelper(pinStr);
if (!pinModeSet.has(pinStr) && (pin === undefined || !loopConfiguredPins.has(pin)) && !warnedPins.has(pinStr)) {
warnedPins.add(pinStr);
messages.push({
id: randomUUID(),
type: "warning",
category: "hardware",
severity: 2,
message: `Pin ${pinStr} used with digitalRead/digitalWrite but pinMode() was not called for this pin.`,
suggestion: `pinMode(${pinStr}, INPUT);`,
line: findLineNumberHelper(code, new RegExp(String.raw`digital(?:Read|Write)\s*\(\s*${pinStr}`)),
});
}
}
}
return messages;
}
private checkVariablePinUsage(code: string, uncommentedCode: string): ParserMessage[] {
const messages: ParserMessage[] = [];
// Identify pins configured via variable names
const pinModeVarRegex = PARSER_PATTERNS.PIN_MODE_VAR;
const pinModeVariables = new Set<string>();
let match;
while ((match = pinModeVarRegex.exec(uncommentedCode)) !== null) {
pinModeVariables.add(match[1]);
}
// Check if variable pins are used without pinMode and for dynamic usage
const digitalReadWriteRegex = PARSER_PATTERNS.DIGITAL_READ_WRITE;
let foundUnconfiguredVariable = false;
while ((match = digitalReadWriteRegex.exec(code)) !== null) {
const pinStr = match[1];
if (!/^\d+/.test(pinStr) && !/^A\d+/.test(pinStr)) {
if (!pinModeVariables.has(pinStr) && !foundUnconfiguredVariable) {
messages.push({
id: randomUUID(),
type: "warning",
category: "hardware",
severity: 2,
message: `Variable '${pinStr}' used in digitalRead/digitalWrite but no pinMode() call found for this variable.`,
suggestion: `pinMode(${pinStr}, INPUT);`,
line: findLineNumberHelper(code, new RegExp(String.raw`digital(?:Read|Write)\s*\(\s*${pinStr}`)),
});
foundUnconfiguredVariable = true;
}
}
}
// Check for dynamic pin usage without any pinMode configuration
const hasPinModeCalls = PARSER_PATTERNS.PIN_MODE_ANY.test(uncommentedCode);
if (!hasPinModeCalls && !foundUnconfiguredVariable) {
Iif (PARSER_PATTERNS.DIGITAL_DYNAMIC_PIN_READ.test(uncommentedCode) ||
PARSER_PATTERNS.DIGITAL_DYNAMIC_PIN_WRITE.test(uncommentedCode)) {
messages.push({
id: randomUUID(),
type: "warning",
category: "hardware",
severity: 2,
message: "digitalRead/digitalWrite uses variable pins without any pinMode() calls. Configure pinMode for the pins being read/written.",
suggestion: "pinMode(<pin>, INPUT);",
line: findLineNumberHelper(code, PARSER_PATTERNS.DIGITAL_READ_WRITE),
});
}
}
return messages;
}
parseHardwareCompatibility(code: string): ParserMessage[] {
const messages: ParserMessage[] = [];
const uncommentedCode = removeCommentsHelper(code);
const pinChecker = new PinCompatibilityChecker(uncommentedCode);
// Check analogWrite on non-PWM pins
messages.push(...this.checkAnalogWritePWM(code));
// Collect pinMode information
const pinModeSet = new Set<string>();
const pinModeRegex = PARSER_PATTERNS.PIN_MODE;
let match;
while ((match = pinModeRegex.exec(code)) !== null) {
pinModeSet.add(match[1]);
}
const pinModeCalls = pinChecker.getPinModeInfo((c) => this.getLoopPinModeCalls(c));
messages.push(...pinChecker.checkPinModeConflicts(pinModeCalls));
const loopConfiguredPins = this.getLoopConfiguredPins(code);
messages.push(
...this.checkDigitalIOSetup(code, pinModeSet, loopConfiguredPins),
...this.checkVariablePinUsage(code, uncommentedCode),
);
// Check OUTPUT pins being read
const outputPins = new Set<number>();
for (const [pin, entry] of pinModeCalls.entries()) {
const pinNum = parsePinNumberHelper(pin);
if (pinNum !== undefined && entry.modes.includes("OUTPUT")) outputPins.add(pinNum);
}
for (const { pin, mode } of this.getLoopPinModeCalls(uncommentedCode)) {
if (mode === "OUTPUT") outputPins.add(pin);
}
messages.push(...pinChecker.checkOutputPinsReadAsInput(uncommentedCode, outputPins, parsePinNumberHelper));
return messages;
}
/**
* Parse pin conflicts (same pin used as digital and analog)
*/
parsePinConflicts(code: string): ParserMessage[] {
const analyzer = new PinConflictAnalyzer(code);
return analyzer.analyze();
}
/**
* Parse performance issues
*/
parsePerformance(code: string): ParserMessage[] {
const uncommentedCode = removeCommentsHelper(code);
const analyzer = new PerformanceAnalyzer(uncommentedCode, code);
return [
...analyzer.analyzeComplexity(),
...analyzer.analyzeLargeArraysAndRecursion(),
];
}
/**
* Parse all categories and combine results
*/
parseAll(code: string): ParserMessage[] {
return [
...this.parseSerialConfiguration(code),
...this.parseStructure(code),
...this.parseHardwareCompatibility(code),
...this.parsePinConflicts(code),
...this.parsePerformance(code),
];
}
}
|