@@ -153,14 +153,59 @@ handle_json_entry(const std::filesystem::path &entry_path,
153153 if (index == 0 ) {
154154 LOG_WARNING () << " The JSONL file is empty\n " ;
155155 }
156+ } else if (canonical.extension () == " .yaml" ||
157+ canonical.extension () == " .yml" ) {
158+ if (std::filesystem::is_empty (canonical)) {
159+ return ;
160+ }
161+ auto stream{sourcemeta::core::read_file (canonical)};
162+ std::vector<std::pair<sourcemeta::core::JSON,
163+ sourcemeta::core::PointerPositionTracker>>
164+ documents;
165+ std::uint64_t line_offset{0 };
166+ std::uint64_t max_line{0 };
167+ while (stream.peek () != std::char_traits<char >::eof ()) {
168+ sourcemeta::core::PointerPositionTracker positions;
169+ const std::uint64_t current_offset{line_offset};
170+ max_line = 0 ;
171+ auto callback = [&positions, current_offset, &max_line](
172+ const sourcemeta::core::JSON::ParsePhase phase,
173+ const sourcemeta::core::JSON::Type type,
174+ const std::uint64_t line,
175+ const std::uint64_t column,
176+ const sourcemeta::core::JSON &value) {
177+ max_line = std::max (max_line, line);
178+ positions (phase, type, line + current_offset, column, value);
179+ };
180+ documents.emplace_back (sourcemeta::core::parse_yaml (stream, callback),
181+ std::move (positions));
182+ // The YAML parser reports the line of the next document separator,
183+ // so we subtract 1 to get the actual lines consumed by this document
184+ line_offset += max_line > 0 ? max_line - 1 : 0 ;
185+ }
186+
187+ if (documents.size () > 1 ) {
188+ LOG_VERBOSE (options) << " Interpreting input as YAML multi-document: "
189+ << canonical.string () << " \n " ;
190+ std::size_t index{0 };
191+ for (auto &entry : documents) {
192+ result.push_back ({canonical, std::move (entry.first ),
193+ std::move (entry.second ), index, true });
194+ index += 1 ;
195+ }
196+ } else if (documents.size () == 1 ) {
197+ result.push_back ({std::move (canonical),
198+ std::move (documents.front ().first ),
199+ std::move (documents.front ().second )});
200+ }
156201 } else {
157202 if (std::filesystem::is_empty (canonical)) {
158203 return ;
159204 }
160205 sourcemeta::core::PointerPositionTracker positions;
161206 // TODO: Print a verbose message for what is getting parsed
162- auto contents{sourcemeta::core::read_yaml_or_json (canonical,
163- std::ref (positions))};
207+ auto contents{
208+ sourcemeta::core::read_json (canonical, std::ref (positions))};
164209 result.push_back (
165210 {std::move (canonical), std::move (contents), std::move (positions)});
166211 }
0 commit comments