Moved basic parsing to a separate module.
This commit is contained in:
28
lib/parsebase/CMakeLists.txt
Normal file
28
lib/parsebase/CMakeLists.txt
Normal file
@@ -0,0 +1,28 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
project(parsebase)
|
||||
|
||||
option(PARSEBASE_BUILD_TESTS "Build unit tests" OFF)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wpedantic -Werror")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "-g")
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
if (CMAKE_GENERATOR STREQUAL "Ninja" AND
|
||||
((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) OR
|
||||
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.5)))
|
||||
# Force colored warnings in Ninja's output, if the compiler has -fdiagnostics-color support.
|
||||
# Rationale in https://github.com/ninja-build/ninja/issues/814
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
|
||||
endif()
|
||||
|
||||
add_subdirectory(src)
|
||||
if(PARSEBASE_BUILD_TESTS)
|
||||
add_subdirectory(tests)
|
||||
endif(PARSEBASE_BUILD_TESTS)
|
31
lib/parsebase/include/parsebase/Location.h
Normal file
31
lib/parsebase/include/parsebase/Location.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef __PARSE_BASE__LOCATION_H
|
||||
#define __PARSE_BASE__LOCATION_H
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
namespace parsebase
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Location
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Location
|
||||
{
|
||||
const char *sectionStart = nullptr;
|
||||
const char *sectionEnd = nullptr;
|
||||
|
||||
std::size_t rowStart = -1;
|
||||
std::size_t rowEnd = -1;
|
||||
|
||||
std::size_t columnStart = -1;
|
||||
std::size_t columnEnd = -1;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
|
||||
#endif
|
567
lib/parsebase/include/parsebase/Parser.h
Normal file
567
lib/parsebase/include/parsebase/Parser.h
Normal file
@@ -0,0 +1,567 @@
|
||||
#ifndef __PARSE_BASE__PARSER_H
|
||||
#define __PARSE_BASE__PARSER_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include <parsebase/ParserException.h>
|
||||
#include <parsebase/ParserPolicy.h>
|
||||
#include <parsebase/Stream.h>
|
||||
|
||||
namespace parsebase
|
||||
{
|
||||
|
||||
template<typename Type>
|
||||
struct Tag
|
||||
{
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Parser
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy = CaseSensitiveParserPolicy>
|
||||
class Parser: public Stream, public ParserPolicy
|
||||
{
|
||||
template<class OtherParserPolicy>
|
||||
friend class Parser;
|
||||
|
||||
public:
|
||||
explicit Parser();
|
||||
explicit Parser(std::string streamName, std::istream &istream);
|
||||
|
||||
template<class OtherParser>
|
||||
Parser(OtherParser &&otherParser)
|
||||
{
|
||||
m_stream = std::move(otherParser.m_stream);
|
||||
m_delimiters = std::move(otherParser.m_delimiters);
|
||||
}
|
||||
|
||||
void removeComments(const std::string &startSequence, const std::string &endSequence, bool removeEnd);
|
||||
|
||||
char currentCharacter() const;
|
||||
|
||||
template<typename Type>
|
||||
Type parse();
|
||||
|
||||
template<typename Type>
|
||||
bool testAndReturn(const Type &expectedValue);
|
||||
|
||||
template<typename Type>
|
||||
bool testAndSkip(const Type &expectedValue);
|
||||
|
||||
template<typename Type>
|
||||
void expect(const Type &expectedValue);
|
||||
|
||||
std::string parseIdentifier();
|
||||
bool testIdentifierAndReturn(const std::string &identifier);
|
||||
bool testIdentifierAndSkip(const std::string &identifier);
|
||||
|
||||
// TODO: remove
|
||||
bool probeNumber();
|
||||
|
||||
std::string parseLine();
|
||||
|
||||
void skipWhiteSpace();
|
||||
void skipBlankSpace();
|
||||
void skipLine();
|
||||
|
||||
private:
|
||||
std::string parseImpl(Tag<std::string>);
|
||||
char parseImpl(Tag<char>);
|
||||
uint64_t parseImpl(Tag<uint64_t>);
|
||||
int64_t parseImpl(Tag<int64_t>);
|
||||
uint32_t parseImpl(Tag<uint32_t>);
|
||||
int32_t parseImpl(Tag<int32_t>);
|
||||
bool parseImpl(Tag<bool>);
|
||||
|
||||
bool testImpl(const std::string &expectedValue);
|
||||
bool testImpl(char expectedValue);
|
||||
bool testImpl(uint64_t expectedValue);
|
||||
bool testImpl(int64_t expectedValue);
|
||||
bool testImpl(uint32_t expectedValue);
|
||||
bool testImpl(int32_t expectedValue);
|
||||
bool testImpl(bool expectedValue);
|
||||
|
||||
uint64_t parseIntegerBody();
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
Parser<ParserPolicy>::Parser()
|
||||
: Stream()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
Parser<ParserPolicy>::Parser(std::string streamName, std::istream &istream)
|
||||
: Stream(streamName, istream)
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
void Parser<ParserPolicy>::skipWhiteSpace()
|
||||
{
|
||||
check();
|
||||
|
||||
while (!atEnd() && ParserPolicy::isWhiteSpaceCharacter(currentCharacter()))
|
||||
advance();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
void Parser<ParserPolicy>::skipBlankSpace()
|
||||
{
|
||||
check();
|
||||
|
||||
while (!atEnd() && ParserPolicy::isBlankCharacter(currentCharacter()))
|
||||
advance();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
void Parser<ParserPolicy>::skipLine()
|
||||
{
|
||||
check();
|
||||
|
||||
while (currentCharacter() != '\n')
|
||||
advance();
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
template<typename Type>
|
||||
Type Parser<ParserPolicy>::parse()
|
||||
{
|
||||
return parseImpl(Tag<Type>());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
template<typename Type>
|
||||
bool Parser<ParserPolicy>::testAndReturn(const Type &expectedValue)
|
||||
{
|
||||
const auto previousPosition = position();
|
||||
|
||||
const auto result = testImpl(expectedValue);
|
||||
|
||||
seek(previousPosition);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
template<typename Type>
|
||||
bool Parser<ParserPolicy>::testAndSkip(const Type &expectedValue)
|
||||
{
|
||||
const auto previousPosition = position();
|
||||
|
||||
const auto result = testImpl(expectedValue);
|
||||
|
||||
if (result == false)
|
||||
seek(previousPosition);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
template<typename Type>
|
||||
void Parser<ParserPolicy>::expect(const Type &expectedValue)
|
||||
{
|
||||
if (testAndSkip(expectedValue))
|
||||
return;
|
||||
|
||||
std::stringstream message;
|
||||
message << "unexpected value, expected “" << expectedValue << "”";
|
||||
|
||||
throw ParserException(location(), message.str());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
std::string Parser<ParserPolicy>::parseIdentifier()
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
std::string value;
|
||||
|
||||
while (true)
|
||||
{
|
||||
const auto character = currentCharacter();
|
||||
|
||||
if (!ParserPolicy::isIdentifierCharacter(character))
|
||||
return value;
|
||||
|
||||
value.push_back(character);
|
||||
advance();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testIdentifierAndSkip(const std::string &expectedValue)
|
||||
{
|
||||
return testAndSkip(expectedValue) && !ParserPolicy::isIdentifierCharacter(currentCharacter());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::probeNumber()
|
||||
{
|
||||
const auto previousPosition = position();
|
||||
|
||||
skipWhiteSpace();
|
||||
|
||||
while (!ParserPolicy::isWhiteSpaceCharacter(currentCharacter()))
|
||||
if (!std::isdigit(currentCharacter()))
|
||||
{
|
||||
seek(previousPosition);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
std::string Parser<ParserPolicy>::parseLine()
|
||||
{
|
||||
std::string value;
|
||||
|
||||
while (true)
|
||||
{
|
||||
const auto character = currentCharacter();
|
||||
advance();
|
||||
|
||||
if (character == '\n')
|
||||
break;
|
||||
else if (character == '\r')
|
||||
continue;
|
||||
|
||||
value.push_back(character);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
void Parser<ParserPolicy>::removeComments(const std::string &startSequence, const std::string &endSequence, bool removeEnd)
|
||||
{
|
||||
const auto inPosition = m_stream.tellg();
|
||||
const auto outPosition = m_stream.tellp();
|
||||
|
||||
m_stream.seekg(0);
|
||||
|
||||
const auto removeRange =
|
||||
[&](const auto &start, const auto &end)
|
||||
{
|
||||
assert(start != -1);
|
||||
|
||||
m_stream.clear();
|
||||
m_stream.seekp(start);
|
||||
m_stream.seekg(start);
|
||||
|
||||
auto position = start;
|
||||
|
||||
while (end == -1 || position < end)
|
||||
{
|
||||
m_stream.ignore(1);
|
||||
|
||||
if (atEnd())
|
||||
return;
|
||||
|
||||
m_stream.put(' ');
|
||||
position += static_cast<std::streamoff>(1);
|
||||
}
|
||||
};
|
||||
|
||||
while (!atEnd())
|
||||
{
|
||||
Position startPosition = m_stream.tellg();
|
||||
|
||||
while (!atEnd())
|
||||
{
|
||||
startPosition = m_stream.tellg();
|
||||
|
||||
if (testAndSkip(startSequence))
|
||||
break;
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
Position endPosition = m_stream.tellg();
|
||||
|
||||
while (!atEnd())
|
||||
{
|
||||
endPosition = m_stream.tellg();
|
||||
|
||||
if (testAndSkip(endSequence))
|
||||
break;
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
if (removeEnd)
|
||||
endPosition = m_stream.tellg();
|
||||
|
||||
removeRange(startPosition, endPosition);
|
||||
}
|
||||
|
||||
m_stream.clear();
|
||||
|
||||
m_stream.seekg(inPosition);
|
||||
m_stream.seekp(outPosition);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
char Parser<ParserPolicy>::currentCharacter() const
|
||||
{
|
||||
return ParserPolicy::transformCharacter(Stream::currentCharacter());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
std::string Parser<ParserPolicy>::parseImpl(Tag<std::string>)
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
const auto startPosition = position();
|
||||
|
||||
while (!ParserPolicy::isWhiteSpaceCharacter(currentCharacter()))
|
||||
advance();
|
||||
|
||||
const auto endPosition = position();
|
||||
const auto length = static_cast<size_t>(endPosition - startPosition);
|
||||
|
||||
std::string value;
|
||||
value.reserve(length);
|
||||
|
||||
seek(startPosition);
|
||||
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
value.push_back(currentCharacter());
|
||||
advance();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
char Parser<ParserPolicy>::parseImpl(Tag<char>)
|
||||
{
|
||||
const auto value = currentCharacter();
|
||||
|
||||
advance();
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
uint64_t Parser<ParserPolicy>::parseIntegerBody()
|
||||
{
|
||||
check();
|
||||
|
||||
if (!std::isdigit(currentCharacter()))
|
||||
throw ParserException(location(), "could not parse integer value");
|
||||
|
||||
uint64_t value = 0;
|
||||
|
||||
while (!atEnd())
|
||||
{
|
||||
const auto character = currentCharacter();
|
||||
|
||||
if (!std::isdigit(character))
|
||||
break;
|
||||
|
||||
value *= 10;
|
||||
value += character - '0';
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
int64_t Parser<ParserPolicy>::parseImpl(Tag<int64_t>)
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
bool positive = testAndSkip<char>('+') || !testAndSkip<char>('-');
|
||||
|
||||
const auto value = parseIntegerBody();
|
||||
|
||||
return (positive ? value : -value);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
uint64_t Parser<ParserPolicy>::parseImpl(Tag<uint64_t>)
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
if (currentCharacter() == '-')
|
||||
throw ParserException(location(), "expected unsigned integer, got signed one");
|
||||
|
||||
return parseIntegerBody();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
int32_t Parser<ParserPolicy>::parseImpl(Tag<int32_t>)
|
||||
{
|
||||
return static_cast<int32_t>(parseImpl(Tag<int64_t>()));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
uint32_t Parser<ParserPolicy>::parseImpl(Tag<uint32_t>)
|
||||
{
|
||||
return static_cast<uint32_t>(parseImpl(Tag<uint64_t>()));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::parseImpl(Tag<bool>)
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
if (testAndSkip<char>('0'))
|
||||
return false;
|
||||
|
||||
if (testAndSkip<char>('1'))
|
||||
return true;
|
||||
|
||||
throw ParserException(location(), "could not parse Boolean value");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(const std::string &expectedValue)
|
||||
{
|
||||
if (!ParserPolicy::isWhiteSpaceCharacter(expectedValue.front()))
|
||||
skipWhiteSpace();
|
||||
|
||||
const auto match = std::find_if(expectedValue.cbegin(), expectedValue.cend(),
|
||||
[&](const auto &expectedCharacter)
|
||||
{
|
||||
const auto character = static_cast<char>(this->currentCharacter());
|
||||
|
||||
if (character != expectedCharacter)
|
||||
return true;
|
||||
|
||||
this->advance();
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return (match == expectedValue.cend());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(char expectedValue)
|
||||
{
|
||||
const auto result = (currentCharacter() == expectedValue);
|
||||
|
||||
advance();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(int64_t expectedValue)
|
||||
{
|
||||
const auto value = parseImpl(Tag<int64_t>());
|
||||
|
||||
return (value == expectedValue);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(uint64_t expectedValue)
|
||||
{
|
||||
const auto value = parseImpl(Tag<uint64_t>());
|
||||
|
||||
return (value == expectedValue);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(int32_t expectedValue)
|
||||
{
|
||||
return testImpl(static_cast<int64_t>(expectedValue));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(uint32_t expectedValue)
|
||||
{
|
||||
return testImpl(static_cast<uint64_t>(expectedValue));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(bool expectedValue)
|
||||
{
|
||||
const auto value = parseImpl(Tag<bool>());
|
||||
|
||||
return (value == expectedValue);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
|
||||
#endif
|
69
lib/parsebase/include/parsebase/ParserException.h
Normal file
69
lib/parsebase/include/parsebase/ParserException.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#ifndef __PARSE_BASE__PARSER_EXCEPTION_H
|
||||
#define __PARSE_BASE__PARSER_EXCEPTION_H
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
#include <parsebase/Location.h>
|
||||
|
||||
namespace parsebase
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ParserException
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class ParserException: public std::exception
|
||||
{
|
||||
public:
|
||||
explicit ParserException(const Location &location)
|
||||
: ParserException(location, "unspecified parser error")
|
||||
{
|
||||
}
|
||||
|
||||
explicit ParserException(const Location &location, const char *message)
|
||||
: ParserException(location, static_cast<std::string>(message))
|
||||
{
|
||||
}
|
||||
|
||||
explicit ParserException(const Location &location, const std::string &message)
|
||||
: m_location{location},
|
||||
m_message{message},
|
||||
// TODO: refactor
|
||||
m_plainMessage{std::string(m_location.sectionStart) + ":" + std::to_string(m_location.rowStart)
|
||||
+ ":" + std::to_string(m_location.columnStart) + " " + m_message}
|
||||
{
|
||||
}
|
||||
|
||||
~ParserException() throw()
|
||||
{
|
||||
}
|
||||
|
||||
const char *what() const throw()
|
||||
{
|
||||
return m_plainMessage.c_str();
|
||||
}
|
||||
|
||||
const Location &location() const
|
||||
{
|
||||
return m_location;
|
||||
}
|
||||
|
||||
const std::string &message() const
|
||||
{
|
||||
return m_message;
|
||||
}
|
||||
|
||||
private:
|
||||
Location m_location;
|
||||
std::string m_message;
|
||||
std::string m_plainMessage;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
|
||||
#endif
|
69
lib/parsebase/include/parsebase/ParserPolicy.h
Normal file
69
lib/parsebase/include/parsebase/ParserPolicy.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#ifndef __PARSE_BASE__PARSER_POLICY_H
|
||||
#define __PARSE_BASE__PARSER_POLICY_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace parsebase
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ParserPolicy
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class CaseSensitiveParserPolicy
|
||||
{
|
||||
public:
|
||||
static constexpr char transformCharacter(char c) noexcept
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
static bool isWhiteSpaceCharacter(char c)
|
||||
{
|
||||
return std::iswspace(c);
|
||||
}
|
||||
|
||||
static bool isBlankCharacter(char c)
|
||||
{
|
||||
return std::isblank(c);
|
||||
}
|
||||
|
||||
static bool isIdentifierCharacter(char c)
|
||||
{
|
||||
return std::isgraph(c);
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class CaseInsensitiveParserPolicy
|
||||
{
|
||||
public:
|
||||
static char transformCharacter(char c) noexcept
|
||||
{
|
||||
return std::tolower(c);
|
||||
}
|
||||
|
||||
static bool isWhiteSpaceCharacter(char c)
|
||||
{
|
||||
return std::iswspace(c);
|
||||
}
|
||||
|
||||
static bool isBlankCharacter(char c)
|
||||
{
|
||||
return std::isblank(c);
|
||||
}
|
||||
|
||||
static bool isIdentifierCharacter(char c)
|
||||
{
|
||||
return std::isgraph(c);
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
|
||||
#endif
|
79
lib/parsebase/include/parsebase/Stream.h
Normal file
79
lib/parsebase/include/parsebase/Stream.h
Normal file
@@ -0,0 +1,79 @@
|
||||
#ifndef __PARSE_BASE__STREAM_H
|
||||
#define __PARSE_BASE__STREAM_H
|
||||
|
||||
#include <experimental/filesystem>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include <parsebase/Location.h>
|
||||
|
||||
namespace parsebase
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Stream
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Stream
|
||||
{
|
||||
public:
|
||||
using Position = std::stringstream::pos_type;
|
||||
|
||||
struct Delimiter
|
||||
{
|
||||
Position position;
|
||||
std::string sectionName;
|
||||
};
|
||||
|
||||
public:
|
||||
explicit Stream();
|
||||
explicit Stream(std::string streamName, std::istream &istream);
|
||||
|
||||
Stream(const Stream &other) = delete;
|
||||
Stream &operator=(const Stream &other) = delete;
|
||||
|
||||
Stream(Stream &&other)
|
||||
: m_stream{std::move(other.m_stream)},
|
||||
m_delimiters{std::move(other.m_delimiters)}
|
||||
{
|
||||
}
|
||||
|
||||
Stream &operator=(Stream &&other)
|
||||
{
|
||||
m_stream = std::move(other.m_stream);
|
||||
m_delimiters = std::move(other.m_delimiters);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Stream() = default;
|
||||
|
||||
void read(std::string streamName, std::istream &istream);
|
||||
void read(const std::experimental::filesystem::path &path);
|
||||
|
||||
void reset();
|
||||
void seek(Position position);
|
||||
Position position() const;
|
||||
Location location() const;
|
||||
|
||||
char currentCharacter() const;
|
||||
void advance();
|
||||
bool atEnd() const;
|
||||
|
||||
void check() const;
|
||||
|
||||
protected:
|
||||
mutable std::stringstream m_stream;
|
||||
|
||||
std::vector<Delimiter> m_delimiters;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
|
||||
#endif
|
21
lib/parsebase/src/CMakeLists.txt
Normal file
21
lib/parsebase/src/CMakeLists.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
set(target parsebase)
|
||||
|
||||
file(GLOB core_sources "parsebase/*.cpp")
|
||||
file(GLOB core_headers "../include/parsebase/*.h")
|
||||
|
||||
set(includes
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
set(sources
|
||||
${core_sources}
|
||||
${core_headers}
|
||||
)
|
||||
|
||||
set(libraries
|
||||
stdc++fs
|
||||
)
|
||||
|
||||
add_library(${target} ${sources})
|
||||
target_include_directories(${target} PRIVATE ${includes})
|
||||
target_link_libraries(${target} ${libraries})
|
162
lib/parsebase/src/parsebase/Stream.cpp
Normal file
162
lib/parsebase/src/parsebase/Stream.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
#include <parsebase/Stream.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
|
||||
#include <parsebase/ParserException.h>
|
||||
|
||||
namespace parsebase
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Stream
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Stream::Stream()
|
||||
{
|
||||
std::setlocale(LC_NUMERIC, "C");
|
||||
|
||||
// Don’t skip whitespace
|
||||
m_stream.exceptions(std::istream::badbit);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Stream::Stream(std::string streamName, std::istream &istream)
|
||||
{
|
||||
read(streamName, istream);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Stream::read(std::string streamName, std::istream &istream)
|
||||
{
|
||||
// Store position of new section
|
||||
const auto position = m_stream.tellp();
|
||||
|
||||
m_delimiters.push_back({position, streamName});
|
||||
|
||||
m_stream << istream.rdbuf();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Stream::read(const std::experimental::filesystem::path &path)
|
||||
{
|
||||
if (!std::experimental::filesystem::is_regular_file(path))
|
||||
throw std::runtime_error("File does not exist: “" + path.string() + "”");
|
||||
|
||||
std::ifstream fileStream(path.string(), std::ios::in);
|
||||
|
||||
read(path.string(), fileStream);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Stream::reset()
|
||||
{
|
||||
m_stream.clear();
|
||||
seek(0);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Stream::seek(Position position)
|
||||
{
|
||||
m_stream.clear();
|
||||
m_stream.seekg(position);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typename Stream::Position Stream::position() const
|
||||
{
|
||||
return m_stream.tellg();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Location Stream::location() const
|
||||
{
|
||||
const auto currentPosition = position();
|
||||
|
||||
// Find current section
|
||||
auto currentFile = std::find_if(m_delimiters.crbegin(), m_delimiters.crend(),
|
||||
[&](const auto &fileDelimiter)
|
||||
{
|
||||
return currentPosition >= fileDelimiter.position;
|
||||
});
|
||||
|
||||
// If the parser is at the end of the stream, still count from the beginning of the last section
|
||||
if (currentFile == m_delimiters.crend())
|
||||
currentFile = m_delimiters.crbegin();
|
||||
|
||||
// Go back to beginning of section
|
||||
m_stream.clear();
|
||||
m_stream.seekg(currentFile->position);
|
||||
|
||||
size_t row = 1;
|
||||
size_t column = 1;
|
||||
|
||||
// Compute the location character by character
|
||||
while (true)
|
||||
{
|
||||
if (currentPosition == -1 && atEnd())
|
||||
break;
|
||||
else if (currentPosition >= 0 && position() >= currentPosition)
|
||||
break;
|
||||
|
||||
const auto character = currentCharacter();
|
||||
|
||||
if (character == '\n')
|
||||
{
|
||||
row++;
|
||||
column = 1;
|
||||
}
|
||||
else if (std::isblank(character) || std::isprint(character))
|
||||
column++;
|
||||
|
||||
m_stream.ignore(1);
|
||||
}
|
||||
|
||||
return {currentFile->sectionName.c_str(), currentFile->sectionName.c_str(), row, row, column, column};
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
char Stream::currentCharacter() const
|
||||
{
|
||||
return m_stream.peek();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool Stream::atEnd() const
|
||||
{
|
||||
return position() == -1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Stream::check() const
|
||||
{
|
||||
if (atEnd())
|
||||
throw ParserException(location(), "reading past end of file");
|
||||
|
||||
if (m_stream.fail())
|
||||
throw ParserException(location());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Stream::advance()
|
||||
{
|
||||
check();
|
||||
m_stream.ignore(1);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
22
lib/parsebase/tests/CMakeLists.txt
Normal file
22
lib/parsebase/tests/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
set(target parsebase-tests)
|
||||
|
||||
file(GLOB core_sources "*.cpp")
|
||||
|
||||
set(includes
|
||||
${Boost_INCLUDE_DIRS}
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
${PROJECT_SOURCE_DIR}/../../lib/catch/single_include
|
||||
)
|
||||
|
||||
set(libraries
|
||||
parsebase
|
||||
)
|
||||
|
||||
add_executable(${target} ${core_sources})
|
||||
target_include_directories(${target} PRIVATE ${includes})
|
||||
target_link_libraries(${target} ${libraries})
|
||||
|
||||
add_custom_target(run-parsebase-tests
|
||||
COMMAND ${CMAKE_BINARY_DIR}/bin/parsebase-tests
|
||||
DEPENDS ${target}
|
||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests)
|
332
lib/parsebase/tests/TestParser.cpp
Normal file
332
lib/parsebase/tests/TestParser.cpp
Normal file
@@ -0,0 +1,332 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <parsebase/Parser.h>
|
||||
#include <parsebase/ParserException.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_CASE("[parser] Simple strings are parsed correctly", "[parser]")
|
||||
{
|
||||
std::stringstream s(" identifier 5 \n-51\t 0 1 100 200 -300 -400");
|
||||
parsebase::Parser<> p("input", s);
|
||||
|
||||
REQUIRE(p.parse<std::string>() == "identifier");
|
||||
REQUIRE(p.parse<size_t>() == 5u);
|
||||
REQUIRE(p.parse<int>() == -51);
|
||||
REQUIRE(p.parse<bool>() == false);
|
||||
REQUIRE(p.parse<bool>() == true);
|
||||
|
||||
REQUIRE(p.parse<int>() == 100);
|
||||
REQUIRE(p.parse<size_t>() == 200u);
|
||||
REQUIRE(p.parse<int>() == -300);
|
||||
REQUIRE_THROWS_AS(p.parse<size_t>(), parsebase::ParserException);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_CASE("[parser] Parsing exceptions are correctly reported", "[parser]")
|
||||
{
|
||||
std::stringstream s(" identifier 5 \n-51\t 0 1 100 200 -300 -400");
|
||||
parsebase::Parser<> p("input", s);
|
||||
|
||||
REQUIRE_NOTHROW(p.expect<std::string>("identifier"));
|
||||
REQUIRE_NOTHROW(p.expect<size_t>(5u));
|
||||
REQUIRE_NOTHROW(p.expect<int>(-51));
|
||||
REQUIRE_NOTHROW(p.expect<bool>(false));
|
||||
REQUIRE_NOTHROW(p.expect<bool>(true));
|
||||
|
||||
REQUIRE_NOTHROW(p.expect<int>(100));
|
||||
REQUIRE_NOTHROW(p.expect<size_t>(200u));
|
||||
REQUIRE_NOTHROW(p.expect<int>(-300));
|
||||
REQUIRE_THROWS_AS(p.expect<size_t>(-400), parsebase::ParserException);
|
||||
|
||||
p.seek(0);
|
||||
REQUIRE_THROWS_AS(p.expect<std::string>("error"), parsebase::ParserException);
|
||||
|
||||
p.seek(14);
|
||||
REQUIRE_THROWS_AS(p.expect<size_t>(6u), parsebase::ParserException);
|
||||
|
||||
p.seek(17);
|
||||
REQUIRE_THROWS_AS(p.expect<int>(-50), parsebase::ParserException);
|
||||
|
||||
p.seek(24);
|
||||
REQUIRE_THROWS_AS(p.expect<bool>(true), parsebase::ParserException);
|
||||
|
||||
p.seek(26);
|
||||
REQUIRE_THROWS_AS(p.expect<bool>(false), parsebase::ParserException);
|
||||
|
||||
p.seek(28);
|
||||
REQUIRE_THROWS_AS(p.expect<int>(101), parsebase::ParserException);
|
||||
|
||||
p.seek(31);
|
||||
REQUIRE_THROWS_AS(p.expect<size_t>(201), parsebase::ParserException);
|
||||
|
||||
p.seek(34);
|
||||
REQUIRE_THROWS_AS(p.expect<int>(-299), parsebase::ParserException);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_CASE("[parser] While parsing, the cursor position is as expected", "[parser]")
|
||||
{
|
||||
std::stringstream s(" identifier 5 \n-51\t 0 1");
|
||||
parsebase::Parser<> p("input", s);
|
||||
|
||||
parsebase::Parser<>::Position pos;
|
||||
|
||||
pos = p.position();
|
||||
REQUIRE(p.testAndReturn<std::string>("error") == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndReturn<std::string>("identifier") == true);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<std::string>("error") == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<std::string>("identifier") == true);
|
||||
REQUIRE(p.position() == 12);
|
||||
|
||||
pos = p.position();
|
||||
REQUIRE(p.testAndReturn<size_t>(6u) == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndReturn<size_t>(5u) == true);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<size_t>(6u) == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<size_t>(5u) == true);
|
||||
REQUIRE(p.position() == 15);
|
||||
|
||||
pos = p.position();
|
||||
REQUIRE(p.testAndReturn<int>(-50) == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndReturn<int>(-51) == true);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<int>(-50) == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<int>(-51) == true);
|
||||
REQUIRE(p.position() == 22);
|
||||
|
||||
pos = p.position();
|
||||
REQUIRE(p.testAndReturn<bool>(true) == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndReturn<bool>(false) == true);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<bool>(true) == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<bool>(false) == true);
|
||||
REQUIRE(p.position() == 25);
|
||||
|
||||
pos = p.position();
|
||||
REQUIRE(p.testAndReturn<bool>(false) == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndReturn<bool>(true) == true);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<bool>(false) == false);
|
||||
REQUIRE(p.position() == pos);
|
||||
REQUIRE(p.testAndSkip<bool>(true) == true);
|
||||
REQUIRE(p.position() == 27);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_CASE("[parser] The end of the input stream is correctly handled", "[parser]")
|
||||
{
|
||||
std::stringstream s1("test");
|
||||
parsebase::Parser<> p1("input", s1);
|
||||
|
||||
REQUIRE_NOTHROW(p1.expect<std::string>("test"));
|
||||
REQUIRE_THROWS_AS(p1.parse<std::string>(), parsebase::ParserException);
|
||||
|
||||
std::stringstream s2("test1 test2 test3");
|
||||
parsebase::Parser<> p2("input", s2);
|
||||
|
||||
REQUIRE_NOTHROW(p2.expect<std::string>("test1"));
|
||||
REQUIRE_NOTHROW(p2.expect<std::string>("test2"));
|
||||
REQUIRE_NOTHROW(p2.expect<std::string>("test3"));
|
||||
REQUIRE_THROWS_AS(p2.parse<std::string>(), parsebase::ParserException);
|
||||
|
||||
std::stringstream s3("-127");
|
||||
parsebase::Parser<> p3("input", s3);
|
||||
|
||||
p3.expect<int>(-127);
|
||||
REQUIRE_THROWS_AS(p3.parse<int>(), parsebase::ParserException);
|
||||
|
||||
std::stringstream s4("128 -1023 -4095");
|
||||
parsebase::Parser<> p4("input", s4);
|
||||
|
||||
REQUIRE_NOTHROW(p4.expect<size_t>(128));
|
||||
REQUIRE_NOTHROW(p4.expect<int>(-1023));
|
||||
REQUIRE_NOTHROW(p4.expect<int>(-4095));
|
||||
REQUIRE_THROWS_AS(p4.parse<int>(), parsebase::ParserException);
|
||||
|
||||
std::stringstream s5("0");
|
||||
parsebase::Parser<> p5("input", s5);
|
||||
|
||||
p5.expect<bool>(false);
|
||||
REQUIRE_THROWS_AS(p5.parse<bool>(), parsebase::ParserException);
|
||||
|
||||
std::stringstream s6("0 1 0");
|
||||
parsebase::Parser<> p6("input", s6);
|
||||
|
||||
REQUIRE_NOTHROW(p6.expect<bool>(false));
|
||||
REQUIRE_NOTHROW(p6.expect<bool>(true));
|
||||
REQUIRE_NOTHROW(p6.expect<bool>(false));
|
||||
REQUIRE_THROWS_AS(p6.parse<bool>(), parsebase::ParserException);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_CASE("[parser] While parsing, the cursor location is as expcected", "[parser]")
|
||||
{
|
||||
std::stringstream s("123 \n4\ntest1\n test2\ntest3 \ntest4\n\n\n\n");
|
||||
parsebase::Parser<> p("input", s);
|
||||
|
||||
const auto startPosition = p.position();
|
||||
|
||||
parsebase::Location l;
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 1u);
|
||||
REQUIRE(l.columnStart == 1u);
|
||||
REQUIRE(p.currentCharacter() == '1');
|
||||
|
||||
REQUIRE_NOTHROW(p.advance());
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 1u);
|
||||
REQUIRE(l.columnStart == 2u);
|
||||
REQUIRE(p.currentCharacter() == '2');
|
||||
|
||||
REQUIRE_NOTHROW(p.advance());
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 1u);
|
||||
REQUIRE(l.columnStart == 3u);
|
||||
REQUIRE(p.currentCharacter() == '3');
|
||||
|
||||
REQUIRE_NOTHROW(p.advance());
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 1u);
|
||||
REQUIRE(l.columnStart == 4u);
|
||||
REQUIRE(p.currentCharacter() == ' ');
|
||||
|
||||
REQUIRE_NOTHROW(p.advance());
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 1u);
|
||||
REQUIRE(l.columnStart == 5u);
|
||||
REQUIRE(p.currentCharacter() == '\n');
|
||||
|
||||
REQUIRE_NOTHROW(p.advance());
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 2u);
|
||||
REQUIRE(l.columnStart == 1u);
|
||||
REQUIRE(p.currentCharacter() == '4');
|
||||
|
||||
REQUIRE_NOTHROW(p.advance());
|
||||
|
||||
REQUIRE_NOTHROW(p.expect<std::string>("test1"));
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 3u);
|
||||
REQUIRE(l.columnStart == 6u);
|
||||
|
||||
REQUIRE_NOTHROW(p.expect<std::string>("test2"));
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 4u);
|
||||
REQUIRE(l.columnStart == 7u);
|
||||
|
||||
REQUIRE_NOTHROW(p.expect<std::string>("test3"));
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 5u);
|
||||
REQUIRE(l.columnStart == 6u);
|
||||
|
||||
REQUIRE_NOTHROW(p.skipLine());
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 6u);
|
||||
REQUIRE(l.columnStart == 1u);
|
||||
|
||||
REQUIRE_NOTHROW(p.skipLine());
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 7u);
|
||||
REQUIRE(l.columnStart == 1u);
|
||||
|
||||
REQUIRE_NOTHROW(p.skipWhiteSpace());
|
||||
|
||||
l = p.location();
|
||||
REQUIRE(l.rowStart == 10u);
|
||||
REQUIRE(l.columnStart == 1u);
|
||||
REQUIRE(p.atEnd());
|
||||
|
||||
p.reset();
|
||||
REQUIRE(p.position() == startPosition);
|
||||
REQUIRE_FALSE(p.atEnd());
|
||||
|
||||
for (size_t i = 0; i < 5; i++)
|
||||
p.advance();
|
||||
|
||||
REQUIRE(p.position() == static_cast<std::istream::pos_type>(5));
|
||||
|
||||
p.seek(static_cast<std::istream::pos_type>(7));
|
||||
|
||||
REQUIRE(p.position() == static_cast<std::istream::pos_type>(7));
|
||||
|
||||
REQUIRE_NOTHROW(p.expect<std::string>("test1"));
|
||||
|
||||
// TODO: test parser with multiple sections
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_CASE("[parser] Comments are correctly removed", "[parser]")
|
||||
{
|
||||
std::stringstream s1("; comment at beginning\ntest1; comment in between\ntest2; comment at end");
|
||||
parsebase::Parser<> p1("input", s1);
|
||||
|
||||
p1.removeComments(";", "\n", false);
|
||||
|
||||
parsebase::Location l;
|
||||
|
||||
REQUIRE_NOTHROW(p1.expect<std::string>("test1"));
|
||||
|
||||
l = p1.location();
|
||||
REQUIRE(l.rowStart == 2u);
|
||||
REQUIRE(l.columnStart == 6u);
|
||||
|
||||
REQUIRE_NOTHROW(p1.expect<std::string>("test2"));
|
||||
|
||||
l = p1.location();
|
||||
REQUIRE(l.rowStart == 3u);
|
||||
REQUIRE(l.columnStart == 6u);
|
||||
|
||||
p1.skipWhiteSpace();
|
||||
|
||||
REQUIRE(p1.atEnd());
|
||||
|
||||
std::stringstream s2("test;");
|
||||
parsebase::Parser<> p2("input", s2);
|
||||
|
||||
p2.removeComments(";", "\n", false);
|
||||
|
||||
REQUIRE_NOTHROW(p2.expect<std::string>("test"));
|
||||
|
||||
p2.skipWhiteSpace();
|
||||
|
||||
REQUIRE(p2.atEnd());
|
||||
|
||||
std::stringstream s3("/* comment at start */ test1 /* comment in between */ test2 /*");
|
||||
parsebase::Parser<> p3("input", s3);
|
||||
|
||||
p3.removeComments("/*", "*/", true);
|
||||
|
||||
REQUIRE_NOTHROW(p3.expect<std::string>("test1"));
|
||||
REQUIRE_NOTHROW(p3.expect<std::string>("test2"));
|
||||
|
||||
p3.skipWhiteSpace();
|
||||
|
||||
REQUIRE(p3.atEnd());
|
||||
}
|
2
lib/parsebase/tests/main.cpp
Normal file
2
lib/parsebase/tests/main.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include <catch.hpp>
|
Reference in New Issue
Block a user