RegexDebug.h 3.9 KB

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