diff options
author | Christian <c@ethdev.com> | 2014-10-17 05:49:45 +0800 |
---|---|---|
committer | Christian <c@ethdev.com> | 2014-10-17 18:37:51 +0800 |
commit | a7f9815c0f68a7cb9571193ded851fbedb418422 (patch) | |
tree | 09adba8ef0aa273f5a84251ed53662b7d7fe3972 /Scanner.h | |
parent | 8a506b505f4725e8a76bbad8399562099e4510c3 (diff) | |
download | dexon-solidity-a7f9815c0f68a7cb9571193ded851fbedb418422.tar.gz dexon-solidity-a7f9815c0f68a7cb9571193ded851fbedb418422.tar.zst dexon-solidity-a7f9815c0f68a7cb9571193ded851fbedb418422.zip |
Coding style and cleanup
Diffstat (limited to 'Scanner.h')
-rw-r--r-- | Scanner.h | 113 |
1 files changed, 33 insertions, 80 deletions
@@ -63,27 +63,13 @@ class ParserRecorder; class CharStream { public: - CharStream() - : m_pos(0) - {} - - explicit CharStream(const std::string& _source) : m_source(_source), m_pos(0) {} + CharStream() : m_pos(0) {} + explicit CharStream(const std::string& _source): m_source(_source), m_pos(0) {} int getPos() const { return m_pos; } bool isPastEndOfInput() const { return m_pos >= m_source.size(); } char get() const { return m_source[m_pos]; } - char advanceAndGet() - { - if (isPastEndOfInput()) return 0; - ++m_pos; - if (isPastEndOfInput()) return 0; - return get(); - } - char rollback(size_t _amount) - { - BOOST_ASSERT(m_pos >= _amount); - m_pos -= _amount; - return get(); - } + char advanceAndGet(); + char rollback(size_t _amount); /// Functions that help pretty-printing parse errors /// Do only use in error cases, they are quite expensive. @@ -96,8 +82,6 @@ private: size_t m_pos; }; -// ---------------------------------------------------------------------------- -// JavaScript Scanner. class Scanner { @@ -107,7 +91,7 @@ public: class LiteralScope { public: - explicit LiteralScope(Scanner* self) : scanner_(self), complete_(false) { scanner_->startNewLiteral(); } + explicit LiteralScope(Scanner* self): scanner_(self), complete_(false) { scanner_->startNewLiteral(); } ~LiteralScope() { if (!complete_) scanner_->dropLiteral(); } void Complete() { complete_ = true; } @@ -118,47 +102,36 @@ public: explicit Scanner(const CharStream& _source); - // Resets the scanner as if newly constructed with _input as input. + /// Resets the scanner as if newly constructed with _input as input. void reset(const CharStream& _source); - // Returns the next token and advances input. + /// Returns the next token and advances input. Token::Value next(); - // Returns the current token again. + + /// Information about the current token + /// @{ + + /// Returns the current token Token::Value getCurrentToken() { return m_current_token.token; } - // Returns the location information for the current token - // (the token last returned by Next()). Location getCurrentLocation() const { return m_current_token.location; } const std::string& getCurrentLiteral() const { return m_current_token.literal; } + /// @} - // Similar functions for the upcoming token. - - // One token look-ahead (past the token returned by Next()). - Token::Value peek() const { return m_next_token.token; } - + /// Information about the next token + /// @{ + /// Returns the next token without advancing input. + Token::Value peekNextToken() const { return m_next_token.token; } Location peekLocation() const { return m_next_token.location; } const std::string& peekLiteral() const { return m_next_token.literal; } + /// @} /// Functions that help pretty-printing parse errors. /// Do only use in error cases, they are quite expensive. /// @{ - std::string getLineAtPosition(int _position) const - { - return m_source.getLineAtPosition(_position); - } - std::tuple<int, int> translatePositionToLineColumn(int _position) const - { - return m_source.translatePositionToLineColumn(_position); - } + std::string getLineAtPosition(int _position) const { return m_source.getLineAtPosition(_position); } + std::tuple<int, int> translatePositionToLineColumn(int _position) const { return m_source.translatePositionToLineColumn(_position); } /// @} - // Returns true if there was a line terminator before the peek'ed token, - // possibly inside a multi-line comment. - bool hasAnyLineTerminatorBeforeNext() const - { - return m_hasLineTerminatorBeforeNext || - m_hasMultilineCommentBeforeNext; - } - private: // Used for the current and look-ahead token. struct TokenDesc @@ -168,34 +141,22 @@ private: std::string literal; }; - // Literal buffer support + /// Literal buffer support + /// @{ inline void startNewLiteral() { m_next_token.literal.clear(); } - inline void addLiteralChar(char c) { m_next_token.literal.push_back(c); } - inline void dropLiteral() { m_next_token.literal.clear(); } - inline void addLiteralCharAndAdvance() { addLiteralChar(m_char); advance(); } + /// @} - // Low-level scanning support. bool advance() { m_char = m_source.advanceAndGet(); return !m_source.isPastEndOfInput(); } - void rollback(int amount) { m_char = m_source.rollback(amount); } + void rollback(int _amount) { m_char = m_source.rollback(_amount); } - inline Token::Value selectToken(Token::Value tok) { advance(); return tok; } + inline Token::Value selectToken(Token::Value _tok) { advance(); return _tok; } + /// If the next character is _next, advance and return _then, otherwise return _else. + inline Token::Value selectToken(char _next, Token::Value _then, Token::Value _else); - inline Token::Value selectToken(char next, Token::Value then, Token::Value else_) - { - advance(); - if (m_char == next) - { - advance(); - return then; - } - else - return else_; - } - - bool scanHexNumber(char& scanned_number, int expected_length); + bool scanHexNumber(char& o_scannedNumber, int _expectedLength); // Scans a single JavaScript token. void scanToken(); @@ -210,12 +171,12 @@ private: Token::Value scanString(); - // Scans an escape-sequence which is part of a string and adds the - // decoded character to the current literal. Returns true if a pattern - // is scanned. + /// Scans an escape-sequence which is part of a string and adds the + /// decoded character to the current literal. Returns true if a pattern + /// is scanned. bool scanEscape(); - // Return the current source position. + /// Return the current source position. int getSourcePos() { return m_source.getPos(); } bool isSourcePastEndOfInput() { return m_source.isPastEndOfInput(); } @@ -224,16 +185,8 @@ private: CharStream m_source; - // one character look-ahead, equals 0 at end of input + /// one character look-ahead, equals 0 at end of input char m_char; - - // Whether there is a line terminator whitespace character after - // the current token, and before the next. Does not count newlines - // inside multiline comments. - bool m_hasLineTerminatorBeforeNext; - // Whether there is a multi-line comment that contains a - // line-terminator after the current token, and before the next. - bool m_hasMultilineCommentBeforeNext; }; } |