<commit_before>
//
// Copyright (c) 2014-2015, <NAME>, <EMAIL>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Made in Japan.
//

// https://github.com/flon-io/aranye


#define _POSIX_C_SOURCE 200809L

#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "aabro.h"
#include "fara_haml.h"


//fabr_parser *haml_parser = NULL;
//fabr_parser *header_parser = NULL;
//
//void haml_parser_init()
//{
//  fabr_parser *jsentry =
//    fabr_n_seq(
//      "jsentry",
//      fabr_n_rex("k", "[^ \t\r\n:]+"),
//      fabr_rex("[ \t]*:[ \t]*"),
//      fabr_alt(
//        fabr_seq(
//          fabr_string("\""),
//          fabr_n_rex("v", "[^\"\r\n,]*"),
//          fabr_string("\""),
//          NULL),
//        fabr_seq(
//          fabr_string("'"),
//          fabr_n_rex("v", "[^'\r\n,]*"),
//          fabr_string("'"),
//          NULL),
//        fabr_n_rex("v", "[^ \t\r\n,]+"),
//        NULL),
//      NULL);
//  fabr_parser *jsatts =
//    fabr_n_seq(
//      "jsatts",
//      fabr_rex("[ \t]*\\{[ \t]*"),
//      fabr_seq(
//        jsentry, fabr_seq(fabr_rex("[ \t]*,[ \t]*"), jsentry, fabr_r("*")), NULL
//      ), fabr_q("?"),
//      fabr_rex("[ \t]*}[ \t]*"),
//      NULL);
//
//  fabr_parser *htatts =
//    fabr_string("( x=\"y\" )"); // TODO
//
//  fabr_parser *tic =
//    fabr_n_alt(
//      "tic",
//      fabr_seq(fabr_str("%"), fabr_n_rex("ta", "[a-zA-Z\\-_0-9]+"), NULL),
//      fabr_seq(fabr_str("#"), fabr_n_rex("id", "[a-zA-Z\\-_0-9]+"), NULL),
//      fabr_seq(fabr_str("."), fabr_n_rex("cl", "[a-zA-Z\\-_0-9]+"), NULL),
//      NULL);
//
//  fabr_parser *ind =
//    fabr_n_rex("ind", "[ ]*");
//
//  fabr_parser *eval_eol =
//    fabr_seq(fabr_str("="), fabr_n_rex("ev", "[^\r\n]*"), NULL);
//
//  fabr_parser *blank_line =
//    fabr_n_rex("bll", "[ \t]*");
//
//  fabr_parser *text_line =
//    fabr_n_seq("txl", ind, fabr_rex("[^\r\n]+"), NULL);
//
//  fabr_parser *haml_comment_line =
//    fabr_n_seq("hacol", fabr_string("-#"), fabr_rex("[^\r\n]*"), NULL);
//
//  fabr_parser *html_comment_line =
//    fabr_n_seq("htcol", fabr_string("/"), fabr_rex("[^\r\n]*"), NULL);
//
//  fabr_parser *filter_line =
//    fabr_n_seq( "fil", ind, fabr_rex(":[a-z]+"), NULL);
//
//  fabr_parser *eval_line =
//    fabr_n_seq("evl", ind, fabr_rex("=[ \t]*"), fabr_n_rex("ev", "[^\r\n]+"), NULL);
//
//  fabr_parser *elt_line =
//    fabr_n_seq(
//      "ell",
//      ind,
//      tic, fabr_q("+"),
//      jsatts, fabr_q("?"),
//      htatts, fabr_q("?"),
//      eval_eol, fabr_q("?"),
//      NULL);
//
//  fabr_parser *line =
//    fabr_n_alt(
//      "l",
//      elt_line, eval_line, filter_line, html_comment_line, haml_comment_line,
//      text_line,
//      blank_line,
//      NULL);
//
//  fabr_parser *doctype =
//    fabr_seq(
//      fabr_rex("!!![ \t]*"),
//      fabr_n_rex("dt", "[^\n\r]*"),
//      fabr_rex("[\n\r]+"),
//      NULL);
//
//  fabr_parser *headers =
//    fabr_seq(
//      fabr_rex("---[ \t]*[\n\r]+"),
//      fabr_n_rep("hes", fabr_rex("[^-].*[\n\r]"), 0, -1),
//      fabr_rex("---[ \t]*[\n\r]+"),
//      NULL);
//
//  haml_parser =
//    fabr_seq(
//      headers, fabr_q("?"),
//      doctype, fabr_q("?"),
//      line,
//      fabr_seq(
//        fabr_rex("[\n\r]"),
//        line,
//        fabr_r("*")),
//      NULL);
//
//  header_parser =
//    fabr_rep(
//      fabr_n_seq(
//        "e",
//        fabr_n_rex("k", "[^ \t:]+"),
//        fabr_rex("[ \t]*:[ \t]*"),
//        fabr_n_rex("v", "[^\n\r]+"),
//        fabr_rex("[\n\r]"),
//        NULL),
//      0, -1);
//}

static fabr_tree *_haml(fabr_input *i)
{
  return NULL;
}

static fabr_tree *_header(fabr_input *i)
{
  return NULL;
}

static fara_node *push_to_parent(
  fara_node *current, fabr_tree *newt, fara_node *newn)
{
  int i = newt->child->length; // first child is "ind"
  newn->data = (void *)i; // ;-)

  while (1)
  {
    int ci = (int)current->data;
    if (i > ci) { fara_node_push(current, newn); break; }
    current = current->parent;
  }

  return newn;
}

static void eval_and_push(
  fabr_tree *t, const char *ev, fara_node *n, flu_dict *callbacks, void *data)
{
  char *code = flu_strtrim(ev);

  fara_haml_callback *cb =
    flu_list_get(
      callbacks,<commit_msg>rewrite the header parser<commit_after>
//
// Copyright (c) 2014-2015, <NAME>, <EMAIL>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Made in Japan.
//

// https://github.com/flon-io/aranye


#define _POSIX_C_SOURCE 200809L

#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "aabro.h"
#include "fara_haml.h"


//fabr_parser *haml_parser = NULL;
//fabr_parser *header_parser = NULL;
//
//void haml_parser_init()
//{
//  fabr_parser *jsentry =
//    fabr_n_seq(
//      "jsentry",
//      fabr_n_rex("k", "[^ \t\r\n:]+"),
//      fabr_rex("[ \t]*:[ \t]*"),
//      fabr_alt(
//        fabr_seq(
//          fabr_string("\""),
//          fabr_n_rex("v", "[^\"\r\n,]*"),
//          fabr_string("\""),
//          NULL),
//        fabr_seq(
//          fabr_string("'"),
//          fabr_n_rex("v", "[^'\r\n,]*"),
//          fabr_string("'"),
//          NULL),
//        fabr_n_rex("v", "[^ \t\r\n,]+"),
//        NULL),
//      NULL);
//  fabr_parser *jsatts =
//    fabr_n_seq(
//      "jsatts",
//      fabr_rex("[ \t]*\\{[ \t]*"),
//      fabr_seq(
//        jsentry, fabr_seq(fabr_rex("[ \t]*,[ \t]*"), jsentry, fabr_r("*")), NULL
//      ), fabr_q("?"),
//      fabr_rex("[ \t]*}[ \t]*"),
//      NULL);
//
//  fabr_parser *htatts =
//    fabr_string("( x=\"y\" )"); // TODO
//
//  fabr_parser *tic =
//    fabr_n_alt(
//      "tic",
//      fabr_seq(fabr_str("%"), fabr_n_rex("ta", "[a-zA-Z\\-_0-9]+"), NULL),
//      fabr_seq(fabr_str("#"), fabr_n_rex("id", "[a-zA-Z\\-_0-9]+"), NULL),
//      fabr_seq(fabr_str("."), fabr_n_rex("cl", "[a-zA-Z\\-_0-9]+"), NULL),
//      NULL);
//
//  fabr_parser *ind =
//    fabr_n_rex("ind", "[ ]*");
//
//  fabr_parser *eval_eol =
//    fabr_seq(fabr_str("="), fabr_n_rex("ev", "[^\r\n]*"), NULL);
//
//  fabr_parser *blank_line =
//    fabr_n_rex("bll", "[ \t]*");
//
//  fabr_parser *text_line =
//    fabr_n_seq("txl", ind, fabr_rex("[^\r\n]+"), NULL);
//
//  fabr_parser *haml_comment_line =
//    fabr_n_seq("hacol", fabr_string("-#"), fabr_rex("[^\r\n]*"), NULL);
//
//  fabr_parser *html_comment_line =
//    fabr_n_seq("htcol", fabr_string("/"), fabr_rex("[^\r\n]*"), NULL);
//
//  fabr_parser *filter_line =
//    fabr_n_seq( "fil", ind, fabr_rex(":[a-z]+"), NULL);
//
//  fabr_parser *eval_line =
//    fabr_n_seq("evl", ind, fabr_rex("=[ \t]*"), fabr_n_rex("ev", "[^\r\n]+"), NULL);
//
//  fabr_parser *elt_line =
//    fabr_n_seq(
//      "ell",
//      ind,
//      tic, fabr_q("+"),
//      jsatts, fabr_q("?"),
//      htatts, fabr_q("?"),
//      eval_eol, fabr_q("?"),
//      NULL);
//
//  fabr_parser *line =
//    fabr_n_alt(
//      "l",
//      elt_line, eval_line, filter_line, html_comment_line, haml_comment_line,
//      text_line,
//      blank_line,
//      NULL);
//
//  fabr_parser *doctype =
//    fabr_seq(
//      fabr_rex("!!![ \t]*"),
//      fabr_n_rex("dt", "[^\n\r]*"),
//      fabr_rex("[\n\r]+"),
//      NULL);
//
//  fabr_parser *headers =
//    fabr_seq(
//      fabr_rex("---[ \t]*[\n\r]+"),
//      fabr_n_rep("hes", fabr_rex("[^-].*[\n\r]"), 0, -1),
//      fabr_rex("---[ \t]*[\n\r]+"),
//      NULL);
//
//  haml_parser =
//    fabr_seq(
//      headers, fabr_q("?"),
//      doctype, fabr_q("?"),
//      line,
//      fabr_seq(
//        fabr_rex("[\n\r]"),
//        line,
//        fabr_r("*")),
//      NULL);

static fabr_tree *_haml(fabr_input *i)
{
  return NULL;
}

//  header_parser =
//    fabr_rep(
//      fabr_n_seq(
//        "e",
//        fabr_n_rex("k", "[^ \t:]+"),
//        fabr_rex("[ \t]*:[ \t]*"),
//        fabr_n_rex("v", "[^\n\r]+"),
//        fabr_rex("[\n\r]"),
//        NULL),
//      0, -1);

static fabr_tree *_hk(fabr_input *i)
{
  return fabr_rex("k", i, "[^ \t:]+");
}
static fabr_tree *_hcolon(fabr_input *i)
{
  return fabr_rex(NULL, i, "[ \t]*:[ \t]*");
}
static fabr_tree *_hv(fabr_input *i)
{
  return fabr_rex("v", i, "[^\n\r]+");
}
static fabr_tree *_heol(fabr_input *i)
{
  return fabr_rex(NULL, i, "[\n\r]");
}
static fabr_tree *_he(fabr_input *i)
{
  return fabr_seq("e", i, _hk, _hcolon, _hv, _heol, NULL);
}

static fabr_tree *_header(fabr_input *i)
{
  return fabr_rep(NULL, i, _he, 0, 0); // 0 or more
}

static fara_node *push_to_parent(
  fara_node *current, fabr_tree *newt, fara_node *newn)
{
  int i = newt->child->length; // first child is "ind"
  newn->data = (void *)i; // ;-)

  while (1)
  {
    int ci = (int)current->data;
    if (i > ci) { fara_node_push(current, newn); break; }
    current = current->parent;
  }

  return newn;
}

static void eval_and_push(
  fabr_tree *t, const char *ev, fara_node *n, flu_dict *callbacks, void *data)
{
  char *code = flu_strtrim(ev);

  fara_haml_callback *cb =
    flu_list_get(
      callbacks,
{'fraction_non_alphanumeric': 0.22610511028410638, 'fraction_numerical': 0.004810747027321412, 'mean_word_length': 2.4111455108359134, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 25, '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': '2830552', 'n_tokens_mistral': 4573, 'n_tokens_neox': 4414, 'n_words': 952}