Skip to content

Commit

Permalink
Add parser for dynout files
Browse files Browse the repository at this point in the history
Co-authored-by: Hampus Adolfsson <[email protected]>
  • Loading branch information
Dragnalith and HampusAdolfsson committed May 20, 2024
1 parent 830f954 commit 7ab135b
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ add_library(libninja OBJECT
src/clparser.cc
src/dyndep.cc
src/dyndep_parser.cc
src/dynout_parser.cc
src/debug_flags.cc
src/deps_log.cc
src/disk_interface.cc
Expand Down Expand Up @@ -259,6 +260,7 @@ if(BUILD_TESTING)
src/deps_log_test.cc
src/disk_interface_test.cc
src/dyndep_parser_test.cc
src/dynout_parser_test.cc
src/edit_distance_test.cc
src/graph_test.cc
src/json_test.cc
Expand Down
1 change: 1 addition & 0 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ def has_re2c() -> bool:
'disk_interface',
'dyndep',
'dyndep_parser',
'dynout_parser',
'edit_distance',
'eval_env',
'graph',
Expand Down
41 changes: 41 additions & 0 deletions src/dynout_parser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Generated by re2c 1.3 */
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "dynout_parser.h"

#include "metrics.h"
#include "string_piece.h"

bool DynoutParser::Parse(const std::string& content,
std::vector<StringPiece>& result, std::string* err) {
METRIC_RECORD("dynout parse");
/// Split `content` into a series of individual text lines, without trailing
/// newlines, and remove empty lines
const char* input = content.c_str();
const char* start = input;
const char* limit = input + content.size();
while (start < limit) {
const char* end = start;
// Note: \r\n will be treated as a line end + an empty line,
// the latter will be ignored. This simplifies the logic in this
// loop to reduce its code and speed it up.
while (end < limit && (*end != '\n' && *end != '\r'))
++end;
if (end > start)
result.push_back(StringPiece(start, end - start));
start = end + 1;
}
return true;
}
32 changes: 32 additions & 0 deletions src/dynout_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NINJA_DYNOUT_PARSER_H_
#define NINJA_DYNOUT_PARSER_H_

#include <string>
#include <vector>

#include "string_piece.h"

/// Parser for dynout file.
struct DynoutParser {

/// Parse a dynout file, placing each listed output into `result`.
/// The resulting StringPieces point into `content`.
static bool Parse(const std::string& content,
std::vector<StringPiece>& result, std::string* err);
};

#endif // NINJA_DYNOUT_PARSER_H_
64 changes: 64 additions & 0 deletions src/dynout_parser_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "dynout_parser.h"

#include "test.h"

using namespace std;

struct DynoutParserTest : public testing::Test {

VirtualFileSystem fs_;
};

TEST_F(DynoutParserTest, Empty) {
std::string input = "";
string err;
std::vector<StringPiece> result;
EXPECT_TRUE(DynoutParser::Parse(input, result, &err));
ASSERT_EQ(err, "");
ASSERT_TRUE(result.empty());
}

TEST_F(DynoutParserTest, MultipleEntries) {
std::string input = "file1\nfile2\nfile3";
string err;
std::vector<StringPiece> result;
EXPECT_TRUE(DynoutParser::Parse(input, result, &err));
ASSERT_EQ(err, "");
ASSERT_EQ(result[0], "file1");
ASSERT_EQ(result[1], "file2");
ASSERT_EQ(result[2], "file3");
}

TEST_F(DynoutParserTest, EmptyLines) {
std::string input = "\nfile1\n\n\nfile2\n\n";
string err;
std::vector<StringPiece> result;
EXPECT_TRUE(DynoutParser::Parse(input, result, &err));
ASSERT_EQ(err, "");
ASSERT_EQ(result[0], "file1");
ASSERT_EQ(result[1], "file2");
}

TEST_F(DynoutParserTest, CRLF) {
std::string input = "\r\nfile1\r\n\r\nfile2\r\n";
string err;
std::vector<StringPiece> result;
EXPECT_TRUE(DynoutParser::Parse(input, result, &err));
ASSERT_EQ(err, "");
ASSERT_EQ(result[0], "file1");
ASSERT_EQ(result[1], "file2");
}

0 comments on commit 7ab135b

Please sign in to comment.