image.vision\aoci\aoci_builder.mbt

68.4% · 52 / 76 points · 172 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # aoci/aoci_builder
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// Image processing library submodule
  6. 6// AOCI-Builder — 源码/mbti 解析 → 符号坐标 + 依赖图谱
  7. 7// 从 .mbti 格式文本解析符号,或手动构建符号索引
  8. 8
  9. 9///|
  10. 10pub fn build_index(symbols : Array[SymbolCoord]) -> SymbolIndex {
  11. 11● SymbolIndex::make(symbols)
  12. 12}
  13. 13
  14. 14///|
  15. 15pub fn build_symbol(
  16. 16 pkg~ : String,
  17. 17 name~ : String,
  18. 18 kind~ : SymbolKind,
  19. 19 signature~ : String = "",
  20. 20 doc~ : String = "",
  21. 21 keywords~ : Array[String] = [],
  22. 22) -> SymbolCoord {
  23. 23● SymbolCoord::make(pkg~, name~, kind~, signature~, doc~, keywords~)
  24. 24}
  25. 25
  26. 26///|
  27. 27fn parse_kind_from_line(line : String) -> SymbolKind {
  28. 28● if line.contains("struct") {
  29. 29● Struct
  30. 30● } else if line.contains("enum") {
  31. 31○ Enum
  32. 32● } else if line.contains("type") {
  33. 33● TypeAlias
  34. 34● } else if line.contains("fn") {
  35. 35● Function
  36. 36 } else {
  37. 37○ Function
  38. 38 }
  39. 39}
  40. 40
  41. 41///|
  42. 42fn extract_name_from_line(line : String) -> String {
  43. 43● let chars = line.to_array()
  44. 44 let mut start = -1
  45. 45● for i in 0..<chars.length() {
  46. 46● let c = chars[i]
  47. 47● if c == 'f' && i + 1 < chars.length() && chars[i + 1] == 'n' {
  48. 48● start = i + 2
  49. 49 break
  50. 50 }
  51. 51 if c == 't' &&
  52. 52● i + 3 < chars.length() &&
  53. 53 chars[i + 1] == 'y' &&
  54. 54 chars[i + 2] == 'p' &&
  55. 55 chars[i + 3] == 'e' {
  56. 56● start = i + 4
  57. 57 break
  58. 58 }
  59. 59 if c == 's' &&
  60. 60● i + 5 < chars.length() &&
  61. 61 chars[i + 1] == 't' &&
  62. 62 chars[i + 2] == 'r' &&
  63. 63 chars[i + 3] == 'u' &&
  64. 64 chars[i + 4] == 'c' &&
  65. 65 chars[i + 5] == 't' {
  66. 66● start = i + 6
  67. 67 break
  68. 68 }
  69. 69 if c == 'e' &&
  70. 70○ i + 3 < chars.length() &&
  71. 71 chars[i + 1] == 'n' &&
  72. 72 chars[i + 2] == 'u' &&
  73. 73 chars[i + 3] == 'm' {
  74. 74○ start = i + 4
  75. 75 break
  76. 76 }
  77. 77 }
  78. 78 if start < 0 {
  79. 79○ return ""
  80. 80 }
  81. 81● let mut end = chars.length()
  82. 82● for i in start..<chars.length() {
  83. 83● let c = chars[i]
  84. 84 if c == '(' || c == '<' || c == ' ' || c == '=' || c == '{' {
  85. 85● end = i
  86. 86 break
  87. 87 }
  88. 88 }
  89. 89 let name_chars : Array[Char] = []
  90. 90 for i in start..<end {
  91. 91○ let c = chars[i]
  92. 92 if c != ' ' && c != '\t' {
  93. 93○ name_chars.push(c)
  94. 94 }
  95. 95 }
  96. 96● String::from_array(name_chars)
  97. 97}
  98. 98
  99. 99///|
  100. 100pub fn build_from_mbti(
  101. 101 package_name : String,
  102. 102 mbti_content : String,
  103. 103) -> SymbolIndex {
  104. 104● let symbols : Array[SymbolCoord] = []
  105. 105● for line in mbti_content.split("\n") {
  106. 106● let line_str = line.to_owned()
  107. 107● let trimmed = line_str.trim().to_owned()
  108. 108● if trimmed.length() == 0 {
  109. 109● continue
  110. 110 }
  111. 111● if trimmed.has_prefix("pub fn ") ||
  112. 112● trimmed.has_prefix("pub(all) struct ") ||
  113. 113● trimmed.has_prefix("pub(all) enum ") ||
  114. 114● trimmed.has_prefix("pub type ") {
  115. 115● let kind = parse_kind_from_line(trimmed)
  116. 116● let name = extract_name_from_line(trimmed)
  117. 117● if name.length() > 0 {
  118. 118○ let keywords = extract_keywords(name)
  119. 119○ symbols.push(
  120. 120○ SymbolCoord::make(
  121. 121 pkg=package_name,
  122. 122 name~,
  123. 123 kind~,
  124. 124 signature=trimmed,
  125. 125 doc="",
  126. 126 keywords~,
  127. 127 ),
  128. 128 )
  129. 129 }
  130. 130 }
  131. 131 }
  132. 132● SymbolIndex::make(symbols)
  133. 133}
  134. 134
  135. 135///|
  136. 136fn extract_keywords(name : String) -> Array[String] {
  137. 137○ let keywords : Array[String] = []
  138. 138○ for part in name.split("_") {
  139. 139○ let part_str = part.to_owned()
  140. 140○ if part_str.length() > 0 {
  141. 141○ keywords.push(part_str)
  142. 142 }
  143. 143 }
  144. 144 keywords
  145. 145}
  146. 146
  147. 147///|
  148. 148pub fn build_dependency_graph(
  149. 149 index : SymbolIndex,
  150. 150) -> Array[(String, Array[String])] {
  151. 151● let graph : Array[(String, Array[String])] = []
  152. 152 for sym in index.symbols {
  153. 153● let deps : Array[String] = []
  154. 154● for part in sym.signature.split(" ") {
  155. 155● let part_str = part.to_owned()
  156. 156● if part_str.contains("::") {
  157. 157○ let mut found = false
  158. 158 for d in deps {
  159. 159○ if d == part_str {
  160. 160○ found = true
  161. 161 break
  162. 162 }
  163. 163 }
  164. 164 if !found {
  165. 165○ deps.push(part_str)
  166. 166 }
  167. 167 }
  168. 168 }
  169. 169● graph.push((sym.name, deps))
  170. 170 }
  171. 171 graph
  172. 172}