Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * Top contributors (to current version): 3 : : * Andrew Reynolds, Aina Niemetz 4 : : * 5 : : * This file is part of the cvc5 project. 6 : : * 7 : : * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS 8 : : * in the top-level source directory and their institutional affiliations. 9 : : * All rights reserved. See the file COPYING in the top-level source 10 : : * directory for licensing information. 11 : : * **************************************************************************** 12 : : * 13 : : * The lexer for smt2 14 : : */ 15 : : 16 : : #include "cvc5parser_private.h" 17 : : 18 : : #ifndef CVC5__PARSER__SMT2__SMT2_LEXER_H 19 : : #define CVC5__PARSER__SMT2__SMT2_LEXER_H 20 : : 21 : : #include <array> 22 : : #include <cstdlib> 23 : : #include <istream> 24 : : #include <map> 25 : : #include <vector> 26 : : 27 : : #include "base/check.h" 28 : : #include "parser/lexer.h" 29 : : #include "parser/tokens.h" 30 : : 31 : : namespace cvc5 { 32 : : namespace parser { 33 : : 34 : : /** 35 : : * The lexer for Smt2. This handles lexing tokens that may appear in smt2 36 : : * terms. It does not lex command tokens. 37 : : * 38 : : * Partially based on 39 : : * https://github.com/bitwuzla/bitwuzla/blob/main/src/parser/smt2/lexer.h 40 : : */ 41 : : class Smt2Lexer : public Lexer 42 : : { 43 : : public: 44 : : Smt2Lexer(bool isStrict, bool isSygus); 45 : : const char* tokenStr() const override; 46 : : /** Are we in strict mode? */ 47 : : bool isStrict() const; 48 : : /** Are we parsing sygus? */ 49 : : bool isSygus() const; 50 : : 51 : : private: 52 : : /** 53 : : * Read and tokenize the next token from the provided input stream. Stores 54 : : * its characters to d_token. 55 : : */ 56 : : Token nextTokenInternal() override; 57 : : /** 58 : : * Computes the next token and adds its characters to d_token. Does not 59 : : * null terminate. 60 : : */ 61 : : Token computeNextToken(); 62 : : /** Push a character to the stored token */ 63 : 94072900 : void pushToToken(int32_t ch) 64 : : { 65 [ - + ][ - + ]: 94072900 : Assert(ch != EOF); [ - - ] 66 : 94072900 : d_token.push_back(static_cast<char>(ch)); 67 : 94072900 : } 68 : : //----------- Utilities for parsing the current character stream 69 : : enum class CharacterClass 70 : : { 71 : : NONE = 0, 72 : : WHITESPACE = (1 << 0), 73 : : DECIMAL_DIGIT = (1 << 1), 74 : : HEXADECIMAL_DIGIT = (1 << 2), 75 : : BIT = (1 << 3), 76 : : SYMBOL_START = (1 << 4), 77 : : SYMBOL = (1 << 5), 78 : : PRINTABLE = (1 << 6), 79 : : }; 80 : : /** The set of non-letter/non-digit characters that may occur in keywords. */ 81 : : inline static const std::string s_extraSymbolChars = "+-/*=%?!.$_~&^<>@"; 82 : : /** The set of legal printable characters. */ 83 : : inline static const std::string s_printableAsciiChars = 84 : : "!\"#$%&'()*+,-./" 85 : : "0123456789" 86 : : ":;<=>?@" 87 : : "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 88 : : "[\\]^_`" 89 : : "abcdefghijklmnopqrstuvwxyz" 90 : : "{|}~" 91 : : " \t\r\n"; 92 : : /** parse <c>, return false if <c> is not ch. */ 93 : : bool parseLiteralChar(int32_t ch); 94 : : /** parse <c>, return false if <c> is not from cc */ 95 : : bool parseChar(CharacterClass cc); 96 : : /** parse <c>+ from cc, return false if the next int32_t is not from cc. */ 97 : : bool parseNonEmptyCharList(CharacterClass cc); 98 : : /** parse <c>* from cc. */ 99 : : void parseCharList(CharacterClass cc); 100 : : /** Return true if ch is in character class cc */ 101 : 163157000 : bool isCharacterClass(int32_t ch, CharacterClass cc) const 102 : : { 103 : 163157000 : return d_charClass[static_cast<uint8_t>(ch)] & static_cast<uint8_t>(cc); 104 : : } 105 : : //----------- Utilizes for tokenizing d_token 106 : : /** 107 : : * Tokenize current symbol stored in d_token. 108 : : * 109 : : * This method changes the string in d_token into the appropriate token. 110 : : * Otherwise, we return Token::SYMBOL. 111 : : * 112 : : * The list of all simple symbols that are converted by this method. 113 : : * as, par, let, match, Constant, Variable, _ 114 : : * 115 : : * We don't handle command tokens here. 116 : : */ 117 : : Token tokenizeCurrentSymbol() const; 118 : : /** The characters in the current token */ 119 : : std::vector<char> d_token; 120 : : /** Is strict parsing enabled */ 121 : : bool d_isStrict; 122 : : /** Is sygus enabled */ 123 : : bool d_isSygus; 124 : : /** The character classes. */ 125 : : std::array<uint8_t, 256> d_charClass{}; // value-initialized to 0 126 : : }; 127 : : 128 : : } // namespace parser 129 : : } // namespace cvc5 130 : : 131 : : #endif /* CVC5__PARSER__SMT2__SMT2_LEXER_NEW_H */