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
|
return subsequents
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func contains(_ char: Character) -> Bool {
|
func contains(_ c: Character) -> Bool {
|
||||||
let cSet = CharacterSet(charactersIn: String(char))
|
let cSet = CharacterSet(charactersIn: String(c))
|
||||||
let isSuperset = self.isSuperset(of: cSet)
|
let containsC = isSuperset(of: cSet)
|
||||||
return isSuperset
|
return containsC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ extension Character {
|
||||||
}
|
}
|
||||||
|
|
||||||
var isIdentifierInitial: Bool {
|
var isIdentifierInitial: Bool {
|
||||||
return false
|
return CharacterSet.identifierInitials.contains(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isIdentifierSubsequent: Bool {
|
var isIdentifierSubsequent: Bool {
|
||||||
|
|
|
@ -13,6 +13,7 @@ struct Token: CustomDebugStringConvertible {
|
||||||
enum Kind {
|
enum Kind {
|
||||||
case LeftParen
|
case LeftParen
|
||||||
case RightParen
|
case RightParen
|
||||||
|
case Identifier
|
||||||
}
|
}
|
||||||
|
|
||||||
let kind: Kind
|
let kind: Kind
|
||||||
|
@ -79,20 +80,28 @@ extension Lexer: Sequence, IteratorProtocol {
|
||||||
}
|
}
|
||||||
|
|
||||||
while state != .Emit {
|
while state != .Emit {
|
||||||
let c = input[index]
|
let c = input[forward]
|
||||||
|
print("processing '\(c)' in \(state)")
|
||||||
switch state {
|
switch state {
|
||||||
case .Initial:
|
case .Initial:
|
||||||
if c.isLeftParen {
|
if c.isLeftParen {
|
||||||
|
emit(.LeftParen)
|
||||||
}
|
}
|
||||||
else if c.isRightParen {
|
else if c.isRightParen {
|
||||||
|
emit(.RightParen)
|
||||||
}
|
}
|
||||||
else if c.isIdentifierInitial {
|
else if c.isIdentifierInitial {
|
||||||
advance()
|
advance()
|
||||||
toState(.Identifier)
|
toState(.Identifier)
|
||||||
}
|
}
|
||||||
case .Identifier:
|
case .Identifier:
|
||||||
|
if c.isIdentifierSubsequent {
|
||||||
|
advance()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
retract()
|
||||||
|
emit(.Identifier)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case .Emit:
|
case .Emit:
|
||||||
// Nothing to do for this state
|
// 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
|
return token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
let l = Lexer(input: "(())")
|
let l = Lexer(input: "((abc))")
|
||||||
for t in l {
|
for t in l {
|
||||||
print(t)
|
print(t)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue