Permalink
Browse files

The remaining "core" files OBJJified (with probable errors)

  • Loading branch information...
1 parent 98b7f3c commit 7aa6b9d645eb70535727b688a4720fe26264c85a Ross Boucher committed Mar 6, 2009
View
@@ -0,0 +1,24 @@
+
+@import "TDCollectionParser.j"
+@import "TDAssembly.j"
+
+@implementation TDAlternation : TDCollectionParser
+{
+}
+
++ (id)alternation
+{
+ return [[self alloc] init];
+}
+
+- (CPSet)allMatchesFor:(CPSet)inAssemblies
+{
+ var outAssemblies = [CPSet set];
+
+ for (var i=0, count=[subparsers count]; i<count; i++)
+ [outAssemblies unionSet:[subparsers[i] matchAndAssemble:inAssemblies]];
+
+ return outAssemblies;
+}
+
+@end
View
@@ -0,0 +1,74 @@
+
+@import "TDTerminal.j"
+
+@implementation TDChar : TDTerminal
+{
+}
+
++ (id)char
+{
+ return [[self alloc] initWithString:nil];
+}
+
+- (BOOL)qualifies:(id)obj
+{
+ return YES;
+}
+
+@end
+
+@implementation TDDigit : TDTerminal
+{
+}
+
++ (id)digit
+{
+ return [[self alloc] initWithString:nil];
+}
+
+- (BOOL)qualifies:(id)obj
+{
+ var c = [obj integerValue];
+ return String(c).match(/[0-9]/g);
+}
+
+@end
+
+@implementation TDLetter : TDTerminal
+{
+}
+
++ (id)letter
+{
+ return [[self alloc] initWithString:nil];
+}
+
+- (BOOL)qualifies:(id)obj
+{
+ var c = [obj intValue];
+ return (c > 65 && c < 91) || (c > 97 && c < 123);
+}
+
+@end
+
+@implementation TDSpecificChar : TDTerminal
+{
+}
+
++ (id)specificCharWithChar:(int)c
+{
+ return [[self alloc] initWithSpecificChar:c];
+}
+
+- (id)initWithSpecificChar:(int)c
+{
+ return [super initWithString:[CPString stringWithFormat:@"%C", c]];
+}
+
+- (BOOL)qualifies:(id)obj
+{
+ var c = [obj integerValue];
+ return c == [string characterAtIndex:0];
+}
+
+@end
View
@@ -0,0 +1,98 @@
+
+@import "TDAssembly.j"
+
+@implementation TDCharacterAssembly : TDAssembly
+{
+}
+
+- (id)init
+{
+ return [self initWithString:nil];
+}
+
+- (id)initWithString:(CPString)s
+{
+ self = [super initWithString:s];
+ if (self) {
+ defaultDelimiter = @"";
+ }
+ return self;
+}
+
+- (id)peek
+{
+ if (index >= string.length) {
+ return nil;
+ }
+
+ return [string characterAtIndex:index];
+}
+
+- (id)next
+{
+ id obj = [self peek];
+ if (obj) {
+ index++;
+ }
+ return obj;
+}
+
+- (BOOL)hasMore
+{
+ return (index < string.length);
+}
+
+- (int)length
+{
+ return string.length;
+}
+
+- (int)objectsConsumed
+{
+ return index;
+}
+
+- (int)objectsRemaining
+{
+ return (string.length - index);
+}
+
+- (CPString)consumedObjectsJoinedByString:(CPString)delimiter
+{
+ return [string substringToIndex:self.objectsConsumed];
+}
+
+- (CPString)remainingObjectsJoinedByString:(CPString)delimiter
+{
+ return [string substringFromIndex:self.objectsConsumed];
+}
+
+// overriding simply to print NSNumber objects as their unichar values
+- (CPString)description
+{
+ var s = "[";
+
+ var i = 0,
+ len = stack.length;
+
+ for (; i<len; i++) {
+ if ([obj isKindOfClass:[CPNumber class]]) { // ***this is needed for Char Assemblies
+ s += sprintf(@"%C", [obj integerValue]);
+ } else {
+ s += [obj description];
+ }
+ if (len - 1 != i++) {
+ s += ", ";
+ }
+ }
+
+ s += "]";
+
+ s += [self consumedObjectsJoinedByString:self.defaultDelimiter];
+ s += @"^";
+ s += [self remainingObjectsJoinedByString:self.defaultDelimiter];
+
+ return s;
+}
+
+@end
View
@@ -0,0 +1,73 @@
+
+@import "TDTokenizerState.j"
+@import "TDToken.j"
+@import "TDReader.j"
+@import "TDSymbolRootNode.j"
+@import "TDMultiLineCommentState.j"
+@import "TDSingleLineCommentState.j"
+
+@implementation TDCommentState : TDTokenizerState
+{
+ TDSymbolRootNode rootNode;
+ TDSingleLineCommentState singleLineState;
+ TDMultiLineCommentState multiLineState;
+
+ BOOL reportsCommentTokens;
+ BOOL balancesEOFTerminatedComments;
+}
+
+- (id)init
+{
+ self = [super init];
+ if (self) {
+ rootNode = [[TDSymbolRootNode alloc] init];
+ singleLineState = [[TDSingleLineCommentState alloc] init];
+ multiLineState = [[TDMultiLineCommentState alloc] init];
+ }
+ return self;
+}
+
+- (void)addSingleLineStartSymbol:(CPString)start
+{
+ [rootNode add:start];
+ [singleLineState addStartSymbol:start];
+}
+
+- (void)removeSingleLineStartSymbol:(CPString)start
+{
+ [rootNode remove:start];
+ [singleLineState removeStartSymbol:start];
+}
+
+- (void)addMultiLineStartSymbol:(CPString)start endSymbol:(CPString)end
+{
+ [rootNode add:start];
+ [rootNode add:end];
+ [multiLineState addStartSymbol:start endSymbol:end];
+}
+
+- (void)removeMultiLineStartSymbol:(CPString)start
+{
+ [rootNode remove:start];
+ [multiLineState removeStartSymbol:start];
+}
+
+- (TDToken)nextTokenFromReader:(TDReader)r startingWith:(int)cin tokenizer:(TDTokenizer)t
+{
+ var symbol = [rootNode nextSymbol:r startingWith:cin];
+
+ if ([multiLineState.startSymbols containsObject:symbol]) {
+ multiLineState.currentStartSymbol = symbol;
+ return [multiLineState nextTokenFromReader:r startingWith:cin tokenizer:t];
+ } else if ([singleLineState.startSymbols containsObject:symbol]) {
+ singleLineState.currentStartSymbol = symbol;
+ return [singleLineState nextTokenFromReader:r startingWith:cin tokenizer:t];
+ } else {
+ for (var i=0, length = symbol.length ; i < symbol.length - 1; i++) {
+ [r unread];
+ }
+ return [TDToken tokenWithTokenType:TDTokenTypeSymbol stringValue:[CPString stringWithFormat:@"%C", cin] floatValue:0.0];
+ }
+}
+
+@end
View
@@ -0,0 +1,110 @@
+
+@import "TDTokenizerState.j"
+@import "TDCommentState.j"
+@import "TDToken.j"
+@import "TDReader.j"
+
+@implementation TDMultiLineCommentState : TDTokenizerState
+{
+ CPArray startSymbols;
+ CPArray endSymbols;
+ CPString currentStartSymbol;
+}
+
+- (id)init
+{
+ self = [super init];
+ if (self) {
+ startSymbols = [];
+ endSymbols = [];
+ }
+ return self;
+}
+
+- (void)addStartSymbol:(CPString)start endSymbol:(CPString)end
+{
+ [startSymbols addObject:start];
+ [endSymbols addObject:end];
+}
+
+- (void)removeStartSymbol:(CPString)start
+{
+ var i = [startSymbols indexOfObject:start];
+ if (CPNotFound != i) {
+ [startSymbols removeObject:start];
+ [endSymbols removeObjectAtIndex:i]; // this should always be in range.
+ }
+}
+
+
+- (void)unreadSymbol:(CPString)s fromReader:(TDReader)r
+{
+ for (var i=0, len = s.length; i < len - 1; i++) {
+ [r unread];
+ }
+}
+
+- (TDToken)nextTokenFromReader:(TDReader)r startingWith:(int)cin tokenizer:(TDTokenizer)t
+{
+ var balanceEOF = t.commentState.balancesEOFTerminatedComments,
+ reportTokens = t.commentState.reportsCommentTokens;
+
+ if (reportTokens) {
+ [self reset];
+ [self appendString:currentStartSymbol];
+ }
+
+ var i = [startSymbols indexOfObject:currentStartSymbol],
+ currentEndSymbol = [endSymbols objectAtIndex:i],
+ e = [currentEndSymbol characterAtIndex:0];
+
+ // get the definitions of all multi-char comment start and end symbols from the commentState
+ var rootNode = t.commentState.rootNode,
+ c;
+
+ while (1) {
+ c = [r read];
+ if (-1 == c) {
+ if (balanceEOF) {
+ [self appendString:currentEndSymbol];
+ }
+ break;
+ }
+
+ if (e == c) {
+ var peek = [rootNode nextSymbol:r startingWith:e];
+ if ([currentEndSymbol isEqualToString:peek]) {
+ if (reportTokens) {
+ [self appendString:currentEndSymbol];
+ }
+ c = [r read];
+ break;
+ } else {
+ [self unreadSymbol:peek fromReader:r];
+ if (e != [peek characterAtIndex:0]) {
+ if (reportTokens) {
+ [self append:c];
+ }
+ c = [r read];
+ }
+ }
+ }
+ if (reportTokens) {
+ [self append:c];
+ }
+ }
+
+ if (-1 != c) {
+ [r unread];
+ }
+
+ currentStartSymbol = nil;
+
+ if (reportTokens) {
+ return [TDToken tokenWithTokenType:TDTokenTypeComment stringValue:[self bufferedString] floatValue:0.0];
+ } else {
+ return [t nextToken];
+ }
+}
+
+@end
Oops, something went wrong.

0 comments on commit 7aa6b9d

Please sign in to comment.