@@ -74,6 +74,71 @@ namespace aaltitoad::hawk::huppaal {
7474 auto parser::should_ignore (const std::filesystem::directory_entry& entry, const std::string& ignore_regex) -> bool {
7575 return std::regex_match (entry.path ().c_str (), std::regex{ignore_regex});
7676 }
77+
78+ auto json_parser_t::scan (compiler& ctx, const std::vector<std::string>& filepaths, const std::vector<std::string>& ignore_list) const noexcept -> std::expected<std::string, error> {
79+ // BOOKMARK: What should the scanner actually return?
80+ // Traditionally, a scanner would give a linear token stream. If we were to take the metaphor directly, we would
81+ // return something like a list of "tokens" i.e. different kindas of nodes and edges, i.e. an adjacency list.
82+ // But isn't that just excactly what the AST would be equivalent to (the parser's output)?
83+ // Say that we do that. The scanner outputs a list of unchecked tokens and reports errors if there are
84+ // unrecognized vertex types, missing keys, etc.
85+ // Then what would the parser do? - Checking graph consistency and semantics is the job of the semantic analyzer
86+ // not the parser, so we can't do that...
87+ // Traditionally, the parser would take the token stream and do LALR walking and generate an AST. But our AST
88+ // (no decorations) _is_ the adjacency list. - maybe the parser step should ensure compileability of the expr
89+ // strings? No. That would require a scoped parser, and that is a semantic analyzer thing.
90+ // WHAT DOES THE PARSER DO?
91+ //
92+ // Okay... Maybe the scanner doesnt output the adjacency list then. The question is then: What thing do we need
93+ // to generate an adjacency list? Just a json structure? I mean... The only thing that seems icky about that is
94+ // that we would make the compiler dependent on nlohmann, but is that a problem? It would ensure a valid json
95+ // syntax. Compared to a traditional scanner, would that fit?
96+ //
97+ // Let's see. A C scanner would gladly accept this nonsense: hello int "world" void = 2 = = ==
98+ // The C parser would say that the token stream is fucking stupid, but that's fine. It's valid scanner input.
99+ // Comparatively, if our scanner gets: `{ "hello": 321 }` - that would also be valid scanner input. Again, the
100+ // parser will flip it's shit, because that is clearly not a hawk graph.
101+ //
102+ // I think that is good. - also, welcome to my blog
103+ for (const auto & filepath : filepaths) {
104+ for (const auto & entry: std::filesystem::directory_iterator (filepath)) {
105+ try {
106+ if (should_ignore (entry, ignore_list)) {
107+ spdlog::trace (" ignoring file {}" , entry.path ().c_str ());
108+ continue ;
109+ }
110+
111+ std::ifstream input_filestream (entry.path ());
112+ auto json_file = nlohmann::json::parse (input_filestream,
113+ /* callback */ nullptr ,
114+ /* allow exceptions */ true ,
115+ /* ignore_comments */ true );
116+ if (json_file.contains (" name" )) {
117+ spdlog::trace (" loading file {0}" , entry.path ().c_str ());
118+ } else if (json_file.contains (" parts" )) {
119+ spdlog::trace (" loading parts file {0}" , entry.path ().c_str ());
120+ } else
121+ spdlog::trace (" ignoring file {0} (not a valid model file)" , entry.path ().c_str ());
122+ } catch (std::exception& e) {
123+ spdlog::error (" unable to parse json file {0}: {1}" , entry.path ().c_str (), e.what ());
124+ return std::unexpected (error ()); // TODO: Create a real diagnostic
125+ }
126+ }
127+ }
128+ }
129+
130+ auto json_parser_t::parse (compiler& ctx, const std::string& stream) const noexcept -> std::expected<int, error> {
131+
132+ }
133+
134+ auto json_parser_t::should_ignore (const std::filesystem::directory_entry& entry, const std::vector<std::string>& ignore_list) const -> bool {
135+ return std::any_of (ignore_list.begin (), ignore_list.end (),
136+ [&entry, this ](const std::string& ig){ return should_ignore (entry, ig); });
137+ }
138+
139+ auto json_parser_t::should_ignore (const std::filesystem::directory_entry& entry, const std::string& ignore_regex) const -> bool {
140+ return std::regex_match (entry.path ().c_str (), std::regex{ignore_regex});
141+ }
77142}
78143
79144extern " C" {
0 commit comments