2828
2929#include < fmt/format.h>
3030
31- #include < sstream>
32-
3331using namespace std ::string_literals;
3432using namespace solidity ;
3533using namespace solidity ::test;
@@ -39,15 +37,21 @@ using namespace solidity::langutil;
3937
4038Json PlainAssemblyParser::parse (std::string _sourceName, std::string const & _source)
4139{
40+ m_sourceStream = std::istringstream (_source);
4241 m_sourceName = std::move (_sourceName);
4342 Json codeJSON = Json::array ();
44- std::istringstream sourceStream (_source);
45- while (getline (sourceStream, m_line))
43+ m_lineNumber = 0 ;
44+
45+ if (!m_line.has_value ())
46+ advanceLine ();
47+
48+ while (m_line.has_value ())
4649 {
47- advanceLine (m_line);
4850 if (m_lineTokens.empty ())
51+ {
52+ advanceLine ();
4953 continue ;
50-
54+ }
5155 if (c_instructions.contains (currentToken ().value ))
5256 {
5357 expectNoMoreArguments ();
@@ -84,6 +88,8 @@ Json PlainAssemblyParser::parse(std::string _sourceName, std::string const& _sou
8488 }
8589 else
8690 BOOST_THROW_EXCEPTION (std::runtime_error (formatError (" Unknown instruction." )));
91+
92+ advanceLine ();
8793 }
8894 return {{" .code" , codeJSON}};
8995}
@@ -125,12 +131,20 @@ void PlainAssemblyParser::expectNoMoreArguments()
125131 BOOST_THROW_EXCEPTION (std::runtime_error (formatError (" Too many arguments." )));
126132}
127133
128- void PlainAssemblyParser::advanceLine (std::string_view _line )
134+ bool PlainAssemblyParser::advanceLine ()
129135{
136+ std::string line;
137+ if (!getline (m_sourceStream, line))
138+ {
139+ m_line = std::nullopt ;
140+ return false ;
141+ }
142+
130143 ++m_lineNumber;
131- m_line = _line ;
132- m_lineTokens = tokenizeLine (m_line);
144+ m_line = std::move (line) ;
145+ m_lineTokens = tokenizeLine (* m_line);
133146 m_tokenIndex = 0 ;
147+ return true ;
134148}
135149
136150std::vector<PlainAssemblyParser::Token> PlainAssemblyParser::tokenizeLine (std::string_view _line)
@@ -162,6 +176,9 @@ std::vector<PlainAssemblyParser::Token> PlainAssemblyParser::tokenizeLine(std::s
162176
163177std::string PlainAssemblyParser::formatError (std::string_view _message) const
164178{
179+ soltestAssert (m_line.has_value ());
180+ soltestAssert (!m_lineTokens.empty ());
181+
165182 std::string lineNumberString = std::to_string (m_lineNumber);
166183 std::string padding (lineNumberString.size (), ' ' );
167184 std::string underline = std::string (currentToken ().position , ' ' ) + std::string (currentToken ().value .size (), ' ^' );
@@ -174,7 +191,7 @@ std::string PlainAssemblyParser::formatError(std::string_view _message) const
174191 _message,
175192 padding, m_sourceName,
176193 padding,
177- m_lineNumber, m_line,
194+ m_lineNumber, * m_line,
178195 padding, underline
179196 );
180197}
0 commit comments