<commit_before>﻿//  Driver.fs contains entry point of MS-SQL parser.
//
//  Copyright 2012 <NAME> <<EMAIL>>
//
//  This file is part of YaccConctructor.
//
//  YaccConstructor is free software:you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

module MSSqlParser

open Microsoft.FSharp.Text.Lexing
open Yard.Generators.RNGLR.AST
open Yard.Examples.MSParser

let justParse (path:string) =
    use reader = new System.IO.StreamReader(path)
    let lexbuf = LexBuffer<_>.FromTextReader reader
    let allTokens = seq{while not lexbuf.IsPastEndOfStream do yield Lexer.tokens lexbuf}

    let translateArgs = {
        tokenToRange = fun x -> 0,0
        zeroPosition = 0
        clearAST = false
        filterEpsilons = true
    }

    buildAst allTokens

let Parse (srcFilePath:string) =    
    match justParse srcFilePath with
    | Yard.Generators.RNGLR.Parser.Error (num, tok, msg,dbg) ->
        printfn "Error in file %s on position %d on Token %A: %s" srcFilePath num tok msg
        dbg.drawGSSDot @"..\..\stack.dot"
    | Yard.Generators.RNGLR.Parser.Success ast ->
        ast.collectWarnings (fun x -> 0,0)
        |> ResizeArray.iter (fun (pos, prods) -> ())
        defaultAstToDot ast @"..\..\ast.dot"
        //ast.ChooseLongestMatch()
        //let translated = translate translateArgs ast : list<Script>            
        //printfn "%A" translated
        //translated.Head  
        
  //let ParseAllDirectory (directoryName:string) =
  //let files : string array = System.IO.Dyrectory.GetFiles(directoryName)  
  //for i in 0 .. files.Length - 1 do
  //Parse Array.get files i 
  
  //do ParseAllDirecrory @"..\..\..\..\..\Tests\Materials\ms-sql\sqlsrvanalysissrvcs

do Parse @"..\..\..\..\..\Tests\Materials\ms-sql\sqlsrvanalysissrvcs\MonitoringSSAS\config_data_server\get_query_text.sql"
<commit_msg>Comment off ParseAllDirectory<commit_after>﻿//  Driver.fs contains entry point of MS-SQL parser.
//
//  Copyright 2012 <NAME> <<EMAIL>>
//
//  This file is part of YaccConctructor.
//
//  YaccConstructor is free software:you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

module MSSqlParser

open Microsoft.FSharp.Text.Lexing
open Yard.Generators.RNGLR.AST
open Yard.Examples.MSParser

let justParse (path:string) =
    use reader = new System.IO.StreamReader(path)
    let lexbuf = LexBuffer<_>.FromTextReader reader
    let allTokens = seq{while not lexbuf.IsPastEndOfStream do yield Lexer.tokens lexbuf}

    let translateArgs = {
        tokenToRange = fun x -> 0,0
        zeroPosition = 0
        clearAST = false
        filterEpsilons = true
    }

    buildAst allTokens

let Parse (srcFilePath:string) =    
    match justParse srcFilePath with
    | Yard.Generators.RNGLR.Parser.Error (num, tok, msg,dbg) ->
        printfn "Error in file %s on position %d on Token %A: %s" srcFilePath num tok msg
        dbg.drawGSSDot @"..\..\stack.dot"
    | Yard.Generators.RNGLR.Parser.Success ast ->
        ast.collectWarnings (fun x -> 0,0)
        |> ResizeArray.iter (fun (pos, prods) -> ())
        defaultAstToDot ast @"..\..\ast.dot"
        //ast.ChooseLongestMatch()
        //let translated = translate translateArgs ast : list<Script>            
        //printfn "%A" translated
        //translated.Head  
        

let ParseAllDirectory (directoryName:string) =
  let files : string array = System.IO.Dyrectory.GetFiles(directoryName)  
  for i in 0 .. files.Length - 1 do
  Parse Array.get files i 
  
  //do ParseAllDirecrory @"..\..\..\..\..\Tests\Materials\ms-sql\sqlsrvanalysissrvcs

do Parse @"..\..\..\..\..\Tests\Materials\ms-sql\sqlsrvanalysissrvcs\MonitoringSSAS\config_data_server\get_query_text.sql"

{'fraction_non_alphanumeric': 0.10826210826210826, 'fraction_numerical': 0.004884004884004884, 'mean_word_length': 2.4685956245589273, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6620582', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1459, 'n_words': 472}