8#ifndef META_OCEAN_IO_SCANNER_H
9#define META_OCEAN_IO_SCANNER_H
39 static constexpr uint32_t invalidId = uint32_t(-1);
44 class OCEAN_IO_EXPORT
Token final
102 Token(std::string&& data,
const uint32_t
id,
const Type type);
110 Token(
const std::string& data,
const uint32_t
id,
const Type type);
116 inline Type type()
const;
123 inline bool isType(
const Type type)
const;
129 inline bool isCharacter()
const;
135 inline bool isIdentifier()
const;
141 inline bool isInteger()
const;
147 inline bool isLine()
const;
153 inline bool isNumber()
const;
159 inline bool isIntegerOrNumber()
const;
165 inline bool isKeyword()
const;
178 inline bool isString()
const;
184 inline bool isSymbol()
const;
197 inline bool isEndOfFile()
const;
233 const std::string&
line()
const;
263 inline const std::string& raw()
const;
269 explicit inline operator bool()
const;
277 uint32_t id_ = invalidId;
288 using IdMap = std::unordered_map<std::string, uint32_t>;
340 explicit Scanner(
const std::shared_ptr<std::istream>& stream,
float* progress =
nullptr,
bool* cancel =
nullptr);
349 inline Scanner(
const std::string& filename,
const std::string& buffer,
float* progress =
nullptr,
bool* cancel =
nullptr);
358 inline Scanner(
const std::string& filename, std::string&& buffer,
float* progress =
nullptr,
bool* cancel =
nullptr);
399 inline size_t line()
const;
405 inline size_t column()
const;
423 inline const std::string& filename()
const;
429 inline bool isValid()
const;
441 static bool findNextToken(
const char* pointer,
const size_t size,
const size_t start,
size_t& tokenStart,
size_t& tokenLength);
452 static bool findNextToken(
const char* pointer,
const size_t start,
size_t& tokenStart,
size_t& tokenLength);
463 static inline bool isWhitespace(
const char& character);
484 uint8_t
get(
const size_t offset = 0);
492 std::string
data(
const size_t size)
const;
501 std::string
data(
const size_t offset,
const size_t size)
const;
696 static inline std::shared_ptr<std::istream> createInputStream(
const std::string& filename, std::string&& buffer);
703 static inline std::shared_ptr<std::istream> createInputStream(
const std::string& filename,
const std::string& buffer);
720 float* progress_ =
nullptr;
723 bool* cancel_ =
nullptr;
729 uint8_t* intermediateBufferPointer_ =
nullptr;
732 size_t intermediateBufferSize_ = 0;
738 uint8_t* extraBufferPointer_ =
nullptr;
741 size_t extraBufferSize_ = 0;
750 size_t position_ = 0;
756 bool keywordsAreCaseSensitive_ =
true;
765 size_t maximalLengthLineRemarks_ = 0;
771 size_t maximalLengthScopeRemarks_ = 0;
783 static constexpr size_t minBufferSize_ = 2048;
786 static constexpr size_t maxBufferSize_ = 8192;
796 return type_ == type;
801 return type_ == TOKEN_CHARACTER;
806 return type_ == TOKEN_IDENTIFIER;
811 return type_ == TOKEN_INTEGER;
816 return type_ == TOKEN_NUMBER;
821 return type_ == TOKEN_INTEGER || type_ == TOKEN_NUMBER;
826 return type_ == TOKEN_LINE;
831 return type_ == TOKEN_KEYWORD;
836 return type_ == TOKEN_STRING;
841 return type_ == TOKEN_SYMBOL;
846 return type_ == TOKEN_END_OF_FILE;
854inline Scanner::Token::operator bool()
const
856 return type_ != TOKEN_INVALID;
862 ocean_assert(!
filename.empty() || !buffer.empty());
870inline Scanner::Scanner(
const std::string& filename, std::string&& buffer,
float* progress,
bool* cancel) :
871 Scanner(createInputStream(filename, std::move(buffer)), progress, cancel)
903 return character ==
' ' || character ==
'\t' || character ==
'\n' || character ==
'\r';
908 ocean_assert(!
filename.empty() || !buffer.empty());
912 return std::shared_ptr<std::istream>(
new std::ifstream(
filename.c_str(), std::ios_base::binary));
915 return std::shared_ptr<std::istream>(
new std::istringstream(std::move(buffer)));
920 ocean_assert(!
filename.empty() || !buffer.empty());
924 return std::shared_ptr<std::istream>(
new std::ifstream(
filename.c_str(), std::ios_base::binary));
927 return std::shared_ptr<std::istream>(
new std::istringstream(buffer));
This class implements a token for the scanner.
Definition Scanner.h:45
Token(std::string &&data, const Type type)
Creates a new token with given data and type.
bool isKeyword() const
Returns whether this token holds a keyword.
Definition Scanner.h:829
uint32_t keyword() const
Returns the id of the keyword of this token.
std::string data_
Holds the data of the token.
Definition Scanner.h:280
bool isSymbol(const uint32_t symbol) const
Returns whether this token holds a special symbol.
Type
Definition of different token types.
Definition Scanner.h:52
@ TOKEN_END_OF_FILE
End of file token.
Definition Scanner.h:58
@ TOKEN_INTEGER
Integer token.
Definition Scanner.h:66
@ TOKEN_KEYWORD
Keyword token.
Definition Scanner.h:68
@ TOKEN_LINE
Line token.
Definition Scanner.h:62
@ TOKEN_IDENTIFIER
Identifier token.
Definition Scanner.h:60
@ TOKEN_NUMBER
Number token.
Definition Scanner.h:64
@ TOKEN_CHARACTER
Character token.
Definition Scanner.h:56
@ TOKEN_STRING
String token.
Definition Scanner.h:70
bool isKeyword(const uint32_t keyword) const
Returns whether this token holds a special keyword.
bool isType(const Type type) const
Returns whether this token is of a specific type.
Definition Scanner.h:794
bool isLine() const
Returns whether this token holds a remaining line.
Definition Scanner.h:824
bool isSymbol() const
Returns whether this token holds a symbol.
Definition Scanner.h:839
const std::string & raw() const
Returns the raw data of the token.
Definition Scanner.h:849
bool isCharacter() const
Returns whether this token holds a character.
Definition Scanner.h:799
bool isString() const
Returns whether this token holds a string.
Definition Scanner.h:834
int32_t integer() const
Returns the integer value of this token.
uint32_t symbol() const
Returns the id of the symbol of this token.
std::string moveString()
Returns the string value of this token and invalidates the token.
bool isNumber() const
Returns whether this token holds a number.
Definition Scanner.h:814
Type type() const
Returns the type of the token.
Definition Scanner.h:789
bool isEndOfFile() const
Returns whether this token holds an end of file.
Definition Scanner.h:844
const std::string & string() const
Returns the string value of this token.
const std::string & line() const
Returns the remaining line of this token.
bool isIdentifier() const
Returns whether this token holds an identifier.
Definition Scanner.h:804
bool isIntegerOrNumber() const
Returns whether this token holds an integer or a number.
Definition Scanner.h:819
Type type_
Holds the type of the token.
Definition Scanner.h:274
double number() const
Returns the floating point value of this token.
Token(const std::string &data, const uint32_t id, const Type type)
Creates a new token with given keyword or symbol id.
Token()=default
Creates an invalid token.
uint8_t character() const
Returns the character value of this token.
double integerOrNumber() const
Returns the integer or floating point value of this token.
Token(std::string &&data, const uint32_t id, const Type type)
Creates a new token with given keyword or symbol id.
Token(const std::string &data, const Type type)
Creates a new token with given data and type.
const std::string & identifier() const
Returns the identifier value of this token.
bool isInteger() const
Returns whether this token holds an integer.
Definition Scanner.h:809
This class implements a simple scanner.
Definition Scanner.h:33
std::unordered_map< std::string, std::string > ScopeRemarks
Definition of an unordered map mapping begin remark symbols to end remark symbols.
Definition Scanner.h:298
const Token & lineToken()
Returns a line token starting at the current position.
std::string filename_
The name of the input file, if the input is a file.
Definition Scanner.h:717
std::string discardNonWhiteSpace()
Discards non white space and jumps to the first white space position.
CharTable followingCharTable_
Table holding the definition of allowed following characters.
Definition Scanner.h:777
bool readKeyword(Token &token, const bool consumeBytes)
Tries to read a keyword as next token.
size_t column_
Holds the current column.
Definition Scanner.h:747
size_t column() const
Returns the recent column.
Definition Scanner.h:886
Token recentToken_
Recent token.
Definition Scanner.h:708
uint32_t symbolId(const std::string &data) const
Returns the symbol id of a given string.
uint8_t get(const size_t offset=0)
Returns one character.
std::unordered_map< std::string, uint32_t > IdMap
Definition of an unordered map mapping strings to ids.
Definition Scanner.h:288
CharTable invalidCharTable_
Table holding the definition of not allowed following characters.
Definition Scanner.h:780
bool readInteger(Token &token, const bool consumeBytes)
Tries to read an integer as next token.
IdMap symbolMap_
Map mapping symbol strings to symbol ids.
Definition Scanner.h:759
static std::shared_ptr< std::istream > createInputStream(const std::string &filename, std::string &&buffer)
Creates a file input stream or a string input stream depending on the given input.
Definition Scanner.h:906
static bool findNextToken(const char *pointer, const size_t start, size_t &tokenStart, size_t &tokenLength)
Finds the next token in a given string starting from a specified position.
bool readIdentifier(Token &token, const bool consumeBytes)
Tries to read a identifier as next token.
bool isValid() const
Returns whether the scanner is valid and ready to use.
Definition Scanner.h:896
size_t position() const
Returns the position of the scanner.
uint32_t keywordId(const std::string &data) const
Returns the keyword id of a given string.
CharTable firstCharTable_
Table holding the definition of allowed first characters.
Definition Scanner.h:774
uint8_t getExtra(const size_t offset=0)
Returns one character from the extra buffer.
FirstChar
Definition of first character types.
Definition Scanner.h:309
void consume(const size_t chars=1)
Consumes one or more character.
bool readCharacter(Token &token, const bool consumeBytes)
Tries to read a character as next token.
std::shared_ptr< std::istream > stream_
The input stream from which the scanner receives the data.
Definition Scanner.h:714
std::unordered_set< std::string > LineRemarks
Definition of an unordered set holding line remark symbols.
Definition Scanner.h:293
void registerKeyword(const std::string &keyword, const uint32_t id)
Registers a new keyword.
bool readScopeRemark()
Reads a scope remark comment.
std::string data(const size_t offset, const size_t size) const
Returns data of a specified size starting at the offset position.
bool readRemark()
Reads remark comments.
void registerLineRemark(const std::string &lineRemark)
Registers a line remark symbol.
Scanner(float *progress, bool *cancel)
Creates a new scanner.
Token nextToken_
Next token.
Definition Scanner.h:711
const Token & token()
Returns the recent token.
Token tokenPop()
Returns the recent token and pops it afterwards.
void setKeywordProperty(const bool caseSensitive)
Sets whether the keywords are case sensitive or not.
bool refillExtraBuffer(const size_t minIndex)
Refills the extra buffer.
std::string data(const size_t size) const
Returns data of a specified size starting at the recent position.
size_t line() const
Returns the recent line.
Definition Scanner.h:881
virtual ~Scanner()
Destructs a scanner.
bool readLine(Token &token, const bool consumeBytes)
Tries to read a remaining line as next token.
void registerSymbol(const std::string &symbol, const uint32_t id)
Registers a new symbol.
static bool isWhitespace(const char &character)
Returns whether a given character is a white space character.
Definition Scanner.h:901
uint8_t readWhiteSpace(bool crossLines=true)
Reads white space.
IdMap keywordMap_
Map mapping keyword strings to identifier ids.
Definition Scanner.h:753
size_t line_
Holds the current line.
Definition Scanner.h:744
const std::string & filename() const
Returns the name of the input file, if the input is a file.
Definition Scanner.h:891
bool readNumber(Token &token, const bool consumeBytes)
Tries to read a number as next token.
ScopeRemarks scopeRemarks_
Scope remarks.
Definition Scanner.h:768
void pop()
Pops the recent token.
virtual Token readToken(const bool consumeBytes=true)
Reads and returns the next token.
bool refillIntermediateBuffer()
Refills the intermediate buffer.
std::array< uint16_t, 256 > CharTable
Definition of a character table.
Definition Scanner.h:303
void registerScopeRemark(const std::string &begin, const std::string &end)
Registers a scope remark symbol.
size_t size() const
Returns the size of the scanner.
static bool findNextToken(const char *pointer, const size_t size, const size_t start, size_t &tokenStart, size_t &tokenLength)
Finds the next token in a given string starting from a specified position.
bool readLineRemark()
Reads a line remark comment.
Memory intermediateBuffer_
Local intermediate buffer.
Definition Scanner.h:726
Scanner(const std::shared_ptr< std::istream > &stream, float *progress=nullptr, bool *cancel=nullptr)
Creates a new scanner using a stream as input.
bool registerWhiteSpaceCharacter(const uint8_t character)
Registers a white space character.
bool readSymbol(Token &token, const bool consumeBytes)
Tries to read a symbol as next token.
bool readString(Token &token, const bool consumeBytes)
Tries to read a string as next token.
Memory extraBuffer_
Local extra buffer, used if the intermediate buffer is too small.
Definition Scanner.h:735
const Token & look()
Returns a lookahead to the next token.
LineRemarks lineRemarks_
Registered line remarks.
Definition Scanner.h:762
This class implements an object able to allocate memory.
Definition base/Memory.h:22
The namespace covering the entire Ocean framework.
Definition Accessor.h:15