Guest User

Untitled

a guest
Apr 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. package org.cfml.util;
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5.  
  6. import org.cfml.Code;
  7. import org.cfml.instr.Instruction;
  8.  
  9. public class InstructionFinder {
  10. private Code code;
  11. private int position = 0;
  12.  
  13. public InstructionFinder(Code c) {
  14. code = c;
  15. }
  16.  
  17. public void setCode(Code c) {
  18. code = c;
  19. position = 0;
  20. }
  21.  
  22. public Code code() {
  23. return code;
  24. }
  25.  
  26. public Instruction[][] find(String regex) {
  27. return find(regex, null);
  28. }
  29.  
  30. public Instruction[][] find(String regex, MatchVerifier mv) {
  31. Instruction[] instructions = code.instructions();
  32. StringBuffer sb = new StringBuffer();
  33. if(instructions.length > 0) {
  34. sb.append(instructions[0].name());
  35. for(int i = 1; i < instructions.length; i++)
  36. sb.append(' ' + instructions[i].name());
  37. }
  38. Matcher m = Pattern.compile(regex).matcher(sb);
  39. List<Instruction[]> matches = new Vector<Instruction[]>();
  40. int found = 0;
  41. while(m.find()) {
  42. if(m.start() != 0 && sb.charAt(m.start() - 1) != ' ')
  43. continue;
  44. int start = sb.substring(0, m.start()).split(" ").length;
  45. if(m.start() == 0 && start == 1)
  46. start = 0;
  47. Instruction[] match = new Instruction[sb.substring(0, m.end()).split(" ").length - start];
  48. System.arraycopy(instructions, start, match, 0, match.length);
  49. if(mv != null && !mv.verify(match))
  50. continue;
  51. matches.add(match);
  52. found++;
  53. }
  54. return matches.toArray(new Instruction[found][]);
  55. }
  56.  
  57. public Instruction[] findNext(String regex) {
  58. return findNext(regex, null);
  59. }
  60.  
  61. public Instruction[] findNext(String regex, MatchVerifier mv) {
  62. Instruction[] instructions = code.instructions();
  63. StringBuffer sb = new StringBuffer();
  64. if(instructions.length > position) {
  65. sb.append(instructions[position].name());
  66. for(int i = position + 1; i < instructions.length; i++)
  67. sb.append(' ' + instructions[i].name());
  68. }
  69. Matcher m = Pattern.compile(regex).matcher(sb);
  70. while(m.find()) {
  71. if(m.start() != 0 && sb.charAt(m.start() - 1) != ' ')
  72. continue;
  73. int start = sb.substring(0, m.start()).split(" ").length;
  74. if(m.start() == 0 && start == 1)
  75. start = 0;
  76. Instruction[] match = new Instruction[sb.substring(0, m.end()).split(" ").length - start];
  77. System.arraycopy(instructions, position + start, match, 0, match.length);
  78. if(mv != null && !mv.verify(match))
  79. continue;
  80. position = position + start + match.length;
  81. return match;
  82. }
  83. return null;
  84. }
  85.  
  86. public static interface MatchVerifier {
  87. public boolean verify(Instruction[] match);
  88. }
  89. }
Add Comment
Please, Sign In to add comment