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

namespace Farkle.CSharp

open Farkle
open Farkle.Collections
open Farkle.PostProcessor
open System
open System.Text
open System.Runtime.CompilerServices
open System.Runtime.InteropServices

module RF = RuntimeFarkle

type PP<'a> = Farkle.PostProcessor.PostProcessor<'a>

[<Extension; AbstractClass; Sealed>]
/// <summary>Extension methods to easily create and work
/// with <see cref="RuntimeFarkle{TResult}"/>.</summary>
type RuntimeFarkleExtensions =
    static member private defaultEncoding enc = if isNull enc then Encoding.UTF8 else enc

    /// <summary>A <see cref="PostProcessor{Object}"/> that just checks if the given string is valid.</summary>
    /// <remarks>It always returns <c>null</c>.</remarks>
    static member SyntaxChecker = 
        {new PP<obj> with
            member __.Transform(_, _, _) = null
            member __.Fuse(_, _) = null}

    [<Extension>]
    /// <summary>Parses and post-processes a string.</summary>
    static member Parse(rf, str) = RF.parseString rf ignore str

    [<Extension>]
    /// <summary>Parses and post-processes a <see cref="ReadOnlyMemory{Char}"/>.</summary>
    static member Parse(rf, mem) = RF.parseMemory rf ignore mem

    [<Extension>]
    /// <summary>Parses and post-processes a <see cref="System.IO.Stream"/>,
    /// encoded with the given <see cref="System.Text.Encoding"/>.</summary>
    /// <remarks>
    /// <p>If not specified, the encoding defaults to UTF-8.</p>
    /// <p>The file can be read all at once in memory, if specified.</p>
    /// </remarks>
    static member Parse(rf, stream, [<Optional; DefaultParameterValue(null: Encoding)>] encoding, [<Optional; DefaultParameterValue(true)>] doLazyLoad) =
        let encoding = RuntimeFarkleExtensions.defaultEncoding encoding
        RF.parseStream rf ignore doLazyLoad encoding stream

    [<Extension>]
    /// <summary>Parses and post-processes the file at the given path, encoded with the given <see cref="Encoding"/>.</summary>
    /// <remarks>If not specified, the encoding defaults to UTF-8.</remarks>
    static member ParseFile(rf, fileName, [<Optional; DefaultParameterValue(null: Encoding)>] encoding) =
        let encoding = RuntimeFarkleExtensions.defaultEncoding encoding
        RF.parseFile rf ignore encoding fileName

    [<Extension>]
    /// <summary>Returns a new <see cref="RuntimeFarkle{TResult}"/> with a changed <see cref="PostProcessor"/>.</summary>
    static member ChangePostProcessor(rf: RuntimeFarkle<'TResult>, pp: Farkle.PostProcessor.PostProcessor<'TNewResult>) =
        RF.changePostProcessor pp rf

    [<Extension>]
    /// <summary>Returns a <see cref="RuntimeFarkle{Object}"/> that just checks if the given input is valid.</summary>
    /// <remarks>If syntax-checking succeeds, the value of the result will be always <c>null</c></remarks>
    static member SyntaxCheck(rf) = RF.changePostProcessor RuntimeFarkleExtensions.SyntaxChecker rf

[<Sealed>]
/// <summary>An object that contains a function to convert a <see cref="Production"/> to an arbitrary object.</summary>
type Fuser private(f: Func<obj[], obj>) =<commit_msg>Add support for logging from the C# API. Document the C# api better.<commit_after>// Copyright (c) 2019 <NAME>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

namespace Farkle.CSharp

open Farkle
open Farkle.Collections
open Farkle.PostProcessor
open System
open System.Text
open System.Runtime.CompilerServices
open System.Runtime.InteropServices

module RF = RuntimeFarkle

type PP<'a> = Farkle.PostProcessor.PostProcessor<'a>

[<Extension; AbstractClass; Sealed>]
/// <summary>Extension methods to easily create and work
/// with <see cref="RuntimeFarkle{TResult}"/>.</summary>
type RuntimeFarkleExtensions =

    static member private fLogIgnore = fun (_: Parser.ParseMessage) -> ()

    static member private defaultLogFunc f = if isNull f then RuntimeFarkleExtensions.fLogIgnore else FuncConvert.FromAction<_> f

    static member private defaultEncoding enc = if isNull enc then Encoding.UTF8 else enc

    /// <summary>A <see cref="PostProcessor{Object}"/> that just checks if the given string is valid.</summary>
    /// <remarks>It always returns <c>null</c>.</remarks>
    static member SyntaxChecker = 
        {new PP<obj> with
            member __.Transform(_, _, _) = null
            member __.Fuse(_, _) = null}

    [<Extension>]
    /// <summary>Parses and post-processes a string.</summary>
    /// <param name="str">The string to parse.</param>
    /// <param name="fLog">An optional delegate that gets called for ever parsing event that happens.</param>
    static member Parse(rf, str, [<Optional>] fLog) =
        let fLog = RuntimeFarkleExtensions.defaultLogFunc fLog
        RF.parseString rf fLog str

    [<Extension>]
    /// <summary>Parses and post-processes a <see cref="ReadOnlyMemory{Char}"/>.</summary>
    /// <param name="mem">The memory object to parse.</param>
    /// <param name="fLog">An optional delegate that gets called for ever parsing event that happens.</param>s
    static member Parse(rf, mem, [<Optional>] fLog) =
        let fLog = RuntimeFarkleExtensions.defaultLogFunc fLog
        RF.parseMemory rf fLog mem

    [<Extension>]
    /// <summary>Parses and post-processes a <see cref="System.IO.Stream"/>.</summary>
    /// <param name="stream">The stream to parse.</param>
    /// <param name="encoding">The character encoding of the stream's data. Defaults to UTF-8.</param>
    /// <param name="doLazyLoad">Whether to gradually read the input instead of reading its entirety in memory.
    /// Defaults to <c>true</c>.</param>
    /// <param name="fLog">An optional delegate that gets called for ever parsing event that happens.</param>
    static member Parse(rf, stream, [<Optional>] encoding, [<Optional; DefaultParameterValue(true)>] doLazyLoad, [<Optional>] fLog) =
        let encoding = RuntimeFarkleExtensions.defaultEncoding encoding
        let fLog = RuntimeFarkleExtensions.defaultLogFunc fLog
        RF.parseStream rf fLog doLazyLoad encoding stream

    [<Extension>]
    /// <summary>Parses and post-processes the file at the given path.</summary>
    /// <param name="fileName">The path of the file to parse.</param>
    /// <param name="encoding">The character encoding of the file's data. Defaults to UTF-8.</param>
    /// <param name="fLog">An optional delegate that gets called for ever parsing event that happens.</param>
    static member ParseFile(rf, fileName, [<Optional>] encoding, [<Optional>] fLog) =
        let encoding = RuntimeFarkleExtensions.defaultEncoding encoding
        let fLog = RuntimeFarkleExtensions.defaultLogFunc fLog
        RF.parseFile rf fLog encoding fileName

    [<Extension>]
    /// <summary>Returns a new <see cref="RuntimeFarkle{TResult}"/> with a changed <see cref="PostProcessor"/>.</summary>
    static member ChangePostProcessor(rf: RuntimeFarkle<'TResult>, pp: Farkle.PostProcessor.PostProcessor<'TNewResult>) =
        RF.changePostProcessor pp rf

    [<Extension>]
    /// <summary>Returns a <see cref="RuntimeFarkle{Object}"/> that just checks if its given input is valid.</summary>
    /// <remarks>If syntax-checking succeeds, the value of the result will be always <c>null</c></remarks>
    static member SyntaxCheck(rf) = RF.changePostProcessor RuntimeFarkleExtensions.SyntaxChecker rf

[<Sealed>]
/// <summary>An object that contains a function to convert a <see cref="Production"/> to an arbitrary object.</summary>
type Fuser private(f: Func<obj[], obj>) =
{'fraction_non_alphanumeric': 0.12315912941483123, 'fraction_numerical': 0.0018245796950345367, 'mean_word_length': 3.7546468401486988, 'pattern_counts': {'":': 0, '<': 146, '<?xml version=': 0, '>': 147, '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': '3813354', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2144, 'n_words': 704}