1 // 2 // TokenRewriteStream.h 3 // ANTLR 4 // 5 // Created by Alan Condit on 6/19/10. 6 // [The "BSD licence"] 7 // Copyright (c) 2010 Alan Condit 8 // All rights reserved. 9 // 10 // Redistribution and use in source and binary forms, with or without 11 // modification, are permitted provided that the following conditions 12 // are met: 13 // 1. Redistributions of source code must retain the above copyright 14 // notice, this list of conditions and the following disclaimer. 15 // 2. Redistributions in binary form must reproduce the above copyright 16 // notice, this list of conditions and the following disclaimer in the 17 // documentation and/or other materials provided with the distribution. 18 // 3. The name of the author may not be used to endorse or promote products 19 // derived from this software without specific prior written permission. 20 // 21 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 #import <Foundation/Foundation.h> 33 #import "CommonTokenStream.h" 34 #import "LinkBase.h" 35 #import "HashMap.h" 36 #import "MapElement.h" 37 #import "TokenSource.h" 38 39 // Define the rewrite operation hierarchy 40 41 @interface RewriteOperation : CommonTokenStream 42 { 43 /** What rwIndex into rewrites List are we? */ 44 NSInteger instructionIndex; 45 /** Token buffer rwIndex. */ 46 NSInteger rwIndex; 47 NSString *text; 48 } 49 50 @property (getter=getInstructionIndex, setter=setInstructionIndex:) NSInteger instructionIndex; 51 @property (assign) NSInteger rwIndex; 52 @property (retain, getter=text, setter=setText:) NSString *text; 53 54 + (RewriteOperation *) newRewriteOperation:(NSInteger)anIndex Text:(NSString *)text; 55 56 - (id) initWithIndex:(NSInteger)anIndex Text:(NSString *)theText; 57 58 /** Execute the rewrite operation by possibly adding to the buffer. 59 * Return the rwIndex of the next token to operate on. 60 */ 61 - (NSInteger) execute:(NSString *)buf; 62 63 - (NSString *)toString; 64 - (NSInteger) indexOf:(char)aChar inString:(NSString *)aString; 65 @end 66 67 @interface ANTLRInsertBeforeOp : RewriteOperation { 68 } 69 70 + (ANTLRInsertBeforeOp *) newANTLRInsertBeforeOp:(NSInteger)anIndex Text:(NSString *)theText; 71 - (id) initWithIndex:(NSInteger)anIndex Text:(NSString *)theText; 72 73 @end 74 75 /** I'm going to try replacing range from x..y with (y-x)+1 ReplaceOp 76 * instructions. 77 */ 78 @interface ANTLRReplaceOp : RewriteOperation { 79 NSInteger lastIndex; 80 } 81 82 @property (assign) NSInteger lastIndex; 83 84 + (ANTLRReplaceOp *) newANTLRReplaceOp:(NSInteger)from ToIndex:(NSInteger)to Text:(NSString*)theText; 85 - (id) initWithIndex:(NSInteger)from ToIndex:(NSInteger)to Text:(NSString *)theText; 86 87 - (NSInteger) execute:(NSString *)buf; 88 - (NSString *)toString; 89 90 @end 91 92 @interface ANTLRDeleteOp : ANTLRReplaceOp { 93 } 94 + (ANTLRDeleteOp *) newANTLRDeleteOp:(NSInteger)from ToIndex:(NSInteger)to; 95 96 - (id) initWithIndex:(NSInteger)from ToIndex:(NSInteger)to; 97 98 - (NSString *)toString; 99 100 @end 101 102 103 @interface TokenRewriteStream : CommonTokenStream { 104 /** You may have multiple, named streams of rewrite operations. 105 * I'm calling these things "programs." 106 * Maps String (name) -> rewrite (List) 107 */ 108 HashMap *programs; 109 110 /** Map String (program name) -> Integer rwIndex */ 111 HashMap *lastRewriteTokenIndexes; 112 } 113 114 @property (retain, getter=getPrograms, setter=setPrograms:) HashMap *programs; 115 @property (retain, getter=getLastRewriteTokenIndexes, setter=setLastRewriteTokenIndexes:) HashMap *lastRewriteTokenIndexes; 116 117 + (TokenRewriteStream *)newTokenRewriteStream; 118 + (TokenRewriteStream *)newTokenRewriteStream:(id<TokenSource>) aTokenSource; 119 + (TokenRewriteStream *)newTokenRewriteStream:(id<TokenSource>) aTokenSource Channel:(NSInteger)aChannel; 120 121 - (id) init; 122 - (id)initWithTokenSource:(id<TokenSource>)aTokenSource; 123 - (id)initWithTokenSource:(id<TokenSource>)aTokenSource Channel:(NSInteger)aChannel; 124 125 - (HashMap *)getPrograms; 126 - (void)setPrograms:(HashMap *)aProgList; 127 128 - (void) rollback:(NSInteger)instructionIndex; 129 - (void) rollback:(NSString *)programName Index:(NSInteger)anInstructionIndex; 130 - (void) deleteProgram; 131 - (void) deleteProgram:(NSString *)programName; 132 - (void) insertAfterToken:(id<Token>)t Text:(NSString *)theText; 133 - (void) insertAfterIndex:(NSInteger)anIndex Text:(NSString *)theText; 134 - (void) insertAfterProgNam:(NSString *)programName Index:(NSInteger)anIndex Text:(NSString *)theText; 135 136 137 - (void) insertBeforeToken:(id<Token>)t Text:(NSString *)theText; 138 - (void) insertBeforeIndex:(NSInteger)anIndex Text:(NSString *)theText; 139 - (void) insertBeforeProgName:(NSString *)programName Index:(NSInteger)anIndex Text:(NSString *)theText; 140 - (void) replaceFromIndex:(NSInteger)anIndex Text:(NSString *)theText; 141 - (void) replaceFromIndex:(NSInteger)from ToIndex:(NSInteger)to Text:(NSString *)theText; 142 - (void) replaceFromToken:(id<Token>)indexT Text:(NSString *)theText; 143 - (void) replaceFromToken:(id<Token>)from ToToken:(id<Token>)to Text:(NSString *)theText; 144 - (void) replaceProgNam:(NSString *)programName Token:(id<Token>)from Token:(id<Token>)to Text:(NSString *)theText; 145 - (void) replaceProgNam:(NSString *)programName FromIndex:(NSInteger)from ToIndex:(NSInteger)to Text:(NSString *)theText; 146 - (void) delete:(NSInteger)anIndex; 147 - (void) delete:(NSInteger)from ToIndex:(NSInteger)to; 148 - (void) deleteToken:(id<Token>)indexT; 149 - (void) deleteFromToken:(id<Token>)from ToToken:(id<Token>)to; 150 - (void) delete:(NSString *)programName FromToken:(id<Token>)from ToToken:(id<Token>)to; 151 - (void) delete:(NSString *)programName FromIndex:(NSInteger)from ToIndex:(NSInteger)to; 152 - (NSInteger)getLastRewriteTokenIndex; 153 - (NSInteger)getLastRewriteTokenIndex:(NSString *)programName; 154 - (void)setLastRewriteTokenIndex:(NSString *)programName Index:(NSInteger)anInt; 155 - (HashMap *) getProgram:(NSString *)name; 156 - (HashMap *) initializeProgram:(NSString *)name; 157 - (NSString *)toOriginalString; 158 - (NSString *)toOriginalString:(NSInteger)start End:(NSInteger)end; 159 - (NSString *)toString; 160 - (NSString *)toString:(NSString *)programName; 161 - (NSString *)toStringFromStart:(NSInteger)start ToEnd:(NSInteger)end; 162 - (NSString *)toString:(NSString *)programName FromStart:(NSInteger)start ToEnd:(NSInteger)end; 163 - (HashMap *)reduceToSingleOperationPerIndex:(HashMap *)rewrites; 164 - (HashMap *)getKindOfOps:(HashMap *)rewrites KindOfClass:(Class)kind; 165 - (HashMap *)getKindOfOps:(HashMap *)rewrites KindOfClass:(Class)kind Index:(NSInteger)before; 166 - (NSString *)catOpText:(id)a PrevText:(id)b; 167 - (NSMutableString *)toDebugString; 168 - (NSMutableString *)toDebugStringFromStart:(NSInteger)start ToEnd:(NSInteger)end; 169 170 @end 171