public CreateSubtreeNode(TsurgeonPattern start, TsurgeonPattern end, AuxiliaryTree tree) : base("combineSubtrees", (end == null) ? new TsurgeonPattern[] { start } : new TsurgeonPattern[] { start, end }) { this.auxTree = tree; FindFoot(); }
public CreateSubtreeNode(TsurgeonPattern start, TsurgeonPattern end, AuxiliaryTree tree) : base("combineSubtrees", (end == null) ? new TsurgeonPattern[] {start} : new TsurgeonPattern[] {start, end}) { this.auxTree = tree; FindFoot(); }
private TsurgeonPattern root; // TODO: can remove? public virtual void SetRoot(TsurgeonPatternRoot root) { this.root = root; foreach (TsurgeonPattern child in children) { child.SetRoot(root); } }
public RelabelNode(TsurgeonPattern child, string newLabel) : base("relabel", new TsurgeonPattern[] { child }) { var m1 = SubstPattern.Match(newLabel); if (m1.Success) { mode = RelabelMode.Regex; this.labelRegex = new Regex(m1.Groups[1].Value); this.replacementString = m1.Groups[2].Value; replacementPieces = new List <string>(); var generalMatcher = OneGeneralReplacementPattern.Match(m1.Groups[2].Value); int lastPosition = 0; var nextMatch = generalMatcher.NextMatch(); while (nextMatch.Success) { if (nextMatch.Index > lastPosition) { replacementPieces.Add(replacementString.Substring(lastPosition, nextMatch.Index)); } lastPosition = nextMatch.Index + nextMatch.Length; string piece = nextMatch.Value; if (piece.Equals("")) { nextMatch = generalMatcher.NextMatch(); continue; } replacementPieces.Add(nextMatch.Value); nextMatch = generalMatcher.NextMatch(); } if (lastPosition < replacementString.Length) { replacementPieces.Add(replacementString.Substring(lastPosition)); } this.newLabel = null; } else { mode = RelabelMode.Fixed; var m2 = RegexPattern.Match(newLabel); if (m2.Success) { // fixed relabel but surrounded by regex slashes string unescapedLabel = m2.Groups[1].Value; this.newLabel = RemoveEscapeSlashes(unescapedLabel); } else { // just a node name to relabel to this.newLabel = newLabel; } this.replacementString = null; this.replacementPieces = null; this.labelRegex = null; } }
public RelabelNode(TsurgeonPattern child, string newLabel) : base("relabel", new TsurgeonPattern[] {child}) { var m1 = SubstPattern.Match(newLabel); if (m1.Success) { mode = RelabelMode.Regex; this.labelRegex = new Regex(m1.Groups[1].Value); this.replacementString = m1.Groups[2].Value; replacementPieces = new List<string>(); var generalMatcher = OneGeneralReplacementPattern.Match(m1.Groups[2].Value); int lastPosition = 0; var nextMatch = generalMatcher.NextMatch(); while (nextMatch.Success) { if (nextMatch.Index > lastPosition) { replacementPieces.Add(replacementString.Substring(lastPosition, nextMatch.Index)); } lastPosition = nextMatch.Index + nextMatch.Length; string piece = nextMatch.Value; if (piece.Equals("")) { nextMatch = generalMatcher.NextMatch(); continue; } replacementPieces.Add(nextMatch.Value); nextMatch = generalMatcher.NextMatch(); } if (lastPosition < replacementString.Length) { replacementPieces.Add(replacementString.Substring(lastPosition)); } this.newLabel = null; } else { mode = RelabelMode.Fixed; var m2 = RegexPattern.Match(newLabel); if (m2.Success) { // fixed relabel but surrounded by regex slashes string unescapedLabel = m2.Groups[1].Value; this.newLabel = RemoveEscapeSlashes(unescapedLabel); } else { // just a node name to relabel to this.newLabel = newLabel; } this.replacementString = null; this.replacementPieces = null; this.labelRegex = null; } }
public AdjoinNode(string name, AuxiliaryTree t, TsurgeonPattern p) : base(name, new TsurgeonPattern[] {p}) { if (t == null || p == null) { throw new NullReferenceException("AdjoinNode: illegal null argument, t=" + t + ", p=" + p); } padjunctionTree = t; }
public AdjoinNode(string name, AuxiliaryTree t, TsurgeonPattern p) : base(name, new TsurgeonPattern[] { p }) { if (t == null || p == null) { throw new NullReferenceException("AdjoinNode: illegal null argument, t=" + t + ", p=" + p); } padjunctionTree = t; }
/** * Parses a tsurgeon script text input and compiles a tregex pattern and a list * of tsurgeon operations into a pair. * * @param reader Reader to read patterns from * @return A pair of a tregex and tsurgeon pattern read from a file, or <code>null</code> * when the operations in the Reader have been exhausted * @throws IOException If any IO problem */ /*public static Tuple<TregexPattern, TsurgeonPattern> getOperationFromReader(BufferedReader reader, TregexPatternCompiler compiler) /*throws IOException#1# { * string patternString = getTregexPatternFromReader(reader); * if ("".equals(patternString)) { * return null; * } * TregexPattern matchPattern = compiler.compile(patternString); * * TsurgeonPattern collectedPattern = getTsurgeonOperationsFromReader(reader); * return new Pair<TregexPattern,TsurgeonPattern>(matchPattern,collectedPattern); * }*/ /** * Assumes that we are at the beginning of a tsurgeon script file and gets the string for the * tregex pattern leading the file * @return tregex pattern string */ /*public static string getTregexPatternFromReader(BufferedReader reader) throws IOException { * StringBuilder matchString = new StringBuilder(); * for (string thisLine; (thisLine = reader.readLine()) != null; ) { * if (matchString.length() > 0 && emptyLinePattern.matcher(thisLine).matches()) { * // A blank line after getting some real content (not just comments or nothing) * break; * } * Matcher m = commentPattern.matcher(thisLine); * if (m.matches()) { * // delete it * thisLine = m.replaceFirst(""); * } * if ( ! emptyLinePattern.matcher(thisLine).matches()) { * matchString.append(thisLine); * } * } * return matchString.ToString(); * }*/ /** * Assumes the given reader has only tsurgeon operations (not a tregex pattern), and parses * these out, collecting them into one operation. Stops on a whitespace line. * * @throws IOException */ /*public static TsurgeonPattern getTsurgeonOperationsFromReader(BufferedReader reader) throws IOException { * List<TsurgeonPattern> operations = new ArrayList<TsurgeonPattern>(); * for (string thisLine; (thisLine = reader.readLine()) != null; ) { * if (emptyLinePattern.matcher(thisLine).matches()) { * break; * } * thisLine = removeComments(thisLine); * if (emptyLinePattern.matcher(thisLine).matches()) { * continue; * } * operations.add(parseOperation(thisLine)); * } * * if (operations.size() == 0) * throw new TsurgeonParseException("No Tsurgeon operation provided."); * * return collectOperations(operations); * }*/ /*private static string removeComments(string line) { * Matcher m = commentPattern.matcher(line); * line = m.replaceFirst(""); * Matcher m1 = escapedCommentCharacterPattern.matcher(line); * line = m1.replaceAll(commentIntroducingCharacter); * return line; * }*/ /** * Assumes the given reader has only tsurgeon operations (not a tregex pattern), and returns * them as a String, mirroring the way the strings appear in the file. This is helpful * for lazy evaluation of the operations, as in a GUI, * because you do not parse the operations on load. Comments are still excised. * @throws IOException */ /*public static string getTsurgeonTextFromReader(BufferedReader reader) throws IOException { * StringBuilder sb = new StringBuilder(); * for (string thisLine; (thisLine = reader.readLine()) != null; ) { * thisLine = removeComments(thisLine); * if (emptyLinePattern.matcher(thisLine).matches()) { * continue; * } * sb.append(thisLine); * sb.append('\n'); * } * return sb.ToString(); * }*/ /** * Parses a tsurgeon script file and compiles all operations in the file into a list * of pairs of tregex and tsurgeon patterns. * * @param filename file containing the tsurgeon script * @return A pair of a tregex and tsurgeon pattern read from a file * @throws IOException If there is any I/O problem */ /*public static List<Pair<TregexPattern, TsurgeonPattern>> getOperationsFromFile(string filename, string encoding, TregexPatternCompiler compiler) throws IOException { * List<Pair<TregexPattern,TsurgeonPattern>> operations = new ArrayList<Pair<TregexPattern, TsurgeonPattern>>(); * BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding)); * for ( ; ; ) { * Pair<TregexPattern, TsurgeonPattern> operation = getOperationFromReader(reader, compiler); * if (operation == null) { * break; * } * operations.add(operation); * } * reader.close(); * return operations; * }*/ /// <summary> /// Applies {#processPattern} to a collection of trees. /// </summary> /// <param name="matchPattern">A {@link TregexPattern} to be matched against a {@link Tree}.</param> /// <param name="p">A {@link TsurgeonPattern} to apply.</param> /// <param name="inputTrees">The input trees to be processed</param> /// <returns>A List of the transformed trees</returns> public static List <Tree> ProcessPatternOnTrees(TregexPattern matchPattern, TsurgeonPattern p, List <Tree> inputTrees) { var result = new List <Tree>(); foreach (Tree tree in inputTrees) { result.Add(ProcessPattern(matchPattern, p, tree)); } return(result); }
// TODO: ideally we should have the tree and the tregex matcher be part of this as well. // That would involve putting some of the functionality in Tsurgeon.java in this object public TsurgeonMatcher(TsurgeonPattern pattern, Dictionary<string, Tree> newNodeNames, CoindexationGenerator coindexer) { this.NewNodeNames = newNodeNames; this.Coindexer = coindexer; this.ChildMatcher = new TsurgeonMatcher[pattern.children.Length]; for (int i = 0; i < pattern.children.Length; ++i) { this.ChildMatcher[i] = pattern.children[i].GetMatcher(newNodeNames, coindexer); } }
// TODO: ideally we should have the tree and the tregex matcher be part of this as well. // That would involve putting some of the functionality in Tsurgeon.java in this object public TsurgeonMatcher(TsurgeonPattern pattern, Dictionary <string, Tree> newNodeNames, CoindexationGenerator coindexer) { this.NewNodeNames = newNodeNames; this.Coindexer = coindexer; this.ChildMatcher = new TsurgeonMatcher[pattern.children.Length]; for (int i = 0; i < pattern.children.Length; ++i) { this.ChildMatcher[i] = pattern.children[i].GetMatcher(newNodeNames, coindexer); } }
/// <summary> /// Tries to match a pattern against a tree. If it succeeds, apply the surgical operations contained in a {@link TsurgeonPattern}. /// </summary> /// <param name="matchPattern">A {@link TregexPattern} to be matched against a {@link Tree}.</param> /// <param name="p">A {@link TsurgeonPattern} to apply.</param> /// <param name="t">the {@link Tree} to match against and perform surgery on.</param> /// <returns>t, which has been surgically modified.</returns> public static Tree ProcessPattern(TregexPattern matchPattern, TsurgeonPattern p, Tree t) { TregexMatcher m = matchPattern.Matcher(t); TsurgeonMatcher tsm = p.GetMatcher(); while (m.Find()) { t = tsm.Evaluate(t, m); if (t == null) { break; } m = matchPattern.Matcher(t); } return(t); }
public CreateSubtreeNode(TsurgeonPattern start, AuxiliaryTree tree) : this(start, null, tree) { }
public CoindexNodes(TsurgeonPattern[] children) : base("coindex", children) { }
public TreeLocation(string relation, TsurgeonPattern p) { this.relation = relation; this.child = p; }
public MoveNode(TsurgeonPattern child, TreeLocation l) : base("move", new TsurgeonPattern[] {child}) { this.location = l; }
/// <summary> /// Excises only the directed node /// </summary> public ExciseNode(TsurgeonPattern node) : base("excise", new TsurgeonPattern[] { node, node }) { }
public ReplaceNode(TsurgeonPattern oldNode, List <AuxiliaryTree> trees) : this(oldNode, trees.Select(n => convertAuxiliaryToHold(n)).ToArray()) { }
public AdjoinNode(AuxiliaryTree t, TsurgeonPattern p) : this("adjoin", t, p) { }
public AdjoinToHeadNode(AuxiliaryTree t, TsurgeonPattern p) : base("adjoinH", t, p) { }
public TsurgeonPatternRoot(TsurgeonPattern child) : this(new TsurgeonPattern[] { child }) { }
public IfExistsNode(string name, bool invert, TsurgeonPattern[] children) : base("if " + (invert ? "not " : "") + "exists " + name, children) { this.name = name; this.invert = invert; }
public AdjoinToFootNode(AuxiliaryTree t, TsurgeonPattern p) : base("adjoinF", t, p) { }
public ReplaceNode(TsurgeonPattern oldNode, TsurgeonPattern[] newNodes) : base("replace", new TsurgeonPattern[] { oldNode }.Union(newNodes).ToArray()) { }
public ReplaceNode(TsurgeonPattern oldNode, TsurgeonPattern[] newNodes) : base("replace", new TsurgeonPattern[] {oldNode}.Union(newNodes).ToArray()) { }
public ReplaceNode(TsurgeonPattern oldNode, List<AuxiliaryTree> trees) : this(oldNode, trees.Select(n => convertAuxiliaryToHold(n)).ToArray()) { }
/// <summary> /// Top should evaluate to a node that dominates bottom, but this is not checked! /// </summary> public ExciseNode(TsurgeonPattern top, TsurgeonPattern bottom) : base("excise", new TsurgeonPattern[] {top, bottom}) { }
/// <summary> /// Top should evaluate to a node that dominates bottom, but this is not checked! /// </summary> public ExciseNode(TsurgeonPattern top, TsurgeonPattern bottom) : base("excise", new TsurgeonPattern[] { top, bottom }) { }
/// <summary> /// Excises only the directed node /// </summary> public ExciseNode(TsurgeonPattern node) : base("excise", new TsurgeonPattern[] {node, node}) { }
public InsertNode(TsurgeonPattern child, TreeLocation l) : base("insert", new TsurgeonPattern[] {child}) { this.location = l; }
public InsertNode(TsurgeonPattern child, TreeLocation l) : base("insert", new TsurgeonPattern[] { child }) { this.location = l; }
public MoveNode(TsurgeonPattern child, TreeLocation l) : base("move", new TsurgeonPattern[] { child }) { this.location = l; }
/// <summary> /// In some cases, the order of the children has special meaning. /// For example, in the case of ReplaceNode, the first child will /// evaluate to the node to be replaced, and the other(s) will /// evaluate to the replacement. /// </summary> public TsurgeonPattern(string label, TsurgeonPattern[] children) { this.label = label; this.children = children; }
/** * Parses a tsurgeon script text input and compiles a tregex pattern and a list * of tsurgeon operations into a pair. * * @param reader Reader to read patterns from * @return A pair of a tregex and tsurgeon pattern read from a file, or <code>null</code> * when the operations in the Reader have been exhausted * @throws IOException If any IO problem */ /*public static Tuple<TregexPattern, TsurgeonPattern> getOperationFromReader(BufferedReader reader, TregexPatternCompiler compiler) /*throws IOException#1# { string patternString = getTregexPatternFromReader(reader); if ("".equals(patternString)) { return null; } TregexPattern matchPattern = compiler.compile(patternString); TsurgeonPattern collectedPattern = getTsurgeonOperationsFromReader(reader); return new Pair<TregexPattern,TsurgeonPattern>(matchPattern,collectedPattern); }*/ /** * Assumes that we are at the beginning of a tsurgeon script file and gets the string for the * tregex pattern leading the file * @return tregex pattern string */ /*public static string getTregexPatternFromReader(BufferedReader reader) throws IOException { StringBuilder matchString = new StringBuilder(); for (string thisLine; (thisLine = reader.readLine()) != null; ) { if (matchString.length() > 0 && emptyLinePattern.matcher(thisLine).matches()) { // A blank line after getting some real content (not just comments or nothing) break; } Matcher m = commentPattern.matcher(thisLine); if (m.matches()) { // delete it thisLine = m.replaceFirst(""); } if ( ! emptyLinePattern.matcher(thisLine).matches()) { matchString.append(thisLine); } } return matchString.ToString(); }*/ /** * Assumes the given reader has only tsurgeon operations (not a tregex pattern), and parses * these out, collecting them into one operation. Stops on a whitespace line. * * @throws IOException */ /*public static TsurgeonPattern getTsurgeonOperationsFromReader(BufferedReader reader) throws IOException { List<TsurgeonPattern> operations = new ArrayList<TsurgeonPattern>(); for (string thisLine; (thisLine = reader.readLine()) != null; ) { if (emptyLinePattern.matcher(thisLine).matches()) { break; } thisLine = removeComments(thisLine); if (emptyLinePattern.matcher(thisLine).matches()) { continue; } operations.add(parseOperation(thisLine)); } if (operations.size() == 0) throw new TsurgeonParseException("No Tsurgeon operation provided."); return collectOperations(operations); }*/ /*private static string removeComments(string line) { Matcher m = commentPattern.matcher(line); line = m.replaceFirst(""); Matcher m1 = escapedCommentCharacterPattern.matcher(line); line = m1.replaceAll(commentIntroducingCharacter); return line; }*/ /** * Assumes the given reader has only tsurgeon operations (not a tregex pattern), and returns * them as a String, mirroring the way the strings appear in the file. This is helpful * for lazy evaluation of the operations, as in a GUI, * because you do not parse the operations on load. Comments are still excised. * @throws IOException */ /*public static string getTsurgeonTextFromReader(BufferedReader reader) throws IOException { StringBuilder sb = new StringBuilder(); for (string thisLine; (thisLine = reader.readLine()) != null; ) { thisLine = removeComments(thisLine); if (emptyLinePattern.matcher(thisLine).matches()) { continue; } sb.append(thisLine); sb.append('\n'); } return sb.ToString(); }*/ /** * Parses a tsurgeon script file and compiles all operations in the file into a list * of pairs of tregex and tsurgeon patterns. * * @param filename file containing the tsurgeon script * @return A pair of a tregex and tsurgeon pattern read from a file * @throws IOException If there is any I/O problem */ /*public static List<Pair<TregexPattern, TsurgeonPattern>> getOperationsFromFile(string filename, string encoding, TregexPatternCompiler compiler) throws IOException { List<Pair<TregexPattern,TsurgeonPattern>> operations = new ArrayList<Pair<TregexPattern, TsurgeonPattern>>(); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding)); for ( ; ; ) { Pair<TregexPattern, TsurgeonPattern> operation = getOperationFromReader(reader, compiler); if (operation == null) { break; } operations.add(operation); } reader.close(); return operations; }*/ /// <summary> /// Applies {#processPattern} to a collection of trees. /// </summary> /// <param name="matchPattern">A {@link TregexPattern} to be matched against a {@link Tree}.</param> /// <param name="p">A {@link TsurgeonPattern} to apply.</param> /// <param name="inputTrees">The input trees to be processed</param> /// <returns>A List of the transformed trees</returns> public static List<Tree> ProcessPatternOnTrees(TregexPattern matchPattern, TsurgeonPattern p, List<Tree> inputTrees) { var result = new List<Tree>(); foreach (Tree tree in inputTrees) { result.Add(ProcessPattern(matchPattern, p, tree)); } return result; }
public PruneNode(TsurgeonPattern[] children) : base("prune", children) { }
/// <summary> /// Tries to match a pattern against a tree. If it succeeds, apply the surgical operations contained in a {@link TsurgeonPattern}. /// </summary> /// <param name="matchPattern">A {@link TregexPattern} to be matched against a {@link Tree}.</param> /// <param name="p">A {@link TsurgeonPattern} to apply.</param> /// <param name="t">the {@link Tree} to match against and perform surgery on.</param> /// <returns>t, which has been surgically modified.</returns> public static Tree ProcessPattern(TregexPattern matchPattern, TsurgeonPattern p, Tree t) { TregexMatcher m = matchPattern.Matcher(t); TsurgeonMatcher tsm = p.GetMatcher(); while (m.Find()) { t = tsm.Evaluate(t, m); if (t == null) { break; } m = matchPattern.Matcher(t); } return t; }
public TsurgeonPatternRoot(TsurgeonPattern child) : this(new TsurgeonPattern[] {child}) { }
// TODO: this is wasteful in terms of creating TsurgeonPatternRoot. // Should separate that out into another production public TsurgeonPatternRoot Root() { /*@bgen(jjtree) Root */ var jjtn000 = new SimpleNode(JjtRoot); bool jjtc000 = true; JjTree.OpenNodeScope(jjtn000); List<TsurgeonPattern> results = null; try { TsurgeonPattern result; switch ((jj_ntk == -1) ? Jj_ntk_f() : jj_ntk) { case Delete: case Prune: case Relabel: case Excise: case Insert: case Move: case Replace: case CreateSubtree: case Adjoin: case AdjoinToHead: case AdjoinToFoot: case Coindex: { result = Operation(); JjTree.CloseNodeScope(jjtn000, true); jjtc000 = false; return new TsurgeonPatternRoot(result); } default: jj_la1[1] = jj_gen; Token name; if (Jj_2_1(2)) { Jj_consume_token(If); Jj_consume_token(Exists); name = Jj_consume_token(Name); result = Root(); JjTree.CloseNodeScope(jjtn000, true); jjtc000 = false; return new TsurgeonPatternRoot(new IfExistsNode(name.Image, false, result.children)); } else if (Jj_2_2(2)) { Jj_consume_token(If); Jj_consume_token(Not); Jj_consume_token(Exists); name = Jj_consume_token(Name); result = Root(); JjTree.CloseNodeScope(jjtn000, true); jjtc000 = false; return new TsurgeonPatternRoot(new IfExistsNode(name.Image, true, result.children)); } else { switch ((jj_ntk == -1) ? Jj_ntk_f() : jj_ntk) { case OpenBracket: { //label_1: while (true) { Jj_consume_token(OpenBracket); result = Root(); Jj_consume_token(CloseBracket); if (results == null) { results = new List<TsurgeonPattern>(); } foreach (TsurgeonPattern child in result.children) { results.Add(child); } switch ((jj_ntk == -1) ? Jj_ntk_f() : jj_ntk) { case OpenBracket: { ; break; } default: jj_la1[0] = jj_gen; goto post_label_1; } } post_label_1: { JjTree.CloseNodeScope(jjtn000, true); jjtc000 = false; var array = new TsurgeonPattern[results.Count]; return new TsurgeonPatternRoot(results.ToArray()); } } default: jj_la1[2] = jj_gen; Jj_consume_token(-1); throw new ParseException(); } } } } catch (Exception jjte000) { if (jjtc000) { JjTree.ClearNodeScope(jjtn000); jjtc000 = false; } else { JjTree.PopNode(); } if (jjte000 is SystemException) { throw jjte000; } if (jjte000 is ParseException) { throw jjte000; } throw jjte000; } finally { if (jjtc000) { JjTree.CloseNodeScope(jjtn000, true); } } }
public TsurgeonPatternRoot(TsurgeonPattern[] children) : base("operations: ", children) { SetRoot(this); }
public DeleteNode(TsurgeonPattern[] children) : base("delete", children) { }