Mercurial > code
changeset 429:31bddece9860
Ini: analyze does not produce spaces/lines/comment anymore
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 14 Oct 2015 19:48:42 +0200 |
parents | c2b02b5f32a9 |
children | 625f5d64b093 |
files | C++/modules/Ini/Ini.cpp C++/modules/Ini/Ini.h C++/tests/Ini/configs/error-lineassigment.conf C++/tests/Ini/configs/novalue.conf C++/tests/Ini/main.cpp CMakeLists.txt |
diffstat | 6 files changed, 25 insertions(+), 99 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Ini/Ini.cpp Wed Oct 14 15:15:59 2015 +0200 +++ b/C++/modules/Ini/Ini.cpp Wed Oct 14 19:48:42 2015 +0200 @@ -36,17 +36,14 @@ using Iterator = std::istreambuf_iterator<char>; using TokenIterator = std::vector<Token>::const_iterator; -#if defined(_WIN32) inline bool isAbsolute(const std::string &path) noexcept { +#if defined(_WIN32) return !PathIsRelative(path.c_str()); -} #else -inline bool isAbsolute(const std::string &path) noexcept -{ return path.size() > 0 && path[0] == '/'; +#endif } -#endif inline bool isQuote(char c) noexcept { @@ -64,24 +61,27 @@ return c == '[' || c == ']' || c == '@' || c == '#' || c == '=' || c == '\'' || c == '"'; } -void analyzeLine(Tokens &list, int &line, int &column, Iterator &it) +void analyzeLine(int &line, int &column, Iterator &it) { - list.push_back({ Token::Line, line++, column }); + ++ line; ++ it; column = 0; } -void analyzeComment(Tokens &list, int &line, int &column, Iterator &it, Iterator end) +void analyzeComment(int &column, Iterator &it, Iterator end) { - std::string value{1, *it}; - int save = column; - while (it != end && *it != '\n') { ++ column; - value += *it++; + ++ it; } +} - list.push_back({ Token::Comment, line, save, std::move(value) }); +void analyzeSpaces(int &column, Iterator &it, Iterator end) +{ + while (it != end && isSpace(*it)) { + ++ column; + ++ it; + } } void analyzeSection(Tokens &list, int &line, int &column, Iterator &it, Iterator end) @@ -118,19 +118,6 @@ ++ it; } -void analyzeSpaces(Tokens &list, int &line, int &column, Iterator &it, Iterator end) -{ - std::string value; - int save = column; - - while (it != end && (*it == ' ' || *it == '\t')) { - ++ column; - value += *it++; - } - - list.push_back({ Token::Space, line, save, std::move(value) }); -} - void analyzeQuotedWord(Tokens &list, int &line, int &column, Iterator &it, Iterator end) { std::string value; @@ -193,15 +180,15 @@ while (it != end) { if (*it == '\n') { - analyzeLine(list, line, column, it); + analyzeLine(line, column, it); } else if (*it == '#') { - analyzeComment(list, line, column, it, end); + analyzeComment(column, it, end); } else if (*it == '[') { analyzeSection(list, line, column, it, end); } else if (*it == '=') { analyzeAssign(list, line, column, it); } else if (isSpace(*it)) { - analyzeSpaces(list, line, column, it, end); + analyzeSpaces(column, it, end); } else if (*it == '@') { analyzeInclude(list, line, column, it, end); } else if (isQuote(*it)) { @@ -214,13 +201,6 @@ return list; } -void parseSpaces(TokenIterator &it, TokenIterator end) -{ - while (it != end && it->type() == Token::Space) { - ++ it; - } -} - void parseOption(Section &sc, TokenIterator &it, TokenIterator end) { std::string key = it->value(); @@ -228,22 +208,16 @@ TokenIterator save = it; - /* Optional spaces before '=' */ - parseSpaces(++it, end); - /* No '=' or something else? */ - if (it == end) { + if (++it == end) { throw Error{save->line(), save->column(), "expected '=' assignment, got <EOF>"}; } if (it->type() != Token::Assign) { throw Error{it->line(), it->column(), "expected '=' assignment, got " + it->value()}; } - /* Optional spaces after '=' */ - parseSpaces(++it, end); - /* Empty options are allowed so just test for words */ - if (it != end) { + if (++it != end) { if (it->type() == Token::Word || it->type() == Token::QuotedWord) { value = it++->value(); } @@ -260,9 +234,6 @@ throw Error{save->line(), save->column(), "expected file name after '@include' statement, got <EOF>"}; } - /* Get file name */ - parseSpaces(it, end); - if (it->type() != Token::Word && it->type() != Token::QuotedWord) { throw Error{it->line(), it->column(), "expected file name after '@include' statement, got " + it->value()}; } @@ -300,16 +271,6 @@ /* Read until next section */ while (it != end && it->type() != Token::Section) { - switch (it->type()) { - case Token::Line: - case Token::Comment: - case Token::Space: - it ++; - continue; - default: - break; - } - if (it->type() != Token::Word) { throw Error{it->line(), it->column(), "unexpected token '" + it->value() + "' in section definition"}; } @@ -334,11 +295,6 @@ case Token::Section: parseSection(doc, it, end); break; - case Token::Comment: - case Token::Line: - case Token::Space: - ++ it; - break; default: throw Error{it->line(), it->column(), "unexpected '" + it->value() + "' on root document"}; } @@ -396,4 +352,4 @@ } } -} // !ini \ No newline at end of file +} // !ini
--- a/C++/modules/Ini/Ini.h Wed Oct 14 15:15:59 2015 +0200 +++ b/C++/modules/Ini/Ini.h Wed Oct 14 19:48:42 2015 +0200 @@ -111,10 +111,7 @@ Section, //!< [section] Word, //!< word without quotes QuotedWord, //!< word with quotes - Comment, //!< # comment like this - Assign, //!< = assignment - Space, //!< space or tabs - Line //!< '\n' + Assign //!< = assignment }; private: @@ -146,18 +143,9 @@ case QuotedWord: m_value = value; break; - case Comment: - m_value = "comment"; - break; case Assign: m_value = "="; break; - case Line: - m_value = "<newline>"; - break; - case Space: - m_value = "<space>"; - break; default: break; } @@ -498,4 +486,4 @@ } // !ini -#endif // !_INI_H_ \ No newline at end of file +#endif // !_INI_H_
--- a/C++/tests/Ini/configs/error-lineassigment.conf Wed Oct 14 15:15:59 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -[general] -host -= -google.fr
--- a/C++/tests/Ini/configs/novalue.conf Wed Oct 14 15:15:59 2015 +0200 +++ b/C++/tests/Ini/configs/novalue.conf Wed Oct 14 19:48:42 2015 +0200 @@ -1,6 +1,6 @@ [plugins] -histedit= -highlight= #empty -general = +histedit= "" +highlight= "" #empty +general = ""
--- a/C++/tests/Ini/main.cpp Wed Oct 14 15:15:59 2015 +0200 +++ b/C++/tests/Ini/main.cpp Wed Oct 14 19:48:42 2015 +0200 @@ -224,19 +224,6 @@ } } -TEST(Errors, lineassigment) -{ - // The = assignment must be on the same line as the option key - try { - ini::Document doc{ini::File{"Ini/error-lineassigment.conf"}}; - - FAIL() << "Failure expected, got success"; - } catch (const ini::Error &ex) { - ASSERT_EQ(2, ex.line()); - ASSERT_EQ(4, ex.column()); - } -} - TEST(Errors, badcomment) { // Comment can't between option-key and = assigment @@ -246,7 +233,7 @@ FAIL() << "Failure expected, got success"; } catch (const ini::Error &ex) { ASSERT_EQ(2, ex.line()); - ASSERT_EQ(8, ex.column()); + ASSERT_EQ(0, ex.column()); } }
--- a/CMakeLists.txt Wed Oct 14 15:15:59 2015 +0200 +++ b/CMakeLists.txt Wed Oct 14 19:48:42 2015 +0200 @@ -236,7 +236,6 @@ ${code_SOURCE_DIR}/C++/tests/Ini/configs/error-badcomment.conf ${code_SOURCE_DIR}/C++/tests/Ini/configs/error-badinclude.conf ${code_SOURCE_DIR}/C++/tests/Ini/configs/error-badsection.conf - ${code_SOURCE_DIR}/C++/tests/Ini/configs/error-lineassigment.conf ${code_SOURCE_DIR}/C++/tests/Ini/configs/error-nosection.conf ${code_SOURCE_DIR}/C++/tests/Ini/configs/error-unterminatedsection.conf ${code_SOURCE_DIR}/C++/tests/Ini/configs/includes.conf