RegexDebug.h 4.0 KB

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