parser_pda/
defs.rs

1use std::collections::BinaryHeap;
2use strum_macros::Display;
3
4use std::fs::{File, create_dir};
5use std::io::{Seek, Write};
6
7#[derive(Display, Clone, PartialEq)]
8pub enum SegmentDelimSyms {
9    SentenceStart,
10    SegmentStart,
11    Bracket,
12}
13
14impl PartialEq<PDAStackCtx> for SegmentDelimSyms {
15    fn eq(&self, other: &PDAStackCtx) -> bool {
16        match self {
17            Self::SentenceStart => Self::SentenceStart == other.sym,
18            Self::SegmentStart => Self::SegmentStart == other.sym,
19            Self::Bracket => Self::Bracket == other.sym,
20        }
21    }
22}  
23
24pub struct PDAStackCtx {
25    pub sym: SegmentDelimSyms,
26    pub seg_start: usize,
27}
28
29impl PartialEq for PDAStackCtx {
30    fn eq(&self, other: &PDAStackCtx) -> bool {
31        self.sym == other.sym
32    }
33}  
34
35#[derive(Display)]
36pub enum SegmentTypes {
37    Sentence,
38    Tail,
39    Plain,
40    Bracketed,
41    InvalidSentence,
42    UnbalancedLeftBracket,
43    UnbalancedRightSth,
44}
45
46pub struct ParsedSegment {
47    pub tp: SegmentTypes,
48    pub seg: (usize, usize),
49    pub rank: usize,
50}
51
52impl std::cmp::PartialEq for ParsedSegment {
53    fn eq(&self, other: &Self) -> bool {
54        self.seg.0 == other.seg.0 && self.seg.1 == other.seg.1 && self.rank == other.rank
55    }
56}
57impl std::cmp::Eq for ParsedSegment {}
58impl std::cmp::PartialOrd for ParsedSegment {
59    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
60        match self.seg.0.partial_cmp(&other.seg.0) {
61            Some(std::cmp::Ordering::Less) => Some(std::cmp::Ordering::Greater),
62            Some(std::cmp::Ordering::Equal) => self.seg.1.partial_cmp(&other.seg.1),
63            Some(std::cmp::Ordering::Greater) => Some(std::cmp::Ordering::Less),
64            _ => None,
65        }            
66    }
67}
68impl Ord for ParsedSegment {
69    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
70        self.partial_cmp(other).unwrap()
71    }
72}
73    
74pub struct ParserCtx {
75    pub segments: BinaryHeap<ParsedSegment>,
76    pub index: usize,
77}
78
79pub fn fsm_code_to_file(fname: &str, path: &str, gen_code: &str) {
80
81    let _res = create_dir(path);
82
83    File::create(&format!("{}/{}.rs", path, fname))
84        .and_then(|mut file| {
85            file.seek(std::io::SeekFrom::End(0))?;
86            file.write_all(gen_code.to_string().as_bytes())?;
87            file.flush()
88        })
89        .expect("file error");
90}
91
92