|
| 1 | +// Copyright 2013 The Flutter 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 "flutter/tools/licenses_cpp/src/deps_parser.h" |
| 6 | + |
| 7 | +#include <re2/re2.h> |
| 8 | + |
| 9 | +#include <string> |
| 10 | +#include <string_view> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "flutter/third_party/re2/re2/re2.h" |
| 14 | + |
| 15 | +DepsParser::DepsParser() = default; |
| 16 | + |
| 17 | +DepsParser::~DepsParser() = default; |
| 18 | + |
| 19 | +// TODO(gaaclarke): Convert this to flex/bison, this is getting a bit |
| 20 | +// complicated. It technically doesn't handle commas in trailing comments. |
| 21 | +std::vector<std::string> DepsParser::Parse(std::string_view input) { |
| 22 | + std::vector<std::string> result; |
| 23 | + re2::StringPiece mutable_input(input); |
| 24 | + |
| 25 | + // 1. Find the start of the deps block |
| 26 | + if (!re2::RE2::FindAndConsume(&mutable_input, R"((?:^|\n)deps\s*=\s*\{)")) { |
| 27 | + return result; |
| 28 | + } |
| 29 | + |
| 30 | + // 2. Find the matching closing brace for the entire deps block |
| 31 | + size_t brace_close_pos = std::string_view::npos; |
| 32 | + int brace_count = 1; |
| 33 | + for (size_t i = 0; i < mutable_input.length(); ++i) { |
| 34 | + if (mutable_input[i] == '{') { |
| 35 | + brace_count++; |
| 36 | + } else if (mutable_input[i] == '}') { |
| 37 | + brace_count--; |
| 38 | + if (brace_count == 0) { |
| 39 | + brace_close_pos = i; |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (brace_close_pos == std::string_view::npos) { |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + // 3. Extract the content of the deps block |
| 50 | + std::string_view deps_content = mutable_input.substr(0, brace_close_pos); |
| 51 | + |
| 52 | + // 4. Filter out comments and build a new string |
| 53 | + std::string cleaned_content; |
| 54 | + size_t current_pos_clean = 0; |
| 55 | + while (current_pos_clean < deps_content.length()) { |
| 56 | + size_t next_newline = deps_content.find('\n', current_pos_clean); |
| 57 | + if (next_newline == std::string_view::npos) { |
| 58 | + next_newline = deps_content.length(); |
| 59 | + } |
| 60 | + |
| 61 | + std::string_view line = deps_content.substr( |
| 62 | + current_pos_clean, next_newline - current_pos_clean); |
| 63 | + |
| 64 | + size_t first_char = line.find_first_not_of(" \t"); |
| 65 | + if (first_char != std::string_view::npos) { |
| 66 | + line = line.substr(first_char); |
| 67 | + } else { |
| 68 | + line = ""; |
| 69 | + } |
| 70 | + |
| 71 | + if (line.empty() || line[0] != '#') { |
| 72 | + cleaned_content.append(line); |
| 73 | + cleaned_content.append("\n"); |
| 74 | + } |
| 75 | + |
| 76 | + current_pos_clean = next_newline + 1; |
| 77 | + } |
| 78 | + |
| 79 | + // 5. Parse the cleaned content |
| 80 | + size_t current_pos = 0; |
| 81 | + while (current_pos < cleaned_content.length()) { |
| 82 | + // Find the start of the key |
| 83 | + size_t key_start = cleaned_content.find('\'', current_pos); |
| 84 | + if (key_start == std::string_view::npos) { |
| 85 | + break; |
| 86 | + } |
| 87 | + key_start++; |
| 88 | + |
| 89 | + // Find the end of the key |
| 90 | + size_t key_end = cleaned_content.find('\'', key_start); |
| 91 | + if (key_end == std::string_view::npos) { |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + std::string_view key = |
| 96 | + std::string_view(&cleaned_content[key_start], key_end - key_start); |
| 97 | + |
| 98 | + // Find the colon after the key |
| 99 | + size_t colon_pos = cleaned_content.find(':', key_end); |
| 100 | + if (colon_pos == std::string_view::npos) { |
| 101 | + break; |
| 102 | + } |
| 103 | + |
| 104 | + // Find the start of the value |
| 105 | + size_t value_start = |
| 106 | + cleaned_content.find_first_not_of(" \t\n", colon_pos + 1); |
| 107 | + if (value_start == std::string_view::npos) { |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + // Find the end of the value |
| 112 | + size_t value_end = std::string_view::npos; |
| 113 | + int brace_level = 0; |
| 114 | + int bracket_level = 0; |
| 115 | + for (size_t i = value_start; i < cleaned_content.length(); ++i) { |
| 116 | + char c = cleaned_content[i]; |
| 117 | + if (c == '{') { |
| 118 | + brace_level++; |
| 119 | + } else if (c == '}') { |
| 120 | + brace_level--; |
| 121 | + } else if (c == '[') { |
| 122 | + bracket_level++; |
| 123 | + } else if (c == ']') { |
| 124 | + bracket_level--; |
| 125 | + } else if (c == ',' && brace_level == 0 && bracket_level == 0) { |
| 126 | + value_end = i; |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (value_end == std::string_view::npos) { |
| 132 | + value_end = cleaned_content.length(); |
| 133 | + } |
| 134 | + |
| 135 | + std::string_view value = std::string_view(&cleaned_content[value_start], |
| 136 | + value_end - value_start); |
| 137 | + |
| 138 | + // Check for 'dep_type': 'cipd' |
| 139 | + if (value.find("'dep_type': 'cipd'") != std::string_view::npos) { |
| 140 | + result.emplace_back(key); |
| 141 | + } |
| 142 | + |
| 143 | + current_pos = value_end + 1; |
| 144 | + } |
| 145 | + |
| 146 | + return result; |
| 147 | +} |
0 commit comments