|
| 1 | +/************************************************************************************ |
| 2 | + * Copyright (c) 2023, xeus-cpp contributors * |
| 3 | + * Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht * |
| 4 | + * * |
| 5 | + * Distributed under the terms of the BSD 3-Clause License. * |
| 6 | + * * |
| 7 | + * The full license is in the file LICENSE, distributed with this software. * |
| 8 | + ************************************************************************************/ |
| 9 | + |
| 10 | +#include "xinput_validator.hpp" |
| 11 | +#include "xlexer.hpp" |
| 12 | + |
| 13 | +namespace xcpp |
| 14 | +{ |
| 15 | + std::string to_string(validation_result res) |
| 16 | + { |
| 17 | + if (res == validation_result::incomplete) |
| 18 | + { |
| 19 | + return "incomplete"; |
| 20 | + } |
| 21 | + else if (res == validation_result::complete) |
| 22 | + { |
| 23 | + return "complete"; |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + return "mismatch"; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + validation_result xinput_validator::validate(const std::string& input) |
| 32 | + { |
| 33 | + auto res = validation_result::complete; |
| 34 | + |
| 35 | + // Only check for 'template' if we're not already indented |
| 36 | + if (m_parent_stack.empty()) |
| 37 | + { |
| 38 | + xlexer luthor(input.data(), true); |
| 39 | + auto tok = luthor.lex(); |
| 40 | + if (tok.kind() == token_kind::ident && tok.identifier() == "template") |
| 41 | + { |
| 42 | + m_parent_stack.push_back(as_int(token_kind::less)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + xlexer luthor(input.data(), true); |
| 47 | + token tok, last_not_space_tok; |
| 48 | + |
| 49 | + do |
| 50 | + { |
| 51 | + if (tok.kind() != token_kind::space) |
| 52 | + { |
| 53 | + last_not_space_tok = tok; |
| 54 | + } |
| 55 | + tok = luthor.lex(); |
| 56 | + |
| 57 | + if (in_block_comment() && tok.kind() != token_kind::r_comment) |
| 58 | + { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + switch(tok.kind()) |
| 63 | + { |
| 64 | + case token_kind::comment: |
| 65 | + tok = luthor.read_to_end_of_line(); |
| 66 | + break; |
| 67 | + case token_kind::r_comment: |
| 68 | + if (in_block_comment()) |
| 69 | + { |
| 70 | + m_parent_stack.pop_back(); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + res = validation_result::mismatch; |
| 75 | + } |
| 76 | + break; |
| 77 | + case token_kind::l_square: |
| 78 | + case token_kind::l_paren: |
| 79 | + case token_kind::l_brace: |
| 80 | + case token_kind::l_comment: |
| 81 | + m_parent_stack.push_back(as_int(tok.kind())); |
| 82 | + break; |
| 83 | + case token_kind::r_square: |
| 84 | + case token_kind::r_paren: |
| 85 | + case token_kind::r_brace: |
| 86 | + { |
| 87 | + auto tos = |
| 88 | + m_parent_stack.empty() ? |
| 89 | + token_kind::unknown : |
| 90 | + static_cast<token_kind>(m_parent_stack.back()); |
| 91 | + if (!tok.closes_brace(tos)) |
| 92 | + { |
| 93 | + res = validation_result::mismatch; |
| 94 | + break; |
| 95 | + } |
| 96 | + m_parent_stack.pop_back(); |
| 97 | + // '}' will also pop a template '<' if their is one |
| 98 | + if (tok.kind() == token_kind::r_brace && in_template()) |
| 99 | + { |
| 100 | + m_parent_stack.pop_back(); |
| 101 | + } |
| 102 | + break; |
| 103 | + } |
| 104 | + case token_kind::semicolon: |
| 105 | + // Template forward declatation, i.e. 'template' '<' ... '>' ... ';' |
| 106 | + if (in_template()) |
| 107 | + { |
| 108 | + m_parent_stack.pop_back(); |
| 109 | + } |
| 110 | + break; |
| 111 | + case token_kind::hash: |
| 112 | + luthor.skip_whitespace(); |
| 113 | + tok = luthor.lex_any_string(); |
| 114 | + if (tok.kind() != token_kind::eof) |
| 115 | + { |
| 116 | + auto pptk = tok.identifier(); |
| 117 | + if (pptk.substr(0u, 2u) == "if") |
| 118 | + { |
| 119 | + m_parent_stack.push_back(as_int(token_kind::hash)); |
| 120 | + } |
| 121 | + else if (pptk.substr(0u, 5u) == "endif" && |
| 122 | + (pptk.size() == 5u || pptk[5u] == '/' || std::isspace(pptk[5u]))) |
| 123 | + { |
| 124 | + if (m_parent_stack.empty() || m_parent_stack.back() != as_int(token_kind::hash)) |
| 125 | + { |
| 126 | + res = validation_result::mismatch; |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + m_parent_stack.pop_back(); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + break; |
| 135 | + default: |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | + while (tok.kind() != token_kind::eof && res != validation_result::mismatch); |
| 140 | + |
| 141 | + const auto last_kind = last_not_space_tok.kind(); |
| 142 | + const bool cont = (last_kind == token_kind::backslash || last_kind == token_kind::comma); |
| 143 | + |
| 144 | + if (cont || (!m_parent_stack.empty() && res != validation_result::mismatch)) |
| 145 | + { |
| 146 | + res = validation_result::incomplete; |
| 147 | + } |
| 148 | + |
| 149 | + return res; |
| 150 | + } |
| 151 | + |
| 152 | + bool xinput_validator::in_block_comment() const |
| 153 | + { |
| 154 | + return !m_parent_stack.empty() && m_parent_stack.back() == as_int(token_kind::l_comment); |
| 155 | + } |
| 156 | + |
| 157 | + bool xinput_validator::in_template() const |
| 158 | + { |
| 159 | + return m_parent_stack.size() == 1u && m_parent_stack.back() == as_int(token_kind::less); |
| 160 | + } |
| 161 | +} |
0 commit comments