Piotr Pawliczek | c3ff7da | 2020-06-26 14:23:56 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "foomatic_shell/grammar.h" |
| 6 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 7 | #include <base/check.h> |
Piotr Pawliczek | c3ff7da | 2020-06-26 14:23:56 -0700 | [diff] [blame] | 8 | #include <base/logging.h> |
| 9 | #include <base/strings/string_number_conversions.h> |
| 10 | |
| 11 | namespace foomatic_shell { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | // Returns the position of the beginning of given StringAtom. |
| 16 | std::string::const_iterator Position(const StringAtom& str) { |
| 17 | DCHECK(!str.components.empty()); |
| 18 | return str.components.front().begin; |
| 19 | } |
| 20 | |
| 21 | } // namespace |
| 22 | |
| 23 | std::string Value(const StringAtom& str) { |
| 24 | std::string out; |
| 25 | for (auto& s : str.components) |
| 26 | out += s.value; |
| 27 | return out; |
| 28 | } |
| 29 | |
| 30 | std::string::const_iterator Position(const PipeSegment& segment) { |
| 31 | if (segment.command) |
| 32 | return Position(*segment.command); |
| 33 | DCHECK(segment.script != nullptr); |
| 34 | return Position(*segment.script); |
| 35 | } |
| 36 | |
| 37 | std::string::const_iterator Position(const Command& cmd) { |
| 38 | if (!cmd.variables_with_values.empty()) |
| 39 | return Position(cmd.variables_with_values.front().new_value); |
| 40 | return cmd.application.begin; |
| 41 | } |
| 42 | |
| 43 | std::string::const_iterator Position(const Script& script) { |
| 44 | DCHECK(!script.pipelines.empty()); |
| 45 | DCHECK(!script.pipelines.front().segments.empty()); |
| 46 | return Position(script.pipelines.front().segments.front()); |
| 47 | } |
| 48 | |
| 49 | std::string CreateErrorLog(const std::string& source, |
| 50 | std::string::const_iterator position, |
| 51 | const std::string& msg) { |
| 52 | std::string out = msg + ". "; |
| 53 | out += "Error occurred"; |
| 54 | if (position >= source.begin() || position < source.end()) { |
| 55 | out += |
| 56 | " at position " + base::NumberToString(position - source.begin() + 1); |
| 57 | } |
| 58 | out += " in the script: \"" + source + "\"."; |
| 59 | return out; |
| 60 | } |
| 61 | |
| 62 | } // namespace foomatic_shell |