Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 4 additions & 6 deletions src/commonMain/kotlin/org/kson/parser/MessageSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ data class LoggedMessage(
* Print a user-friendly version of a [List] of [LoggedMessage]
*/
fun print(messages: List<LoggedMessage>): String {
return messages.map {
val location = it.location
return messages.map { message ->
val location = message.location
"Error:${location.firstLine}.${location.firstColumn}" +
"\u2013${location.lastLine}.${location.lastColumn}, ${
it.message.format(
*it.args
)
" - ${location.lastLine}.${location.lastColumn}, ${
message.message.format(*message.args)
}"
}.joinToString("\n")
}
Expand Down
11 changes: 9 additions & 2 deletions src/commonMain/kotlin/org/kson/parser/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ class Parser(tokens: List<Token>) {
/**
* kson -> (objectInternals | value) EOF ;
*/
fun parse(): KsonRoot {
fun parse(): KsonRoot? {
val objectInternals = objectInternals()
if (objectInternals != null) {
return KsonRoot(ObjectDefinitionNode(internalsNode = objectInternals))
}

return KsonRoot(value() ?: TODO("make this a user-friendly parse error"))
val parsedValue = value()
// parser todo validate we're at EOF to ensure we've parsed everything

if (parsedValue != null) {
return KsonRoot(parsedValue)
} else {
// invalid KSON cannot be parsed into a useful AST
return null
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/commonTest/kotlin/org/kson/ResourceReader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.kson

expect fun readResourceAsString(path: String) : String
2 changes: 1 addition & 1 deletion src/commonTest/kotlin/org/kson/parser/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class ParserTest {
@Test
fun testSanityCheckParse() {
val nullTokenStream = listOf(Token(TokenType.NULL, Lexeme("null", Location(0, 0, 0, 4)), "null"))
assertEquals(Parser(nullTokenStream).parse().toKsonSource(0), "null")
assertEquals(Parser(nullTokenStream).parse()?.toKsonSource(0), "null")
}
}
70 changes: 70 additions & 0 deletions src/commonTest/kotlin/org/kson/parser/json/JSONParsingTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.kson.parser.json


import org.kson.Kson
import org.kson.ParseResult
import org.kson.parser.LoggedMessage
import org.kson.readResourceAsString
import kotlin.test.*

/**
* We execute the parsing tests provided by https://github.com/nst/JSONTestSuite.
* Tests are organized into files with one of the following prefixes:
* y_: content must be accepted by parsers
* n_: content must be rejected by parsers
* i_: parsers are free to accept or reject content
*/
class JSONParsingTests {

@Test
fun testJSONTestSuite() {
val allTestCases = readResourceAsString("JSONTestSuite/parsing-test-cases-list.txt")
val assertionErrorsByTestCase = LinkedHashMap<String, AssertionError>()

for (testCase in allTestCases.split("\n")) {
try {
when {
testCase.isBlank() -> continue
testCase.startsWith("test_parsing/i_") ->
// don't care whether this parses, just don't blow up
runTestCase(testCase)
testCase.startsWith("test_parsing/n_") ->
assertInvalidKson(testCase)
testCase.startsWith("test_parsing/y_") ->
assertValidKson(testCase)
else ->
// we should never get here; all test cases should be prefixed with i_, n_, or y_
throw IllegalStateException("unexpected test file: $testCase")
}
} catch (assertionError: AssertionError) {
// TODO: include file:// goodness for developers
println("Failure in test case $testCase: ${assertionError.stackTraceToString()}")
assertionErrorsByTestCase[testCase] = assertionError
}
}

assertTrue(assertionErrorsByTestCase.isEmpty(), "${assertionErrorsByTestCase.size} test cases failed")
}

private fun assertValidKson(testCase: String): ParseResult {
val result = runTestCase(testCase)
val detailedMessage = LoggedMessage.print(result.messages)
if (result.messages.isNotEmpty()) {
// let's fail with formatted details
fail("Expected no messages but got ${result.messages.size}: \n $detailedMessage")
}
assertNotNull(result.ast, "Expected the parsed AST to be non-null")
return result
}

private fun assertInvalidKson(testCase: String) {
val result = runTestCase(testCase)
assertTrue(result.messages.isNotEmpty()) // there must be some error messages
assertNull(result.ast) // we should not have any AST generated
}

private fun runTestCase(testCase: String): ParseResult {
val contents = readResourceAsString("JSONTestSuite/$testCase")
return Kson.parse(contents)
}
}
27 changes: 27 additions & 0 deletions src/commonTest/resources/JSONTestSuite/JSONTestSuite-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
The original JSONTestSuite license is included here:
https://github.com/nst/JSONTestSuite/blob/master/LICENSE

--


MIT License

Copyright (c) 2016 Nicolas Seriot

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
# TODO: Issue #22 Enable "y_" tests
test_parsing/y_number_double_close_to_zero.json
test_parsing/y_number_minus_zero.json
test_parsing/y_number_negative_int.json
test_parsing/y_number_negative_one.json
test_parsing/y_number_negative_zero.json
test_parsing/y_object_extreme_numbers.json
test_parsing/y_structure_lonely_negative_real.json

# TODO: Issue #21 Enable more tests that require parse failures
test_parsing/n_array_1_true_without_comma.json
test_parsing/n_array_a_invalid_utf8.json
test_parsing/n_array_colon_instead_of_comma.json
test_parsing/n_array_comma_after_close.json
test_parsing/n_array_comma_and_number.json
test_parsing/n_array_double_comma.json
test_parsing/n_array_double_extra_comma.json
test_parsing/n_array_extra_close.json
test_parsing/n_array_extra_comma.json
test_parsing/n_array_incomplete_invalid_value.json
test_parsing/n_array_incomplete.json
test_parsing/n_array_inner_array_no_comma.json
test_parsing/n_array_invalid_utf8.json
test_parsing/n_array_items_separated_by_semicolon.json
test_parsing/n_array_just_comma.json
test_parsing/n_array_just_minus.json
test_parsing/n_array_missing_value.json
test_parsing/n_array_newlines_unclosed.json
test_parsing/n_array_number_and_comma.json
test_parsing/n_array_number_and_several_commas.json
test_parsing/n_array_spaces_vertical_tab_formfeed.json
test_parsing/n_array_star_inside.json
test_parsing/n_array_unclosed.json
test_parsing/n_array_unclosed_trailing_comma.json
test_parsing/n_array_unclosed_with_new_lines.json
test_parsing/n_array_unclosed_with_object_inside.json
test_parsing/n_incomplete_false.json
test_parsing/n_incomplete_null.json
test_parsing/n_incomplete_true.json
test_parsing/n_multidigit_number_then_00.json
test_parsing/n_number_0.1.2.json
test_parsing/n_number_-01.json
test_parsing/n_number_0.3e+.json
test_parsing/n_number_0.3e.json
test_parsing/n_number_0_capital_E+.json
test_parsing/n_number_0_capital_E.json
test_parsing/n_number_0.e1.json
test_parsing/n_number_0e+.json
test_parsing/n_number_0e.json
test_parsing/n_number_1_000.json
test_parsing/n_number_1.0e+.json
test_parsing/n_number_1.0e-.json
test_parsing/n_number_1.0e.json
test_parsing/n_number_-1.0..json
test_parsing/n_number_1eE2.json
test_parsing/n_number_+1.json
test_parsing/n_number_.-1.json
test_parsing/n_number_2.e+3.json
test_parsing/n_number_2.e-3.json
test_parsing/n_number_2.e3.json
test_parsing/n_number_.2e-3.json
test_parsing/n_number_-2..json
test_parsing/n_number_9.e+.json
test_parsing/n_number_expression.json
test_parsing/n_number_hex_1_digit.json
test_parsing/n_number_hex_2_digits.json
test_parsing/n_number_infinity.json
test_parsing/n_number_+Inf.json
test_parsing/n_number_Inf.json
test_parsing/n_number_invalid+-.json
test_parsing/n_number_invalid-negative-real.json
test_parsing/n_number_invalid-utf-8-in-bigger-int.json
test_parsing/n_number_invalid-utf-8-in-exponent.json
test_parsing/n_number_invalid-utf-8-in-int.json
test_parsing/n_number_++.json
test_parsing/n_number_minus_infinity.json
test_parsing/n_number_minus_sign_with_trailing_garbage.json
test_parsing/n_number_minus_space_1.json
test_parsing/n_number_-NaN.json
test_parsing/n_number_NaN.json
test_parsing/n_number_neg_int_starting_with_zero.json
test_parsing/n_number_neg_real_without_int_part.json
test_parsing/n_number_neg_with_garbage_at_end.json
test_parsing/n_number_real_garbage_after_e.json
test_parsing/n_number_real_with_invalid_utf8_after_e.json
test_parsing/n_number_real_without_fractional_part.json
test_parsing/n_number_starting_with_dot.json
test_parsing/n_number_U+FF11_fullwidth_digit_one.json
test_parsing/n_number_with_alpha_char.json
test_parsing/n_number_with_alpha.json
test_parsing/n_number_with_leading_zero.json
test_parsing/n_object_bad_value.json
test_parsing/n_object_bracket_key.json
test_parsing/n_object_comma_instead_of_colon.json
test_parsing/n_object_double_colon.json
test_parsing/n_object_emoji.json
test_parsing/n_object_garbage_at_end.json
test_parsing/n_object_key_with_single_quotes.json
test_parsing/n_object_lone_continuation_byte_in_key_and_trailing_comma.json
test_parsing/n_object_missing_colon.json
test_parsing/n_object_missing_key.json
test_parsing/n_object_missing_semicolon.json
test_parsing/n_object_missing_value.json
test_parsing/n_object_no-colon.json
test_parsing/n_object_non_string_key_but_huge_number_instead.json
test_parsing/n_object_non_string_key.json
test_parsing/n_object_repeated_null_null.json
test_parsing/n_object_several_trailing_commas.json
test_parsing/n_object_single_quote.json
test_parsing/n_object_trailing_comma.json
test_parsing/n_object_trailing_comment.json
test_parsing/n_object_trailing_comment_open.json
test_parsing/n_object_trailing_comment_slash_open_incomplete.json
test_parsing/n_object_trailing_comment_slash_open.json
test_parsing/n_object_two_commas_in_a_row.json
test_parsing/n_object_unquoted_key.json
test_parsing/n_object_unterminated-value.json
test_parsing/n_object_with_single_string.json
test_parsing/n_object_with_trailing_garbage.json
test_parsing/n_single_space.json
test_parsing/n_string_1_surrogate_then_escape.json
test_parsing/n_string_1_surrogate_then_escape_u1.json
test_parsing/n_string_1_surrogate_then_escape_u1x.json
test_parsing/n_string_1_surrogate_then_escape_u.json
test_parsing/n_string_accentuated_char_no_quotes.json
test_parsing/n_string_backslash_00.json
test_parsing/n_string_escaped_backslash_bad.json
test_parsing/n_string_escaped_ctrl_char_tab.json
test_parsing/n_string_escaped_emoji.json
test_parsing/n_string_escape_x.json
test_parsing/n_string_incomplete_escaped_character.json
test_parsing/n_string_incomplete_escape.json
test_parsing/n_string_incomplete_surrogate_escape_invalid.json
test_parsing/n_string_incomplete_surrogate.json
test_parsing/n_string_invalid_backslash_esc.json
test_parsing/n_string_invalid_unicode_escape.json
test_parsing/n_string_invalid_utf8_after_escape.json
test_parsing/n_string_invalid-utf-8-in-escape.json
test_parsing/n_string_leading_uescaped_thinspace.json
test_parsing/n_string_no_quotes_with_bad_escape.json
test_parsing/n_string_single_doublequote.json
test_parsing/n_string_single_quote.json
test_parsing/n_string_single_string_no_double_quotes.json
test_parsing/n_string_start_escape_unclosed.json
test_parsing/n_string_unescaped_ctrl_char.json
test_parsing/n_string_unescaped_newline.json
test_parsing/n_string_unescaped_tab.json
test_parsing/n_string_unicode_CapitalU.json
test_parsing/n_string_with_trailing_garbage.json
test_parsing/n_structure_100000_opening_arrays.json
test_parsing/n_structure_angle_bracket_..json
test_parsing/n_structure_angle_bracket_null.json
test_parsing/n_structure_array_trailing_garbage.json
test_parsing/n_structure_array_with_extra_array_close.json
test_parsing/n_structure_array_with_unclosed_string.json
test_parsing/n_structure_ascii-unicode-identifier.json
test_parsing/n_structure_capitalized_True.json
test_parsing/n_structure_close_unopened_array.json
test_parsing/n_structure_comma_instead_of_closing_brace.json
test_parsing/n_structure_double_array.json
test_parsing/n_structure_end_array.json
test_parsing/n_structure_incomplete_UTF8_BOM.json
test_parsing/n_structure_lone-invalid-utf-8.json
test_parsing/n_structure_lone-open-bracket.json
test_parsing/n_structure_no_data.json
test_parsing/n_structure_null-byte-outside-string.json
test_parsing/n_structure_number_with_trailing_garbage.json
test_parsing/n_structure_object_followed_by_closing_object.json
test_parsing/n_structure_object_unclosed_no_value.json
test_parsing/n_structure_object_with_comment.json
test_parsing/n_structure_object_with_trailing_garbage.json
test_parsing/n_structure_open_array_apostrophe.json
test_parsing/n_structure_open_array_comma.json
test_parsing/n_structure_open_array_object.json
test_parsing/n_structure_open_array_open_object.json
test_parsing/n_structure_open_array_open_string.json
test_parsing/n_structure_open_array_string.json
test_parsing/n_structure_open_object_close_array.json
test_parsing/n_structure_open_object_comma.json
test_parsing/n_structure_open_object.json
test_parsing/n_structure_open_object_open_array.json
test_parsing/n_structure_open_object_open_string.json
test_parsing/n_structure_open_object_string_with_apostrophes.json
test_parsing/n_structure_open_open.json
test_parsing/n_structure_single_eacute.json
test_parsing/n_structure_single_star.json
test_parsing/n_structure_trailing_#.json
test_parsing/n_structure_U+2060_word_joined.json
test_parsing/n_structure_uescaped_LF_before_string.json
test_parsing/n_structure_unclosed_array.json
test_parsing/n_structure_unclosed_array_partial_null.json
test_parsing/n_structure_unclosed_array_unfinished_false.json
test_parsing/n_structure_unclosed_array_unfinished_true.json
test_parsing/n_structure_unclosed_object.json
test_parsing/n_structure_unicode-identifier.json
test_parsing/n_structure_UTF8_BOM_no_data.json
test_parsing/n_structure_whitespace_formfeed.json
test_parsing/n_structure_whitespace_U+2060_word_joiner.json

# TODO: Issue #23 Enable "i_" tests
test_parsing/i_number_neg_int_huge_exp.json
test_parsing/i_number_real_neg_overflow.json
test_parsing/i_number_too_big_neg_int.json
test_parsing/i_number_very_big_negative_int.json
test_parsing/i_string_utf16BE_no_BOM.json
test_parsing/i_string_UTF-16LE_with_BOM.json
test_parsing/i_structure_UTF-8_BOM_empty_object.json
Loading