RegexDebug.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include "AK/StringBuilder.h"
  28. #include "LibRegex/RegexMatcher.h"
  29. #include <AK/Debug.h>
  30. #if REGEX_DEBUG
  31. namespace regex {
  32. class RegexDebug {
  33. public:
  34. RegexDebug(FILE* file = stdout)
  35. : m_file(file)
  36. {
  37. }
  38. virtual ~RegexDebug() = default;
  39. template<typename T>
  40. void print_raw_bytecode(Regex<T>& regex) const
  41. {
  42. auto& bytecode = regex.parser_result.bytecode;
  43. size_t index { 0 };
  44. for (auto& value : bytecode) {
  45. fprintf(m_file, "OpCode i=%3lu [0x%02X]\n", index, (u32)value);
  46. ++index;
  47. }
  48. }
  49. template<typename T>
  50. void print_bytecode(const Regex<T>& regex) const
  51. {
  52. MatchState state;
  53. auto& bytecode = regex.parser_result.bytecode;
  54. for (;;) {
  55. auto* opcode = bytecode.get_opcode(state);
  56. if (!opcode) {
  57. dbgln("Wrong opcode... failed!");
  58. return;
  59. }
  60. print_opcode("PrintBytecode", *opcode, state);
  61. fprintf(m_file, "%s", m_debug_stripline.characters());
  62. if (is<OpCode_Exit>(*opcode))
  63. break;
  64. state.instruction_position += opcode->size();
  65. }
  66. fflush(m_file);
  67. }
  68. void print_opcode(const String& system, OpCode& opcode, MatchState& state, size_t recursion = 0, bool newline = true) const
  69. {
  70. fprintf(m_file, "%-15s | %-5lu | %-9lu | %-35s | %-30s | %-20s%s",
  71. system.characters(),
  72. state.instruction_position,
  73. recursion,
  74. opcode.to_string().characters(),
  75. opcode.arguments_string().characters(),
  76. String::format("ip: %3lu, sp: %3lu", state.instruction_position, state.string_position).characters(),
  77. newline ? "\n" : "");
  78. if (newline && is<OpCode_Compare>(opcode)) {
  79. for (auto& line : to<OpCode_Compare>(opcode).variable_arguments_to_string()) {
  80. fprintf(m_file, "%-15s | %-5s | %-9s | %-35s | %-30s | %-20s%s", "", "", "", "", line.characters(), "", "\n");
  81. }
  82. }
  83. }
  84. void print_result(const OpCode& opcode, const ByteCode& bytecode, const MatchInput& input, MatchState& state, ExecutionResult result) const
  85. {
  86. StringBuilder builder;
  87. builder.append(execution_result_name(result));
  88. builder.appendff(", fc: {}, ss: {}", input.fail_counter, input.saved_positions.size());
  89. if (result == ExecutionResult::Succeeded) {
  90. builder.appendf(", ip: %lu/%lu, sp: %lu/%lu", state.instruction_position, bytecode.size() - 1, state.string_position, input.view.length() - 1);
  91. } else if (result == ExecutionResult::Fork_PrioHigh) {
  92. builder.appendf(", next ip: %lu", state.fork_at_position + opcode.size());
  93. } else if (result != ExecutionResult::Failed) {
  94. builder.appendf(", next ip: %lu", state.instruction_position + opcode.size());
  95. }
  96. fprintf(m_file, " | %-20s\n", builder.to_string().characters());
  97. if (is<OpCode_Compare>(opcode)) {
  98. for (auto& line : to<OpCode_Compare>(opcode).variable_arguments_to_string(input)) {
  99. fprintf(m_file, "%-15s | %-5s | %-9s | %-35s | %-30s | %-20s%s", "", "", "", "", line.characters(), "", "\n");
  100. }
  101. }
  102. fprintf(m_file, "%s", m_debug_stripline.characters());
  103. }
  104. void print_header()
  105. {
  106. StringBuilder builder;
  107. builder.appendf("%-15s | %-5s | %-9s | %-35s | %-30s | %-20s | %-20s\n", "System", "Index", "Recursion", "OpCode", "Arguments", "State", "Result");
  108. auto length = builder.length();
  109. for (size_t i = 0; i < length; ++i) {
  110. builder.append('=');
  111. }
  112. auto str = builder.to_string();
  113. VERIFY(!str.is_empty());
  114. fprintf(m_file, "%s\n", str.characters());
  115. fflush(m_file);
  116. builder.clear();
  117. for (size_t i = 0; i < length; ++i) {
  118. builder.append('-');
  119. }
  120. builder.append('\n');
  121. m_debug_stripline = builder.to_string();
  122. }
  123. private:
  124. String m_debug_stripline;
  125. FILE* m_file;
  126. };
  127. }
  128. using regex::RegexDebug;
  129. #endif