Try to lex identifiers and parens

This commit is contained in:
Eryn Wells 2016-12-19 09:46:58 -08:00
parent 59e55a39e4
commit ed27494783
3 changed files with 21 additions and 9 deletions

View file

@ -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 {

View file

@ -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
}
}

View file

@ -8,7 +8,7 @@
import Foundation
let l = Lexer(input: "(())")
let l = Lexer(input: "((abc))")
for t in l {
print(t)
}