Skip to content

feat(decode): implement optimized dual-path parser with enhanced error reporting per #10#54

Merged
harningt merged 5 commits into
developfrom
feature/enhanced-error-reporting
May 26, 2026
Merged

feat(decode): implement optimized dual-path parser with enhanced error reporting per #10#54
harningt merged 5 commits into
developfrom
feature/enhanced-error-reporting

Conversation

@harningt

Copy link
Copy Markdown
Owner

Description

Overview

This PR implements positioned error reporting with line and column numbers.
It provides human-readable diagnostic messages for common syntax and validation
errors (e.g., trailing commas, duplicate/missing values, and missing separators) without introducing any performance overhead on successful parsing paths.

Key Improvements

  1. Dual-Path Parsing Architecture:
    • Fast-Path: Normal decodes execute using the original fast parser without capturing token positions or incurring pcall loop overhead.
    • Diagnostic-Path: If the fast-path parser fails, the decoder catches the error and executes a secondary diagnostics parser that captures position indices (lpeg.Cp()), wrapping the token state machine in a pcall to format precise line and column location info.
  2. Context-Aware Error Messages:
    • Upgraded state-machine assertions to provide clear, actionable explanations (e.g. "Expected value after ':'", "Expected key before ':'", and trailing comma disallowance reasons).
    • Captured error as a local upvalue to maintain strict compliance with sandbox environments (_ENV = nil).
  3. 1-Indexed Line & Column Info:
    • Fixed a bug in get_invalid_character_info where line numbers were 0-indexed, and resolved a regex limitation that prevented matching single-character lines.
  4. Behavior Clarifications & Code Annotations:
    • Added comments and README sections clarifying the interaction between trailingComma and allowEmptyElement (wherein a trailing comma is treated as a trailing empty element if allowEmptyElement is enabled).

Performance Comparison

Using the baseline test matrix benchmark (tests/dataTest.lua decoding a 500-key object 1,000 times on Lua 5.4):

  • Baseline (develop): 0.470s
  • Naive position tracking (Always-on): 0.666s (~41% overhead)
  • Dual-Path Design (This PR): 0.481s (~2% overhead / within run-to-run noise)

Testing & Validation

  • Integrated a new unit test suite tests/lunit-error-reporting.lua asserting exact line/column positions across 15 failure conditions.
  • Ran tests against the full matrix (Lua 5.1–5.5, LuaJIT, MoonJIT) with 100% pass rates.

@codecov

codecov Bot commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.06250% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.42%. Comparing base (d8f39af) to head (e883555).
⚠️ Report is 7 commits behind head on develop.

Files with missing lines Patch % Lines
lua/json/decode/util.lua 37.50% 5 Missing ⚠️
lua/json/decode.lua 94.11% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #54      +/-   ##
===========================================
- Coverage    95.90%   95.42%   -0.49%     
===========================================
  Files           18       18              
  Lines         1026     1071      +45     
===========================================
+ Hits           984     1022      +38     
- Misses          42       49       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@harningt harningt merged commit 3947bd8 into develop May 26, 2026
50 of 52 checks passed
@harningt harningt deleted the feature/enhanced-error-reporting branch May 26, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant