<commit_before>// Copyright (c) 2021 <NAME>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

// The other two parsing libraries do not support syntax checking out
// of the box like Farkle. Their implementation will be placed here.
namespace Farkle.Benchmarks.JsonSyntaxCheckers

// Thankfully FsLexYacc does not require rewriting a complete
// grammar; only its "post-processor" needs to be rewritten.
module FsLexYacc =

    open FSharp.Text.Lexing
    open FsLexYacc.JSON.Lexer
    open FsLexYacc.JSON.Parser

    // I know that not reusing the same tables object is horribly
    // inefficient but that's exactly what FsYacc does.
    let private syntaxCheckParserTables() =
        let tables = tables()
        {tables with
            reductions = Array.replicate tables.reductions.Length (fun _ -> null)}

    let private floatToken = FLOAT (Chiron.Json.Number 0m)
    let private stringToken = STRING ""

    // The "transformer" needs to be rewritten to ensure it does not allocate.
    let rec private syntaxCheckLexerRead lexbuf =
        match _fslex_tables.Interpret(6, lexbuf) with
        | 0 -> syntaxCheckLexerRead lexbuf
        | 1 ->
            lexbuf.StartPos <- lexbuf.StartPos.NextLine
            syntaxCheckLexerRead lexbuf
        | 2 -> floatToken
        | 3 -> TRUE
        | 4 -> FALSE
        | 5 -> NULL
        | 6 -> syntax_check_read_string false lexbuf
        | 7 -> LEFT_BRACE
        | 8 -> RIGHT_BRACE
        | 9 -> LEFT_BRACK
        | 10 -> RIGHT_BRACK
        | 11 -> COLON
        | 12 -> COMMA
        | 13 -> EOF
        | 14 ->
            failwithf "SyntaxError: Unexpected char: '%s' Line: %d Column: %d"
                (LexBuffer<_>.LexemeString lexbuf) (lexbuf.StartPos.Line + 1) lexbuf.StartPos.Column
        | _ -> failwith "read"

    and private syntax_check_read_string ignorequote lexbuf =
        match _fslex_tables.Interpret(0, lexbuf) with
        | 0 ->
            if ignorequote then
                syntax_check_read_string false lexbuf
            else
                stringToken
        | 1 -> syntax_check_read_string true lexbuf
        | 2 -> syntax_check_read_string false lexbuf
        | 3 -> failwith "String is not terminated"
        | _ -> failwith "read_string"

    let parseString x =
        let lexBuf = LexBuffer<_>.FromString x
        syntaxCheckParserTables().Interpret(syntaxCheckLexerRead, lexBuf, 0) |> ignore
        Unchecked.defaultof<Chiron.Json>

    let parseTextReader tr =
        let lexBuf = LexBuffer<_>.FromTextReader tr
        syntaxCheckParserTables().Interpret(syntaxCheckLexerRead, lexBuf, 0) |> ignore
        Unchecked.defaultof<Chiron.Json>

// Because FParsec's "grammars" and "post-processors" are
// tightly coupled, the entire parser has to be rewritten
// to parse JSON but keep nothing. Code adapted from
// Chiron's 6.x.x branch.
module Chiron =

    open FParsec

    let (.>>) (x1: Parser<_,unit>) x2 = x1 .>> x2

    module private Escaping =<commit_msg>Update the syntax-checking FsYacc parser to work with FsYacc 10.2.0<commit_after>// Copyright (c) 2021 <NAME>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

// The other two parsing libraries do not support syntax checking out
// of the box like Farkle. Their implementation will be placed here.
namespace Farkle.Benchmarks.JsonSyntaxCheckers

// Thankfully FsLexYacc does not require rewriting a complete
// grammar; only its "post-processor" needs to be rewritten.
module FsLexYacc =

    open FSharp.Text.Lexing
    open FsLexYacc.JSON.Lexer
    open FsLexYacc.JSON.Parser

    let private syntaxCheckParserTables =
        let tables = tables
        {tables with
            reductions = Array.replicate tables.reductions.Length (fun _ -> null)}

    let private floatToken = FLOAT (Chiron.Json.Number 0m)
    let private stringToken = STRING ""

    // The "transformer" needs to be rewritten to ensure it does not allocate.
    let rec private syntaxCheckLexerRead lexbuf =
        match _fslex_tables.Interpret(6, lexbuf) with
        | 0 -> syntaxCheckLexerRead lexbuf
        | 1 ->
            lexbuf.StartPos <- lexbuf.StartPos.NextLine
            syntaxCheckLexerRead lexbuf
        | 2 -> floatToken
        | 3 -> TRUE
        | 4 -> FALSE
        | 5 -> NULL
        | 6 -> syntax_check_read_string false lexbuf
        | 7 -> LEFT_BRACE
        | 8 -> RIGHT_BRACE
        | 9 -> LEFT_BRACK
        | 10 -> RIGHT_BRACK
        | 11 -> COLON
        | 12 -> COMMA
        | 13 -> EOF
        | 14 ->
            failwithf "SyntaxError: Unexpected char: '%s' Line: %d Column: %d"
                (LexBuffer<_>.LexemeString lexbuf) (lexbuf.StartPos.Line + 1) lexbuf.StartPos.Column
        | _ -> failwith "read"

    and private syntax_check_read_string ignorequote lexbuf =
        match _fslex_tables.Interpret(0, lexbuf) with
        | 0 ->
            if ignorequote then
                syntax_check_read_string false lexbuf
            else
                stringToken
        | 1 -> syntax_check_read_string true lexbuf
        | 2 -> syntax_check_read_string false lexbuf
        | 3 -> failwith "String is not terminated"
        | _ -> failwith "read_string"

    let parseString x =
        let lexBuf = LexBuffer<_>.FromString x
        syntaxCheckParserTables.Interpret(syntaxCheckLexerRead, lexBuf, 0) |> ignore
        Unchecked.defaultof<Chiron.Json>

    let parseTextReader tr =
        let lexBuf = LexBuffer<_>.FromTextReader tr
        syntaxCheckParserTables.Interpret(syntaxCheckLexerRead, lexBuf, 0) |> ignore
        Unchecked.defaultof<Chiron.Json>

// Because FParsec's "grammars" and "post-processors" are
// tightly coupled, the entire parser has to be rewritten
// to parse JSON but keep nothing. Code adapted from
// Chiron's 6.x.x branch.
module Chiron =

    open FParsec

    let (.>>) (x1: Parser<_,unit>) x2 = x1 .>> x2

    module private Escaping =
{'fraction_non_alphanumeric': 0.08553732842316705, 'fraction_numerical': 0.013726146635420154, 'mean_word_length': 2.3492152466367715, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6637965', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1720, 'n_words': 537}