1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/// 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