yaml.anubis 5.78 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 07/06/2016
 * Time: 23:15
 * © Calexium 
 */

read tools/basis.anubis
read tools/streams.anubis
    
transmit yaml_type.anubis
read yaml_parser.anubis

define One
  print_yaml_value
  (
    String indent_str,
    String type,
    String value
  )=
  println(indent_str + "   - Type: " + type);
  if value = "" then unique else println(indent_str + "   - Value: " + value).
  
define One
  print_yaml_map
  (
    String               indent_str,
    List(YAML_Key_Value) so_far
  )=
  if so_far is
  {
    [ ]       then unique,
    [map . t] then
      since map is key_value(key, yaml_value),
      println(indent_str + "   - Name: " + key);
      if *yaml_value is
      {
        null then
          print_yaml_value(indent_str, "null", ""),
        
        string(str) then
          print_yaml_value(indent_str, "String", *str),
          
        int(integer) then
          print_yaml_value(indent_str, "Integer", "" + *integer),
          
        bool(boolean) then
          with value_str = 
            if *boolean then
              "true"
            else
              "false",
           print_yaml_value(indent_str, "Boolean", "" + value_str),
        
        map(key_values) then
          print_yaml_map(indent_str + "  ", *key_values),
          
        list(yaml_values) then
          println("list content (todo)")
      };
      
      (if t is [] then unique else println(""));
      print_yaml_map(indent_str, t)
  }.

define One
  print_yaml_nodes
  (
    List(YAML_Node) so_far
  )=
  if so_far is
  {
    [ ]        then unique,
    [node . t] then
      print(" YAML Node (");
      if node is
      {
        map(key_values) then
          println("map)");
          print_yaml_map("", reverse(*key_values)),
          
        list(values) then
          println("list (todo))")
      };
      
      print_yaml_nodes(t)
  }.
  
define One
  print_yaml_documents
  (
    List(YAML_Document) so_far,
    Int n
  )=
  if so_far is
  {
    [ ]       then unique,
    [doc . t] then
      since doc is yaml_document(nodes),
      println("YAML Document " + n + ":");
      print_yaml_nodes(reverse(*nodes));
      print_yaml_documents(t, n + 1)
  }.

public define One
  print_yaml_data
  (
    YAML yaml
  )=
  since yaml is yaml(documents),
  print_yaml_documents(*documents, 1).

public define Maybe(YAML_Value)
  get_value
  (
    List(YAML_Key_Value)  key_values,
    String                search_key
  )=
  if key_values is 
  {
    []      then failure,
    [h . t] then
      since h is key_value(key, value),
      if search_key = key then
        success(*value)
      else
        get_value(t, search_key)
  }.
  
public define Maybe(List(YAML_Key_Value))
  get_map_in_list_entry
  (
    List(YAML_Value) list_values,
    String           search_key
  )=
  if list_values is
  {
    []      then failure,
    [h . t] then
      //check if the first entry of the list value is a map
      if h is map(key_values) then
      //if yes check if the map's first entry key is equal to search key
        if *key_values is
        {
          []                then  get_map_in_list_entry(t, search_key),
          [_key_value . _]  then
            since _key_value is key_value(key, value),
              if key = search_key then
                //check if the entry is map
                if *value is map(_key_values) then
                  success(*_key_values)
                else
                  get_map_in_list_entry(t, search_key)
              else
                get_map_in_list_entry(t, search_key)
        }
      else
        get_map_in_list_entry(t, search_key)
  }.
  
public define Maybe(YAML_Value)
  get_YAML_value
  (
    List(YAML_Key_Value)  root,
    List(String)          path
  )=
  if path is 
  {
    []                      then failure,
    [current_name . tail]   then
      with final = if tail is [] then true else false,
      if get_value(root, current_name) is
      {
        failure           then failure,
        success(r_value)  then
          //if is the final round hence we return the result
          if final then
            success(r_value)
            
          // NOT Final round, continue to recurse on path to get targeted YAML_Value
          else if r_value is map(new_root) then
            //this is a map hencec recurse next
            get_YAML_value(*new_root, tail)
            
            //this is a list. 
          else if r_value is list(current_list) then
            //Hence try to get an entry (1st level only) with a yaml_value which is YAML_Key_Value
            //get the map within a list which the name of the key is current_name
            if get_map_in_list_entry(*current_list, current_name) is
            {
              failure           then failure,
              success(new_root) then get_YAML_value(new_root, tail)
            }
            
          else
            failure
      }
  }
  .
  
public define Maybe(Bool)
  get_Bool
  (
    List(YAML_Key_Value)  root,
    List(String)          path
  )=
  if get_YAML_value(root, path) is
  {
    failure           then  failure,
    success(y_value)  then
      if y_value is bool(vb_value) then
        success(*vb_value)
      else
        failure
  }.
  
public define Bool
  get_Bool
  (
    List(YAML_Key_Value)  root,
    List(String)          path,
    Bool                  default
  )=
  if get_Bool(root, path) is
  {
    failure           then default,
    success(b_value)  then b_value
  }.
  
public define YAML
  load
  (
    String  yaml_file
  )=
  if (Maybe(RStream))file(yaml_file, read) is
  {
     failure    then yaml(var([])),
     success(f) then parse_YAML_file(make_stream(f))
  }.  
  
global define One
  yaml_test
  (
    List(String) dummy
  )=
  print_yaml_data(load(my_anubis_directory+"/IDPS/suricata/test.yaml")).