diff options
Diffstat (limited to 'tools/common')
-rw-r--r-- | tools/common/path.h | 7 | ||||
-rw-r--r-- | tools/common/target_detect.h | 11 | ||||
-rw-r--r-- | tools/common/toml.cpp | 85 | ||||
-rw-r--r-- | tools/common/toml.h | 21 |
4 files changed, 93 insertions, 31 deletions
diff --git a/tools/common/path.h b/tools/common/path.h index eae4951f..1ad6f506 100644 --- a/tools/common/path.h +++ b/tools/common/path.h @@ -38,6 +38,13 @@ public: return m_str != ""; } + bool operator==(const path& p) const { + return m_str == p.m_str; + } + bool operator!=(const path& p) const { + return m_str != p.m_str; + } + path& operator/=(const path& p) { if(!p.is_valid()) diff --git a/tools/common/target_detect.h b/tools/common/target_detect.h index a4c44ba4..1bfc7dd9 100644 --- a/tools/common/target_detect.h +++ b/tools/common/target_detect.h @@ -24,6 +24,8 @@ # define DEFAULT_TARGET_NAME "arm-linux-gnu" # elif defined(__i386__) # define DEFAULT_TARGET_NAME "i586-linux-gnu" +# elif defined(__m68k__) +# define DEFAULT_TARGET_NAME "m68k-linux-gnu" # else # warning "Unable to detect a suitable default target (linux-gnu)" # endif @@ -73,6 +75,15 @@ // - Apple devices #elif defined(__APPLE__) # define DEFAULT_TARGET_NAME "x86_64-apple-macosx" +// - Haiku +#elif defined(__HAIKU__) +# if defined(__x86_64__) +# define DEFAULT_TARGET_NAME "x86_64-unknown-haiku" +# elif defined(__arm__) +# define DEFAULT_TARGET_NAME "arm-unknown-haiku" +# else +# warning "Unable to detect a suitable default target (Haiku)" +# endif // - Unknown #else # warning "Unable to detect a suitable default target" diff --git a/tools/common/toml.cpp b/tools/common/toml.cpp index 7e24b610..4e8e6da6 100644 --- a/tools/common/toml.cpp +++ b/tools/common/toml.cpp @@ -80,11 +80,8 @@ struct Token }; TomlFile::TomlFile(const ::std::string& filename): - m_if(filename) + m_lexer(filename) { - if( !m_if.is_open() ) { - throw ::std::runtime_error("Unable to open file '" + filename + "'"); - } } TomlFileIter TomlFile::begin() { @@ -99,12 +96,14 @@ TomlFileIter TomlFile::end() TomlKeyValue TomlFile::get_next_value() { - auto t = Token::lex_from(m_if); + auto t = m_lexer.get_token(); if(m_current_composite.empty()) { while( t.m_type == Token::Type::Newline ) - t = Token::lex_from(m_if); + { + t = m_lexer.get_token(); + } // Expect '[', a string, or an identifier switch(t.m_type) @@ -116,32 +115,32 @@ TomlKeyValue TomlFile::get_next_value() m_current_block.clear(); do { - t = Token::lex_from(m_if); + t = m_lexer.get_token(); bool is_array = false; if(t.m_type == Token::Type::SquareOpen) { is_array = true; - t = Token::lex_from(m_if); + t = m_lexer.get_token(); } assert(t.m_type == Token::Type::Ident || t.m_type == Token::Type::String); m_current_block.push_back(t.as_string()); if(is_array) { m_current_block.push_back(::format(m_array_counts[t.as_string()]++)); - t = Token::lex_from(m_if); + t = m_lexer.get_token(); assert(t.m_type == Token::Type::SquareClose); } - t = Token::lex_from(m_if); + t = m_lexer.get_token(); } while(t.m_type == Token::Type::Dot); if( t.m_type != Token::Type::SquareClose ) { - throw ::std::runtime_error(::format("Unexpected token in block header - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token in block header - ", t)); } - t = Token::lex_from(m_if); + t = m_lexer.get_token(); if (t.m_type != Token::Type::Newline) { - throw ::std::runtime_error(::format("Unexpected token after block block - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token after block block - ", t)); } DEBUG("Start block " << m_current_block); // Recurse! @@ -156,7 +155,7 @@ TomlKeyValue TomlFile::get_next_value() if( t.m_type == Token::Type::Eof ) { // EOF isn't allowed here - throw ::std::runtime_error(::format("Unexpected EOF in composite")); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected EOF in composite")); } } switch (t.m_type) @@ -165,14 +164,14 @@ TomlKeyValue TomlFile::get_next_value() case Token::Type::Ident: break; default: - throw ::std::runtime_error(::format("Unexpected token for key - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token for key - ", t)); } ::std::string key_name = t.as_string(); - t = Token::lex_from(m_if); + t = m_lexer.get_token(); if(t.m_type != Token::Type::Assign) - throw ::std::runtime_error(::format("Unexpected token after key - ", t)); - t = Token::lex_from(m_if); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token after key - ", t)); + t = m_lexer.get_token(); // --- Value --- TomlKeyValue rv; @@ -193,10 +192,12 @@ TomlKeyValue TomlFile::get_next_value() rv.path.push_back(key_name); rv.value.m_type = TomlValue::Type::List; - while( (t = Token::lex_from(m_if)).m_type != Token::Type::SquareClose ) + while( (t = m_lexer.get_token()).m_type != Token::Type::SquareClose ) { while( t.m_type == Token::Type::Newline ) - t = Token::lex_from(m_if); + { + t = m_lexer.get_token(); + } if( t.m_type == Token::Type::SquareClose ) break; @@ -208,15 +209,15 @@ TomlKeyValue TomlFile::get_next_value() rv.value.m_sub_values.push_back(TomlValue { t.as_string() }); break; default: - throw ::std::runtime_error(::format("Unexpected token in array value position - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token in array value position - ", t)); } - t = Token::lex_from(m_if); + t = m_lexer.get_token(); if(t.m_type != Token::Type::Comma) break; } if(t.m_type != Token::Type::SquareClose) - throw ::std::runtime_error(::format("Unexpected token after array - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token after array - ", t)); break; case Token::Type::BraceOpen: m_current_composite.push_back(key_name); @@ -228,7 +229,7 @@ TomlKeyValue TomlFile::get_next_value() rv.path.insert(rv.path.end(), m_current_composite.begin(), m_current_composite.end()); rv.path.push_back(key_name); rv.value = TomlValue { t.m_intval }; - return rv; + break; case Token::Type::Ident: if( t.m_data == "true" ) { @@ -247,33 +248,57 @@ TomlKeyValue TomlFile::get_next_value() } else { - throw ::std::runtime_error(::format("Unexpected identifier in value position - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected identifier in value position - ", t)); } break; default: - throw ::std::runtime_error(::format("Unexpected token in value position - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token in value position - ", t)); } - t = Token::lex_from(m_if); + t = m_lexer.get_token(); while (!m_current_composite.empty() && t.m_type == Token::Type::BraceClose) { DEBUG("Leave composite block " << m_current_block << ", " << m_current_composite); m_current_composite.pop_back(); - t = Token::lex_from(m_if); + t = m_lexer.get_token(); } if( m_current_composite.empty() ) { if(t.m_type != Token::Type::Newline && t.m_type != Token::Type::Eof) - throw ::std::runtime_error(::format("Unexpected token in TOML file after entry - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token in TOML file after entry - ", t)); } else { if( t.m_type != Token::Type::Comma ) - throw ::std::runtime_error(::format("Unexpected token in TOML file after composite entry - ", t)); + throw ::std::runtime_error(::format(m_lexer, ": Unexpected token in TOML file after composite entry - ", t)); } return rv; } +TomlLexer::TomlLexer(const ::std::string& filename) + :m_if(filename) + ,m_filename(filename) + ,m_line(1) +{ + if( !m_if.is_open() ) { + throw ::std::runtime_error("Unable to open file '" + filename + "'"); + } +} +Token TomlLexer::get_token() +{ + auto rv = Token::lex_from(m_if); + if( rv.m_type == Token::Type::Newline ) + { + m_line ++; + } + return rv; +} +::std::ostream& operator<<(::std::ostream& os, const TomlLexer& x) +{ + os << x.m_filename << ":" << x.m_line; + return os; +} + Token Token::lex_from(::std::ifstream& is) { auto rv = Token::lex_from_inner(is); diff --git a/tools/common/toml.h b/tools/common/toml.h index 4c97e7f2..17e05142 100644 --- a/tools/common/toml.h +++ b/tools/common/toml.h @@ -15,11 +15,28 @@ class TomlFileIter; struct TomlKeyValue; -class TomlFile +struct Token; +class TomlLexer { + friend class TomlFile; /// Input file stream ::std::ifstream m_if; + ::std::string m_filename; + unsigned m_line; +protected: + TomlLexer(const ::std::string& filename); + Token get_token(); + +public: + friend ::std::ostream& operator<<(::std::ostream& os, const TomlLexer& x); +}; + +class TomlFile +{ + /// Input file stream + TomlLexer m_lexer; + /// Name of the current `[]` block ::std::vector<::std::string> m_current_block; @@ -40,6 +57,8 @@ public: // Obtain the next value in the file TomlKeyValue get_next_value(); + + const TomlLexer& lexer() const; }; struct TomlValue |