blob: 21c81777427d4cf9d32d62f433b4ba61568daadd [file] [log] [blame]
Piotr Pawliczekc3ff7da2020-06-26 14:23:56 -07001// 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 Fan713061e2021-03-08 15:45:12 +09007#include <base/check.h>
Piotr Pawliczekc3ff7da2020-06-26 14:23:56 -07008#include <base/logging.h>
9#include <base/strings/string_number_conversions.h>
10
11namespace foomatic_shell {
12
13namespace {
14
15// Returns the position of the beginning of given StringAtom.
16std::string::const_iterator Position(const StringAtom& str) {
17 DCHECK(!str.components.empty());
18 return str.components.front().begin;
19}
20
21} // namespace
22
23std::string Value(const StringAtom& str) {
24 std::string out;
25 for (auto& s : str.components)
26 out += s.value;
27 return out;
28}
29
30std::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
37std::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
43std::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
49std::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