             return result
         }
 
-        var state: State = .ScanningLiterals
+        func findOperatorInExpression(expression: String) -> (operator: Character?, error: URITemplateError?) {
+            var count = countElements(expression)
+
+            if count == 0 {
+                return (nil, URITemplateError.InvalidOperator)
+            }
+
+            var operator: Character? = nil
+            var error: URITemplateError? = nil
+            var startCharacher = expression[expression.startIndex]
+            if startCharacher == "%" {
+                if count < 3 {
+                    return (nil, URITemplateError.InvalidOperator)
+                }
+
+                var c1 = expression[advance(expression.startIndex, 1)]
+                var c2 = expression[advance(expression.startIndex, 2)]
+                if !find(HEXDIG, c1) {
+                    return (nil, URITemplateError.InvalidOperator)
+                }
+                if !find(HEXDIG, c2) {
+                    return (nil, URITemplateError.InvalidOperator)
+                }
+                var str = "%" + c1 + c2
+                str = str.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
+                operator = str[str.startIndex]
+            } else {
+                operator = startCharacher
+            }
+
+            if operator {
+                if !find(VARCHAR, operator!) {
+                    return (nil, URITemplateError.InvalidOperator)
+                }
+
+                if !BehaviorTable[String(operator!)] {
+                    return (nil, URITemplateError.InvalidOperator)
+                }
+            }
+
+            return (operator, error)
+        }
+
+        var state: State = .ScanningLiteral
         var result = ""
         var pctEncoded = ""
-        var errors = Dictionary<Int, URITemplateError>()
+        var expression = ""
+        var expressionCount = 0
+        var syntaxErrors = Array<(URITemplateError, Int)>()
 
         for (index, c) in enumerate(template) {
             switch state {
-            case .ScanningLiterals:
+            case .ScanningLiteral:
                 if c == "(" {
-                    state = .ScanningExpressions
+                    state = .ScanningExpression
+                    ++expressionCount
 
                 } else if (countElements(pctEncoded) > 0) {
                     switch countElements(pctEncoded) {

                        if find(HEXDIG, c) {
                            pctEncoded += c
                            result += pctEncoded
                            state = .ScanningLiterals
                            pctEncoded = ""

                        } else {
                            errors[index] = URITemplateError.MalformedPctEncodedInLiterals
                            result = appendLiteralString(pctEncoded, result)
                            result = appendLiteralCharacter(c, result)
                            state = .ScanningLiterals
                            pctEncoded = ""
                        }


            static let HEXDIG = "0123456789abcdefABCDEF"
            static let RESERVED = ":/?#[]@!$&'()*+,;="
            static let UNRESERVED = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~" // 66
            static let VARCHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" // exclude pct-encoded
        }

        let BehaviorTable = ClassVariable.BehaviorTable
        let HEXDIG = ClassVariable.HEXDIG
        let RESERVED = ClassVariable.RESERVED
        let UNRESERVED = ClassVariable.UNRESERVED
        let VARCHAR = ClassVariable.VARCHAR

        // Pct-encoded isn't taken into account
        func appendLiteralCharacter(character: Character, toString: String) -> String {
