Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions json/internal_types.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
///|
priv struct ParseContext {
mut offset : Int
input : String
input : StringView
end_offset : Int
mut remaining_available_depth : Int
}

///|
fn ParseContext::make(input : String, max_nesting_depth : Int) -> ParseContext {
fn ParseContext::make(
input : StringView,
max_nesting_depth : Int,
) -> ParseContext {
{
offset: 0,
input,
Expand Down
8 changes: 4 additions & 4 deletions json/lex_main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ fn ParseContext::lex_value(
match ctx.read_char() {
Some('0') => {
let (n, repr) = ctx.lex_zero(start=ctx.offset - 2)
return Number(n, repr)
return Number(n, repr.map(_.to_string()))
}
Some(c2) => {
if c2 is ('1'..='9') {
let (n, repr) = ctx.lex_decimal_integer(start=ctx.offset - 2)
return Number(n, repr)
return Number(n, repr.map(_.to_string()))
}
ctx.invalid_char(shift=-1)
}
None => raise InvalidEof
}
Some('0') => {
let (n, repr) = ctx.lex_zero(start=ctx.offset - 1)
return Number(n, repr)
return Number(n, repr.map(_.to_string()))
}
Some('1'..='9') => {
let (n, repr) = ctx.lex_decimal_integer(start=ctx.offset - 1)
return Number(n, repr)
return Number(n, repr.map(_.to_string()))
}
Some('"') => {
let s = ctx.lex_string()
Expand Down
18 changes: 9 additions & 9 deletions json/lex_number.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
fn ParseContext::lex_decimal_integer(
ctx : ParseContext,
start~ : Int,
) -> (Double, String?) raise ParseError {
) -> (Double, StringView?) raise ParseError {
for {
match ctx.read_char() {
Some('.') => return ctx.lex_decimal_point(start~)
Expand All @@ -37,7 +37,7 @@ fn ParseContext::lex_decimal_integer(
fn ParseContext::lex_decimal_point(
ctx : ParseContext,
start~ : Int,
) -> (Double, String?) raise ParseError {
) -> (Double, StringView?) raise ParseError {
match ctx.read_char() {
Some(c) =>
if c >= '0' && c <= '9' {
Expand All @@ -53,7 +53,7 @@ fn ParseContext::lex_decimal_point(
fn ParseContext::lex_decimal_fraction(
ctx : ParseContext,
start~ : Int,
) -> (Double, String?) raise ParseError {
) -> (Double, StringView?) raise ParseError {
for {
match ctx.read_char() {
Some('e' | 'E') => return ctx.lex_decimal_exponent(start~)
Expand All @@ -73,7 +73,7 @@ fn ParseContext::lex_decimal_fraction(
fn ParseContext::lex_decimal_exponent(
ctx : ParseContext,
start~ : Int,
) -> (Double, String?) raise ParseError {
) -> (Double, StringView?) raise ParseError {
match ctx.read_char() {
Some('+') | Some('-') => return ctx.lex_decimal_exponent_sign(start~)
Some(c) => {
Expand All @@ -91,7 +91,7 @@ fn ParseContext::lex_decimal_exponent(
fn ParseContext::lex_decimal_exponent_sign(
ctx : ParseContext,
start~ : Int,
) -> (Double, String?) raise ParseError {
) -> (Double, StringView?) raise ParseError {
match ctx.read_char() {
Some(c) => {
if c >= '0' && c <= '9' {
Expand All @@ -108,7 +108,7 @@ fn ParseContext::lex_decimal_exponent_sign(
fn ParseContext::lex_decimal_exponent_integer(
ctx : ParseContext,
start~ : Int,
) -> (Double, String?) {
) -> (Double, StringView?) {
for {
match ctx.read_char() {
Some(c) => {
Expand All @@ -127,7 +127,7 @@ fn ParseContext::lex_decimal_exponent_integer(
fn ParseContext::lex_zero(
ctx : ParseContext,
start~ : Int,
) -> (Double, String?) raise ParseError {
) -> (Double, StringView?) raise ParseError {
match ctx.read_char() {
Some('.') => ctx.lex_decimal_point(start~)
Some('e' | 'E') => ctx.lex_decimal_exponent(start~)
Expand All @@ -148,8 +148,8 @@ fn ParseContext::lex_number_end(
ctx : ParseContext,
start : Int,
end : Int,
) -> (Double, String?) {
let s = ctx.input.unsafe_substring(start~, end~)
) -> (Double, StringView?) {
let s = ctx.input.view(start_offset=start, end_offset=end)
if !s.contains(".") && !s.contains("e") && !s.contains("E") {
// If the string does not contain a decimal point or exponent, it is likely an integer
// We can try to parse it as an integer first
Expand Down
6 changes: 5 additions & 1 deletion json/lex_string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ fn ParseContext::lex_string(ctx : ParseContext) -> String raise ParseError {
let mut start = ctx.offset
fn flush(end : Int) {
if start > 0 && end > start {
buf.write_substring(ctx.input, start, end - start)
buf.write_substring(
ctx.input.data(),
ctx.input.start_offset() + start,
end - start,
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions json/parse.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

///|
pub fn valid(input : String) -> Bool {
pub fn valid(input : StringView) -> Bool {
try {
parse(input) |> ignore
true
Expand All @@ -25,7 +25,7 @@ pub fn valid(input : String) -> Bool {
///|
/// Parse a JSON input string into a Json value, with an optional maximum nesting depth (default is 1024)
pub fn parse(
input : String,
input : StringView,
max_nesting_depth? : Int = 1024,
) -> Json raise ParseError {
let ctx = ParseContext::make(input, max_nesting_depth)
Expand Down
4 changes: 2 additions & 2 deletions json/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ fn[T : FromJson] from_json(Json, path? : JsonPath) -> T raise JsonDecodeError
#callsite(autofill(args_loc, loc))
fn inspect(&ToJson, content? : Json, loc~ : SourceLoc, args_loc~ : ArgsLoc) -> Unit raise InspectError

fn parse(String, max_nesting_depth? : Int) -> Json raise ParseError
fn parse(StringView, max_nesting_depth? : Int) -> Json raise ParseError

fn valid(String) -> Bool
fn valid(StringView) -> Bool

// Errors
pub(all) suberror JsonDecodeError (JsonPath, String)
Expand Down
2 changes: 1 addition & 1 deletion json/utils.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

///|
fn offset_to_position(input : String, offset : Int) -> Position {
fn offset_to_position(input : StringView, offset : Int) -> Position {
let mut line = 1
let mut column = 0
for i in 0..<offset {
Expand Down
Loading