1 // 2 // ANTLRTokenRewriteStream.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 <Cocoa/Cocoa.h> 33 #import "ANTLRCommonTokenStream.h" 34 #import "ANTLRLinkBase.h" 35 #import "ANTLRHashMap.h" 36 #import "ANTLRMapElement.h" 37 #import "ANTLRTokenSource.h" 38 39 // Define the rewrite operation hierarchy 40 41 @interface ANTLRRewriteOperation : ANTLRCommonTokenStream 42 { 43 /** What index into rewrites List are we? */ 44 NSInteger instructionIndex; 45 /** Token buffer index. */ 46 NSInteger index; 47 NSString *text; 48 } 49 50 @property (getter=getInstructionIndex, setter=setInstructionIndex:) NSInteger instructionIndex; 51 @property (getter=getIndex, setter=setIndex:) NSInteger index; 52 @property (retain, getter=getText, setter=setText:) NSString *text; 53 54 + (ANTLRRewriteOperation *) newANTLRRewriteOperation:(NSInteger)index 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 index 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 : ANTLRRewriteOperation { 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 : ANTLRRewriteOperation { 79 NSInteger lastIndex; 80 } 81 82 @property (getter=getLastIndex, setter=setLastIndex:) 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 ANTLRTokenRewriteStream : ANTLRCommonTokenStream { 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 ANTLRHashMap *programs; 109 110 /** Map String (program name) -> Integer index */ 111 ANTLRHashMap *lastRewriteTokenIndexes; 112 } 113 114 @property (retain, getter=getPrograms, setter=setPrograms:) ANTLRHashMap *programs; 115 @property (retain, getter=getLastRewriteTokenIndexes, setter=setLastRewriteTokenIndexes:) ANTLRHashMap *lastRewriteTokenIndexes; 116 117 + (ANTLRTokenRewriteStream *)newANTLRTokenRewriteStream; 118 + (ANTLRTokenRewriteStream *)newANTLRTokenRewriteStream:(id<ANTLRTokenSource>) aTokenSource; 119 + (ANTLRTokenRewriteStream *)newANTLRTokenRewriteStream:(id<ANTLRTokenSource>) aTokenSource Channel:(NSInteger)aChannel; 120 121 - (id) init; 122 - (id)initWithTokenSource:(id<ANTLRTokenSource>)aTokenSource; 123 - (id)initWithTokenSource:(id<ANTLRTokenSource>)aTokenSource Channel:(NSInteger)aChannel; 124 125 - (ANTLRHashMap *)getPrograms; 126 - (void)setPrograms:(ANTLRHashMap *)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<ANTLRToken>)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<ANTLRToken>)t Text:(NSString *)theText; 138 - (void) insertBeforeIndex:(NSInteger)anIndex Text:(NSString *)theText; 139 - (void) insertBeforeProgName:(NSString *)programName Index:(NSInteger)index 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<ANTLRToken>)indexT Text:(NSString *)theText; 143 - (void) replaceFromToken:(id<ANTLRToken>)from ToToken:(id<ANTLRToken>)to Text:(NSString *)theText; 144 - (void) replaceProgNam:(NSString *)programName Token:(id<ANTLRToken>)from Token:(id<ANTLRToken>)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<ANTLRToken>)indexT; 149 - (void) deleteFromToken:(id<ANTLRToken>)from ToToken:(id<ANTLRToken>)to; 150 - (void) delete:(NSString *)programName FromToken:(id<ANTLRToken>)from ToToken:(id<ANTLRToken>)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 - (ANTLRHashMap *) getProgram:(NSString *)name; 156 - (ANTLRHashMap *) 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 - (ANTLRHashMap *)reduceToSingleOperationPerIndex:(ANTLRHashMap *)rewrites; 164 - (ANTLRHashMap *)getKindOfOps:(ANTLRHashMap *)rewrites KindOfClass:(Class)kind; 165 - (ANTLRHashMap *)getKindOfOps:(ANTLRHashMap *)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