Implement complete RFC 8259 number format parsing and tests#13
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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:
All 40 tests pass under -Wall -Wextra -Werror -pedantic -std=c99.
https://claude.ai/code/session_01UZpNhYhgmEd893sSpBNi6P