RegexDebug.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "AK/StringBuilder.h"
  8. #include "LibRegex/RegexMatcher.h"
  9. #include <AK/Debug.h>
  10. namespace regex {
  11. class RegexDebug {
  12. public:
  13. RegexDebug(FILE* file = stdout)
  14. : m_file(file)
  15. {
  16. }
  17. virtual ~RegexDebug() = default;
  18. template<typename T>
  19. void print_raw_bytecode(Regex<T>& regex) const
  20. {
  21. auto& bytecode = regex.parser_result.bytecode;
  22. size_t index { 0 };
  23. for (auto& value : bytecode) {
  24. outln(m_file, "OpCode i={:3} [{:#02X}]", index, (u32)value);
  25. ++index;
  26. }
  27. }
  28. template<typename T>
  29. void print_bytecode(const Regex<T>& regex) const
  30. {
  31. MatchState state;
  32. auto& bytecode = regex.parser_result.bytecode;
  33. for (;;) {
  34. auto* opcode = bytecode.get_opcode(state);
  35. if (!opcode) {
  36. dbgln("Wrong opcode... failed!");
  37. return;
  38. }
  39. print_opcode("PrintBytecode", *opcode, state);
  40. out(m_file, "{}", m_debug_stripline);
  41. if (is<OpCode_Exit>(*opcode))
  42. break;
  43. state.instruction_position += opcode->size();
  44. }
  45. fflush(m_file);
  46. }
  47. void print_opcode(const String& system, OpCode& opcode, MatchState& state, size_t recursion = 0, bool newline = true) const
  48. {
  49. out(m_file, "{:15} | {:5} | {:9} | {:35} | {:30} | {:20}",
  50. system.characters(),
  51. state.instruction_position,
  52. recursion,
  53. opcode.to_string().characters(),
  54. opcode.arguments_string().characters(),
  55. String::formatted("ip: {:3}, sp: {:3}", state.instruction_position, state.string_position));
  56. if (newline)
  57. outln();
  58. if (newline && is<OpCode_Compare>(opcode)) {
  59. for (auto& line : to<OpCode_Compare>(opcode).variable_arguments_to_string())
  60. outln(m_file, "{:15} | {:5} | {:9} | {:35} | {:30} | {:20}", "", "", "", "", line, "");
  61. }
  62. }
  63. void print_result(const OpCode& opcode, const ByteCode& bytecode, const MatchInput& input, MatchState& state, ExecutionResult result) const
  64. {
  65. StringBuilder builder;
  66. builder.append(execution_result_name(result));
  67. builder.appendff(", fc: {}, ss: {}", input.fail_counter, input.saved_positions.size());
  68. if (result == ExecutionResult::Succeeded) {
  69. builder.appendff(", ip: {}/{}, sp: {}/{}", state.instruction_position, bytecode.size() - 1, state.string_position, input.view.length() - 1);
  70. } else if (result == ExecutionResult::Fork_PrioHigh) {
  71. builder.appendff(", next ip: {}", state.fork_at_position + opcode.size());
  72. } else if (result != ExecutionResult::Failed) {
  73. builder.appendff(", next ip: {}", state.instruction_position + opcode.size());
  74. }
  75. out(m_file, " | {:20}", builder.to_string());
  76. if (is<OpCode_Compare>(opcode)) {
  77. for (auto& line : to<OpCode_Compare>(opcode).variable_arguments_to_string(input)) {
  78. outln(m_file, "{:15} | {:5} | {:9} | {:35} | {:30} | {:20}", "", "", "", "", line, "");
  79. }
  80. }
  81. out(m_file, "{}", m_debug_stripline);
  82. }
  83. void print_header()
  84. {
  85. StringBuilder builder;
  86. builder.appendff("{:15} | {:5} | {:9} | {:35} | {:30} | {:20} | {:20}\n", "System", "Index", "Recursion", "OpCode", "Arguments", "State", "Result");
  87. auto length = builder.length();
  88. for (size_t i = 0; i < length; ++i) {
  89. builder.append('=');
  90. }
  91. auto str = builder.to_string();
  92. VERIFY(!str.is_empty());
  93. outln(m_file, "{}", str);
  94. fflush(m_file);
  95. builder.clear();
  96. for (size_t i = 0; i < length; ++i) {
  97. builder.append('-');
  98. }
  99. builder.append('\n');
  100. m_debug_stripline = builder.to_string();
  101. }
  102. private:
  103. String m_debug_stripline;
  104. FILE* m_file;
  105. };
  106. }
  107. using regex::RegexDebug;