Skip to content

Commit

Permalink
Update the JSON parser to account for fabricereix changes
Browse files Browse the repository at this point in the history
  • Loading branch information
robozati authored and hurl-bot committed Nov 2, 2023
1 parent e0d9bb9 commit 97a02f3
Show file tree
Hide file tree
Showing 30 changed files with 98 additions and 87 deletions.
6 changes: 3 additions & 3 deletions integration/tests_error_parser/json.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: Parsing JSON
--> tests_error_parser/json.hurl:2:1
--> tests_error_parser/json.hurl:3:1
|
2 | { "name":
| ^ this brace is not closed later
3 | HTTP 200
| ^ expecting a boolean, number, string, array, object or null
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Parsing JSON
--> tests_error_parser/json_list_expecting_element.hurl:3:17
|
3 | "name": ["c", x
| ^ expecting a boolean, number, string, array, object or null
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POST http://localhost:8000/data
{
"name": ["c", x
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/json_list_expecting_element.hurl
3 changes: 3 additions & 0 deletions integration/tests_error_parser/json_list_expecting_element.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/json_list_expecting_element.hurl
7 changes: 7 additions & 0 deletions integration/tests_error_parser/json_list_trailing_comma.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Parsing JSON
--> tests_error_parser/json_list_trailing_comma.hurl:3:20
|
3 | "name": ["c", "d", ]
| ^ trailing comma is not allowed
|

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
4 changes: 4 additions & 0 deletions integration/tests_error_parser/json_list_trailing_comma.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POST http://localhost:8000/data
{
"name": ["c", "d", ]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/json_unexpected_character.hurl
hurl tests_error_parser/json_list_trailing_comma.hurl
3 changes: 3 additions & 0 deletions integration/tests_error_parser/json_list_trailing_comma.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/json_list_trailing_comma.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Parsing JSON
--> tests_error_parser/json_object_expecting_element.hurl:2:11
|
2 | { "name": x
| ^ expecting a boolean, number, string, array, object or null
|

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/json_object_expecting_element.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/json_object_expecting_element.hurl
7 changes: 7 additions & 0 deletions integration/tests_error_parser/json_object_trailing_comma.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Parsing JSON
--> tests_error_parser/json_object_trailing_comma.hurl:5:13
|
5 | "c": "d",
| ^ trailing comma is not allowed
|

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
POST http://localhost:8000/data
{
"toto": {
"a": "b",
"c": "d",
}
}
3 changes: 3 additions & 0 deletions integration/tests_error_parser/json_object_trailing_comma.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/json_object_trailing_comma.hurl
3 changes: 3 additions & 0 deletions integration/tests_error_parser/json_object_trailing_comma.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/json_object_trailing_comma.hurl
7 changes: 0 additions & 7 deletions integration/tests_error_parser/json_unexpected_character.err

This file was deleted.

3 changes: 0 additions & 3 deletions integration/tests_error_parser/json_unexpected_character.sh

This file was deleted.

4 changes: 2 additions & 2 deletions integration/tests_error_parser/json_unexpected_eof.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: Parsing JSON
--> tests_error_parser/json_unexpected_eof.hurl:2:1
--> tests_error_parser/json_unexpected_eof.hurl:2:10
|
2 | { "name":
| ^ this brace is not closed later
| ^ expecting an element; found empty element instead
|

7 changes: 2 additions & 5 deletions packages/hurl_core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,9 @@ impl Error for parser::Error {
ParseError::TemplateVariable => "expecting a variable".to_string(),
ParseError::Json(variant) => {
match variant {
JsonErrorVariant::UnexpectedCharcter { character } => format!("unexpected character: '{character}'"),
JsonErrorVariant::TrailingComma => "trailing comma is not allowed".to_string(),
JsonErrorVariant::EmptyElement => "expecting an element; found empty element instead".to_string(),
JsonErrorVariant::UnclosedBrace => "this brace is not closed later".to_string(),
JsonErrorVariant::CannotResolve { name } => {
format!("failed to resolve '{name}'. If it's a variable, try enclosing it with double braces, e.g. {{{{{name}}}}}")
}
JsonErrorVariant::ExpectingElement => "expecting a boolean, number, string, array, object or null".to_string(),
}
}
ParseError::Predicate => "expecting a predicate".to_string(),
Expand Down
6 changes: 4 additions & 2 deletions packages/hurl_core/src/parser/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ mod tests {
fn test_bytes_json_error() {
let mut reader = Reader::new("{ x ");
let error = bytes(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(error.pos, Pos { line: 1, column: 3 });
assert_eq!(
error.inner,
ParseError::Json(JsonErrorVariant::UnclosedBrace)
ParseError::Expecting {
value: "\"".to_string()
},
);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/hurl_core/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ pub enum ParseError {

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum JsonErrorVariant {
UnexpectedCharcter { character: String },
CannotResolve { name: String },
TrailingComma,
ExpectingElement,
EmptyElement,
UnclosedBrace,
}

impl Error {
Expand Down
74 changes: 15 additions & 59 deletions packages/hurl_core/src/parser/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,16 @@ fn parse_in_json(reader: &mut Reader) -> ParseResult<JsonValue> {
Err(e) => match e {
Error {
recoverable: true, ..
} => {
return Err(error::Error {
pos: e.pos,
recoverable: false,
inner: error::ParseError::Json(JsonErrorVariant::CannotResolve {
name: reader
.read_while(|c| !c.is_whitespace() && !c.is_ascii_punctuation()),
}),
})
}
_ => {
return Err(Error {
recoverable: false,
pos: e.pos,
inner: e.inner,
})
}
} => Err(error::Error {
pos: e.pos,
recoverable: false,
inner: error::ParseError::Json(JsonErrorVariant::ExpectingElement),
}),
_ => Err(Error {
recoverable: false,
pos: e.pos,
inner: e.inner,
}),
},
}
}
Expand Down Expand Up @@ -304,9 +297,7 @@ fn list_value(reader: &mut Reader) -> ParseResult<JsonValue> {
return Err(Error {
pos: save,
recoverable: false,
inner: ParseError::Json(JsonErrorVariant::UnexpectedCharcter {
character: ','.to_string(),
}),
inner: ParseError::Json(JsonErrorVariant::TrailingComma),
});
}
let element = list_element(reader)?;
Expand All @@ -330,9 +321,7 @@ fn list_element(reader: &mut Reader) -> ParseResult<JsonListElement> {
}

pub fn object_value(reader: &mut Reader) -> ParseResult<JsonValue> {
let save = reader.state.clone();
try_literal("{", reader)?;
peek_until_close_brace(reader, save)?;
let space0 = whitespace(reader);
let mut elements = vec![];
if reader.peek() != Some('}') {
Expand All @@ -358,9 +347,7 @@ pub fn object_value(reader: &mut Reader) -> ParseResult<JsonValue> {
return Err(Error {
pos: save,
recoverable: false,
inner: ParseError::Json(JsonErrorVariant::UnexpectedCharcter {
character: ','.to_string(),
}),
inner: ParseError::Json(JsonErrorVariant::TrailingComma),
});
}
let element = object_element(reader)?;
Expand Down Expand Up @@ -388,6 +375,7 @@ fn key(reader: &mut Reader) -> ParseResult<Template> {
Ok(name)
}
}

fn object_element(reader: &mut Reader) -> ParseResult<JsonObjectElement> {
let space0 = whitespace(reader);
//literal("\"", reader)?;
Expand Down Expand Up @@ -424,34 +412,6 @@ fn whitespace(reader: &mut Reader) -> String {
reader.read_while(|c| *c == ' ' || *c == '\t' || *c == '\n' || *c == '\r')
}

/// Helper to find if the user forgot to place a close brace, e.g. `{"a":\n`.
fn peek_until_close_brace(reader: &mut Reader, state: ReaderState) -> ParseResult<()> {
let mut offset = state.cursor;
// It's necessary to count the open braces found, because something like `{"a" : {"b": 1}` has
// a closing brace but the user still forgot to place the last brace.
let mut open_braces_found = 0;
loop {
if let Some(c) = reader.buffer.get(offset).copied() {
if c == '{' {
open_braces_found += 1;
} else if c == '}' {
if open_braces_found > 0 {
open_braces_found -= 1;
continue;
}
return Ok(());
}
} else {
return Err(Error {
pos: state.pos,
recoverable: false,
inner: ParseError::Json(JsonErrorVariant::UnclosedBrace),
});
}
offset += 1;
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -473,9 +433,7 @@ mod tests {
assert_eq!(error.pos, Pos { line: 1, column: 5 });
assert_eq!(
error.inner,
error::ParseError::Json(JsonErrorVariant::UnexpectedCharcter {
character: ','.to_string(),
}),
error::ParseError::Json(JsonErrorVariant::TrailingComma),
);
assert!(!error.recoverable);
}
Expand Down Expand Up @@ -874,9 +832,7 @@ mod tests {
assert_eq!(error.pos, Pos { line: 1, column: 6 });
assert_eq!(
error.inner,
error::ParseError::Json(JsonErrorVariant::UnexpectedCharcter {
character: ','.to_string(),
}),
error::ParseError::Json(JsonErrorVariant::TrailingComma),
);
assert!(!error.recoverable);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hurl_core/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn parse_hurl_file(s: &str) -> ParseResult<HurlFile> {
parsers::hurl_file(&mut reader)
}

pub use self::error::{Error, ParseError, JsonErrorVariant};
pub use self::error::{Error, JsonErrorVariant, ParseError};
pub use self::json::{
boolean_value as parse_json_boolean, null_value as parse_json_null,
number_value as parse_json_number, parse as parse_json,
Expand Down
2 changes: 1 addition & 1 deletion packages/hurl_core/src/parser/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ mod tests {

let mut reader = Reader::new("{x");
let error = body(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(error.pos, Pos { line: 1, column: 2 });
assert!(!error.recoverable);
}
}

0 comments on commit 97a02f3

Please sign in to comment.