RegexDebug.h 4.0 KB

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