-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
74 lines (55 loc) · 1.69 KB
/
parser.h
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
#pragma once
#include <exception>
#include <string>
#include <vector>
#include "dictionary.h"
#include "fixionary.h"
#include "vos_node.h"
#include "word_node.h"
namespace timlan {
class parser {
public:
parser(fixionary fix, dictionary dict);
std::vector<vos_node> parse(const std::string &input);
private:
fixionary fix_;
dictionary dict_;
std::vector<std::string> tokenise(const std::string &input);
template <typename iterator>
vos_node parse_tokens(iterator &begin, const iterator &end);
word_node parse_word(std::string word_str);
};
class parse_error : public std::exception {
public:
parse_error(const std::string& what);
std::string what_;
const char *what() const noexcept override;
};
template <typename iterator>
vos_node parser::parse_tokens(iterator &begin, const iterator &end)
{
if (begin == end)
throw parse_error{"Too many verbs!"};
std::unique_ptr<word_node> word, prev_word;
do {
word = std::make_unique<word_node>(parse_word(*begin++));
if (prev_word) {
word->supplement_ = std::move(prev_word);
}
prev_word = std::move(word);
}
while (prev_word->is_supplement_ && begin != end);
if (begin == end && prev_word->is_supplement_)
throw parse_error{"Spurious supplement"};
// OK, after that minor chaos, we now have prev_word containing a
// non-supplement which now owns its supplement (if any). Now we should
// figure out if our current word is a verboid or not.
vos_node node;
node.word_node_ = std::move(*prev_word);
if (prev_word->is_verboid_) {
node.object_ = std::make_unique<vos_node>(parse_tokens(begin, end));
node.subject_ = std::make_unique<vos_node>(parse_tokens(begin, end));
}
return node;
}
}