Skip to content

Implement complete RFC 8259 number format parsing and tests#13

Merged
ionux merged 1 commit intomainfrom
claude/json-parser-1.0-review-c8muX
Mar 5, 2026
Merged

Implement complete RFC 8259 number format parsing and tests#13
ionux merged 1 commit intomainfrom
claude/json-parser-1.0-review-c8muX

Conversation

@ionux
Copy link
Copy Markdown
Owner

@ionux ionux commented Mar 5, 2026

The old number scanner was a single while-loop over digits and '.'. It silently accepted illegal inputs (lone '-', leading zeros like '012') and caused OKJ_ERROR_SYNTAX on valid exponent notation like '1e10'.

Replace with an explicit four-step grammar walk that matches RFC 8259 §6:
number = [ minus ] int [ frac ] [ exp ]

Changes in src/ok_json.c:

  • Step 1 (minus): consume '-' only when a digit follows; reject bare '-' with OKJ_ERROR_BAD_NUMBER
  • Step 2 (int): accept '0' only when NOT followed by another digit (leading-zero rule); otherwise consume digit1-9 *DIGIT
  • Step 3 (frac): if '.' present, require and consume 1+ digits after it
  • Step 4 (exp): if 'e'/'E' present, consume optional sign then require and consume 1+ digits; reject 'e' or 'e+' with no digits

Valid formats now accepted: 0, -0, 42, -42, 3.14, -1.5, 1e10,
2.5E-3, 1E+2, 0.5e2

Invalid formats now rejected: '-', '012', '1.', '1e', '1e+'

Changes in test/ok_json_tests.c — 8 new tests:

  • test_number_negative: -42 accepted, length=3
  • test_number_float: 3.14 and -1.5 accepted with correct lengths
  • test_number_exponent: 1e10, 2.5E-3, 1E+2 all accepted
  • test_number_zero_variants: 0 (length=1) and -0 (length=2) accepted
  • test_number_invalid_lone_minus: '-' alone -> OKJ_ERROR_BAD_NUMBER
  • test_number_invalid_leading_zero: '012' -> OKJ_ERROR_BAD_NUMBER
  • test_number_invalid_trailing_decimal: '1.' -> OKJ_ERROR_BAD_NUMBER
  • test_number_invalid_exponent_no_digits: '1e' and '1e+' -> OKJ_ERROR_BAD_NUMBER

All 40 tests pass under -Wall -Wextra -Werror -pedantic -std=c99.

https://claude.ai/code/session_01UZpNhYhgmEd893sSpBNi6P

The old number scanner was a single while-loop over digits and '.'.
It silently accepted illegal inputs (lone '-', leading zeros like '012')
and caused OKJ_ERROR_SYNTAX on valid exponent notation like '1e10'.

Replace with an explicit four-step grammar walk that matches RFC 8259 §6:
  number = [ minus ] int [ frac ] [ exp ]

Changes in src/ok_json.c:
- Step 1 (minus): consume '-' only when a digit follows; reject bare '-'
  with OKJ_ERROR_BAD_NUMBER
- Step 2 (int): accept '0' only when NOT followed by another digit
  (leading-zero rule); otherwise consume digit1-9 *DIGIT
- Step 3 (frac): if '.' present, require and consume 1+ digits after it
- Step 4 (exp): if 'e'/'E' present, consume optional sign then require
  and consume 1+ digits; reject 'e' or 'e+' with no digits

Valid formats now accepted: 0, -0, 42, -42, 3.14, -1.5, 1e10,
  2.5E-3, 1E+2, 0.5e2

Invalid formats now rejected: '-', '012', '1.', '1e', '1e+'

Changes in test/ok_json_tests.c — 8 new tests:
- test_number_negative: -42 accepted, length=3
- test_number_float: 3.14 and -1.5 accepted with correct lengths
- test_number_exponent: 1e10, 2.5E-3, 1E+2 all accepted
- test_number_zero_variants: 0 (length=1) and -0 (length=2) accepted
- test_number_invalid_lone_minus: '-' alone -> OKJ_ERROR_BAD_NUMBER
- test_number_invalid_leading_zero: '012' -> OKJ_ERROR_BAD_NUMBER
- test_number_invalid_trailing_decimal: '1.' -> OKJ_ERROR_BAD_NUMBER
- test_number_invalid_exponent_no_digits: '1e' and '1e+' -> OKJ_ERROR_BAD_NUMBER

All 40 tests pass under -Wall -Wextra -Werror -pedantic -std=c99.

https://claude.ai/code/session_01UZpNhYhgmEd893sSpBNi6P
@ionux ionux merged commit 9838583 into main Mar 5, 2026
2 checks passed
@ionux ionux deleted the claude/json-parser-1.0-review-c8muX branch March 5, 2026 21:41
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.

2 participants