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


read yaml_type.anubis
read tools/streams.anubis
read tools/string.anubis
read system/string.anubis

public define Maybe(YAML_Document)
  get_yaml_document_content
  (
    Stream        s,
    YAML_Document doc
  )=
  if read_line(s) is
  {
    failure        then success(doc),
    success(_line) then
      with line = trim(_line),
      //check for comment
      if start_with(line, "#") then
        //println("skipping comment ["+line+"]");
        get_yaml_document_content(s, doc)
      //check for blank line
      else if length(line) = 0 then
        //println("skipping blank line");
        get_yaml_document_content(s, doc)
      //check for item of the list
      else if start_with(line, "- ") then
        with indent = count_blank(_line),      
        println("list item indent["+indent+"]["+line+"]");
        get_yaml_document_content(s, doc)
      //check for entry of the map
      else 
        if find(":", line, 0) is
        {
          failure       then 
            println("unrecognize line ["+line+"]");
            get_yaml_document_content(s, doc),
          success(pos)  then
            with indent = count_blank(_line),          
            println("map entry indent["+indent+"]["+line+"]");
            get_yaml_document_content(s, doc)            
        }
  }.
  
public define Maybe(YAML_Document)
  get_yaml_document
  (
      Stream  s,
  )=
  if read_line(s) is
  {
    failure       then failure,
    success(line) then
      if start_with(line, "---") then
        println("Start parsing YAML document");
        get_yaml_document_content(s, yaml_document(var([])))
      else
        get_yaml_document(s)
  }.
  
public define YAML
  parse_YAML_file
  (
    Stream                    s,
    Var(List(YAML_Document))  docs
  )=
  if get_yaml_document(s) is
  {
    failure       then yaml(var(reverse(*docs))),
    success(doc)  then 
      docs <- [doc . *docs];
      parse_YAML_file(s, docs)
  }.
  
public define YAML
  parse_YAML_file
  (
    Stream s
  )=
  if read_line(s) is
  { 
    failure       then  yaml(var([])),
    success(line) then
      if start_with(line, "%YAML 1.1") then
        println("%YAML 1.1 file");
        parse_YAML_file(s, var([]))
      else  
        yaml(var([]))
  }.