Try to lex identifiers and parens
This commit is contained in:
		
							parent
							
								
									59e55a39e4
								
							
						
					
					
						commit
						ed27494783
					
				
					 3 changed files with 21 additions and 9 deletions
				
			
		|  | @ -33,10 +33,10 @@ extension CharacterSet { | |||
|         return subsequents | ||||
|     }() | ||||
| 
 | ||||
|     func contains(_ char: Character) -> Bool { | ||||
|         let cSet = CharacterSet(charactersIn: String(char)) | ||||
|         let isSuperset = self.isSuperset(of: cSet) | ||||
|         return isSuperset | ||||
|     func contains(_ c: Character) -> Bool { | ||||
|         let cSet = CharacterSet(charactersIn: String(c)) | ||||
|         let containsC = isSuperset(of: cSet) | ||||
|         return containsC | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | @ -51,7 +51,7 @@ extension Character { | |||
|     } | ||||
| 
 | ||||
|     var isIdentifierInitial: Bool { | ||||
|         return false | ||||
|         return CharacterSet.identifierInitials.contains(self) | ||||
|     } | ||||
| 
 | ||||
|     var isIdentifierSubsequent: Bool { | ||||
|  |  | |||
|  | @ -13,6 +13,7 @@ struct Token: CustomDebugStringConvertible { | |||
|     enum Kind { | ||||
|         case LeftParen | ||||
|         case RightParen | ||||
|         case Identifier | ||||
|     } | ||||
|      | ||||
|     let kind: Kind | ||||
|  | @ -79,20 +80,28 @@ extension Lexer: Sequence, IteratorProtocol { | |||
|         } | ||||
| 
 | ||||
|         while state != .Emit { | ||||
|             let c = input[index] | ||||
|             let c = input[forward] | ||||
|             print("processing '\(c)' in \(state)") | ||||
|             switch state { | ||||
|             case .Initial: | ||||
|                 if c.isLeftParen { | ||||
| 
 | ||||
|                     emit(.LeftParen) | ||||
|                 } | ||||
|                 else if c.isRightParen { | ||||
| 
 | ||||
|                     emit(.RightParen) | ||||
|                 } | ||||
|                 else if c.isIdentifierInitial { | ||||
|                     advance() | ||||
|                     toState(.Identifier) | ||||
|                 } | ||||
|             case .Identifier: | ||||
|                 if c.isIdentifierSubsequent { | ||||
|                     advance() | ||||
|                 } | ||||
|                 else { | ||||
|                     retract() | ||||
|                     emit(.Identifier) | ||||
|                 } | ||||
|                 break | ||||
|             case .Emit: | ||||
|                 // Nothing to do for this state | ||||
|  | @ -100,6 +109,9 @@ extension Lexer: Sequence, IteratorProtocol { | |||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // Set up for the next token. | ||||
|         index = input.index(after: forward) | ||||
| 
 | ||||
|         return token | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -8,7 +8,7 @@ | |||
| 
 | ||||
| import Foundation | ||||
| 
 | ||||
| let l = Lexer(input: "(())") | ||||
| let l = Lexer(input: "((abc))") | ||||
| for t in l { | ||||
|     print(t) | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue