cave-fmt 0.1.3

Parse various cave /speleo related data formats
Documentation
/// th2 files are used in Therion for scrap (drawing) related information.
/// Tey contain the actual lines, curves and room names.
///
/// There are 3 definitions of the th2 format in Therion:
///
/// 1. The Therion Book
/// 2. The TCL parser in xtherion
/// 3. The C++ parser in therion
///
/// The book makes little destination between th-files (centerline, shots, survey)
/// and th2 files (drawings).
///
/// As of this Writing (May 2022) there are following definitions:
///
/// ## scrap
///
/// Options:
///
/// ### -projection <specification>
///
/// Specification can be
///
/// 1. none
/// 2. plan
/// 3. elevation ([elevation 10] or [elevation 10 deg])
/// 4. extended
///
/// ### -scale <specification>
///
/// Specification can be
///
/// 1. <number>
/// 2. [<number> <length units>]
/// 3. [<num1> <num2> <length units>]
/// 4. [<num1> ... <num8> [<length units>]]
///
/// ### -cs <coordinate system>
///
/// ### -stations <list of station names>
/// ### -sketch <filename> <x> <y>
/// ### -walls <on/off/auto>
/// ### -flip (none)/horizontal/vertical
/// ### -station-names <prefix> <suffix>
/// ### -author <date> <person>
/// ### -copyright <date> <string>
/// ### -title <string>

/// proc xth_me_cmds_create_all {lns} {
// encoding  utf-8

// ^\s*encoding\s+(\S+)\s*$
// ^\s*\#\#XTHERION\#\#\s+(\S.*)\s*$
// ^\s*\#\#BEGIN\#\#\s*$
// ^\s*\#\#END\#\#\s*$
// {(.*)\\\s*$

// scrap <id> [OPTIONS]
// ... point, line and area commands ...
// endscrap [<id>]

// ^\s*point\s+(\S+)\s+(\S+)\s+(\S+)\s*(.*)$
// ^\s*line\s+(\S+)\s*(.*)$    set rxl [list [list "\\s*\\-$opt\\s+\\\[(\[^\\\]\]*)\\\]" "\["]\    [list "\\s*\\-$opt\\s+\\\"((\\\"\\\"|\[^\\\"])+)\\\"" "\""]\    [list "\\s*\\-$opt\\s+(\\S+)" {}]]
//   if {[regexp {^\s*\!?\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*(\#.*)?$} \
// 	  $ln dum x1 y1 x2 y2 x y cmt] && (![catch {expr $x * $y * $x1 * $y1 * $x2 * $y2}])} {
//       set what 2
//     } elseif {[regexp {^\s*\!?\s*(\S+)\s+(\S+)\s*(\#.*)?$} $ln dum x y cmt] && (![catch {expr $x * $y}])} {
//       # skusi jednotlive options rotation size r-size l-size smooth
//       # skusi jednotlive options rotation size r-size l-size smooth
//       set cmt $ln
//       set optl [xth_me_cmds_get_option $ln orientation]
// 	set optl [xth_me_cmds_get_option $ln orient]
// 	  set optl [xth_me_cmds_get_option $ln smooth]
// 	    set smth [xth_me_cmds_get_onoffauto [lindex $optl 0]]
// 	    set optl [xth_me_cmds_get_option $ln size]
// 	      set optl [xth_me_cmds_get_option $ln r-size]
// 		set optl [xth_me_cmds_get_option $ln l-size]

// ^\s*area\s+(\S+)\s*(.*)$
// ^\s*endarea(\s|$)
use crate::pest::*;
use pest::iterators::Pair;
use std::fs;
use vek::geom::repr_c::Aabr;
use vek::Vec2;

#[derive(Parser)]
#[grammar = "th2.pest"]

pub struct TH2Parser;

#[derive(Debug)]
pub struct Scrap {
    pub name: String,
}

#[derive(Debug)]
/// Represents a parsed TH2 file.
pub struct Th2 {
    pub scraps: Vec<Scrap>,
}

#[cfg(test)]
mod test {
    use super::Rule;
    use super::TH2Parser;
    use crate::pest::*;

    #[test]
    fn parse_encoding() {
        parses_to! {
            parser: TH2Parser,
            input:  "encoding utf-8",
            rule:   Rule::encoding,
            tokens: [encoding( 0, 14)]
        };
        parses_to! {
            parser: TH2Parser,
            input:  "encoding  utf-8",
            rule:   Rule::encoding,
            tokens: [encoding( 0, 15)]
        };
        parses_to! {
            parser: TH2Parser,
            input:  "encoding  utf-8\n",
            rule:   Rule::encoding,
            tokens: [encoding( 0, 16)]
        };
        parses_to! {
            parser: TH2Parser,
            input:  "encoding  utf-8 \n",
            rule:   Rule::encoding,
            tokens: [encoding( 0, 17)]
        };
        parses_to! {
            parser: TH2Parser,
            input:  "encoding  utf-8\r\n",
            rule:   Rule::encoding,
            tokens: [encoding( 0, 17)]
        };
    }

    #[test]
    fn parse_scrapstart() {
        parses_to! {
            parser: TH2Parser,
            input:  "scrap s_g27b -projection plan -station-names \"\" \"@g27.windloch2019\" -author 2019.06.22 \"Carsten Ebenau\" -scale [0 0 1600 0 0.0 0.0 40.64 0.0 m]\n",
            rule:   Rule::thscrapstart,
            tokens: [
                thscrapstart( 0, 68, [
                    NAME(6,12), 
                    thscrapoptionplan(13, 29, [
                        thscrapoptionplanspec(25, 29)
                    ]),
                    thscrapoptionstationnames(30, 67, [
                        STRING(45, 47), 
                        STRING(48, 67)]),
                ])
            ]
        };
    }
}
// /Users/md/Documents/AKKH-Vermessungen/windloch/scraps/hades_g203_g204_g122.th2