                 case "{", "}", "&", "$", "#", "^", "/", "<", ">":
                     state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
                 default:
-                    state = .Identifier(start: i)
-                }
+                    state = .Identifier(identifierStart: i)
+                }
+                
             case .LeadingDot:
                 switch c {
                 case " ", "\r", "\n", "\r\n", "\t":
-                    state = .IdentifierDone
-                case ".":
-                    state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
-                case "(":
-                    state = .Initial
-                    filterExpressionStack.append(currentExpression!)
-                    currentExpression = nil
-                case ")":
-                    if let filterExpression = filterExpressionStack.last {
-                        state = .FilterDone
-                        filterExpressionStack.removeLast()
-                        currentExpression = Expression.filter(filterExpression: filterExpression, argumentExpression: currentExpression!, partialApplication: false)
-                    } else {
-                        state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
-                    }
-                case ",":
-                    if let filterExpression = filterExpressionStack.last {
-                        state = .Initial
-                        filterExpressionStack.removeLast()
-                        filterExpressionStack.append(Expression.filter(filterExpression: filterExpression, argumentExpression: currentExpression!, partialApplication: true))
-                        currentExpression = nil
+                    state = .IdentifierDone(expression: Expression.ImplicitIterator)
+                case ".":
+                    state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
+                case "(":
+                    filterExpressionStack.append(Expression.ImplicitIterator)
+                    state = .WaitingForAnyExpression
+                case ")":
+                    if let filterExpression = filterExpressionStack.last {
+                        filterExpressionStack.removeLast()
+                        let expression = Expression.filter(filterExpression: filterExpression, argumentExpression: Expression.ImplicitIterator, partialApplication: false)
+                        state = .FilterDone(expression: expression)
+                    } else {
+                        state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
+                    }
+                case ",":
+                    if let filterExpression = filterExpressionStack.last {
+                        filterExpressionStack.removeLast()
+                        filterExpressionStack.append(Expression.filter(filterExpression: filterExpression, argumentExpression: Expression.ImplicitIterator, partialApplication: true))
+                        state = .WaitingForAnyExpression
                     } else {
                         state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
                     }
                 case "{", "}", "&", "$", "#", "^", "/", "<", ">":
                     state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
                 default:
-                    state = .Identifier(start: i)
-                }
-            case .Identifier(start: let identifierStart):
-                switch c {
-                case " ", "\r", "\n", "\r\n", "\t":
-                    let identifier = string.substringWithRange(identifierStart..<i)
-                    if currentExpression != nil {
-                        currentExpression = Expression.scoped(baseExpression: currentExpression!, identifier: identifier)
-                    } else {
-                        currentExpression = Expression.identifier(identifier)
-                    }
-                    state = .IdentifierDone
-                case ".":
-                    let identifier = string.substringWithRange(identifierStart..<i)
-                    if currentExpression != nil {
-                        currentExpression = Expression.scoped(baseExpression: currentExpression!, identifier: identifier)
-                    } else {
-                        currentExpression = Expression.identifier(identifier)
-                    }
-                    state = .WaitingForIdentifier
-                case "(":
-                    let identifier = string.substringWithRange(identifierStart..<i)
-                    if currentExpression != nil {
-                        currentExpression = Expression.scoped(baseExpression: currentExpression!, identifier: identifier)
-                    } else {
-                        currentExpression = Expression.identifier(identifier)
-                    }
-                    state = .Initial
-                    filterExpressionStack.append(currentExpression!)
-                    currentExpression = nil
-                case ")":
-                    let identifier = string.substringWithRange(identifierStart..<i)
-                    if currentExpression != nil {
-                        currentExpression = Expression.scoped(baseExpression: currentExpression!, identifier: identifier)
-                    } else {
-                        currentExpression = Expression.identifier(identifier)
-                    }
-                    if let filterExpression = filterExpressionStack.last {
-                        state = .FilterDone
-                        filterExpressionStack.removeLast()
-                        currentExpression = Expression.filter(filterExpression: filterExpression, argumentExpression: currentExpression!, partialApplication: false)
-                    } else {
-                        state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
-                    }
-                case ",":
-                    let identifier = string.substringWithRange(identifierStart..<i)
-                    if currentExpression != nil {
-                        currentExpression = Expression.scoped(baseExpression: currentExpression!, identifier: identifier)
-                    } else {
-                        currentExpression = Expression.identifier(identifier)
-                    }
-                    if let filterExpression = filterExpressionStack.last {
-                        state = .Initial
-                        filterExpressionStack.removeLast()
-                        filterExpressionStack.append(Expression.filter(filterExpression: filterExpression, argumentExpression: currentExpression!, partialApplication: true))
-                        currentExpression = nil
-                    } else {
-                        state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
-                    }
-                default:
-                    break
-                }
-            case .WaitingForIdentifier:
+                    state = .CompoundIdentifier(identifierStart: i, baseExpression: Expression.ImplicitIterator)
+                }
+                
+            case .Identifier(identifierStart: let identifierStart):
+                switch c {
+                case " ", "\r", "\n", "\r\n", "\t":
+                    let identifier = string.substringWithRange(identifierStart..<i)
+                    state = .IdentifierDone(expression: Expression.identifier(identifier))
+                case ".":
+                    let identifier = string.substringWithRange(identifierStart..<i)
+                    state = .WaitingForCompoundIdentifier(baseExpression: Expression.identifier(identifier))
+                case "(":
+                    let identifier = string.substringWithRange(identifierStart..<i)
+                    filterExpressionStack.append(Expression.identifier(identifier))
+                    state = .WaitingForAnyExpression
+                case ")":
+                    if let filterExpression = filterExpressionStack.last {
+                        filterExpressionStack.removeLast()
+                        let identifier = string.substringWithRange(identifierStart..<i)
+                        let expression = Expression.filter(filterExpression: filterExpression, argumentExpression: Expression.identifier(identifier), partialApplication: false)
+                        state = .FilterDone(expression: expression)
+                    } else {
+                        state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
+                    }
+                case ",":
+                    if let filterExpression = filterExpressionStack.last {
+                        filterExpressionStack.removeLast()
+                        let identifier = string.substringWithRange(identifierStart..<i)
+                        filterExpressionStack.append(Expression.filter(filterExpression: filterExpression, argumentExpression: Expression.identifier(identifier), partialApplication: true))
+                        state = .WaitingForAnyExpression
+                    } else {
+                        state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
+                    }
+                default:
+                    break
+                }
+                
+            case .CompoundIdentifier(identifierStart: let identifierStart, baseExpression: let baseExpression):
+                switch c {
+                case " ", "\r", "\n", "\r\n", "\t":
+                    let identifier = string.substringWithRange(identifierStart..<i)
+                    let scopedExpression = Expression.scoped(baseExpression: baseExpression, identifier: identifier)
+                    state = .IdentifierDone(expression: scopedExpression)
+                case ".":
+                    let identifier = string.substringWithRange(identifierStart..<i)
+                    let scopedExpression = Expression.scoped(baseExpression: baseExpression, identifier: identifier)
+                    state = .WaitingForCompoundIdentifier(baseExpression: scopedExpression)
+                case "(":
+                    let identifier = string.substringWithRange(identifierStart..<i)
+                    let scopedExpression = Expression.scoped(baseExpression: baseExpression, identifier: identifier)
+                    filterExpressionStack.append(scopedExpression)
+                    state = .WaitingForAnyExpression
+                case ")":
+                    if let filterExpression = filterExpressionStack.last {
+                        filterExpressionStack.removeLast()
+                        let identifier = string.substringWithRange(identifierStart..<i)
+                        let scopedExpression = Expression.scoped(baseExpression: baseExpression, identifier: identifier)
+                        let expression = Expression.filter(filterExpression: filterExpression, argumentExpression: scopedExpression, partialApplication: false)
+                        state = .FilterDone(expression: expression)
+                    } else {
+                        state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
+                    }
+                case ",":
+                    if let filterExpression = filterExpressionStack.last {
+                        filterExpressionStack.removeLast()
+                        let identifier = string.substringWithRange(identifierStart..<i)
+                        let scopedExpression = Expression.scoped(baseExpression: baseExpression, identifier: identifier)
+                        filterExpressionStack.append(Expression.filter(filterExpression: filterExpression, argumentExpression: scopedExpression, partialApplication: true))
+                        state = .WaitingForAnyExpression
+                    } else {
+                        state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
+                    }
+                default:
+                    break
+                }
+                
+            case .WaitingForCompoundIdentifier(let baseExpression):
                 switch c {
                 case " ", "\r", "\n", "\r\n", "\t":
                     state = .Error("Unexpected white space character at index \(distance(string.startIndex, i))")

            switch state {
            case .Error:
                break stringLoop
            case .Initial:
                switch c {
                case " ", "\r", "\n", "\r\n", "\t":
                    break
                case ".":
                    state = .LeadingDot
                    currentExpression = Expression.ImplicitIterator
                case "(":
                    state = .Error("Unexpected character `\(c)` at index \(distance(string.startIndex, i))")
                case ")":

    func parse(string: String, inout empty outEmpty: Bool) throws -> Expression {
        enum State {
            case Error(String)
            case WaitingForAnyExpression
            case LeadingDot
            case Identifier(identifierStart: String.Index)
            case CompoundIdentifier(identifierStart: String.Index, baseExpression: Expression)
            case WaitingForCompoundIdentifier(baseExpression: Expression)
            case IdentifierDone(expression: Expression)
            case FilterDone(expression: Expression)
            case Empty
            case Valid(expression: Expression)
        }
        
        var state: State = .WaitingForAnyExpression
        var filterExpressionStack: [Expression] = []
        
        var i = string.startIndex
        let end = string.endIndex
