<commit_before>// Copyright 2010-2013 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdio>
#include "flatzinc2/parser.h"
#include "flatzinc2/parser.tab.hh"

// Declare external functions in the flatzinc.tab.cc generated file.
extern int orfz_parse(operations_research::FzParserContext* parser,
                      operations_research::FzModel* model, bool* ok,
                      void* scanner);
extern int orfz_lex_init(void** scanner);
extern int orfz_lex_destroy(void* scanner);
extern void orfz_set_in(FILE* in_str, void* yyscanner);
// Declare external functions and structures in the flatzinc.yy.cc
// generated file.
struct yy_buffer_state;
extern yy_buffer_state* orfz__scan_bytes(const char* input, size_t size,
                                         void* scanner);
extern void orfz__delete_buffer(yy_buffer_state* b, void* scanner);

namespace operations_research {
// ----- public parsing API -----

bool ParseFlatzincFile(const std::string& filename, FzModel* const model) {
  // Init.
  FILE* const input = fopen(filename.c_str(), "r");
  if (input == nullptr) {
    LOG(INFO) << "Could not open file " << filename;
    return false;
  }
  FzParserContext context;
  bool ok = true;
  void* scanner = nullptr;
  orfz_lex_init(&scanner);
  orfz_set_in(input, scanner);
  // Parse.
  orfz_parse(&context, model, &ok, scanner);
  // Clean up.
  if (scanner != nullptr) {
    orfz_lex_destroy(scanner);
  }
  fclose(input);
  return ok;
}

bool ParseFlatzincString(const std::string& input, FzModel* const model) {
  // Init.
  FzParserContext context;
  bool ok = true;
  void* scanner = nullptr;
  orfz_lex_init(&scanner);
  yy_buffer_state* const string_buffer =
      orfz__scan_bytes(input.data(), input.size(), scanner);
  // Parse.
  orfz_parse(&context, model, &ok, scanner);
  // Clean up.
  if (string_buffer != nullptr) {
    orfz__delete_buffer(string_buffer, scanner);
  }
  if (scanner != nullptr) {
    orfz_lex_destroy(scanner);
  }
  return ok;
}
}  // namespace operations_research<commit_msg>print parse time<commit_after>// Copyright 2010-2013 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdio>
#include "base/file.h"
#include "base/timer.h"
#include "flatzinc2/parser.h"
#include "flatzinc2/parser.tab.hh"

// Declare external functions in the flatzinc.tab.cc generated file.
extern int orfz_parse(operations_research::FzParserContext* parser,
                      operations_research::FzModel* model, bool* ok,
                      void* scanner);
extern int orfz_lex_init(void** scanner);
extern int orfz_lex_destroy(void* scanner);
extern void orfz_set_in(FILE* in_str, void* yyscanner);
// Declare external functions and structures in the flatzinc.yy.cc
// generated file.
struct yy_buffer_state;
extern yy_buffer_state* orfz__scan_bytes(const char* input, size_t size,
                                         void* scanner);
extern void orfz__delete_buffer(yy_buffer_state* b, void* scanner);

DECLARE_bool(fz_verbose);

namespace operations_research {
// ----- public parsing API -----

bool ParseFlatzincFile(const std::string& filename, FzModel* const model) {
  // Init.
  FILE* const input = fopen(filename.c_str(), "r");
  if (input == nullptr) {
    LOG(INFO) << "Could not open file " << filename;
    return false;
  }
  WallTimer timer;
  timer.Start();
  FzParserContext context;
  bool ok = true;
  void* scanner = nullptr;
  orfz_lex_init(&scanner);
  orfz_set_in(input, scanner);
  // Parse.
  orfz_parse(&context, model, &ok, scanner);
  // Clean up.
  if (scanner != nullptr) {
    orfz_lex_destroy(scanner);
  }
  fclose(input);
  FZVLOG << "Model " << filename << " parsed in " << timer.GetInMs() << " ms"
         << FZENDL;
  return ok;
}

bool ParseFlatzincString(const std::string& input, FzModel* const model) {
  // Init.
  FzParserContext context;
  bool ok = true;
  void* scanner = nullptr;
  orfz_lex_init(&scanner);
  yy_buffer_state* const string_buffer =
      orfz__scan_bytes(input.data(), input.size(), scanner);
  // Parse.
  orfz_parse(&context, model, &ok, scanner);
  // Clean up.
  if (string_buffer != nullptr) {
    orfz__delete_buffer(string_buffer, scanner);
  }
  if (scanner != nullptr) {
    orfz_lex_destroy(scanner);
  }
  return ok;
}
}  // namespace operations_research
{'fraction_non_alphanumeric': 0.11539184363841383, 'fraction_numerical': 0.005262168765269686, 'mean_word_length': 3.5216652506372133, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5181823', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1641, 'n_words': 505}