From d9cb8c8c5e68e8156daf064d25847054ddb4a32c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 10 Jul 2020 16:05:52 +0100 Subject: [PATCH] Add ScannerKind and replace supportPeriodInIdentifier --- liblangutil/Scanner.cpp | 10 ++-------- liblangutil/Scanner.h | 21 ++++++++++++++++----- libyul/AsmParser.cpp | 4 ++-- test/libsolidity/SolidityScanner.cpp | 6 +++--- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/liblangutil/Scanner.cpp b/liblangutil/Scanner.cpp index 3e125f04e..017a65e18 100644 --- a/liblangutil/Scanner.cpp +++ b/liblangutil/Scanner.cpp @@ -147,7 +147,7 @@ void Scanner::reset(shared_ptr _source) void Scanner::reset() { m_source->reset(); - m_supportPeriodInIdentifier = false; + m_kind = ScannerKind::Solidity; m_char = m_source->get(); skipWhitespace(); next(); @@ -163,12 +163,6 @@ void Scanner::setPosition(size_t _offset) next(); } -void Scanner::supportPeriodInIdentifier(bool _value) -{ - m_supportPeriodInIdentifier = _value; - rescan(); -} - bool Scanner::scanHexByte(char& o_scannedByte) { char x = 0; @@ -970,7 +964,7 @@ tuple Scanner::scanIdentifierOrKeyword() LiteralScope literal(this, LITERAL_TYPE_STRING); addLiteralCharAndAdvance(); // Scan the rest of the identifier characters. - while (isIdentifierPart(m_char) || (m_char == '.' && m_supportPeriodInIdentifier)) + while (isIdentifierPart(m_char) || (m_char == '.' && m_kind == ScannerKind::Yul)) addLiteralCharAndAdvance(); literal.complete(); return TokenTraits::fromIdentifierOrKeyword(m_tokens[NextNext].literal); diff --git a/liblangutil/Scanner.h b/liblangutil/Scanner.h index 24ec098ab..f44a9940a 100644 --- a/liblangutil/Scanner.h +++ b/liblangutil/Scanner.h @@ -68,6 +68,12 @@ class AstRawString; class AstValueFactory; class ParserRecorder; +enum class ScannerKind +{ + Solidity, + Yul +}; + enum class ScannerError { NoError, @@ -107,9 +113,14 @@ public: /// Resets scanner to the start of input. void reset(); - /// Enables or disables support for period in identifier. - /// This re-scans the current token and comment literal and thus invalidates it. - void supportPeriodInIdentifier(bool _value); + /// Changes the scanner mode. + void setScannerMode(ScannerKind _kind) + { + m_kind = _kind; + + // Invalidate lookahead buffer. + rescan(); + } /// @returns the next token and advances input Token next(); @@ -249,8 +260,6 @@ private: size_t sourcePos() const { return m_source->position(); } bool isSourcePastEndOfInput() const { return m_source->isPastEndOfInput(); } - bool m_supportPeriodInIdentifier = false; - enum TokenIndex { Current, Next, NextNext }; TokenDesc m_skippedComments[3] = {}; // desc for the current, next and nextnext skipped comment @@ -258,6 +267,8 @@ private: std::shared_ptr m_source; + ScannerKind m_kind = ScannerKind::Solidity; + /// one character look-ahead, equals 0 at end of input char m_char; }; diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index 6c5d97e36..5dd0d854e 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -42,8 +42,8 @@ unique_ptr Parser::parse(std::shared_ptr const& _scanner, bool _ { m_recursionDepth = 0; - _scanner->supportPeriodInIdentifier(true); - ScopeGuard resetScanner([&]{ _scanner->supportPeriodInIdentifier(false); }); + _scanner->setScannerMode(ScannerKind::Yul); + ScopeGuard resetScanner([&]{ _scanner->setScannerMode(ScannerKind::Solidity); }); try { diff --git a/test/libsolidity/SolidityScanner.cpp b/test/libsolidity/SolidityScanner.cpp index 8f2e6ed59..381e8e318 100644 --- a/test/libsolidity/SolidityScanner.cpp +++ b/test/libsolidity/SolidityScanner.cpp @@ -214,7 +214,7 @@ BOOST_AUTO_TEST_CASE(leading_dot_in_identifier) BOOST_CHECK_EQUAL(scanner.next(), Token::LParen); BOOST_CHECK_EQUAL(scanner.next(), Token::EOS); scanner.reset(CharStream("function .a(", "")); - scanner.supportPeriodInIdentifier(true); + scanner.setScannerMode(ScannerKind::Yul); BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Function); BOOST_CHECK_EQUAL(scanner.next(), Token::Period); BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier); @@ -233,7 +233,7 @@ BOOST_AUTO_TEST_CASE(middle_dot_in_identifier) BOOST_CHECK_EQUAL(scanner.next(), Token::LParen); BOOST_CHECK_EQUAL(scanner.next(), Token::EOS); scanner.reset(CharStream("function a...a(", "")); - scanner.supportPeriodInIdentifier(true); + scanner.setScannerMode(ScannerKind::Yul); BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Function); BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier); BOOST_CHECK_EQUAL(scanner.next(), Token::LParen); @@ -249,7 +249,7 @@ BOOST_AUTO_TEST_CASE(trailing_dot_in_identifier) BOOST_CHECK_EQUAL(scanner.next(), Token::LParen); BOOST_CHECK_EQUAL(scanner.next(), Token::EOS); scanner.reset(CharStream("function a.(", "")); - scanner.supportPeriodInIdentifier(true); + scanner.setScannerMode(ScannerKind::Yul); BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Function); BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier); BOOST_CHECK_EQUAL(scanner.next(), Token::LParen);