Skip to content

Commit 09fc1d4

Browse files
committed
PlainAssemblyParser: Store the stream globally in the class
1 parent b136829 commit 09fc1d4

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

test/libevmasm/PlainAssemblyParser.cpp

+27-10
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
#include <fmt/format.h>
3030

31-
#include <sstream>
32-
3331
using namespace std::string_literals;
3432
using namespace solidity;
3533
using namespace solidity::test;
@@ -39,15 +37,21 @@ using namespace solidity::langutil;
3937

4038
Json 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

136150
std::vector<PlainAssemblyParser::Token> PlainAssemblyParser::tokenizeLine(std::string_view _line)
@@ -162,6 +176,9 @@ std::vector<PlainAssemblyParser::Token> PlainAssemblyParser::tokenizeLine(std::s
162176

163177
std::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
}

test/libevmasm/PlainAssemblyParser.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <libsolutil/JSON.h>
2222

23+
#include <sstream>
2324
#include <string>
2425
#include <string_view>
2526
#include <vector>
@@ -63,17 +64,18 @@ class PlainAssemblyParser
6364
bool advanceToken();
6465
std::string_view expectArgument();
6566
void expectNoMoreArguments();
66-
void advanceLine(std::string_view _line);
67+
bool advanceLine();
6768

6869
static std::vector<Token> tokenizeLine(std::string_view _line);
6970
std::string formatError(std::string_view _message) const;
7071

7172
private:
72-
std::string m_sourceName; ///< Name of the file the source comes from.
73-
size_t m_lineNumber = 0; ///< The number of the current line within the source, 1-based.
74-
std::string m_line; ///< The current line, unparsed.
75-
std::vector<Token> m_lineTokens; ///< Decomposition of the current line into tokens (does not include comments).
76-
size_t m_tokenIndex = 0; ///< Points at a token within m_lineTokens.
73+
std::istringstream m_sourceStream; ///< The source code being parsed.
74+
std::string m_sourceName; ///< Name of the file the source comes from.
75+
size_t m_lineNumber = 0; ///< The number of the current line within the source, 1-based.
76+
std::optional<std::string> m_line; ///< The current line, unparsed.
77+
std::vector<Token> m_lineTokens; ///< Decomposition of the current line into tokens (does not include comments).
78+
size_t m_tokenIndex = 0; ///< Points at a token within m_lineTokens.
7779
};
7880

7981
}

0 commit comments

Comments
 (0)