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