Full Code of davedufresne/SwiftParsec for AI

master 00ae75a5e9e0 cached
56 files
587.4 KB
133.6k tokens
1 requests
Download .txt
Showing preview only (612K chars total). Download the full file or copy to clipboard to get everything.
Repository: davedufresne/SwiftParsec
Branch: master
Commit: 00ae75a5e9e0
Files: 56
Total size: 587.4 KB

Directory structure:
gitextract_e2cprpt0/

├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── LICENSE
├── Package.swift
├── README.md
├── Sources/
│   └── SwiftParsec/
│       ├── CharacterConversion.swift
│       ├── CharacterMembership.swift
│       ├── CharacterParsers.swift
│       ├── CharacterSet.swift
│       ├── CollectionAggregation.swift
│       ├── CombinatorParsers.swift
│       ├── Configuration.swift
│       ├── Either.swift
│       ├── ExpressionParser.swift
│       ├── GenericParser.swift
│       ├── GenericTokenParser.swift
│       ├── Info.plist
│       ├── LanguageDefinition.swift
│       ├── ParseError.swift
│       ├── Parsec.swift
│       ├── Permutation.swift
│       ├── Position.swift
│       ├── RangeReplaceableCollectionInsertion.swift
│       ├── SequenceAggregation.swift
│       ├── SequenceConversion.swift
│       ├── SetAggregation.swift
│       ├── String.swift
│       ├── TokenParser.swift
│       ├── UInt16.swift
│       ├── UnicodeScalar.swift
│       └── en.lproj/
│           └── Localizable.strings
├── SubmittingPatches.md
├── SwiftParsec.podspec
├── SwiftParsec.xcodeproj/
│   ├── project.pbxproj
│   ├── project.xcworkspace/
│   │   ├── contents.xcworkspacedata
│   │   └── xcshareddata/
│   │       └── IDEWorkspaceChecks.plist
│   └── xcshareddata/
│       └── xcschemes/
│           └── SwiftParsec.xcscheme
├── Tests/
│   ├── LinuxMain.swift
│   └── SwiftParsecTests/
│       ├── CharacterParsersTests.swift
│       ├── CharacterSetTests.swift
│       ├── CombinatorParsersTests.swift
│       ├── ErrorMessageTest.swift
│       ├── ExpressionParserTests.swift
│       ├── GenericParserTests.swift
│       ├── GenericTokenParserTests.swift
│       ├── Info.plist
│       ├── JSONBenchmarkTests.swift
│       ├── PermutationTests.swift
│       ├── PositionTests.swift
│       ├── SampleJSON.json
│       ├── StringTests.swift
│       ├── TestUtilities.swift
│       └── UnicodeScalarTests.swift
└── scripts/
    ├── install-swift.sh
    └── run-tests.sh

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData
.build/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
*.xcscmblueprint
.DS_Store
.swift-version

## Obj-C/Swift specific
*.hmap
*.ipa

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md

fastlane/report.xml
fastlane/screenshots


================================================
FILE: .travis.yml
================================================
os: osx
osx_image: xcode8
language: objective-c


matrix:
  include:
    - env: XCODE_DESTINATION="platform=iOS Simulator,OS=10.0,name=iPhone 5s"
    - env: XCODE_DESTINATION="platform=iOS Simulator,OS=10.0,name=iPhone 6"
    - env: XCODE_DESTINATION="platform=iOS Simulator,OS=10.0,name=iPhone 6s Plus"
    - env: XCODE_DESTINATION="platform=iOS Simulator,OS=10.0,name=iPad Air"
    - env: XCODE_DESTINATION="platform=iOS Simulator,OS=10.0,name=iPad Air 2"
    - env: XCODE_DESTINATION="platform=iOS Simulator,OS=10.0,name=iPad Pro (12.9 inch)"
    - env: XCODE_DESTINATION="platform=iOS Simulator,OS=10.0,name=iPad Retina"
    - os: linux
      dist: trusty
      language: generic


# BUG FIX: https://github.com/travis-ci/travis-ci/issues/6307
before_install: rvm get head


install: . ./scripts/install-swift.sh


script: ./scripts/run-tests.sh


================================================
FILE: CHANGELOG.md
================================================
# Release 4.0.1

Fix compilation error in Xcode 12.5

# Release 4.0.0

Migrated source code to Swift 5.0

# Release 3.0.3

Add missing 'products' entry in package description

# Release 3.0.2

Update package description

# Release 3.0.1

Fix compilation error for Swift 4.2

# Release 3.0.0

Migrated source code to Swift 4.0

# Release 2.1

Added support for linux.

# Release 2.0.1

Moved operators implementation in `Parsec` extension

# Release 2.0

## General

- Migrated source code to Swift 3.0
- Now Support Swift Package Manager
- Improved files and source code layout
- More documentation

## Performance

A benchmark was added to test the performance of the library.

An internal design modification greatly improved the parsing speed and memory
usage. Before the modification the benchmark measured 648.32s (≈10.8m) to
execute the parsing of a huge JSON file. Now it only takes 6.7s, a bit more than
96 times faster!

## API

- Added the `userState: GenericParser<StreamType, UserState, UserState>`
parser.
- Now the `run(userState: UserState, sourceName: String, input: StreamType)
throws -> Result` only returns the result of the parsing. As an example, if one
wants to get the user state and the result at the same time:
```
let countLine = GenericParser<String, Int, Character>.endOfLine >>- { newLine in

    GenericParser<String, Int, Int>.userState >>- { userState in

        GenericParser(result: (newLine, userState + 1))

    }

}

```
- Added the `Parsec.runSafe(userState: UserState, sourceName: String,
input: StreamType) -> Either<ParseError, Result>` method. This new method does
not throw exceptions but returns the result wrap in an `Either` type.
- Added a parser returning the current source position:
`GenericParser.sourcePosition: GenericParser<StreamType, UserState, SourcePosition>`
- Various minor changes to conform to the Swift API design guide lines

# Release 1.1

- Fixed wrong parse error type returned by `GenericParser.unexpected()`
- Added missing guard statement to prevent crash in `UnicodeScalar.fromUInt32()`
- Added `ClosedInterval` variant of `ParsecType.oneOf()`
- Migration to Swift 2.2
- Internal code improvement
- Increased tests coverage
- Documentation improvement


================================================
FILE: LICENSE
================================================
Copyright (c) 2015, David Dufresne
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

================================================
FILE: Package.swift
================================================
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
//
//  Package.swift
//  SwiftParsec
//
//  Created by David Dufresne on 2016-05-10.
//  Copyright © 2016 David Dufresne. All rights reserved.
//

import PackageDescription

let package = Package(
    name: "SwiftParsec",
    products: [
        // Products define the executables and libraries produced by a package, and
        // make them visible to other packages.
        .library(
            name: "SwiftParsec",
            targets: ["SwiftParsec"]
        ),
    ],
    targets: [
        .target(name: "SwiftParsec"),
    ]
)


================================================
FILE: README.md
================================================
# SwiftParsec
SwiftParsec is a Swift port of the [Parsec](https://github.com/aslatter/parsec) parser combinator library. It allows the creation of sophisticated parsers from a set of simple parsers. It is also easy to extend the available parsers. The parsers are fully integrated into the language, they can be put into arrays, passed as parameters, returned as values, etc. SwiftParsec provides expressiveness, is well documented and simple.

# Key Features
- Reusable combinators
- Lexical analysis
- Expression parser
- Permutation phrases parser
- Extensive error messages
- Unicode support

# Documentation
See the [wiki](https://github.com/davedufresne/SwiftParsec/wiki)

# License
SwiftParsec is released under the “Simplified BSD License”. See the LICENSE file in the repository.


================================================
FILE: Sources/SwiftParsec/CharacterConversion.swift
================================================
//==============================================================================
// CharacterConversion.swift
// SwiftParsec
//
// Created by David Dufresne on 2016-09-24.
// Copyright © 2016 David Dufresne. All rights reserved.
//
// Character extension
//==============================================================================

//==============================================================================
// Extension containing methods related to the conversion of a character.
extension Character {
    
    /// The first `UnicodeScalar` of `self`.
    var unicodeScalar: UnicodeScalar {
        
        let unicodes = String(self).unicodeScalars
        return unicodes[unicodes.startIndex]
        
    }
    
    /// Lowercase `self`.
    var lowercase: Character {
        
        let str = String(self).lowercased()
        return str[str.startIndex]
        
    }
    
    /// Uppercase `self`.
    var uppercase: Character {
        
        let str = String(self).uppercased()
        return str[str.startIndex]
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/CharacterMembership.swift
================================================
//==============================================================================
// CharacterMembership.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-09-19.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// Character extension
//==============================================================================

private let uppercaseSet = CharacterSet.uppercaseLetters
private let lowercaseSet = CharacterSet.lowercaseLetters
private let alphaSet = CharacterSet.letters
private let alphaNumericSet = CharacterSet.alphanumerics
private let symbolSet = CharacterSet.symbols
private let digitSet = CharacterSet.decimalDigits

//==============================================================================
// Extension containing methods to test if a character is a member of a
// character set.
extension Character {
    
    /// True for any space character, and the control characters \t, \n, \r, \f,
    /// \v.
    var isSpace: Bool {
        
        switch self {
            
        case " ", "\t", "\n", "\r", "\r\n": return true
            
        case "\u{000B}", "\u{000C}": return true // Form Feed, vertical tab
           
        default: return false
            
        }
        
    }
    
    /// True for any Unicode space character, and the control characters \t, \n,
    /// \r, \f, \v.
    var isUnicodeSpace: Bool {
        
        switch self {
            
        case " ", "\t", "\n", "\r", "\r\n": return true
            
        // Form Feed, vertical tab, next line (nel)
        case "\u{000C}", "\u{000B}", "\u{0085}": return true
            
        // No-break space, ogham space mark, mongolian vowel
        case "\u{00A0}", "\u{1680}", "\u{180E}": return true
            
        // En quad, em quad, en space, em space, three-per-em space, four-per-em
        // space, six-per-em space, figure space, ponctuation space, thin space,
        // hair space, zero width space, zero width non-joiner, zero width
        // joiner.
        case "\u{2000}", "\u{2001}", "\u{2002}", "\u{2003}", "\u{2004}",
             "\u{2005}", "\u{2006}", "\u{2007}", "\u{2008}", "\u{2009}",
             "\u{200A}", "\u{200B}", "\u{200C}", "\u{200D}":
            
            return true
            
        // Line separator, paragraph separator.
        case "\u{2028}", "\u{2029}": return true
            
        // Narrow no-break space, medium mathematical space, word joiner,
        // ideographic space, zero width no-break space.
        case "\u{202F}", "\u{205F}", "\u{2060}", "\u{3000}", "\u{FEFF}":
            
            return true
            
        default: return false
            
        }
        
    }
    
    /// `true` if `self` normalized contains a single code unit that is in the
    /// categories of Uppercase and Titlecase Letters.
    var isUppercase: Bool {
        
        return isMember(of: uppercaseSet)
        
    }
    
    /// `true` if `self` normalized contains a single code unit that is in the
    /// category of Lowercase Letters.
    var isLowercase: Bool {
        
        return isMember(of: lowercaseSet)
        
    }
    
    /// `true` if `self` normalized contains a single code unit that is in the
    /// categories of Letters and Marks.
    var isAlpha: Bool {
        
        return isMember(of: alphaSet)
        
    }
    
    /// `true` if `self` normalized contains a single code unit that is in th
    /// categories of Letters, Marks, and Numbers.
    var isAlphaNumeric: Bool {
        
        return isMember(of: alphaNumericSet)
        
    }
    
    /// `true` if `self` normalized contains a single code unit that is in the
    /// category of Symbols. These characters include, for example, the dollar
    /// sign ($) and the plus (+) sign.
    var isSymbol: Bool {
        
        return isMember(of: symbolSet)
        
    }
    
    /// `true` if `self` normalized contains a single code unit that is in the
    /// category of Decimal Numbers.
    var isDigit: Bool {
        
        return isMember(of: digitSet)
        
    }
    
    /// `true` if `self` is an ASCII decimal digit, i.e. between "0" and "9".
    var isDecimalDigit: Bool {
        
        return "0123456789".contains(self)
        
    }
    
    /// `true` if `self` is an ASCII hexadecimal digit, i.e. "0"..."9",
    /// "a"..."f", "A"..."F".
    var isHexadecimalDigit: Bool {
        
        return "01234567890abcdefABCDEF".contains(self)
        
    }
    
    /// `true` if `self` is an ASCII octal digit, i.e. between '0' and '7'.
    var isOctalDigit: Bool {
        
        return "01234567".contains(self)
        
    }
    
    /// Return `true` if `self` normalized contains a single code unit that is a
    /// member of the supplied character set.
    ///
    /// - parameter set: The `NSCharacterSet` used to test for membership.
    /// - returns: `true` if `self` normalized contains a single code unit that
    ///   is a member of the supplied character set.
    func isMember(of set: CharacterSet) -> Bool {
        
        let normalized = String(self).precomposedStringWithCanonicalMapping
        let unicodes = normalized.unicodeScalars
        
        guard unicodes.count == 1 else { return false }
        
        return set.contains(unicodes.first!)
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/CharacterParsers.swift
================================================
//==============================================================================
// CharacterParsers.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-09-16.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// Commonly used character parsers.
//==============================================================================

/// String parser with an empty `UserState`.
public typealias StringParser = GenericParser<String, (), Character>

//==============================================================================
// Extension containing methods related to character parsing.
public extension Parsec
where StreamType.Iterator.Element == Character, Result == Character {
    
    /// Return a parser that succeeds for any character for which the supplied
    /// function `predicate` returns `true`. The parser returns the character
    /// that is actually parsed.
    ///
    /// - parameter predicate: The predicate to apply on the `Character`.
    /// - returns: A parser that succeeds for any character for which the
    ///   supplied function `predicate` returns `true`.
    static func satisfy(
        _ predicate: @escaping (Character) -> Bool
    ) -> GenericParser<StreamType, UserState, Result> {
        
        return tokenPrimitive(
            tokenDescription: { String(reflecting: $0) },
            nextPosition: { position, elem in
                
                var pos = position
                pos.updatePosition(elem)
                
                return pos
                
            },
            match: { elem in
                
                predicate(elem) ? elem : nil
                
            })
        
    }
    
    /// Return a parser that succeeds if the current character is in the
    /// supplied list of characters. It returns the parsed character.
    ///
    ///     let vowel = StringParser.oneOf("aeiou")
    ///
    /// - parameter list: A `String` of possible characters to match.
    /// - returns: A parser that succeeds if the current character is in the
    ///   supplied list of characters.
    /// - SeeAlso:
    ///   `GenericParser.satisfy(
    ///       predicate: Character -> Bool
    ///   ) -> GenericParser`
    static func oneOf(
        _ list: String
    ) -> GenericParser<StreamType, UserState, Result> {
        
        return satisfy(list.contains)
        
    }
    
    /// Return a parser that succeeds if the current character is in the
    /// supplied interval of characters. It returns the parsed character.
    ///
    ///     let digit = StringParser.oneOf("0"..."9")
    ///
    /// - parameter interval: A `ClosedInterval` of possible characters to
    ///   match.
    /// - returns: A parser that succeeds if the current character is in the
    ///   supplied interval of characters.
    /// - SeeAlso:
    ///   `GenericParser.satisfy(
    ///       predicate: Character -> Bool
    ///   ) -> GenericParser`
    static func oneOf(
        _ interval: ClosedRange<Character>
    ) -> GenericParser<StreamType, UserState, Result> {
        
        return satisfy(interval.contains)
        
    }
    
    /// Return a parser that succeeds if the current character is _not_ in the
    /// supplied list of characters. It returns the parsed character.
    ///
    ///     let consonant = StringParser.noneOf("aeiou")
    ///
    /// - parameter list: A `String` of possible _not_ to match.
    /// - returns: A parser that succeeds if the current character is _not_ in
    ///   the supplied list of characters.
    static func noneOf(
        _ list: String
    ) -> GenericParser<StreamType, UserState, Result> {
        
        return satisfy { !list.contains($0) }
        
    }
    
    /// A Parser that skips _zero_ or more unicode white space characters.
    ///
    /// - SeeAlso: `GenericParser.skipMany`.
    static var spaces: GenericParser<StreamType, UserState, ()> {
        
        return space.skipMany <?> LocalizedString("white space")
        
    }
    
    /// A Parser that parses a white space character (any Unicode space
    /// character, and the control characters \t, \n, \r, \f, \v). It returns
    /// the parsed character.
    static var unicodeSpace:
        GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isUnicodeSpace } <?> LocalizedString("unicode space")
        
    }
    
    /// A Parser that parses any space character, and the control characters \t,
    /// \n, \r, \f, \v. It returns the parsed character.
    static var space: GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isSpace } <?> LocalizedString("space")
        
    }
    
    /// A Parser that parses a newline character ("\n"). It returns a newline
    /// character.
    static var newLine: GenericParser<StreamType, UserState, Result> {
        
        return character("\n") <?> LocalizedString("lf new-line")
        
    }
    
    /// A Parser that parses a carriage return character ("\r") followed by a
    /// newline character ("\n"). It returns a newline character.
    static var crlf: GenericParser<StreamType, UserState, Result> {
        
        // "\r\n" is combined in one Unicode Scalar.
        return character("\r\n") *> GenericParser(result: "\n") <|>
            character("\r") *> character("\n") <?>
            LocalizedString("crlf new-line")
        
    }
    
    /// A Parser that parses a CRLF (see `crlf`) or LF (see `newLine`)
    /// end-of-line. It returns a newline character ("\n").
    ///
    ///     let endOfLine = StringParser.newline <|> StringParser.crlf
    static var endOfLine: GenericParser<StreamType, UserState, Result> {
        
        return newLine <|> crlf <?> LocalizedString("new-line")
        
    }
    
    /// A Parser that parses a tab character ("\t"). It returns a tab character.
    static var tab: GenericParser<StreamType, UserState, Result> {
        
        return character("\t") <?> LocalizedString("tab")
        
    }
    
    /// A Parser that parses a character in the category of Uppercase and
    /// Titlecase Letters. It returns the parsed character.
    static var uppercase: GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isUppercase } <?> LocalizedString("uppercase letter")
        
    }
    
    /// A Parser that parses a character in the category of Lowercase Letters.
    /// It returns the parsed character.
    static var lowercase: GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isLowercase } <?> LocalizedString("lowercase letter")
        
    }
    
    /// A Parser that parses a character in the categories of Letters, Marks,
    /// and Numbers. It returns the parsed character.
    static var alphaNumeric:
        GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isAlphaNumeric } <?>
            LocalizedString("letter or digit")
        
    }
    
    /// A Parser that parses a character in the categories of Letters and Marks.
    /// It returns the parsed character.
    static var letter: GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isAlpha } <?> LocalizedString("letter")
        
    }
    
    /// A Parser that parses a character in the categories of Symbols. It
    /// returns the parsed character.
    static var symbol: GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isSymbol } <?> LocalizedString("symbol")
        
    }
    
    /// A Parser that parses a character in the category of Numbers. It returns
    /// the parsed character.
    static var digit: GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isDigit } <?> LocalizedString("digit")
        
    }
    
    /// A Parser that parses an ASCII decimal digit, i.e. between "0" and "9".
    /// It returns the parsed character.
    static var decimalDigit:
        GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isDecimalDigit } <?> LocalizedString("digit")
        
    }
    
    /// A Parser that parses an ASCII hexadecimal digit, i.e. "0"..."9",
    /// "a"..."f", "A"..."F". It returns the parsed character.
    static var hexadecimalDigit:
        GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isHexadecimalDigit } <?>
            LocalizedString("hexadecimal digit")
        
    }
    
    /// A Parser that parses an octal digit (a character between "0" and "7").
    /// It returns the parsed character.
    static var octalDigit: GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isOctalDigit } <?>  LocalizedString("octal digit")
        
    }
    
    /// Return a parser that parses a single character `Character`. It returns
    /// the parsed character (i.e. `char`).
    ///
    ///     let semicolon  = StringParser.character(";")
    ///
    /// - parameter char: The character to parse.
    /// - returns: A parser that parses a single character `Character`.
    static func character(
        _ char: Character
    ) -> GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0 == char } <?> String(reflecting: char)
        
    }
    
    /// A Parser that succeeds for any character. It returns the parsed
    /// character.
    static var anyCharacter:
        GenericParser<StreamType, UserState, Result> {
        
        return satisfy { _ in true }
        
    }
    
    /// A Parser that maps a `Character` to a `String`.
    var stringValue: GenericParser<StreamType, UserState, String> {
        
        return map { String($0) }
        
    }
    
    /// Return a parser that succeeds for any character that are member of the
    /// supplied `CharacterSet`. It returns the parsed character.
    ///
    /// - parameter set: The `CharacterSet` used to test for membership.
    /// - returns: The parsed character.
    static func memberOf(
        _ set: CharacterSet
    ) -> GenericParser<StreamType, UserState, Result> {
        
        return satisfy { $0.isMember(of: set) }
        
    }
    
}

//==============================================================================
// Extension containing methods related to result conversion.
public extension Parsec
where Result: Sequence, Result.Iterator.Element == Character {
    
    /// A Parser that maps an array of `Character` to a `String`.
    var stringValue: GenericParser<StreamType, UserState, String> {
        
        return map { String($0) }
        
    }
    
}

//==============================================================================
// Extension containing methods related to string parsing.
public extension Parsec
where StreamType.Iterator.Element == Character {
    
    /// Return a parser that parses a `String`. It returns the parsed string
    /// (i.e. `str`).
    ///
    ///     let divOrMod = StringParser.string("div") <|>
    ///         StringParser.string("mod")
    ///
    /// - parameter str: The string to parse.
    /// - returns: A parser that parses a `String`.
    static func string(
        _ str: StreamType
    ) -> GenericParser<StreamType, UserState, StreamType> {
        
        return tokens(
            tokensDescription: { String(reflecting: $0) },
            nextPosition: { position, charStreamType in
                
                var pos = position
                for char in charStreamType {
                    
                    pos.updatePosition(char)
                    
                }
                
                return pos
                
            },
            tokens: str)
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/CharacterSet.swift
================================================
//==============================================================================
// CharacterSet.swift
// SwiftParsec
//
// CharacterSet compatibility wrapper
//==============================================================================

import struct Foundation.CharacterSet

#if _runtime(_ObjC)

/// Ideally, we could use this for all platforms, but open-source
/// `Foundation.CharacterSet` will fail at large sizes
public typealias CharacterSet = Foundation.CharacterSet

#else

//==============================================================================
/// The `CharacterSet` is a thin wrapper around `Foundation.CharacterSet`. It
/// helps us avoid bugs in open-source version.
public struct CharacterSet {
    
    /// Indicates whether the set contains the given unicode character.
    public let contains: (UnicodeScalar) -> Bool
    
    /// Convenience factory for `Foundation.CharacterSet#uppercaseLetters`
    public static var uppercaseLetters: CharacterSet {
        
        return CharacterSet(Foundation.CharacterSet.uppercaseLetters)
        
    }
    
    /// Convenience factory for `Foundation.CharacterSet#lowercaseLetters`
    public static var lowercaseLetters: CharacterSet {
        
        return CharacterSet(Foundation.CharacterSet.lowercaseLetters)
        
    }
    
    /// Convenience factory for `Foundation.CharacterSet#decimalDigits`
    public static var decimalDigits: CharacterSet {
        
        return CharacterSet(Foundation.CharacterSet.decimalDigits)
        
    }
    
    /// Convenience factory for `Foundation.CharacterSet#symbols`
    public static var symbols: CharacterSet {
        
        return CharacterSet(Foundation.CharacterSet.symbols)
        
    }
    
    /// Convenience factory for `Foundation.CharacterSet#letters`
    public static var letters: CharacterSet {
        
        return CharacterSet(Foundation.CharacterSet.letters)
        
    }
    
    /// Convenience factory for `Foundation.CharacterSet#alphanumerics`
    public static var alphanumerics: CharacterSet {
        
        return CharacterSet(Foundation.CharacterSet.alphanumerics)
        
    }
    
    /// Alternative to `Foundation.CharacterSet#init` that does not fail for
    /// large inputs. It is likely less performant.
    public init(charactersIn: String) {
        
        self.contains = Set(charactersIn.unicodeScalars).contains
        
    }
    
    /// Convert a `Foundation.CharacterSet` to a `SwiftParsec.CharacterSet`
    public init(_ foundationSet: Foundation.CharacterSet) {
        
        self.contains = foundationSet.contains
        
    }
    
}

#endif


================================================
FILE: Sources/SwiftParsec/CollectionAggregation.swift
================================================
//==============================================================================
// CollectionAggregation.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-10-09.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// Collection extension
//==============================================================================

//==============================================================================
// Extension containing aggregation methods.
extension Collection {
    
    /// Return the result of repeatedly calling `combine` with an accumulated
    /// value initialized to `initial` and each element of `self`, in turn from
    /// the right, i.e. return combine(combine(...combine(combine(initial,
    /// self[count-1]), self[count-2]), self[count-3]), ... self[0]).
    ///
    /// - parameters:
    ///   - initial: The initial value.
    ///   - combine: The combining function.
    /// - returns: The combined result of each element of `self`.
    func reduceRight<T>(
        _ initial: T,
        combine: (T, Self.Iterator.Element) throws -> T
    ) rethrows -> T {
        
        var acc = initial
        for elem in reversed() {
            
            acc = try combine(acc, elem)
            
        }
        
        return acc
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/CombinatorParsers.swift
================================================
//==============================================================================
// CombinatorParsers.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-09-26.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// Commonly used generic combinators
//==============================================================================

//==============================================================================
// Extension containing methods related to parser combinators.
public extension GenericParser {
    
    /// Return a parser that tries to apply the parsers in the array `parsers`
    /// in order, until one of them succeeds. It returns the value of the
    /// succeeding parser.
    ///
    /// - parameter parsers: An array of parsers to try.
    /// - returns: A parser that tries to apply the parsers in the array
    ///   `parsers` in order, until one of them succeeds.
    static func choice<S: Sequence>(_ parsers: S) -> GenericParser
    where S.Iterator.Element == GenericParser {
        
        return parsers.reduce(GenericParser.empty, <|>)
        
    }
    
    /// A parser that tries to apply `self`. If `self` fails without consuming
    /// input, it returns `nil`, otherwise it returns the value returned by
    /// `self`.
    var optional: GenericParser<StreamType, UserState, Result?> {
        
        return map({ $0 }).otherwise(nil)
        
    }
    
    /// Return a parser that tries to apply `self`. If `self` fails without
    /// consuming input, it returns `result`, or else the value returned by
    /// `self`.
    ///
    /// - parameter result: The result to return if `self` fails without
    ///   consuming input.
    /// - returns: A parser that tries to apply `self`. If `self` fails without
    ///   consuming input, it returns `result`.
    func otherwise(_ result: Result) -> GenericParser {
        
        return self <|> GenericParser(result: result)
        
    }
    
    /// A parser that tries to apply `self`. It will parse `self` or nothing.
    /// It only fails if `self` fails after consuming input. It discards the
    /// result of `self`.
    var discard: GenericParser<StreamType, UserState, ()> {
        
        return map { _ in () } <|> GenericParser<StreamType, UserState, ()>(
            result: ()
        )
        
    }
    
    /// Return a parser that parses `opening`, followed by `self` and `closing`.
    /// It returns the value returned by `self`.
    ///
    /// - parameters:
    ///   - opening: The first parser to apply.
    ///   - closing: The last parser to apply.
    /// - returns: A parser that parses `opening`, followed by `self` and
    ///   `closing`.
    func between<U, V>(
        _ opening: GenericParser<StreamType, UserState, U>,
        _ closing: GenericParser<StreamType, UserState, V>
    ) -> GenericParser {
        
        return opening *> self <* closing
        
    }
    
    /// A parser that applies `self` _one_ or more times, skipping its result.
    var skipMany1: GenericParser<StreamType, UserState, ()> {
        
        return self >>- { _ in self.skipMany }
        
    }
    
    /// A parser that applies `self` _one_ or more times. It returns an array of
    /// the returned values of `self`.
    ///
    ///     let word = StringParser.letter.many1
    var many1: GenericParser<StreamType, UserState, [Result]> {
        
        return self >>- { result in
            
            self.many >>- { results in
                
                let rs = results.prepending(result)
                return GenericParser<StreamType, UserState, [Result]>(
                    result: rs
                )
                
            }
            
        }
        
    }
    
    /// Return a parser that parses _zero_ or more occurrences of `self`,
    /// separated by `separator`. It returns an array of values returned by
    /// `self`.
    ///
    ///     let comma = StringParser.character(",")
    ///     let letter = StringParser.letter
    ///     let commaSeparated = letter.separatedBy(comma)
    ///
    /// - parameter separator: The separator parser.
    /// - returns: A parser that parses _zero_ or more occurrences of `self`,
    ///   separated by `separator`.
    func separatedBy<Separator>(
        _ separator: GenericParser<StreamType, UserState, Separator>
    ) -> GenericParser<StreamType, UserState, [Result]> {
        
        return separatedBy1(separator) <|>
            GenericParser<StreamType, UserState, [Result]>(result: [])
        
    }
    
    /// Return a parser that parses _one_ or more occurrences of `self`,
    /// separated by `separator`. It returns an array of values returned by
    /// `self`.
    ///
    /// - parameter separator: The separator parser.
    /// - returns: A parser that parses _one_ or more occurrences of `self`,
    ///   separated by `separator`.
    func separatedBy1<Separator>(
        _ separator: GenericParser<StreamType, UserState, Separator>
    ) -> GenericParser<StreamType, UserState, [Result]> {
        
        return self >>- { result in
            
            (separator *> self).many >>- { results in
                
                let rs = results.prepending(result)
                return GenericParser<StreamType, UserState, [Result]>(
                    result: rs
                )
                
            }
            
        }
        
    }
    
    /// Return a parser that parses _zero_ or more occurrences of `self`,
    /// separated and optionally ended by `separator`. It returns an array of
    /// values returned by `self`.
    ///
    ///     let cStatements = cStatement.dividedBy(semicolon)
    ///
    ///     let swiftStatements = swiftStatement.dividedBy(semicolon,
    ///         endSeparatorRequired: false)
    ///
    /// - parameters:
    ///   - separator: The separator parser.
    ///   - endSeparatorRequired: Indicates if the separator is required at the
    ///     end. The default value is true.
    /// - returns: A parser that parses _zero_ or more occurrences of `self`,
    ///   separated and optionally ended by `separator`.
    func dividedBy<Separator>(
        _ separator: GenericParser<StreamType, UserState, Separator>,
        endSeparatorRequired: Bool = true
    ) -> GenericParser<StreamType, UserState, [Result]> {
        
        if endSeparatorRequired {
            
            return (self <* separator).many
            
        }
        
        return dividedBy1(separator, endSeparatorRequired: false) <|>
            GenericParser<StreamType, UserState, [Result]>(result: [])
        
    }
    
    /// Return a parser that parses _one_ or more occurrences of `self`,
    /// separated and optionally ended by `separator`. It returns an array of
    /// values returned by `self`.
    ///
    /// - parameters:
    ///   - separator: The separator parser.
    ///   - endSeparatorRequired: Indicates if the separator is required at the
    ///     end. The default value is true.
    /// - returns: A parser that parses _one_ or more occurrences of `self`,
    ///   separated and optionally ended by `separator`.
    func dividedBy1<Separator>(
        _ separator: GenericParser<StreamType, UserState, Separator>,
        endSeparatorRequired: Bool = true
    ) -> GenericParser<StreamType, UserState, [Result]> {
        
        if endSeparatorRequired {
            
            return (self <* separator).many1
            
        }
        
        return self >>- { result in
            
            // Type inference bug.
            let optionalSeparator:
                GenericParser<StreamType, UserState, [Result]> =
            separator >>- { _ in
                
                self.dividedBy(separator, endSeparatorRequired: false) >>-
                { results in
                    
                    let rs = results.prepending(result)
                    return GenericParser<StreamType, UserState, [Result]>(
                        result: rs
                    )
                    
                }
                
            }
            
            return optionalSeparator <|>
                GenericParser<StreamType, UserState, [Result]>(result: [result])
            
        }
        
    }
    
    /// Return a parser that parses `n` occurrences of `self`. If `n` is
    /// smaller or equal to zero, the parser returns `[]`. It returns an array
    /// of `n` values returned by `self`.
    ///
    /// - parameter n: The number of occurences of `self` to parse.
    /// - returns: A parser that parses `n` occurrences of `self`.
    func count(
        _ n: Int
    ) -> GenericParser<StreamType, UserState, [Result]> {
        
        func count(
            _ n: Int,
            results: [Result]
        ) -> GenericParser<StreamType, UserState, [Result]> {
            
            guard n > 0 else {
                
                return GenericParser<StreamType, UserState, [Result]>(
                    result: results
                )
                
            }
            
            return self >>- { result in
                
                let rs = results.appending(result)
                return count(n - 1, results: rs)
                
            }
            
        }
        
        return GenericParser<StreamType, UserState, [Result]> { state in
            
            return count(n, results: []).parse(state)
            
        }
        
    }
    
    /// Return a parser that parses _zero_ or more occurrences of `self`,
    /// separated by `oper`. Returns a value obtained by a _right_ associative
    /// application of all functions returned by `oper` to the values returned
    /// by `self`. If there are no occurrences of `self`, the value `otherwise`
    /// is returned.
    ///
    /// - parameters:
    ///   - oper: The operator parser.
    ///   - otherwise: Default value returned when there are no occurences of
    ///     `self`.
    /// - returns: A parser that parses _zero_ or more occurrences of `self`,
    ///   separated by `oper`.
    func chainRight(
        _ oper: GenericParser<StreamType, UserState, (Result, Result) -> Result>,
        otherwise: Result
    ) -> GenericParser {
    
        return chainRight1(oper) <|> GenericParser(result: otherwise)
        
    }
    
    /// Return a parser that parses _one_ or more occurrences of `self`,
    /// separated by `oper`. Returns a value obtained by a _right_ associative
    /// application of all functions returned by `op` to the values returned by
    /// `self`.
    ///
    /// - parameter oper: The operator parser.
    /// - returns: A parser that parses _one_ or more occurrences of `self`,
    ///   separated by `oper`.
    func chainRight1(
        _ oper: GenericParser<StreamType, UserState, (Result, Result) -> Result>
    ) -> GenericParser {
        
        func scan() -> GenericParser {
            
            return self >>- { result in rest(result) }
            
        }
        
        func rest(_ left: Result) -> GenericParser {
            
            // Type inference bug.
            let operParser: GenericParser = oper >>- { op in
                
                scan() >>- { right in
                    
                    let result = op(left, right)
                    return GenericParser(result: result)
                    
                }
                
            }
            
            return operParser <|> GenericParser(result: left)
            
        }
        
        return scan()
        
    }
    
    /// Return a parser that parses _zero_ or more occurrences of `self`,
    /// separated by `oper`. Returns a value obtained by a _left_ associative
    /// application of all functions returned by `oper` to the values returned
    /// by `self`. If there are zero occurrences of `self`, the value
    /// `otherwise` is returned.
    ///
    /// - parameters:
    ///   - oper: The operator parser.
    ///   - otherwise: Default value returned when there are no occurences of
    ///     `self`.
    /// - returns: A parser that parses _zero_ or more occurrences of `self`,
    ///   separated by `oper`.
    func chainLeft(
        _ oper: GenericParser<StreamType, UserState, (Result, Result) -> Result>,
        otherwise: Result
    ) -> GenericParser {
        
        return chainLeft1(oper) <|> GenericParser(result: otherwise)
        
    }
    
    /// Return a parser that parses _one_ or more occurrences of `self`,
    /// separated by oper`. Returns a value obtained by a _left_ associative
    /// application of all functions returned by `oper` to the values returned
    /// by `self`. This parser can for example be used to eliminate left
    /// recursion which typically occurs in expression grammars.
    ///
    ///     let addOp: GenericParser<String, (), (Int, Int) -> Int> =
    ///         StringParser.character("+") *> GenericParser(result: +) <|>
    ///         StringParser.character("-") *> GenericParser(result: -)
    ///
    ///     let expr = number.chainLeft1(addOp)
    ///
    /// - parameter oper: The operator parser.
    /// - returns: A parser that parses _one_ or more occurrences of `self`,
    ///   separated by `oper`.
    func chainLeft1(
        _ oper: GenericParser<StreamType, UserState, (Result, Result) -> Result>
    ) -> GenericParser {
        
        func rest(_ left: Result) -> GenericParser {
            
            let operParser = oper >>- { op in
                
                self >>- { right in rest(op(left, right)) }
                
            }
            
            return operParser <|> GenericParser(result: left)
            
        }
        
        return self >>- { result in rest(result) }
        
    }
    
    /// A parser that only succeeds when parser `self` fails. This parser does
    /// not consume any input. This parser can be used to implement the
    /// 'longest match' rule. For example, when recognizing keywords (for
    /// example `let`), we want to make sure that a keyword is not followed by a
    /// legal identifier character, in which case the keyword is actually an
    /// identifier (for example `lets`). We can program this behaviour as
    /// follows:
    ///
    ///     let alphaNum = StringParser.alphaNumeric
    ///     let keyworkLet = StringParser.string("let") <* alphaNum.noOccurence
    var noOccurence: GenericParser<StreamType, UserState, ()> {
        
        let selfAttempt = attempt >>- { result in
            
            GenericParser<StreamType, UserState, ()>.unexpected(
                String(reflecting: result)
            )
            
        }
        
        return (selfAttempt <|> GenericParser<StreamType, UserState, ()>(
            result: ())).attempt
        
    }
    
    /// Return a parser that applies parser `self` _zero_ or more times until
    /// parser `end` succeeds. Returns the list of values returned by `self`.
    /// This parser can be used to scan comments:
    ///
    ///     let anyChar = StringParser.anyCharacter
    ///     let start = StringParser.string("<!--")
    ///     let end = StringParser.string("-->")
    ///     let comment = start *> anyChar.manyTill(end.attempt)
    ///
    /// Note the overlapping parsers `anyChar` and `end`, and therefore the use
    /// of the 'attempt' combinator.
    ///
    /// - parameter end: End parser.
    /// - returns: A parser that applies parser `self` _zero_ or more times
    ///   until parser `end` succeeds.
    func manyTill<End>(
        _ end: GenericParser<StreamType, UserState, End>
    ) -> GenericParser<StreamType, UserState, [Result]> {
        
        func scan() -> GenericParser<StreamType, UserState, [Result]> {
            
            let empty = end *>
                GenericParser<StreamType, UserState, [Result]>(result: [])
            
            return empty <|> (self >>- { result in
                
                scan() >>- { results in
                    
                    let rs = results.prepending(result)
                    return GenericParser<StreamType, UserState, [Result]>(
                        result: rs
                    )
                    
                }
                
            })
            
        }
        
        return scan()
        
    }
    
    /// Return a recursive parser combined with itself. It can be used to parse
    /// nested expressions. As an exemple, an expression inside a pair of
    /// parentheses is itself an expression that can be nested inside another
    /// pair of parentheses.
    ///
    ///     let expression = GenericParser<...>.recursive { expression in
    ///         parentheses(expression) <|>
    ///         identifier <|>
    ///         legalOperator <|> ...
    ///
    /// - parameter combine: A function receiving a placeholder parser as
    ///   parameter that can be nested in other expressions.
    /// - returns: A recursive parser combined with itself.
    static func recursive(
        _ combine: (GenericParser) -> GenericParser
    ) -> GenericParser {
        
        var expression: GenericParser!
        let placeholder = GenericParser { expression }
        
        expression = combine(placeholder)
        
        return expression
        
    }
    
}

//==============================================================================
// Extension containing methods related to special parsers.
public extension Parsec where StreamType.Iterator.Element == Result {
    
    /// A parser that accepts any kind of token. It is for example used to
    /// implement 'eof'. It returns the accepted token.
    static var anyToken: GenericParser<StreamType, UserState, Result> {
        
        return GenericParser.tokenPrimitive(
            tokenDescription: { String(reflecting: $0) },
            nextPosition: { pos, _ in pos },
            match: { $0 })
        
    }
    
    /// A parser that only succeeds at the end of the input. This is not a
    /// primitive parser but it is defined using `GenericParser.noOccurence`.
    ///
    /// - returns: A parser that only succeeds at the end of the input.
    static var eof: GenericParser<StreamType, UserState, ()> {
        
        return GenericParser.anyToken.noOccurence <?>
            LocalizedString("end of input")
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/Configuration.swift
================================================
//==============================================================================
// Configuration.swift
// SwiftParsec
//
// Created by David Dufresne on 2016-05-27.
// Copyright © 2016 David Dufresne. All rights reserved.
//
// Configuration of the parsers framework
//==============================================================================

//==============================================================================
/// The `Configuration` type is used to customize the framework.
public struct Configuration {
    
    /// A hook to customize the localization of the strings contained in the
    /// framework. The default function simply returns the `key` passed as
    /// argument. A bundled application could use the sample
    /// _Localizable.strings_ file as a starting point for its own strings
    /// files and customize the framework this way:
    ///
    ///     Configuration.localizeString =
    ///         { NSLocalizedString($0, comment: "") }
    ///
    public static var localizeString: (_ key: String) -> String = { $0 }
    
}

//==============================================================================
/// Function calling the string localization hook.
///
/// - parameters:
///   - key: The key for a string in the default table.
func LocalizedString(_ key: String) -> String {
    
    return Configuration.localizeString(key)

}


================================================
FILE: Sources/SwiftParsec/Either.swift
================================================
//==============================================================================
// Either.swift
// SwiftParsec
//
// Created by David Dufresne on 2016-09-18.
// Copyright © 2016 David Dufresne. All rights reserved.
//
// Either type
//==============================================================================

//==============================================================================
/// The Either enumeration represents values with two possibilities: a value of
/// type `Either<L, R>` is either `Left(L)` or `Right(R)`.
public enum Either<L, R> {
    
    /// Left posibility.
    case left(L)
    
    /// Right posibility.
    case right(R)
    
}


================================================
FILE: Sources/SwiftParsec/ExpressionParser.swift
================================================
//==============================================================================
// Expression.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-10-23.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// A helper module to parse "expressions". Builds a parser given a table of
// operators and associativities.
//==============================================================================

//==============================================================================
/// This enumeration specifies the associativity of operators: right, left or
/// none.
public enum Associativity {
    
    /// Right associative
    case right
    
    /// Left associative
    case left
    
    /// No associativity
    case none
    
}

//==============================================================================
/// This data type specifies operators that work on values of type `Result`. An
/// operator is either binary infix or unary prefix or postfix. A binary
/// operator has also an associated associativity.
public enum Operator<StreamType: Stream, UserState, Result> {
    
    /// Infix operator and associativity.
    case infix(
        GenericParser<StreamType, UserState, (Result, Result) -> Result>,
        Associativity
    )
    
    /// Prefix operator.
    case prefix(GenericParser<StreamType, UserState, (Result) -> Result>)
    
    /// Postfix operator.
    case postfix(GenericParser<StreamType, UserState, (Result) -> Result>)
    
}

//==============================================================================
/// Represents a table of  operators. The `makeExpressionParser()` method is
/// used to create parsers based on the table.
public struct OperatorTable<StreamType: Stream, UserState, Result>:
RangeReplaceableCollection, ExpressibleByArrayLiteral {
    
    /// Represents a valid position in the operator table.
    public typealias Index = Int
    
    /// Operator table's generator.
    public typealias Iterator = IndexingIterator<OperatorTable>
    
    /// Element type of the operator table.
    public typealias Element = [Operator<StreamType, UserState, Result>]
    
    /// The position of the first element.
    public let startIndex = 0
    
    /// The operator table's "past the end" position.
    public var endIndex: Int { return table.count }
    
    // Backing store.
    private var table: [Element]
    
    /// Create an instance initialized with elements.
    ///
    /// - parameter arrayLiteral: Arrays of `Operator`.
    public init(arrayLiteral elements: Element...) {
        
        table = elements
        
    }
    
    /// Create an empty instance.
    public init() { table = [] }
    
    /// Returns the position immediately after i.
    ///
    /// - SeeAlso: `IndexableBase` protocol.
    public func index(after i: OperatorTable.Index) -> OperatorTable.Index {
        
        return table.index(after: i)
        
    }
    
    /// Build an expression parser for terms returned by `combined` with
    /// operators from `self`, taking the associativity and precedence specified
    /// in `self` into account. Prefix and postfix operators of the same
    /// precedence can only occur once (i.e. `--2` is not allowed if `-` is
    /// prefix negate). Prefix and postfix operators of the same precedence
    /// associate to the left (i.e. if `++` is postfix increment, than `-2++`
    /// equals `-1`, not `-3`).
    ///
    /// It takes care of all the complexity involved in building expression
    /// parser. Here is an example of an expression parser that handles prefix
    /// signs, postfix increment and basic arithmetic:
    ///
    ///     func binary(
    ///         name: String,
    ///         function: (Int, Int) -> Int,
    ///         assoc: Associativity
    ///     ) -> Operator<String, (), Int> {
    ///
    ///         let opParser = StringParser.string(name) *>
    ///             GenericParser(result: function)
    ///
    ///         return .Infix(opParser, assoc)
    ///
    ///     }
    ///
    ///     func prefix(
    ///         name: String,
    ///         function: Int -> Int
    ///     ) -> Operator<String, (), Int> {
    ///
    ///         let opParser = StringParser.string(name) *>
    ///             GenericParser(result: function)
    ///
    ///         return .Prefix(opParser)
    ///
    ///     }
    ///
    ///     func postfix(
    ///         name: String,
    ///         function: Int -> Int
    ///     ) -> Operator<String, (), Int> {
    ///
    ///         let opParser = StringParser.string(name) *>
    ///             GenericParser(result: function)
    ///
    ///         return .Postfix(opParser.attempt)
    ///
    ///     }
    ///
    ///     let opTable: OperatorTable<String, (), Int> = [
    ///
    ///         [
    ///             prefix("-", function: -),
    ///             prefix("+", function: { $0 })
    ///         ],
    ///         [
    ///             binary("^", function: power, assoc: .right)
    ///         ],
    ///         [
    ///             binary("*", function: *, assoc: .left),
    ///             binary("/", function: /, assoc: .left)
    ///         ],
    ///         [
    ///             binary("+", function: +, assoc: .left),
    ///             binary("-", function: -, assoc: .left)
    ///         ]
    ///
    ///     ]
    ///
    ///     let openingParen = StringParser.character("(")
    ///     let closingParen = StringParser.character(")")
    ///     let decimal = GenericTokenParser<()>.decimal
    ///
    ///     let expression = opTable.makeExpressionParser { expression in
    ///
    ///         expression.between(openingParen, closingParen) <|>
    ///             decimal <?> "simple expression"
    ///
    ///     } <?> "expression"
    ///
    /// - parameters:
    ///   - combine: A function receiving a 'simple expression' as
    ///     parameter that can be nested in other expressions.
    ///   - expression: A parser that can be combined with other expressions.
    /// - returns: An expression parser for terms returned by `combined`
    ///   with operators from `self`.
    /// - SeeAlso:
    ///   `GenericParser.recursive(
    ///       combine: GenericParser -> GenericParser
    ///   ) -> GenericParser
    public func makeExpressionParser(
        _ combine: (
            _ expression: GenericParser<StreamType, UserState, Result>
        ) -> GenericParser<StreamType, UserState, Result>
    ) -> GenericParser<StreamType, UserState, Result> {
        
        var term: GenericParser<StreamType, UserState, Result>!
        let lazyTerm = GenericParser<StreamType, UserState, Result> { term }
        
        let expr = reduce(lazyTerm) { buildParser($0, operators: $1) }
        term = combine(expr)
        
        return expr
        
    }
    
    private typealias InfixOperatorParser =
        GenericParser<StreamType, UserState, (Result, Result) -> Result>
    private typealias PrefixOperatorParser =
        GenericParser<StreamType, UserState, (Result) -> Result>
    private typealias PostfixOperatorParser =
        GenericParser<StreamType, UserState, (Result) -> Result>
    
    private typealias OperatorsTuple = (
        right: [InfixOperatorParser],
        left: [InfixOperatorParser],
        none: [InfixOperatorParser],
        prefix: [PrefixOperatorParser],
        postfix: [PostfixOperatorParser]
    )
    
    private func buildParser(
        _ term: GenericParser<StreamType, UserState, Result>,
        operators: [Operator<StreamType, UserState, Result>]
    ) -> GenericParser<StreamType, UserState, Result> {
        
        let ops: OperatorsTuple = operators.reduce(
            ([], [], [], [], []),
            splitOperators
        )
        
        let rightAssocOp = GenericParser.choice(ops.right)
        let leftAssocOp = GenericParser.choice(ops.left)
        let nonAssocOp = GenericParser.choice(ops.none)
        let prefixOp = GenericParser.choice(ops.prefix)
        let postfixOp = GenericParser.choice(ops.postfix)
        
        let rightAssocMsg = LocalizedString("right")
        let ambigiousRight = ambigious(rightAssocOp, assoc: rightAssocMsg)
        
        let leftAssocMsg = LocalizedString("left")
        let ambigiousLeft = ambigious(leftAssocOp, assoc: leftAssocMsg)
        
        let nonAssocMsg = LocalizedString("non")
        let ambigiousNon = ambigious(rightAssocOp, assoc: nonAssocMsg)
        
        let prefixParser = prefixOp <|> GenericParser(result: { $0 })
        let postfixParser = postfixOp <|> GenericParser(result: { $0 })
        
        let termParser = prefixParser >>- { pre in
            
            term >>- { t in
                
                postfixParser >>- { post in
                    
                    GenericParser(result: post(pre(t)))
                    
                }
                
            }
            
        }
        
        func rightAssocParser(
            _ left: Result
        ) -> GenericParser<StreamType, UserState, Result> {
            
            let rightTerm = termParser >>- { rightAssocParser1($0) }
            
            let apply = rightAssocOp >>- { f in
                
                rightTerm >>- { right in GenericParser(result: f(left, right)) }
                
            }
            
            return apply <|> ambigiousLeft <|> ambigiousNon
            
        }
        
        func rightAssocParser1(
            _ right: Result
        ) -> GenericParser<StreamType, UserState, Result> {
            
            return rightAssocParser(right) <|> GenericParser(result: right)
            
        }
        
        func leftAssocParser(
            _ left: Result
        ) -> GenericParser<StreamType, UserState, Result> {
            
            let apply = leftAssocOp >>- { f in
                
                termParser >>- { right in
                    
                    leftAssocParser1(f(left, right))
                
                }
                
            }
            
            return apply <|> ambigiousRight <|> ambigiousNon
                
        }
        
        func leftAssocParser1(
            _ right: Result
        ) -> GenericParser<StreamType, UserState, Result> {
            
            return leftAssocParser(right) <|> GenericParser(result: right)
            
        }
        
        func nonAssocParser(
            _ left: Result
        ) -> GenericParser<StreamType, UserState, Result> {
            
            return nonAssocOp >>- { f in
                
                termParser >>- { right in
                    
                    ambigiousRight <|> ambigiousLeft <|> ambigiousNon <|>
                        GenericParser(result: f(left, right))
                    
                }
                
            }
            
        }
        
        return termParser >>- { t in
            
            rightAssocParser(t) <|> leftAssocParser(t) <|> nonAssocParser(t) <|>
                GenericParser(result: t) <?> LocalizedString("operator")
            
        }
        
    }
    
    private func splitOperators(
        operators: OperatorsTuple,
        op: Operator<StreamType, UserState, Result>
    ) -> OperatorsTuple {
        
        var ops = operators
        
        switch op {
            
        case .infix(let parser, let assoc):
            
            switch assoc {
                
            case .none:
                
                var n = ops.none
                n.append(parser)
                
                ops.none = n
                
            case .left:
                
                var l = ops.left
                l.append(parser)
                
                ops.left = l
                
            case .right:
                
                var r = ops.right
                r.append(parser)
                
                ops.right = r
                
            }
            
        case .prefix(let parser):
            
            var pre = ops.prefix
            pre.append(parser)
            
            ops.prefix = pre
            
        case .postfix(let parser):
            
            var post = ops.postfix
            post.append(parser)
            
            ops.postfix = post
            
        }
        
        return ops
        
    }
    
    private func ambigious(
        _ op: InfixOperatorParser,
        assoc: String
    ) -> GenericParser<StreamType, UserState, Result> {
        
        #if _runtime(_ObjC)
        
        let msg = LocalizedString("ambiguous use of a %@ associative operator")
        let localizedMsg = String.localizedStringWithFormat(msg, assoc as CVarArg)
        
        #else
        
        // https://bugs.swift.org/browse/SR-957
        let localizedMsg = LocalizedString("ambiguous use of a \(assoc) associative operator")
        
        #endif
        
        return (op *> GenericParser.fail(localizedMsg)).attempt
        
    }
    
    /// Replace the given subRange of elements with newElements.
    ///
    /// - parameters:
    ///   - subRange: Range of elements to replace.
    ///   - newElements: New elements replacing the previous elements contained
    ///     in `subRange`.
    public mutating func replaceSubrange<C: Collection>(
        _ subrange: Range<Index>, with newElements: C
    ) where C.Iterator.Element == Iterator.Element {
        
        table.replaceSubrange(subrange, with: newElements)
        
    }
    
    public subscript(position: Index) -> Element { return table[position] }
    
}


================================================
FILE: Sources/SwiftParsec/GenericParser.swift
================================================
//==============================================================================
// Primitive.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-09-04.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// The primitive parser combinators.
//==============================================================================

//==============================================================================
/// `GenericParser` is a generic implementation of the `Parsec`.
///
/// - requires: StreamType.Iterator has to be a value type.
public final class GenericParser<StreamType: Stream, UserState, Result>:
Parsec {
    
    /// Create a parser containing the injected result.
    ///
    /// - parameter result: The result to inject into the parser.
    public init(result: Result) {
        
        parse = { state in
            
            .none(.ok(result, state,
                      ParseError.unknownParseError(state.position)))
            
        }
        
    }
    
    /// Create a parser containing a function that return a parser. Used to
    /// execute functions lazily.
    ///
    /// - parameter function: The function to execute when the parser is run.
    public init(function: @escaping () -> GenericParser) {
        
        parse = { state in
            
            return function().parse(state)
            
        }
        
    }
    
    /// Create an instance with the given parse function.
    init(
        parse: @escaping (
            ParserState<StreamType.Iterator, UserState>
        ) -> Consumed<StreamType, UserState, Result>
    ) {
        
        self.parse = parse
        
    }
    
    /// The function executed when the parser is run.
    ///
    /// - Parameter state: The state of the parser.
    /// - returns: The result of the parsing.
    let parse: (_ state: ParserState<StreamType.Iterator, UserState>)
    -> Consumed<StreamType, UserState, Result>
    
    /// Return a parser containing the result of mapping transform over `self`.
    ///
    /// This method has the synonym infix operator `<^>`.
    ///
    /// - parameter transform: A mapping function.
    /// - returns: A new parser with the mapped content.
    public func map<T>(
        _ transform: @escaping (Result) -> T
    ) -> GenericParser<StreamType, UserState, T> {
        
        return GenericParser<StreamType, UserState, T>(parse: { state in
            
            let consumed = self.parse(state)
            return consumed.map(transform)
            
        })
        
    }
    
    /// Return a parser by applying the function contained in the supplied
    /// parser to self.
    ///
    /// This method has the synonym infix operator `<*>`.
    ///
    /// - parameter parser: The parser containing the function to apply to self.
    /// - returns: A parser with the applied function.
    public func apply<T>(
        _ parser: GenericParser<StreamType, UserState, (Result) -> T>
    ) -> GenericParser<StreamType, UserState, T> {
        
        return parser >>- { f in self.map(f) }
        
    }
    
    /// This combinator implements choice. The parser `p.alternative(q)` first
    /// applies `p`. If it succeeds, the value of `p` is returned. If `p` fails
    /// _without consuming any input_, parser `q` is tried. The parser is called
    /// _predictive_ since `q` is only tried when parser `p` didn't consume any
    /// input (i.e.. the look ahead is 1). This non-backtracking behaviour
    /// allows for both an efficient implementation of the parser combinators
    /// and the generation of good error messages.
    ///
    /// This method has the synonym infix operator `<|>`.
    ///
    /// - parameter altParser: The alternative parser to try if `self` fails.
    /// - returns: A parser that will first try `self`. If it consumed no input,
    ///   it will try `altParser`.
    public func alternative(_ altParser: GenericParser) -> GenericParser {
        
        return GenericParser(parse: { state in
            
            let consumed = self.parse(state)
            guard case .none(let reply) = consumed,
                case .error(let error) = reply else {
                
                return consumed
                
            }
            
            let altConsumed = altParser.parse(state)
            switch altConsumed {
                
            case .some: return altConsumed
                
            case .none(let reply):
                
                return .none(reply.mergeParseError(error))
                
            }
            
        })
        
    }
    
    /// Return a parser containing the result of mapping transform over `self`.
    ///
    /// This method has the synonym infix operator `>>-` (bind).
    ///
    /// - parameter transform: A mapping function returning a parser.
    /// - returns: A new parser with the mapped content.
    public func flatMap<T>(
        _ transform: @escaping (
            Result
        ) -> GenericParser<StreamType, UserState, T>
    ) -> GenericParser<StreamType, UserState, T> {
        
        func runRightParser(
            _ constructor: (
                ParserReply<StreamType, UserState, T>
            ) -> Consumed<StreamType, UserState, T>,
            result: Result,
            state: ParserState<StreamType.Iterator, UserState>,
            error: ParseError
        ) -> Consumed<StreamType, UserState, T> {
            
            let parser = transform(result)
            
            let consumed = parser.parse(state)
            switch consumed {
                
            // If parser consumes, return the result right away.
            case .some: return consumed
                
            case .none(let reply):
                
                // If the left parser consumes and the right parser doesn't
                // consume input, but is okay, we return that it successfully
                // consumed some input. But if the left and right parser didn't
                // consume we return that it successfully didn't consumed some
                // input.
                //
                // If the left parser consumes and the right parser doesn't
                // consume input, but errors, we return that it failed while
                // consuming some input. But if the left and right parser didn't
                // consume we return that it failed while not consuming any
                // input.
                return constructor(reply.mergeParseError(error))
                
            }
            
        }
        
        func consumed(
            _ constructor: (
                ParserReply<StreamType, UserState, T>
            ) -> Consumed<StreamType, UserState, T>,
            reply: ParserReply<StreamType, UserState, Result>
        ) -> Consumed<StreamType, UserState, T> {
            
            switch reply {
                
            case .ok(let result, let state, let error):
                
                return runRightParser(constructor, result: result, state: state,
                                      error: error)
                
            case .error(let error):
                
                return constructor(.error(error))
                
            }
            
        }
        
        return GenericParser<StreamType, UserState, T>(parse: { state in
            
            switch self.parse(state) {
                
            case .some(let reply):
                
                return consumed(Consumed.some, reply: reply)
                
            case .none(let reply):
                
                return consumed(Consumed.none, reply: reply)
                
            }
            
        })
        
    }
    
    /// This combinator is used whenever arbitrary look ahead is needed. Since
    /// it pretends that it hasn't consumed any input when `self` fails, the
    /// ('<|>') combinator will try its second alternative even when the first
    /// parser failed while consuming input.
    ///
    /// The `attempt` combinator can for example be used to distinguish
    /// identifiers and reserved words. Both reserved words and identifiers are
    /// a sequence of letters. Whenever we expect a certain reserved word where
    /// we can also expect an identifier we have to use the `attempt`
    /// combinator. Suppose we write:
    ///
    ///     let letExpr = StringParser.string("let")
    ///     let identifier = letter.many1
    ///
    ///     let expr = letExpr <|> identifier <?> "expression"
    ///
    /// If the user writes \"lexical\", the parser fails with: _unexpected 'x',
    /// expecting 't' in "let"_. Indeed, since the ('<|>') combinator only tries
    /// alternatives when the first alternative hasn't consumed input, the
    /// `identifier` parser is never tried (because the prefix "le" of the
    /// `string("let")` parser is already consumed). The right behaviour can be
    /// obtained by adding the `attempt` combinator:
    ///
    ///     let letExpr = StringParser.string("let")
    ///     let identifier = StringParser.letter.many1
    ///
    ///     let expr = letExpr.attempt <|> identifier <?> "expression"
    ///
    /// - returns: A parser that pretends that it hasn't consumed any input when
    ///   `self` fails.
    public var attempt: GenericParser {
        
        return GenericParser(parse: { state in
            
            let consumed = self.parse(state)
            if case .some(let reply) = consumed, case .error = reply {
                
                return .none(reply)
                
            }
            
            return consumed
            
        })
        
    }
    
    /// A combinator that parses without consuming any input.
    ///
    /// If `self` fails and consumes some input, so does `lookAhead`. Combine
    /// with `attempt` if this is undesirable.
    ///
    /// - returns: A parser that parses without consuming any input.
    public var lookAhead: GenericParser {
        
        return GenericParser(parse: { state in
            
            let consumed = self.parse(state)
            
            if case .some(let reply) = consumed,
            case .ok(let result, _, _) = reply {
                
                return .none(
                    .ok(result,
                        state,
                        ParseError.unknownParseError(state.position)
                    )
                )
                
            }
            
            return consumed
            
        })
        
    }
    
    /// The `many` combinator applies the parser `self` _zero_ or more times. It
    /// returns an array of the returned values of `self`.
    ///
    ///     let identifier = identifierStart >>- { char in
    ///
    ///         identifierLetter.many >>- { (var chars) in
    ///
    ///             chars.insert(char, at: 0)
    ///             return GenericParser(result: String(chars))
    ///
    ///         }
    ///
    ///     }
    public var many: GenericParser<StreamType, UserState, [Result]> {
        
        return manyAccumulator { (result, results) in
            
            return results.appending(result)
            
        }
        
    }
    
    /// The `skipMany` combinator applies the parser `self` _zero_ or more
    /// times, skipping its result.
    ///
    ///     let spaces = space.skipMany
    ///
    /// - returns: An parser with an empty result.
    public var skipMany: GenericParser<StreamType, UserState, ()> {
        
        let manyAcc = manyAccumulator { (_, accum: [Result]) in accum }
        return manyAcc.map { _ in () }
        
    }
    
    /// This combinator applies `self` _zero_ or more times. It returns an
    /// accumulation of the returned values of `self` that were passed to the
    /// `accumulator` function.
    ///
    /// - parameter accumulator: An accumulator function that process the value
    ///   returned by `self`. The first argument is the value returned by `self`
    ///   and the second argument is the previous processed values returned by
    ///   this accumulator function. It returns the result of processing the
    ///   passed value and the accumulated values.
    /// - returns: The processed values of the accumulator function.
    public func manyAccumulator<Accumulator: EmptyInitializable>(
        _ accumulator: @escaping (Result, Accumulator) -> Accumulator
    ) -> GenericParser<StreamType, UserState, Accumulator> {
        
        return GenericParser<StreamType, UserState, Accumulator>(parse:
        { initState in
            
            var results = Accumulator()
            var newState = initState
            
            var hasConsumed = false
            
            repeat {
                
                let consumed = self.parse(newState)
                switch consumed {
                    
                case .some(let reply):
                    
                    switch reply {
                        
                    case .ok(let result, let state, _):
                        
                        results = accumulator(result, results)
                        newState = state
                        
                    case .error(let error):
                        
                        return .some(.error(error))
                        
                    }
                    
                case .none(let reply):
                    
                    switch reply {
                        
                    case .ok:
            
                        let failureMsg = LocalizedString("Combinator 'many' is applied to a parser that accepts an empty string.")
                        assertionFailure(failureMsg)
                        
                    case .error(let error):
                        
                        if hasConsumed {
                            
                            return .some(.ok(results, newState, error))
                            
                        }
            
                        return .none(.ok(results, newState, error))
                        
                    }
            
                }
                
                hasConsumed = true
                
            } while true // Loop while the parser consumes.
            
        })
        
    }
    
    /// A parser that always fails without consuming any input.
    public static var empty: GenericParser {
        
        return GenericParser(parse: { state in
            
            let position = state.position
            return .none(.error(ParseError.unknownParseError(position)))
            
        })
        
    }
    
    /// The parser returned by `p.labels(message)` behaves as parser `p`, but
    /// whenever the parser `p` fails _without consuming any input_, it replaces
    /// expected error messages with the expected error message `message`.
    ///
    /// This is normally used at the end of a set alternatives where we want to
    /// return an error message in terms of a higher level construct rather than
    /// returning all possible characters. For example, if the `expr` parser
    /// from the `attempt` example would fail, the error message is: '...:
    /// expecting expression'. Without the `GenericParser.labels()` combinator,
    /// the message would be like '...: expecting "let" or "letter"', which is
    /// less friendly.
    ///
    /// This method has the synonym infix operator `<?>`.
    ///
    /// - parameter message: The new error message.
    /// - returns: A parser with a replaced error message.
    public func labels(_ messages: String...) -> GenericParser {
        
        return GenericParser(parse: { state in
            
            let consumed = self.parse(state)
            switch consumed {
                
            case .some: return consumed
                
            case .none(let reply):
                
                switch reply {
                    
                case .ok(let result, let state, var error):
                    
                    if !error.isUnknown {
                        
                        error.insertLabelsAsExpected(messages)
                        
                    }
                    
                    return .none(.ok(result, state, error))
                    
                case .error(var error):
                    
                    error.insertLabelsAsExpected(messages)
                    return .none(.error(error))
                    
                }
                
            }
            
        })
        
    }
    
    /// Return a parser that always fails with an unexpected error message
    /// without consuming any input.
    ///
    /// The parsers 'fail', '\<?\>' and `unexpected` are the three parsers used
    /// to generate error messages. Of these, only '<?>' is commonly used. For
    /// an example of the use of `unexpected`, see the definition of
    /// `GenericParser.noOccurence`.
    ///
    /// - parameter message: The error message.
    /// - returns: A parser that always fails with an unexpected error message
    ///   without consuming any input.
    /// - SeeAlso: `GenericParser.noOccurence`,
    ///   `GenericParser.fail(message: String)` and `<?>`
    public static func unexpected(_ message: String) -> GenericParser {
        
        return GenericParser { state in

            .none(
                .error(
                    ParseError(
                        position: state.position,
                        messages: [.unexpected(message)]
                    )
                )
            )
            
        }
        
    }
    
    /// Return a parser that always fail with the supplied message.
    ///
    /// - parameter message: The failure message.
    /// - returns: A parser that always fail.
    public static func fail(_ message: String) -> GenericParser {
        
        return GenericParser(parse: { state in
            
            let position = state.position
            let error = ParseError(
                position: position,
                messages: [.generic(message)]
            )
            
            return .none(.error(error))
            
        })
        
    }
    
    /// Return the current source position.
    ///
    /// - returns: The current source position.
    /// - SeeAlso 'SourcePosition'.
    public static var
    sourcePosition: GenericParser<StreamType, UserState, SourcePosition> {
        
        return GenericParser<StreamType, UserState, SourcePosition>(parse:
        { state in
            
            return .none(.ok(state.position, state,
                             ParseError.unknownParseError(state.position)))
            
        })
        
    }
    
    /// Return a parser that applies the result of the supplied parsers to the
    /// lifted function. The parsers are applied from left to right.
    ///
    /// - parameters:
    ///   - function: The Binary function to lift into the parser.
    ///   - parser1: The parser returning the first argument passed to the
    ///     lifted function.
    ///   - parser2: The parser returning the second argument passed to the
    ///     lifted function.
    /// - returns: A parser that applies the result of the supplied parsers to
    ///   the lifted function.
    public static func lift2<Param1, Param2>(
        _ function: @escaping (Param1, Param2) -> Result,
        parser1: GenericParser<StreamType, UserState, Param1>,
        parser2: GenericParser<StreamType, UserState, Param2>
    ) -> GenericParser {
        
        return parser1 >>- { result1 in
            
            parser2 >>- { result2 in
                
                let combinedResult = function(result1, result2)
                return GenericParser(result: combinedResult)
                
            }
            
        }
        
    }
    
    /// Return a parser that applies the result of the supplied parsers to the
    /// lifted function. The parsers are applied from left to right.
    ///
    /// - parameters:
    ///   - function: The Ternary function to lift into the parser.
    ///   - parser1: The parser returning the first argument passed to the
    ///     lifted function.
    ///   - parser2: The parser returning the second argument passed to the
    ///     lifted function.
    ///   - parser3: The parser returning the third argument passed to the
    ///     lifted function.
    /// - returns: A parser that applies the result of the supplied parsers to
    ///   the lifted function.
    public static func lift3<Param1, Param2, Param3>(
        _ function: @escaping (Param1, Param2, Param3) -> Result,
        parser1: GenericParser<StreamType, UserState, Param1>,
        parser2: GenericParser<StreamType, UserState, Param2>,
        parser3: GenericParser<StreamType, UserState, Param3>
    ) -> GenericParser {
        
        return parser1 >>- { result1 in
            
            parser2 >>- { result2 in
                
                parser3 >>- { result3 in
                    
                    let combinedResult = function(result1, result2, result3)
                    return GenericParser(result: combinedResult)
                    
                }

            }
            
        }

    }
    
    /// Return a parser that applies the result of the supplied parsers to the
    /// lifted function. The parsers are applied from left to right.
    ///
    /// - parameters:
    ///   - function: The function to lift into the parser.
    ///   - parser1: The parser returning the first argument passed to the
    ///     lifted function.
    ///   - parser2: The parser returning the second argument passed to the
    ///     lifted function.
    ///   - parser3: The parser returning the third argument passed to the
    ///     lifted function.
    ///   - parser4: The parser returning the fourth argument passed to the
    ///     lifted function.
    /// - returns: A parser that applies the result of the supplied parsers to
    ///   the lifted function.
    public static func lift4<Param1, Param2, Param3, Param4>(
        _ function: @escaping (Param1, Param2, Param3, Param4) -> Result,
        parser1: GenericParser<StreamType, UserState, Param1>,
        parser2: GenericParser<StreamType, UserState, Param2>,
        parser3: GenericParser<StreamType, UserState, Param3>,
        parser4: GenericParser<StreamType, UserState, Param4>
    ) -> GenericParser {
        
        return parser1 >>- { result1 in
            
            parser2 >>- { result2 in
                
                parser3 >>- { result3 in
                    
                    parser4 >>- { result4 in
                        
                        let combinedResult = function(
                            result1,
                            result2,
                            result3,
                            result4
                        )
                        return GenericParser(result: combinedResult)
                        
                    }

                }
                
            }
            
        }

    }
    
    /// Return a parser that applies the result of the supplied parsers to the
    /// lifted function. The parsers are applied from left to right.
    ///
    /// - parameters:
    ///   - function: The function to lift into the parser.
    ///   - parser1: The parser returning the first argument passed to the
    ///     lifted function.
    ///   - parser2: The parser returning the second argument passed to the
    ///     lifted function.
    ///   - parser3: The parser returning the third argument passed to the
    ///     lifted function.
    ///   - parser4: The parser returning the fourth argument passed to the
    ///     lifted function.
    ///   - parser5: The parser returning the fifth argument passed to the
    ///     lifted function.
    /// - returns: A parser that applies the result of the supplied parsers to
    ///   the lifted function.
    public static func lift5<Param1, Param2, Param3, Param4, Param5>(
        _ function: @escaping (Param1, Param2, Param3, Param4, Param5) -> Result,
        parser1: GenericParser<StreamType, UserState, Param1>,
        parser2: GenericParser<StreamType, UserState, Param2>,
        parser3: GenericParser<StreamType, UserState, Param3>,
        parser4: GenericParser<StreamType, UserState, Param4>,
        parser5: GenericParser<StreamType, UserState, Param5>
    ) -> GenericParser {
        
        return parser1 >>- { result1 in
            
            parser2 >>- { result2 in
                
                parser3 >>- { result3 in
                    
                    parser4 >>- { result4 in
                        
                        parser5 >>- { result5 in
                            
                            let combinedResult = function(
                                result1,
                                result2,
                                result3,
                                result4,
                                result5
                            )
                            return GenericParser(result: combinedResult)
                            
                        }
                        
                    }
                    
                }
                
            }
            
        }

    }
    
    /// Return the user state.
    ///
    /// - returns: The user state
    static public var
    userState: GenericParser<StreamType, UserState, UserState> {
        
        return GenericParser<StreamType, UserState, UserState>(parse: { state in
            
            return .none(.ok(state.userState, state,
                             ParseError.unknownParseError(state.position)))
            
        })
        
    }

    /// The `updateUserState` method applies the function `update` to the user
    /// state. Suppose that we want to count identifiers in a source, we could
    /// use the user state as:
    ///
    ///     let incrementCount = StringParser.updateUserState { ++$0 }
    ///     let expr = identifier <* incrementCount
    ///
    /// - parameter update: The function applied to the `UserState`. It returns
    ///   the updated `UserState`.
    /// - returns: An empty parser that will update the `UserState`.
    public static func updateUserState(
        _ update: @escaping (UserState) -> UserState
    ) -> GenericParser<StreamType, UserState, ()> {
        
        return GenericParser<StreamType, UserState, ()>(parse: { parserState in
            
            let userState = update(parserState.userState)
            
            var state = parserState
            state.userState = userState
            
            let position = state.position
            
            return .none(.ok((), state, ParseError.unknownParseError(position)))
            
        })
        
    }
    
    /// Run the parser and return the result of the parsing if it succeeded.
    /// If an error occured, it is returned. Contrary to the `run()` method, it
    /// doesn't throw an exception.
    ///
    /// - parameters:
    ///   - userState: The state supplied by the user.
    ///   - sourceName: The name of the source (i.e. file name).
    ///   - input: The input StreamType to parse.
    /// - returns: The result of the parsing on success, otherwise the parse
    ///   error.
    public func runSafe(
        userState: UserState,
        sourceName: String,
        input: StreamType
    ) -> Either<ParseError, Result> {
        
        let position = SourcePosition(name: sourceName, line: 1, column: 1)
        let state = ParserState(
            input: input.makeIterator(),
            position: position,
            userState: userState
        )
        
        let reply = parse(state).parserReply
        switch reply {
            
        case .ok(let result, _, _):
            
            return .right(result)
            
        case .error(let error):
            
            return .left(error)
            
        }

    }
    
}

//==============================================================================
// Parsec extension
public extension Parsec {
    
    // TODO: Move this function into the `Parsec` protocol extension when Swift
    // will allow to add requirements to `associatedtype` type constraint
    // (Ex.: `associatedtype StreamType: CollectionType
    // where StreamType.SubSequence == Stream`)
    
    /// Return a parser that accepts a token `Element` with `Result` when the
    /// function `match(Element) -> Result` returns
    /// `Optional.SomeWrapped(Result)`. The token can be shown using
    /// `tokenDescription(Element) -> String`. The position of the _next_ token
    /// should be returned when
    /// `nextPosition(SourcePosition, Element, StreamType) -> SourcePosition`
    /// is called with the current source position, the current token and the
    /// rest of the tokens.
    ///
    /// This is the most primitive combinator for accepting tokens. For example,
    /// the `GenericParser.character()` parser could be implemented as:
    ///
    ///     public static func character(
    ///         char: Character
    ///     ) -> GenericParser<StreamType, UserState, Result> {
    ///
    ///         return tokenPrimitive(
    ///             tokenDescription: { "\"" + $0 + "\"" },
    ///             nextPosition: { (var position, elem, _) in
    ///
    ///                 position.updatePosition(elem)
    ///                 return position
    ///
    ///             },
    ///             match: { elem in
    ///
    ///                 char == elem ? elem : nil
    ///
    ///             })
    ///
    ///     }
    ///
    /// - parameters:
    ///   - tokenDescription: A function to describe the token.
    ///   - nextPosition: A function returning the position of the next token.
    ///   - match: A function returning an optional result when the token match
    ///     a predicate.
    /// - returns: Return a parser that accepts a token `Element` with result
    ///   `Result` when the token matches.
    static func tokenPrimitive(
        tokenDescription: @escaping (StreamType.Iterator.Element) -> String,
        nextPosition: @escaping (
            SourcePosition, StreamType.Iterator.Element
        ) -> SourcePosition,
        match: @escaping (StreamType.Iterator.Element) -> Result?
    ) -> GenericParser<StreamType, UserState, Result> {
        
        return GenericParser(parse: { state in
            
            var input = state.input
            let position = state.position
            
            guard let tok = input.next() else {
                
                let error =
                    ParseError.unexpectedParseError(position, message: "")
                return .none(.error(error))
                
            }
            
            guard let result = match(tok) else {
                
                let error = ParseError.unexpectedParseError(
                    position,
                    message: tokenDescription(tok)
                )
                return .none(.error(error))
                
            }
            
            let newPosition = nextPosition(position, tok)
            let newState = ParserState(
                input: input,
                position: newPosition,
                userState: state.userState
            )
            let unknownError = ParseError.unknownParseError(newPosition)
            
            return .some(.ok(result, newState, unknownError))
            
        })
        
    }
    
}

//==============================================================================
// Parsec extension where the elements are `Equatable`
public extension Parsec
where StreamType.Iterator.Element: Equatable {
    
    // TODO: Move this function into the `Parsec` protocol extension when Swift
    // will allow to add requirements to `associatedtype` type constraint
    // (Ex.: `associatedtype StreamType: CollectionType where
    // StreamType.SubSequence == Stream`)
    
    /// Return a parser that parses a collection of tokens.
    ///
    /// - parameters:
    ///   - tokensDescription: A function to describe the tokens.
    ///   - nextPosition: A function returning the position after the tokens.
    ///   - tokens: The collection of tokens to parse.
    /// - returns: A parser that parses a collection of tokens.
    static func tokens(
        tokensDescription: @escaping (StreamType) -> String,
        nextPosition: @escaping (
            SourcePosition, StreamType
        ) -> SourcePosition,
        tokens: StreamType
    ) -> GenericParser<StreamType, UserState, StreamType> {
        
        return GenericParser(parse: { state in
            
            let position = state.position
            
            var tokensIterator = tokens.makeIterator()
            var token = tokensIterator.next()
            
            guard token != nil else {
                
                let error = ParseError.unknownParseError(position)
                return .none(.ok([], state, error))
                
            }
            
            var input = state.input
            
            var hasConsumed = false
            var consumedConstructor =
                Consumed<StreamType, UserState, StreamType>.none
            
            repeat {
                
                guard let inputToken = input.next() else {
                    
                    var eofError =
                        ParseError.unexpectedParseError(position, message: "")
                    eofError.insertMessage(.expected(tokensDescription(tokens)))
                    
                    return consumedConstructor(.error(eofError))
                    
                }
                
                if token! != inputToken {
                    
                    let tokDesc = tokensDescription([inputToken])
                    
                    var expectedError = ParseError.unexpectedParseError(
                        position,
                        message: tokDesc
                    )
                    
                    let expected = Message.expected(tokensDescription(tokens))
                    expectedError.insertMessage(expected)
                    
                    return consumedConstructor(.error(expectedError))
                    
                }
                
                if !hasConsumed {
                    
                    hasConsumed = true
                    consumedConstructor = Consumed.some
                    
                }
                
                token = tokensIterator.next()
                
            } while token != nil
            
            let newPosition = nextPosition(position, tokens)
            let newState = ParserState(
                input: input,
                position: newPosition,
                userState: state.userState
            )
            let error = ParseError.unknownParseError(newPosition)
            
            return .some(.ok(tokens, newState, error))
            
        })
        
    }
    
}

//==============================================================================
/// The `Consumed` enumeration indicates if a parser consumed some or none from
/// an input.
enum Consumed<StreamType: Stream, UserState, Result> {
    
    /// Indicates that some of the input was consumed.
    case some(ParserReply<StreamType, UserState, Result>)
    
    /// Indicates that none of the input was consumed.
    case none(ParserReply<StreamType, UserState, Result>)
    
    /// The `ParserReply` either from `.some` or `.none`.
    var parserReply: ParserReply<StreamType, UserState, Result> {
        
        switch self {
            
        case .some(let reply): return reply
            
        case .none(let reply): return reply
            
        }
        
    }
    
    /// Return a `Consumed` enumeration containing the result of mapping
    /// transform over the result of the `ParserReply`. In other words it calls
    /// `map` on the parser reply's result.
    ///
    /// - parameter transform: A mapping function.
    /// - returns: A new `Consumed` enumeration with the mapped content.
    func map<T>(
        _ transform: (Result) -> T
    ) -> Consumed<StreamType, UserState, T> {
        
        switch self {
            
        case .some(let reply):
            
            return .some(reply.map(transform))
            
        case .none(let reply):
            
            return .none(reply.map(transform))
        }
        
    }
    
}

//==============================================================================
/// The `ParserReply` enumeration indicates the result of a parse.
enum ParserReply<StreamType: Stream, UserState, Result> {
    
    /// Indicates that the parsing was successfull. It contains a `Result` type,
    /// the `ParserState` and a `ParseError` as associated values.
    case ok(Result, ParserState<StreamType.Iterator, UserState>, ParseError)
    
    /// Indicates that the parsing failed. It contains a `ParseError` as an
    /// associated value.
    case error(ParseError)
    
    /// Return a `ParserReply` enumeration containing the result of mapping
    /// transform over `self`.
    ///
    /// - parameter transform: A mapping function.
    /// - returns: A new `ParserReply` enumeration with the mapped content.
    func map<T>(
        _ transform: (Result) -> T
    ) -> ParserReply<StreamType, UserState, T> {
        
        switch self {
            
        case .ok(let result, let state, let error):
            
            return .ok(transform(result), state, error)
            
        case .error(let error): return .error(error)
            
        }
        
    }
    
    /// Merge the `ParseError` contained by self with the supplied `ParseError`.
    ///
    /// - parameter otherError: The other error to merge with the error
    ///   contained by `self`.
    /// - returns: A new `ParserReply` with the errors merged.
    func mergeParseError(_ otherError: ParseError) -> ParserReply {
        
        var mergedError = otherError
        
        switch self {
            
        case .ok(let parserResult, let parserState, let parserError):
            
            mergedError.merge(parserError)
            return .ok(parserResult, parserState, mergedError)
            
        case .error(let parserError):
            
            mergedError.merge(parserError)
            return .error(mergedError)
            
        }
        
    }
    
}

//==============================================================================
/// ParserState contains the state of the parser and the user state.
struct ParserState<StreamTypeIterator, UserState> {
    
    /// The input StreamType of the parser.
    var input: StreamTypeIterator
    
    /// The position in the input StreamType.
    var position: SourcePosition
    
    /// The supplied user state.
    var userState: UserState
    
}

//==============================================================================
// Implementation of different parser operators.

/// Infix operator for `map`. It has the same precedence as the equality
/// operator (`==`).
///
/// - parameters:
///   - transform: A mapping function.
///   - parser: The parser whose result is mapped.
public func <^><StreamType, UserState, Result, T>(
    transform: @escaping (Result) -> T,
    parser: GenericParser<StreamType, UserState, Result>
) -> GenericParser<StreamType, UserState, T> {
    
    return parser.map(transform)
    
}

/// Infix operator for `apply`. It has the same precedence as the equality
/// operator (`==`).
///
/// - parameters:
///   - leftParser: The parser containing the function to apply to the parser on
///     the right.
///   - rightParser: The parser on which the function is applied.
/// - returns: A parser with the applied function.
public func<*><StreamType, UserState, Result, T>(
    leftParser: GenericParser<StreamType, UserState, (Result) -> T>,
    rightParser: GenericParser<StreamType, UserState, Result>
) -> GenericParser<StreamType, UserState, T> {
    
    return rightParser.apply(leftParser)
    
}

/// Sequence parsing, discarding the value of the first parser. It has the same
/// precedence as the equality operator (`==`).
///
/// - parameters:
///   - leftParser: The first parser executed.
///   - rightParser: The second parser executed.
/// - returns: A parser returning the result of the second parser.
public func *><StreamType, UserState, Param1, Param2>(
    leftParser: GenericParser<StreamType, UserState, Param1>,
    rightParser: GenericParser<StreamType, UserState, Param2>
) -> GenericParser<StreamType, UserState, Param2> {
    
    return GenericParser.lift2(
        { $1 },
        parser1: leftParser,
        parser2: rightParser
    )
    
}

/// Sequence parsing, discarding the value of the second parser. It has the same
/// precedence as the equality operator (`==`).
///
/// - parameters:
///   - leftParser: The first parser executed.
///   - rightParser: The second parser executed.
/// - returns: A parser returning the result of the first parser.
public func <*<StreamType, UserState, Param1, Param2>(
    leftParser: GenericParser<StreamType, UserState, Param1>,
    rightParser: GenericParser<StreamType, UserState, Param2>
) -> GenericParser<StreamType, UserState, Param1> {
    
    return GenericParser.lift2(
        { p0, _ in p0 },
        parser1: leftParser,
        parser2: rightParser
    )
    
}

/// Infix operator for `flatMap` named _bind_. It has the same precedence as the
/// `nil` coalescing operator (`??`).
///
/// - parameters:
///   - parser: The parser whose result is passed to the `transform` function.
///   - transform: The function receiving the result of `parser`.
public func >>-<StreamType, UserState, Result, T>(
    parser: GenericParser<StreamType, UserState, Result>,
    transform: @escaping (Result) -> GenericParser<StreamType, UserState, T>
) -> GenericParser<StreamType, UserState, T> {
    
    return parser.flatMap(transform)
    
}


================================================
FILE: Sources/SwiftParsec/GenericTokenParser.swift
================================================
//==============================================================================
// GenericTokenParser.swift
// SwiftParsec
//
// Created by David Dufresne on 2016-10-08.
// Copyright © 2016 David Dufresne. All rights reserved.
//==============================================================================

//==============================================================================
/// Generic implementation of the `TokenParser`.
public struct GenericTokenParser<UserState>: TokenParser {
    
    /// Language definition parameterizing the lexer.
    public let languageDefinition: LanguageDefinition<UserState>
    
    /// Creates a `TokenParser` that contains lexical parsers that are defined
    /// using the definitions in the `LanguageDefinition` structure.
    ///
    /// One uses the appropiate language definition and selects the lexical
    /// parsers that are needed from the resulting `GenericTokenParser`.
    ///
    ///     import SwiftParsec
    ///
    ///     // The lexer
    ///     let swiftDef = LanguageDefinition<()>.swift
    ///     let lexer = GenericTokenParser(languageDefinition: swiftDef)
    ///
    ///     // The parser
    ///     let expression = lexer.identifier <|>
    ///         lexer.legalOperator <|> ...
    ///
    /// - parameter languageDefinition: Language definition for the lexical
    ///   parsers.
    public init(languageDefinition: LanguageDefinition<UserState>) {
        
        self.languageDefinition = languageDefinition
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/Info.plist
================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>en</string>
	<key>CFBundleExecutable</key>
	<string>$(EXECUTABLE_NAME)</string>
	<key>CFBundleIdentifier</key>
	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>$(PRODUCT_NAME)</string>
	<key>CFBundlePackageType</key>
	<string>FMWK</string>
	<key>CFBundleShortVersionString</key>
	<string>$(MARKETING_VERSION)</string>
	<key>CFBundleSignature</key>
	<string>????</string>
	<key>CFBundleVersion</key>
	<string>$(CURRENT_PROJECT_VERSION)</string>
	<key>NSPrincipalClass</key>
	<string></string>
</dict>
</plist>


================================================
FILE: Sources/SwiftParsec/LanguageDefinition.swift
================================================
//==============================================================================
// LanguageDefinition.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-10-14.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// A helper module that defines some language definitions that can be used to
// instantiate a token parser (see "Token").
//==============================================================================

//==============================================================================
/// The `LanguageDefinition` structure contains all parameterizable features of
/// the token parser. There is some default definitions provided by SwiftParsec.
public struct LanguageDefinition<UserState> {
    
    /// Describe the start of a block comment. Use the empty string if the
    /// language doesn't support block comments. For example "/*".
    public var commentStart: String
    
    /// Describe the end of a block comment. Use the empty string if the
    /// language doesn't support block comments. For example "*/".
    public var commentEnd: String
    
    /// Describe the start of a line comment. Use the empty string if the
    /// language doesn't support line comments. For example "//".
    public var commentLine: String
    
    /// Set to `true` if the language supports nested block comments.
    public var allowNestedComments: Bool
    
    /// This parser should accept any start characters of identifiers. For
    /// example `letter <|> character("_")`.
    public var identifierStart: GenericParser<String, UserState, Character>
    
    /// This parser should accept any legal tail characters of identifiers. For
    /// example `alphaNum <|> character("_")`. The function receives the
    /// character parsed by `identifierStart` as parameter, allowing to handle
    /// special cases (i.e. implicit parameters in swift start with a '$' that
    /// must be followed by decimal digits only).
    public var identifierLetter:
    (Character) -> GenericParser<String, UserState, Character>
    
    /// This parser should accept any start characters of operators. For example
    /// `oneOf(":!#$%&*+./<=>?@\\^|-~")`
    public var operatorStart: GenericParser<String, UserState, Character>
    
    /// This parser should accept any legal tail characters of operators. Note
    /// that this parser should even be defined if the language doesn't support
    /// user-defined operators, or otherwise the `reservedOperators` parser
    /// won't work correctly.
    public var operatorLetter: GenericParser<String, UserState, Character>
    
    /// The set of reserved identifiers.
    public var reservedNames: Set<String>
    
    /// The set of reserved operators.
    public var reservedOperators: Set<String>
    
    /// This optional parser should accept escaped characters. This parser will
    /// also replace the string gap and zero-width escape sequence parsers. The
    /// default escape sequences have the following form: '\97' '\x61', '\o141',
    /// '\^@', '\n', \NUL.
    public var characterEscape: GenericParser<String, UserState, Character>?
    
    /// Set to `true` if the language is case sensitive.
    public var isCaseSensitive: Bool
    
}

//==============================================================================
// LanguageDefinition extension containing factory methods to create language
// definitions for different languages.
public extension LanguageDefinition {
    
    /// This is the most minimal token definition. It is recommended to use this
    /// definition as the basis for other definitions. `empty` has no reserved
    /// names or operators, is case sensitive and doesn't accept comments,
    /// identifiers or operators.
    static var empty: LanguageDefinition {
        
        return LanguageDefinition(
            commentStart:        "",
            commentEnd:          "",
            commentLine:         "",
            allowNestedComments: true,
            identifierStart:     GenericParser.letter <|>
                GenericParser.character("_"),
            identifierLetter: { _ in
                GenericParser.alphaNumeric <|> GenericParser.character("_")
            },
            operatorStart:       GenericParser.oneOf(
                emptyOperatorLetterCharacters
            ),
            operatorLetter:      GenericParser.oneOf(
                emptyOperatorLetterCharacters
            ),
            reservedNames:       [],
            reservedOperators:   [],
            characterEscape:     nil,
            isCaseSensitive:     true
        )
        
    }
    
    /// This is a minimal token definition for Java style languages. It defines
    /// the style of comments, valid identifiers and case sensitivity. It does
    /// not define any reserved words or operators.
    static var javaStyle: LanguageDefinition {
        
        var javaDef = empty
        
        javaDef.commentStart = "/*"
        javaDef.commentEnd   = "*/"
        javaDef.commentLine  = "//"
        
        return javaDef
        
    }
    
    // This is a definition for the JSON language-independent data interchange
    // format.
    static var json: LanguageDefinition {
        
        var jsonDef = empty
        
        let charEscParsers: [GenericParser<String, UserState, Character>] =
        jsonEscapeMap.map { escCode in
            
            GenericParser.character(escCode.esc) *>
                GenericParser(result: escCode.code)
            
        }
        
        let charEscape = GenericParser.choice(charEscParsers)
        
        let hexaNum: GenericParser<String, UserState, UInt16> =
        GenericParser.hexadecimalDigit.count(jsonMaxEscapeDigit) >>- { digits in
            
            // The max possible value of `digits` is 0xFFFF, so no possible
            // overflow.
            let integer = UInt16(String(digits), radix: 16)!
            return GenericParser(result: integer)
            
        }
        
        let backslash =
            GenericParser<String, UserState, Character>.character("\\")
        
        let codePoint = GenericParser.character("u") *> hexaNum
        let encodedChar: GenericParser<String, UserState, Character> =
        codePoint >>- { cp1 in
            
            if cp1.isSingleCodeUnit {
                
                return GenericParser(result: Character(UnicodeScalar(cp1)!))
                
            }
            
            return backslash *> codePoint >>- { cp2 in
                
                let cps = [cp1, cp2]
                guard let str = String(codeUnits: cps, codec: UTF16()) else {
            
                    let decodingErrorMsg = LocalizedString("decoding error")
                    return GenericParser.fail(decodingErrorMsg)
                    
                }
                
                return GenericParser(result: str[str.startIndex])
                
            } <?> LocalizedString("surrogate pair")
            
        }
        
        let escapeCodeMsg = LocalizedString("escape code")
        let characterEscape = backslash *>
            (charEscape <|> encodedChar <?> escapeCodeMsg)
        jsonDef.characterEscape = characterEscape
        
        return jsonDef
        
    }
    
    /// This is a minimal token definition for the swift 2.1 language. It
    /// defines the style of comments, valid identifiers and operators, reserved
    /// names and operators, character escaping, and case sensitivity.
    static var swift: LanguageDefinition {
        
        var swiftDef = empty
        
        swiftDef.commentStart = "/*"
        swiftDef.commentEnd = "*/"
        swiftDef.commentLine = "//"
        
        swiftDef.identifierStart =
            GenericParser.memberOf(swiftIdentifierStartSet) <|>
            GenericParser.character(swiftImplicitParameterStart)
        
        swiftDef.identifierLetter = { char in
            
            if char == swiftImplicitParameterStart {
                
                return GenericParser.decimalDigit
                
            }
            
            return GenericParser.memberOf(swiftIdentifierLetterSet)
            
        }
        
        swiftDef.operatorStart =
            GenericParser.memberOf(swiftOperatorStartSet)
        swiftDef.operatorLetter =
            GenericParser.memberOf(swiftOperatorLetterSet)
        
        swiftDef.reservedNames = [
            "Self", "__COLUMN__", "__FILE__", "__FUNCTION__", "__LINE__", "as",
            "break", "case", "catch", "class", "continue", "default", "defer",
            "deinit", "do", "dynamicType", "else", "enum", "extension",
            "fallthrough", "false", "for", "func", "guard", "if", "import",
            "in", "init", "inout", "internal", "is", "let", "nil", "operator",
            "private", "protocol", "public", "repeat", "rethrows", "return",
            "self", "static­", "struct", "subscript", "super", "switch", "throw",
            "throws", "true", "try", "typealias", "var", "where", "while"
        ]
        swiftDef.reservedOperators = [
            "=", "->", ".", ",", ":", "@", "#", "<", "&", "`", "?", ">", "!"
        ]
        
        let charEscParsers: [GenericParser<String, UserState, Character>] =
        swiftEscapeMap.map { escCode in
            
            GenericParser.character(escCode.esc) *>
                GenericParser(result: escCode.code)
            
        }
        
        let charEscape = GenericParser.choice(charEscParsers)
        
        let hexaChar: GenericParser<String, UserState, Character> =
        (GenericParser.hexadecimalDigit <?> "").many1 >>- { digits in
            
            let num = String(digits)
            return GenericTokenParser.integerWithDigits(num, base: 16) >>-
            { intVal in
                
                GenericTokenParser.characterFromInt(intVal)
        
            } <?> LocalizedString("escape sequence")
            
        } <?> LocalizedString("hexadecimal digit(s)")
        
        let charNumber =
            GenericParser<String, UserState, Character>.string("u{") *>
            hexaChar <* GenericParser.character("}")
        
        let escapeCodeMsg = LocalizedString("escape code")
        let characterEscape = GenericParser.character("\\") *>
            (charEscape <|> charNumber <?> escapeCodeMsg)
        swiftDef.characterEscape = characterEscape
        
        return swiftDef
        
    }
    
}

//==============================================================================
// Private variables related to different language definitions.

//
// Empty definition
//
private let emptyOperatorLetterCharacters = ":!$%&*+./<=>?\\^|-~"

//
// JSON definition
//
private let jsonEscapeMap: [(esc: Character, code: Character)] = [
    ("\"", "\""), ("\\", "\\"), ("/", "/"), ("b", "\u{0008}"),
    ("f", "\u{000C}"), ("n", "\n"), ("r", "\r"), ("t", "\t")
]

private let jsonMaxEscapeDigit = 4

//
// Swift definition
//
private let swiftImplicitParameterStart: Character = "$"

// Initialized this way to overcome Swift 4.2 compiler error
private let swiftIdentifierStartCharacters: String = {
    let strands: [String] = [
        (0x0041...0x005A).stringValue, // 'A' to 'Z'
        (0x0061...0x007A).stringValue, // 'a' to 'z'
        "_",
        "\u{00A8}\u{00AA}\u{00AD}\u{00AF}",
        (0x00B2...0x00B5).stringValue,
        (0x00B7...0x00BA).stringValue,
        (0x00BC...0x00BE).stringValue,
        (0x00C0...0x00D6).stringValue,
        (0x00D8...0x00F6).stringValue,
        (0x00F8...0x00FF).stringValue,
        (0x0100...0x02FF).stringValue,
        (0x0370...0x167F).stringValue,
        (0x1681...0x180D).stringValue,
        (0x180F...0x1DBF).stringValue,
        (0x1E00...0x1FFF).stringValue,
        (0x200B...0x200D).stringValue,
        (0x202A...0x202E).stringValue,
        (0x203F...0x2040).stringValue,
        "\u{2054}",
        (0x2060...0x206F).stringValue,
        (0x2070...0x20CF).stringValue,
        (0x2100...0x218F).stringValue,
        (0x2460...0x24FF).stringValue,
        (0x2776...0x2793).stringValue,
        (0x2C00...0x2DFF).stringValue,
        (0x2E80...0x2FFF).stringValue,
        (0x3004...0x3007).stringValue,
        (0x3021...0x302F).stringValue,
        (0x3031...0x303F).stringValue,
        (0x3040...0xD7FF).stringValue,
        (0xF900...0xFD3D).stringValue,
        (0xFD40...0xFDCF).stringValue,
        (0xFDF0...0xFE1F).stringValue,
        (0xFE30...0xFE44).stringValue,
        (0xFE47...0xFFFD).stringValue,
        (0x10000...0x1FFFD).stringValue,
        (0x20000...0x2FFFD).stringValue,
        (0x30000...0x3FFFD).stringValue,
        (0x40000...0x4FFFD).stringValue,
        (0x50000...0x5FFFD).stringValue,
        (0x60000...0x6FFFD).stringValue,
        (0x70000...0x7FFFD).stringValue,
        (0x80000...0x8FFFD).stringValue,
        (0x90000...0x9FFFD).stringValue,
        (0xA0000...0xAFFFD).stringValue,
        (0xB0000...0xBFFFD).stringValue,
        (0xC0000...0xCFFFD).stringValue,
        (0xD0000...0xDFFFD).stringValue,
        (0xE0000...0xEFFFD).stringValue]
    return strands.reduce(into: "") { $0 += $1 }
}()

private let swiftIdentifierStartSet =
    CharacterSet(charactersIn: swiftIdentifierStartCharacters)

private let swiftIdentifierLetterCharacters =
    swiftIdentifierStartCharacters +
    "0123456789" +
    (0x0300...0x036F).stringValue +
    (0x1DC0...0x1DFF).stringValue +
    (0x20D0...0x20FF).stringValue +
    (0xFE20...0xFE2F).stringValue

private let swiftIdentifierLetterSet =
    CharacterSet(charactersIn: swiftIdentifierLetterCharacters)

private let swiftOperatorStartCharacters =
    "/=-+!*%<>&|^?~" +
    (0x00A1...0x00A7).stringValue +
    "\u{00A9}\u{00AB}" +
    "\u{00AC}\u{00AE}" +
    "\u{00B0}\u{00B1}\u{00B6}\u{00BB}\u{00BF}\u{00D7}\u{00F7}" +
    (0x2016...0x2017).stringValue +
    (0x2020...0x2027).stringValue +
    (0x2030...0x203E).stringValue +
    (0x2041...0x2053).stringValue +
    (0x2055...0x205E).stringValue +
    (0x2190...0x23FF).stringValue +
    (0x2500...0x2775).stringValue +
    (0x2794...0x2BFF).stringValue +
    (0x2E00...0x2E7F).stringValue +
    (0x3001...0x3003).stringValue +
    (0x3008...0x3030).stringValue

private let swiftOperatorStartSet =
    CharacterSet(charactersIn: swiftOperatorStartCharacters)

private let swiftOperatorLetterCharacters =
    swiftOperatorStartCharacters +
    (0x0300...0x036F).stringValue +
    (0x1DC0...0x1DFF).stringValue +
    (0x20D0...0x20FF).stringValue +
    (0xFE00...0xFE0F).stringValue +
    (0xFE20...0xFE2F).stringValue +
    (0xE0100...0xE01EF).stringValue

private let swiftOperatorLetterSet =
    CharacterSet(charactersIn: swiftOperatorLetterCharacters)

private let swiftEscapeMap: [(esc: Character, code: Character)] = [
    ("n", "\n"), ("r", "\r"), ("t", "\t"), ("\\", "\\"), ("\"", "\""),
    ("'", "'"), ("0", "\0")
]


================================================
FILE: Sources/SwiftParsec/ParseError.swift
================================================
//==============================================================================
// Error.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-09-04.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// Parse errors.
//==============================================================================

//==============================================================================
/// Message represents parse error messages. The fine distinction between
/// different kinds of parse errors allows the system to generate quite good
/// error messages for the user. It also allows error messages that are
/// formatted in different languages. Each kind of message is generated by
/// different combinators.
///
/// The `Comparable` protocol is implemented based on the index of a message.
public enum Message: Comparable {
    
    /// A `SystemUnexpected` message is automatically generated by the
    /// `satisfy` combinator. The argument is the unexpected input.
    case systemUnexpected(String)
    
    /// An `Unexpected` message is generated by the `unexpected` combinator.
    /// The argument describes the unexpected item.
    case unexpected(String)
    
    /// An `Expect` message is generated by the `<?>` combinator. The argument
    /// describes the expected item.
    case expected(String)
    
    /// A `Generic` message is generated by the `fail` combinator. The argument
    /// is some general parser message.
    case generic(String)
    
    /// The index of the message type.
    var index: Int {
        
        switch self {
            
        case .systemUnexpected: return 0
            
        case .unexpected: return 1
            
        case .expected: return 2
            
        case .generic: return 3
            
        }
        
    }
    
    /// The message string.
    var messageString: String {
        
        switch self {
            
        case .systemUnexpected(let str): return str
            
        case .unexpected(let str): return str
        
        case .expected(let str): return str
        
        case .generic(let str): return str
            
        }
        
    }
    
}

//==============================================================================
// Operator implementations for the `Message` type.

/// Equality based on the index.
public func ==(leftMsg: Message, rightMsg: Message) -> Bool {
    
    return leftMsg.index == rightMsg.index
    
}

/// Comparison based on the index.
public func <(leftMsg: Message, rightMsg: Message) -> Bool {
    
    return leftMsg.index < rightMsg.index
    
}

//==============================================================================
/// `ParseError` represents parse errors. It provides the source position
/// (`SourcePosition`) of the error and an array of error messages (`Message`).
/// A `ParseError` can be returned by the function `parse`.
public struct ParseError: Error, CustomStringConvertible {
    
    /// Return an unknown parse error.
    ///
    /// - parameter position: The current position.
    /// - returns: An unknown parse error.
    static func unknownParseError(_ position: SourcePosition) -> ParseError {
        
        return ParseError(position: position, messages: [])
        
    }
    
    /// Return a system unexpected parse error.
    ///
    /// - parameters:
    ///   - position: The current position.
    ///   - message: The message string.
    /// - returns: An unexpected parse error.
    static func unexpectedParseError(
        _ position: SourcePosition,
        message: String
    ) -> ParseError {
        
        return ParseError(
            position: position,
            messages: [.systemUnexpected(message)]
        )
        
    }
    
    /// Source position of the error.
    public var position: SourcePosition
    
    /// Sorted array of error messages.
    public var messages: [Message] {
        
        get { return _messages.sorted() }
        
        set { _messages = newValue }
        
    }
    
    // Backing store for `messages`.
    private var _messages = [Message]()
    
    /// A textual representation of `self`.
    public var description: String {
        
        return String(describing: position) + ":\n" + messagesDescription
        
    }
    
    /// Indicates if `self` is an unknown parse error.
    var isUnknown: Bool { return messages.isEmpty }
    
    private var messagesDescription: String {
        
        guard !messages.isEmpty else {
            
            return LocalizedString("unknown parse error")
            
        }
        
        let (sysUnexpected, msgs1) =
            messages.part { $0 == .systemUnexpected("") }
        let (unexpected, msgs2) = msgs1.part { $0 == .unexpected("") }
        let (expected, generic) = msgs2.part { $0 == .expected("") }
        
        // System unexpected messages.
        let sysUnexpectedDesc: String
        
        let unexpectedMsg = LocalizedString("unexpected")
        
        if !unexpected.isEmpty || sysUnexpected.isEmpty {
            
            sysUnexpectedDesc = ""
            
        } else {
            
            let firstMsg = sysUnexpected.first!.messageString
            
            if firstMsg.isEmpty {
                
                sysUnexpectedDesc = LocalizedString("unexpected end of input")
                
            } else {
                
                sysUnexpectedDesc = unexpectedMsg + " " + firstMsg
                
            }
            
        }
        
        // Unexpected messages.
        let unexpectedDesc =
            formatMessages(unexpected, havingType: unexpectedMsg)
        
        // Expected messages.
        let expectingMsg = LocalizedString("expecting")
        let expectedDesc = formatMessages(expected, havingType: expectingMsg)
        
        // Generic messages.
        let genericDesc = formatMessages(generic, havingType: "")
        
        let descriptions = [
            sysUnexpectedDesc, unexpectedDesc, expectedDesc, genericDesc
        ]
        
        return descriptions.removingDuplicatesAndEmpties().joined(
            separator: "\n"
        )
        
    }
    
    /// Initializes from a source position and an array of messages.
    init(position: SourcePosition, messages: [Message]) {
        
        self.position = position
        self.messages = messages
        
    }
    
    /// Insert a message error in `messages`. All messages equal to the inserted
    /// messages are removed and the new message is inserted at the beginning of
    /// `messages`.
    ///
    /// - parameter message: The new message to insert in `messages`.
    mutating func insertMessage(_ message: Message) {
        
        messages = messages.filter({ $0 != message }).prepending(message)
        
    }
    
    /// Insert the labels as `.Expected` message errors in `messages`.
    ///
    /// - parameter labels: The labels to insert.
    mutating func insertLabelsAsExpected(_ labels: [String]) {
        
        guard !labels.isEmpty else {
            
            insertMessage(.expected(""))
            return
            
        }
        
        insertMessage(.expected(labels[0]))
        
        for label in labels.suffix(from: 1) {
            
            messages.append(.expected(label))
            
        }
        
    }
    
    /// Merge this `ParseError` with another `ParseError`.
    ///
    /// - parameter other: `ParseError` to merge with `self`.
    mutating func merge(_ other: ParseError) {
        
        let otherIsEmpty = other.messages.isEmpty
        
        // Prefer meaningful error.
        if messages.isEmpty && !otherIsEmpty {
            
            self = other
            
        } else if !otherIsEmpty {
            
            // Select the longest match
            if position == other.position {
                
                messages += other.messages
                
            } else if position < other.position {
                
                self = other
                
            }
            
        }
        
    }
    
    private func formatMessages(
        _ messages: [Message],
        havingType messageType: String
    ) -> String {
        
        let msgs = messages.map({
            $0.messageString
        }).removingDuplicatesAndEmpties()
        
        guard !msgs.isEmpty else { return "" }
        
        let msgType = messageType.isEmpty ? "" : messageType + " "
        
        if msgs.count == 1 {
            
            return msgType + msgs.first!
            
        }
        
        let commaSep = msgs.dropLast().joined(separator: ", ")
        
        let orStr =  LocalizedString("or")
        
        return msgType + commaSep + " " + orStr + " " + msgs.last!
        
    }

}

//==============================================================================
// Extension to add ad-hoc methods on the `Sequence` type.
extension Sequence where Iterator.Element == String {
    
    /// Return an array with duplicate and empty strings removed.
    func removingDuplicatesAndEmpties() -> [Self.Iterator.Element] {
        
        return self.removingDuplicates().filter { !$0.isEmpty }
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/Parsec.swift
================================================
//==============================================================================
// Parsec.swift
// SwiftParsec
//
// Created by David Dufresne on 2016-05-02.
// Copyright © 2016 David Dufresne. All rights reserved.
//
// Parsec protocol and related operator definitions.
//==============================================================================

// TODO: - Make `Parsec` the model of a true monad when Swift will allow it.

//==============================================================================
/// `Parsec` is a parser with stream type `Stream`, user state type `UserState`
/// and return type `Result`.
public protocol Parsec {
    
    /// The input stream to parse.
    associatedtype StreamType: Stream
    
    /// The state supplied by the user.
    associatedtype UserState
    
    /// The result of the parser.
    associatedtype Result
    
    /// Return a parser containing the result of mapping transform over `self`.
    ///
    /// This method has the synonym infix operator `<^>`.
    ///
    /// - parameter transform: A mapping function.
    /// - returns: A new parser with the mapped content.
    func map<T>(
        _ transform: @escaping (Result) -> T
    ) -> GenericParser<StreamType, UserState, T>
    
    /// Return a parser by applying the function contained in the supplied
    /// parser to self.
    ///
    /// This method has the synonym infix operator `<*>`.
    ///
    /// - parameter parser: The parser containing the function to apply to self.
    /// - returns: A parser with the applied function.
    func apply<T>(
        _ parser: GenericParser<StreamType, UserState, (Result) -> T>
    ) -> GenericParser<StreamType, UserState, T>
    
    /// This combinator implements choice. The parser `p.alternative(q)` first
    /// applies `p`. If it succeeds, the value of `p` is returned. If `p` fails
    /// _without consuming any input_, parser `q` is tried. The parser is called
    /// _predictive_ since `q` is only tried when parser `p` didn't consume any
    /// input (i.e.. the look ahead is 1). This non-backtracking behaviour
    /// allows for both an efficient implementation of the parser combinators
    /// and the generation of good error messages.
    ///
    /// This method has the synonym infix operator `<|>`.
    ///
    /// - parameter altParser: The alternative parser to try if `self` fails.
    /// - returns: A parser that will first try `self`. If it consumed no input,
    ///   it will try `altParser`.
    func alternative(_ altParser: Self) -> Self
    
    /// Return a parser containing the result of mapping transform over `self`.
    ///
    /// This method has the synonym infix operator `>>-` (bind).
    ///
    /// - parameter transform: A mapping function returning a parser.
    /// - returns: A new parser with the mapped content.
    func flatMap<T>(
        _ transform: @escaping (
            Result
        ) -> GenericParser<StreamType, UserState, T>
    ) -> GenericParser<StreamType, UserState, T>
    
    /// This combinator is used whenever arbitrary look ahead is needed. Since
    /// it pretends that it hasn't consumed any input when `self` fails, the
    /// ('<|>') combinator will try its second alternative even when the first
    /// parser failed while consuming input.
    ///
    /// The `attempt` combinator can for example be used to distinguish
    /// identifiers and reserved words. Both reserved words and identifiers are
    /// a sequence of letters. Whenever we expect a certain reserved word where
    /// we can also expect an identifier we have to use the `attempt`
    /// combinator. Suppose we write:
    ///
    ///     let letExpr = StringParser.string("let")
    ///     let identifier = letter.many1
    ///
    ///     let expr = letExpr <|> identifier <?> "expression"
    ///
    /// If the user writes \"lexical\", the parser fails with: _unexpected 'x',
    /// expecting 't' in "let"_. Indeed, since the ('<|>') combinator only tries
    /// alternatives when the first alternative hasn't consumed input, the
    /// `identifier` parser is never tried (because the prefix "le" of the
    /// `string("let")` parser is already consumed). The right behaviour can be
    /// obtained by adding the `attempt` combinator:
    ///
    ///     let letExpr = StringParser.string("let")
    ///     let identifier = StringParser.letter.many1
    ///
    ///     let expr = letExpr.attempt <|> identifier <?> "expression"
    ///
    /// - returns: A parser that pretends that it hasn't consumed any input when
    ///   `self` fails.
    var attempt: Self { get }
    
    /// A combinator that parses without consuming any input.
    ///
    /// If `self` fails and consumes some input, so does `lookAhead`. Combine
    /// with `attempt` if this is undesirable.
    ///
    /// - returns: A parser that parses without consuming any input.
    var lookAhead: Self { get }
    
    /// This combinator applies `self` _zero_ or more times. It returns an
    /// accumulation of the returned values of `self` that were passed to the
    /// `accumulator` function.
    ///
    /// - parameter accumulator: An accumulator function that process the value
    ///   returned by `self`. The first argument is the value returned by `self`
    ///   and the second argument is the previous processed values returned by
    ///   this accumulator function. It returns the result of processing the
    ///   passed value and the accumulated values.
    /// - returns: The processed values of the accumulator function.
    func manyAccumulator(
        _ accumulator: @escaping (Result, [Result]) -> [Result]
    ) -> GenericParser<StreamType, UserState, [Result]>
    
    /// A parser that always fails without consuming any input.
    static var empty: Self { get }
    
    /// The parser returned by `p.labels(message)` behaves as parser `p`, but
    /// whenever the parser `p` fails _without consuming any input_, it replaces
    /// expected error messages with the expected error message `message`.
    ///
    /// This is normally used at the end of a set alternatives where we want to
    /// return an error message in terms of a higher level construct rather than
    /// returning all possible characters. For example, if the `expr` parser
    /// from the `attempt` example would fail, the error message is: '...:
    /// expecting expression'. Without the `GenericParser.labels()` combinator,
    /// the message would be like '...: expecting "let" or "letter"', which is
    /// less friendly.
    ///
    /// This method has the synonym infix operator `<?>`.
    ///
    /// - parameter message: The new error message.
    /// - returns: A parser with a replaced error message.
    func labels(_ message: String...) -> Self
    
    /// Return a parser that always fails with an unexpected error message
    /// without consuming any input.
    ///
    /// The parsers 'fail', '\<?\>' and `unexpected` are the three parsers used
    /// to generate error messages. Of these, only '<?>' is commonly used. For
    /// an example of the use of `unexpected`, see the definition of
    /// `GenericParser.noOccurence`.
    ///
    /// - parameter message: The error message.
    /// - returns: A parser that always fails with an unexpected error message
    ///   without consuming any input.
    /// - SeeAlso: `GenericParser.noOccurence`, `GenericParser.fail(message:
    ///   String)` and `<?>`
    static func unexpected(_ message: String) -> Self
    
    /// Return a parser that always fails with the supplied message.
    ///
    /// - parameter message: The failure message.
    /// - returns: A parser that always fail.
    static func fail(_ message: String) -> Self
    
    /// Return the current source position.
    ///
    /// - returns: The current source position.
    /// - SeeAlso 'SourcePosition'.
    static var
    sourcePosition: GenericParser<StreamType, UserState, SourcePosition> { get }
    
    /// Return the user state.
    ///
    /// - returns: The user state
    static var
    userState: GenericParser<StreamType, UserState, UserState> { get }
    
    /// The `updateUserState` method applies the function `update` to the user
    /// state. Suppose that we want to count identifiers in a source, we could
    /// use the user state as:
    ///
    ///     let incrementCount = StringParser.updateUserState { ++$0 }
    ///     let expr = identifier <* incrementCount
    ///
    /// - parameter update: The function applied to the `UserState`. It returns
    ///   the updated `UserState`.
    /// - returns: An empty parser that will update the `UserState`.
    static func updateUserState(
        _ update: @escaping (UserState) -> UserState
    ) -> GenericParser<StreamType, UserState, ()>
    
    /// Run the parser and return the result of the parsing if it succeeded.
    /// If an error occured, it is returned. Contrary to the `run()` method, it
    /// doesn't throw an exception.
    ///
    /// - parameters:
    ///   - userState: The state supplied by the user.
    ///   - sourceName: The name of the source (i.e. file name).
    ///   - input: The input StreamType to parse.
    /// - returns: The result of the parsing on success, otherwise the parse
    ///   error.
    func runSafe(
        userState: UserState,
        sourceName: String,
        input: StreamType
    ) -> Either<ParseError, Result>
    
}

//==============================================================================
// Operator definitions.

/// Precedence of infix operator for `Parsec.labels()`. It has a higher
/// precedence than the `AssignmentPrecedence` group but a lower precedence than
/// the `LogicalDisjunctionPrecedence` group.
precedencegroup LabelPrecedence {
    associativity: right
    higherThan: AssignmentPrecedence
    lowerThan: LogicalDisjunctionPrecedence
}

/// Infix operator for `Parsec.labels()`. It has the lowest precedence of the
/// parsers operators.
infix operator <?> : LabelPrecedence

/// Precedence of infix operator for `Parsec.flatMap()` (bind). It has a higher
/// precedence than the `LabelPrecedence` group.
precedencegroup FlatMapPrecedence {
    associativity: left
    higherThan: LabelPrecedence
}

/// Infix operator for `Parsec.flatMap()` (bind). It has a higher precedence
/// than the `<?>` operator.
infix operator >>- : FlatMapPrecedence

/// Precedence of infix operator for `Parsec.alternative()`. It has a higher
/// precedence than the `FlatMapPrecedence` group.
precedencegroup ChoicePrecedence {
    associativity: left
    higherThan: FlatMapPrecedence
}

/// Infix operator for `Parsec.alternative()`. It has a higher precedence than
/// the `>>-` operator.
infix operator <|> : ChoicePrecedence

/// Precedence of infix operators for sequence parsing. It has a higher
/// precedence than the `ChoicePrecedence` group.
precedencegroup SequencePrecedence {
    associativity: left
    higherThan: ChoicePrecedence
}

/// Sequence parsing, discarding the value of the first parser. It has a higher
/// precedence than the `<|>` operator.
infix operator *> : SequencePrecedence

/// Sequence parsing, discarding the value of the second parser. It has a higher
/// precedence than the `<|>` operator.
infix operator <* : SequencePrecedence

/// Infix operator for `Parsec.apply()`. It has a higher precedence than the
/// `<|>` operator.
infix operator <*> : SequencePrecedence

/// Infix operator for `Parsec.map()`. It has a higher precedence than the `<|>`
/// operator.
infix operator <^> : SequencePrecedence

extension Parsec {
    
    /// Infix operator for `Parsec.labels()`. It has the lowest precedence of the
    /// parsers operators.
    ///
    /// - parameters:
    ///   - parser: The parser whose error message is to be replaced.
    ///   - message: The new error message.
    public static func <?>(parser: Self, message: String) -> Self {
        
        return parser.labels(message)
        
    }
    
    /// Infix operator for `Parsec.alternative`. It has a higher precedence than
    /// the `>>-` operator.
    ///
    /// - parameters:
    ///   - leftParser: The first parser to try.
    ///   - rightParser: The second parser to try.
    public static func <|>(leftParser: Self, rightParser: Self) -> Self {
        
        return leftParser.alternative(rightParser)
        
    }
   
}

//==============================================================================
// Extension containing useful methods to run a parser.
public extension Parsec {
    
    /// Run the parser and return the result of the parsing.
    ///
    /// - parameters:
    ///   - userState: The state supplied by the user.
    ///   - sourceName: The name of the source (i.e. file name).
    ///   - input: The input stream to parse.
    /// - throws: A `ParseError` when an error occurs.
    /// - returns: The result of the parsing.
    func run(
        userState: UserState,
        sourceName: String,
        input: StreamType
        ) throws -> Result {
        
        let result = runSafe(
            userState: userState,
            sourceName: sourceName,
            input: input
        )
        
        switch result {
            
        case .left(let error):
            
            throw error
            
        case .right(let result):
            
            return result
            
        }
        
    }
    
}

//==============================================================================
// Extension containing useful methods to run a parser with an empty user state.
public extension Parsec where UserState == () {
    
    /// Run the parser and return the result of the parsing.
    ///
    /// - parameters:
    ///   - userState: The state supplied by the user.
    ///   - sourceName: The name of the source (i.e. file name).
    ///   - input: The input stream to parse.
    /// - throws: A `ParseError` when an error occurs.
    /// - returns: The result of the parsing.
    func run(sourceName: String, input: StreamType) throws -> Result {
        
        return try run(
            userState: (),
            sourceName: sourceName,
            input: input
        )
        
    }
    
    /// Used for testing parsers. It applies `self` against `input` and prints
    /// the result.
    ///
    /// - parameter input: The input stream to parse.
    func test(input: StreamType) {
        
        do {
            
            let result = try run(sourceName: "", input: input)
            print(result)
            
        } catch let parseError as ParseError {
            
            let parseErrorMsg = LocalizedString("parse error at ")
            print(parseErrorMsg + String(describing: parseError))
            
        } catch let error {
            
            print(String(describing: error))
            
        }
        
    }
    
}

//==============================================================================
// Extension containing useful methods to run a parser.
/// A `Stream` instance is responsible for maintaining the position of the
/// parser's stream.
public protocol Stream: Collection, ExpressibleByArrayLiteral
where ArrayLiteralElement == Element {}

extension String: Stream {
    
    /// Create an instance containing `elements`.
    public init(arrayLiteral elements: String.Iterator.Element...) {
        
        self.init(elements)
        
    }
    
}

//==============================================================================
/// Types conforming to the `EmptyInitializable` protocol provide an empty
/// intializer.
public protocol EmptyInitializable {
    
    init()
    
}


//==============================================================================
// Extensions implementing the `Stream` protocol for various collections.

extension Array: Stream, EmptyInitializable {}

extension ContiguousArray: Stream, EmptyInitializable {}

extension ArraySlice: Stream, EmptyInitializable {}

extension Dictionary: EmptyInitializable {}

extension Set: EmptyInitializable {}


================================================
FILE: Sources/SwiftParsec/Permutation.swift
================================================
//==============================================================================
// Permutation.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-11-01.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// This module implements permutation parsers.
//==============================================================================

//==============================================================================
/// The type `Permutation` denotes a permutation that can be converted to a
/// `GenericParser` that returns an array of values of type `Result` on success.
/// The values in the array have the same order as the parsers in the
/// permutation. In the following exemple `parser` applies to any permutation of
/// the characters 'a', 'b' and 'c' and always returns the string `"abc"` on
/// success.
///
///     let permutation: Permutation = [
///
///         (StringParser.character("a"), nil),
///         (StringParser.character("b"), nil),
///         (StringParser.character("c"), nil)
///
///     ]
///
///     let parser = permutation.parser.stringValue
///
public struct Permutation<StreamType: Stream, UserState, Result>:
RangeReplaceableCollection, ExpressibleByArrayLiteral {
    
    /// Represents a valid position in the permutation.
    public typealias Index = Int
    
    /// Permutation's generator.
    public typealias Iterator = IndexingIterator<Permutation>
    
    /// Element type of the permutation.
    public typealias Element = (
        parser: GenericParser<StreamType, UserState, Result>,
        otherwise: Result?
    )
    
    /// The position of the first element.
    public let startIndex = 0
    
    /// The permutation's "past the end" position.
    public var endIndex: Int { return parsers.count }
    
    // Backing store.
    private var parsers: [Element]
    
    /// Create an instance initialized with elements.
    ///
    /// - parameter arrayLiteral: Arrays of tuple containing a parser and an
    ///   optional default value.
    public init(arrayLiteral elements: Element...) {
        
        parsers = elements
        
    }
    
    /// Create an empty instance.
    public init() { parsers = [] }
    
    /// Returns the position immediately after i.
    ///
    /// - SeeAlso: `IndexableBase` protocol.
    public func index(after i: Permutation.Index) -> Permutation.Index {
        
        return parsers.index(after: i)
        
    }
    
    /// A parser applying to the permutation of all the parsers contained in
    /// `self`.
    public func makeParser() -> GenericParser<StreamType, UserState, [Result]> {
        
        return makeParser(separator: GenericParser(result: ()))
        
    }
    
    /// A parser applying to the permutation of all the parsers contained in
    /// `self` separated by `separator`.
    ///
    /// - parameter separator: A separator to apply between each element of the
    ///   permutation.
    public func makeParser<Separator>(
        separator: GenericParser<StreamType, UserState, Separator>
    ) -> GenericParser<StreamType, UserState, [Result]> {
        
        let ps = parsers.map { elem in
            
            (parser: elem.parser.map { [$0] }, otherwise: elem.otherwise)
            
        }
        
        return permute(ps, separator: separator)
        
    }
    
    private typealias PermParser =
        GenericParser<StreamType, UserState, [Result]>
    
    private func permute<Separator>(
        _ elements: [(parser: PermParser, otherwise: Result?)],
        separator: GenericParser<StreamType, UserState, Separator>
    ) -> PermParser {
        
        var permutation = ContiguousArray<PermParser>()
        
        let elementsRange = elements.indices
        for index in elementsRange {
            
            let element = elements[index]
            
            var parser = element.parser
            if index == elementsRange.last {
                
                parser = emptyParser(parser, otherwise: element.otherwise)
                
            }
            
            let perm: PermParser = parser >>- { result in
                
                var elems = elements
                elems.remove(at: index)
                
                let p: PermParser
                if elems.count > 1  {
                    
                    p = separator *> self.permute(elems, separator: separator)
                    
                } else {
                    
                    let elem = elems[0]
                    p = self.emptyParser(
                        separator *> elem.parser,
                        otherwise: elem.otherwise
                    )
                    
                }
                
                return p >>- { results in
                    
                    var rs = results
                    rs.insert(contentsOf: result, at: index)
                    
                    return GenericParser(result: rs)
                    
                }
                
            }
            
            permutation.append(perm)
            
        }
        
        return GenericParser.choice(permutation)
        
    }
    
    private func emptyParser(
        _ parser: PermParser,
        otherwise: Result?
    ) -> PermParser {
        
        guard let def = otherwise else { return parser }
        
        return parser.otherwise([def])
        
    }
    
    /// Append a parser to the permutation. The added parser is not allowed to
    /// accept empty input - use `appendOptionalParser` instead.
    ///
    /// - parameter parser: The parser to append to the permutation.
    public mutating func appendParser(
        _ parser: GenericParser<StreamType, UserState, Result>
    ) {
        
        parsers.append((parser, nil))
        
    }
    
    /// Append an optional parser to the permutation. The parser is optional -
    /// if it cannot be applied, the default value `otherwise` will be used
    /// instead.
    ///
    /// - parameters:
    ///   - parser: The optional parser to append to the permutation.
    ///   - otherwise: The default value to use if the parser cannot be applied.
    public mutating func appendOptionalParser(
        _ parser: GenericParser<StreamType, UserState, Result>,
        otherwise: Result
    ) {
        
        parsers.append((parser, otherwise))
        
    }
    
    /// Replace the given subRange of elements with newElements.
    ///
    /// - parameters:
    ///   - subRange: Range of elements to replace.
    ///   - newElements: New elements replacing the previous elements contained
    ///     in `subRange`.
    public mutating func replaceSubrange<C: Collection>(
        _ subrange: Range<Index>,
        with newElements: C
    ) where C.Iterator.Element == Iterator.Element {
        
        parsers.replaceSubrange(subrange, with: newElements)
        
    }
    
    public subscript(position: Index) -> Element { return parsers[position] }
    
}


================================================
FILE: Sources/SwiftParsec/Position.swift
================================================
//==============================================================================
// Position.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-09-04.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// Textual source positions.
//==============================================================================

//==============================================================================
/// SourcePosition represents source positions. It contains the name of the
/// source (i.e. file name), a line number and a column number. The upper left
/// is 1, 1. It implements the `Comparable` and `CustomStringConvertible`
/// protocols. The comparison is made using line and column number.
public struct SourcePosition: Comparable, CustomStringConvertible {
    
    /// The name of the source (i.e. file name)
    public var name: String
    
    /// The line number in the source.
    public var line: Int
    
    /// The column number in the source.
    public var column: Int
    
    /// A textual representation of `self`.
    public var description: String {
        
        let lineMsg = LocalizedString("line")
        let columnMsg = LocalizedString("column")
        
        var desc = "(" + lineMsg + " \(line), " + columnMsg + " \(column))"
        
        if !name.isEmpty {
            
            desc = "\"\(name)\" " + desc
            
        }
        
        return desc
        
    }
    
    /// Update a source position given a character. If the character is a
    /// newline ("\n") or carriage return ("\r") the line number is incremented
    /// by 1. If the character is a tab ("\t") the column number is incremented
    /// to the nearest 8'th column, ie. `column + 8 - ((column - 1) % 8)`. In
    /// all other cases, the column is incremented by 1.
    ///
    /// - parameter char: The tested character indicating how to update the
    ///   position.
    mutating func updatePosition(_ char: Character) {
        
        switch char {
            
        case "\n":
            
            line += 1
            column = 1
            
        case "\t":
            
            column = column + 8 - ((column - 1) % 8)
            
        default: column += 1
            
        }
        
    }
    
}

//==============================================================================
// Operator implementations for the `SourcePosition` type.

/// Equality based on the line and column number.
public func ==(leftPos: SourcePosition, rightPos: SourcePosition) -> Bool {
    
    return leftPos.line == rightPos.line && leftPos.column == rightPos.column
    
}

/// Comparison based on the line and column number.
public func <(leftPos: SourcePosition, rightPos: SourcePosition) -> Bool {
    
    if leftPos.line < rightPos.line {
        
        return true
        
    } else if leftPos.line == rightPos.line {
        
        if leftPos.column < rightPos.column { return true }
        
    }
    
    return false
    
}


================================================
FILE: Sources/SwiftParsec/RangeReplaceableCollectionInsertion.swift
================================================
//==============================================================================
// RangeReplaceableCollectionInsertion.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-09-11.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// RangeReplaceableCollection extension
//==============================================================================

//==============================================================================
// Extension containing insertion methods.
public extension RangeReplaceableCollection {
    
    /// Prepend `newElement` to the collection.
    ///
    /// - parameter newElement: New element to prepend to the collection.
    mutating func prepend(_ newElement: Iterator.Element) {
        
        insert(newElement, at: startIndex)
        
    }
    
    /// Returns a new collection containing the elements of `self` with
    /// `newElement` prepended at the beginning.
    ///
    /// - parameter newElement: New element to prepend.
    /// - returns: A copy of `self` plus `newElement` prepended.
    func prepending(_ newElement: Iterator.Element) -> Self {
        
        var mutableSelf = self
        mutableSelf.prepend(newElement)
        
        return mutableSelf
        
    }
    
    /// Returns a new collection containing the elements of `self` with
    /// `newElement` appended to the end.
    ///
    /// - parameter newElement: New element to append.
    /// - returns: A copy of `self` plus `newElement` appended.
    func appending(_ newElement: Iterator.Element) -> Self {
        
        var mutableSelf = self
        mutableSelf.append(newElement)
        
        return mutableSelf
        
    }

}


================================================
FILE: Sources/SwiftParsec/SequenceAggregation.swift
================================================
//==============================================================================
// SequenceAggregation.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-09-14.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// Sequence extension
//==============================================================================

//==============================================================================
// Extension containing aggregation methods.
extension Sequence {
    
    /// Return a tuple containing the elements of `self`, in order, that satisfy
    /// the predicate `includeElement`. The second array of the tuple contains
    /// the remainder of the list.
    ///
    /// - parameter includeElement: The predicate function used to split the
    ///   sequence.
    /// - returns:
    ///   - included: The elements that satisfied the predicate.
    ///   - remainder: The remainder of `self`.
    func part(
        _ includeElement: (Self.Iterator.Element) throws -> Bool
    ) rethrows
    -> (included: [Self.Iterator.Element], remainder: [Self.Iterator.Element]) {
        
        var included: [Self.Iterator.Element] = []
        var remainder: [Self.Iterator.Element] = []
        
        for elem in self {
            
            if (try includeElement(elem)) {
                
                included.append(elem)
                
            } else {
                
                remainder.append(elem)
                
            }
            
        }
        
        return (included, remainder)
        
    }
    
}

//==============================================================================
// Extension containing aggregation methods when the `Sequence` contains
// elements that are `Equatable`.
extension Sequence where Iterator.Element: Equatable {
    
    /// Return an array with the duplicate elements removed. In particular, it
    /// keeps only the first occurrence of each element.
    ///
    /// - returns: An array with the duplicate elements removed.
    func removingDuplicates() -> [Self.Iterator.Element] {
        
        return reduce([]) { (acc, elem) in
            
            guard !acc.contains(elem) else { return acc }
            
            return acc.appending(elem)
            
        }
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/SequenceConversion.swift
================================================
//==============================================================================
// SequenceConversion.swift
// SwiftParsec
//
// Created by David Dufresne on 2016-09-24.
// Copyright © 2016 David Dufresne. All rights reserved.
//
// Sequence extension
//==============================================================================

//==============================================================================
// Extension containing conversion methods.
extension Sequence where Iterator.Element == Int {
    
    /// Converts each `Int` in its `Character` equivalent and build a String
    /// with the result.
    var stringValue: String {
        
        let chars = map { Character(UnicodeScalar($0)!) }
        
        return String(chars)
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/SetAggregation.swift
================================================
//==============================================================================
// SetAggregation.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-10-10.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// Set extension
//==============================================================================

//==============================================================================
// Extension containing aggregation methods.
extension Set {
    
    /// Return a `Set` containing the results of mapping transform over `self`.
    ///
    /// - parameter transform: The transform function.
    /// - returns: A `Set` containing the results of mapping transform over
    ///   `self`.
    func map<T>(
        _ transform: (Iterator.Element) throws -> T
    ) rethrows -> Set<T> {
        
        var mappedSet = Set<T>()
        
        for elem in self {
            
            mappedSet.insert(try transform(elem))
            
        }
        
        return mappedSet
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/String.swift
================================================
//==============================================================================
// String.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-10-10.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// String extension
//==============================================================================

//==============================================================================
// Extension containing various utility methods and initializers.
extension String {
    
    /// Initialize a `String` from a sequence of code units.
    ///
    /// - parameters:
    ///   - codeUnits: Sequence of code units.
    ///   - codec: A unicode encoding scheme.
    init?<C: UnicodeCodec, S: Sequence>(codeUnits: S, codec: C)
    where S.Iterator.Element == C.CodeUnit {
        
        var unicodeCode = codec
        var str = ""
        
        var iterator = codeUnits.makeIterator()
        var done = false
        while !done {
            
            let result = unicodeCode.decode(&iterator)
            switch result {
                
            case .emptyInput: done = true
                
            case let .scalarValue(val):
                
                str.append(Character(val))
                
            case .error: return nil
                
            }
            
        }
        
        self = str
        
    }
    
    /// The last character of the string.
    ///
    /// If the string is empty, the value of this property is `nil`.
    var last: Character? {
        
        guard !isEmpty else { return nil }
        
        return self[index(before: endIndex)]
        
    }
    
    /// Return a new `String` with `c` adjoined to the end.
    ///
    /// - parameter c: Character to append.
    func appending(_ c: Character) -> String {
        
        var mutableSelf = self
        mutableSelf.append(c)
        
        return mutableSelf
        
    }
    
}


================================================
FILE: Sources/SwiftParsec/TokenParser.swift
================================================
//==============================================================================
// TokenParser.swift
// SwiftParsec
//
// Created by David Dufresne on 2015-10-05.
// Copyright © 2015 David Dufresne. All rights reserved.
//
// A helper module to parse lexical elements (tokens). See the initializer for
// the `TokenParser` structure for a description of how to use it.
// Operator implementations for the `Message` type.
//==============================================================================

import func Foundation.pow

//==============================================================================
/// Types implementing this protocol hold lexical parsers.
public protocol TokenParser {
    
    /// The state supplied by the user.
    associatedtype UserState
    
    /// Language definition parameterizing the lexer.
    var languageDefinition: LanguageDefinition<UserState> { get }
    
    /// This lexeme parser parses a legal identifier. Returns the identifier
    /// string. This parser will fail on identifiers that are reserved words.
    /// Legal identifier (start) characters and reserved words are defined in
    /// the `LanguageDefinition` that is passed to the initializer of this token
    /// parser. An `identifier` is treated as a single token using
    /// `GenericParser.attempt`.
    var identifier: GenericParser<String, UserState, String> { get }
    
    /// The lexeme parser `reservedName(name)` parses `symbol(name)`, but it
    /// also checks that the `name` is not a prefix of a valid identifier. A
    /// _reserved_ word is treated as a single token using
    /// `GenericParser.attempt`.
    ///
    /// - parameter name: The reserved name to parse.
    /// - returns: A parser returning nothing.
    func reservedName(_ name: String) -> GenericParser<String, UserState, ()>
    
    /// This lexeme parser parses a legal operator and returns the name of the
    /// operator. This parser will fail on any operators that are reserved
    /// operators. Legal operator (start) characters and reserved operators are
    /// defined in the `LanguageDefinition` that is passed to the initializer of
    /// this token parser. An 'operator' is treated as a single token using
    /// `GenericParser.attempt`.
    var legalOperator: GenericParser<String, UserState, String> { get }
    
    /// The lexeme parser `reservedOperator(name)` parses `symbol(name)`, but it
    /// also checks that the `name` is not a prefix of a valid operator. A
    /// 'reservedOperator' is treated as a single token using
    /// `GenericParser.attempt`.
    ///
    /// - parameter name: The operator name.
    /// - returns: A parser returning nothing.
    func reservedOperator(
        _ name: String
    ) -> GenericParser<String, UserState, ()>
    
    /// This lexeme parser parses a single literal character and returns the
    /// literal character value. This parser deals correctly with escape
    /// sequences.
    var characterLiteral: GenericParser<String, UserState, Character> { get }
    
    /// This lexeme parser parses a literal string and returns the literal
    /// string value. This parser deals correctly with escape sequences and
    /// gaps.
    var stringLiteral: GenericParser<String, UserState, String> { get }
    
    /// This lexeme parser parses a natural number (a positive whole number) and
    /// returns the value of the number. The number can be specified in
    /// 'decimal', 'hexadecimal' or 'octal'.
    var natural: GenericParser<String, UserState, Int> { get }
    
    /// This lexeme parser parses an integer (a whole number). This parser is
    /// like `natural` except that it can be prefixed with sign (i.e. "-" or
    /// "+"). It returns the value of the number. The number can be specified in
    /// 'decimal', 'hexadecimal' or 'octal'.
    var integer: GenericParser<String, UserState, Int> { get }
    
    /// This lexeme parser parses an integer (a whole number). It is like
    /// `integer` except that it can parse bigger numbers. Returns the value of
    /// the number as a `Double`.
    var integerAsFloat: GenericParser<String, UserState, Double> { get }
    
    /// This lexeme parser parses a floating point value and returns the value
    /// of the number.
    var float: GenericParser<String, UserState, Double> { get }
    
    /// This lexeme parser parses either `integer` or a `float` and returns the
    /// value of the number. This parser deals with any overlap in the grammar
    /// rules for integers and floats.
    var number: GenericParser<String, UserState, Either<Int, Double>> { get }
    
    /// Parses a positive whole number in the decimal system. Returns the value
    /// of the number.
    static var decimal: GenericParser<String, UserState, Int> { get }
    
    /// Parses a positive whole number in the hexadecimal system. The number
    /// should be prefixed with "x" or "X". Returns the value of the number.
    static var hexadecimal: GenericParser<String, UserState, Int> { get }
    
    /// Parses a positive whole number in the octal system. The number should be
    /// prefixed with "o" or "O". Returns the value of the number.
    static var octal: GenericParser<String, UserState, Int> { get }
    
    /// Lexeme parser `symbol(str)` parses `str` and skips trailing white space.
    ///
    /// - parameter name: The name of the symbol to parse.
    /// - returns: `name`.
    func symbol(_ name: String) -> GenericParser<String, UserState, String>
    
    /// `lexeme(parser)` first applies `parser` and than the `whiteSpace`
    /// parser, returning the value of `parser`. Every lexical token (lexeme) is
    /// defined using `lexeme`, this way every parse starts at a point without
    /// white space. Parsers that use `lexeme` are called _lexeme_ parsers in
    /// this document.
    ///
    /// The only point where the 'whiteSpace' parser should be called explicitly
    /// is the start of the main parser in order to skip any leading white
    /// space.
    ///
    ///     let mainParser = sum <^> whiteSpace *> lexeme(digit) <* eof
    ///
    /// - parameter parser: The parser to transform in a 'lexeme'.
    /// - returns: The value of `parser`.
    func lexeme<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result>
    
    /// Parses any white space. White space consists of _zero_ or more
    /// occurrences of a 'space', a line comment or a block (multiline) comment.
    /// Block comments may be nested. How comments are started and ended is
    /// defined in the `LanguageDefinition` that is passed to the initializer of
    /// this token parser.
    var whiteSpace: GenericParser<String, UserState, ()> { get }
    
    /// Lexeme parser `parentheses(parser)` parses `parser` enclosed in
    /// parentheses, returning the value of `parser`.
    ///
    /// - parameter parser: The parser applied between the parentheses.
    /// - returns: The value of `parser`.
    func parentheses<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result>
    
    /// Lexeme parser `braces(parser)` parses `parser` enclosed in braces "{"
    /// and "}", returning the value of `parser`.
    ///
    /// - parameter parser: The parser applied between the braces.
    /// - returns: The value of `parser`.
    func braces<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result>
    
    /// Lexeme parser `angles(parser)` parses `parser` enclosed in angle
    /// brackets "<" and ">", returning the value of `parser`.
    ///
    /// - parameter parser: The parser applied between the angles.
    /// - returns: The value of `parser`.
    func angles<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result>
    
    /// Lexeme parser `brackets(parser)` parses `parser` enclosed in brackets
    /// "[" and "]", returning the value of `parser`.
    ///
    /// - parameter parser: The parser applied between the brackets.
    /// - returns: The value of `parser`.
    func brackets<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result>
    
    /// Lexeme parser `semicolon` parses the character ";" and skips any
    /// trailing white space. Returns the string ";".
    var semicolon: GenericParser<String, UserState, String> { get }
    
    /// Lexeme parser `comma` parses the character "," and skips any trailing
    /// white space. Returns the string ",".
    var comma: GenericParser<String, UserState, String> { get }
    
    /// Lexeme parser `colon` parses the character ":" and skips any trailing
    /// white space. Returns the string ":".
    var colon: GenericParser<String, UserState, String> { get }
    
    /// Lexeme parser `dot` parses the character "." and skips any trailing
    /// white space. Returns the string ".".
    var dot: GenericParser<String, UserState, String> { get }
    
    /// Lexeme parser `semicolonSeperated(parser)` parses _zero_ or more
    /// occurrences of `parser` separated by `semicolon`. Returns an array of
    /// values returned by `parser`.
    ///
    /// - parameter parser: The parser applied between semicolons.
    /// - returns: An array of values returned by `parser`.
    func semicolonSeparated<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, [Result]>
    
    /// Lexeme parser `semicolonSeperated1(parser)` parses _one_ or more
    /// occurrences of `parser` separated by `semicolon`. Returns an array of
    /// values returned by `parser`.
    ///
    /// - parameter parser: The parser applied between semicolons.
    /// - returns: An array of values returned by `parser`.
    func semicolonSeparated1<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, [Result]>
    
    /// Lexeme parser `commaSeparated(parser)` parses _zero_ or more occurrences
    /// of `parser` separated by `comma`. Returns an array of values returned by
    /// `parser`.
    ///
    /// - parameter parser: The parser applied between commas.
    /// - returns: An array of values returned by `parser`.
    func commaSeparated<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, [Result]>
    
    /// Lexeme parser `commaSeparated1(parser)` parses _one_ or more occurrences
    /// of `parser` separated by `comma`. Returns an array of values returned by
    /// `parser`.
    ///
    /// - parameter parser: The parser applied between commas.
    /// - returns: An array of values returned by `parser`.
    func commaSeparated1<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, [Result]>
    
}

// Default implementation of the methods of the `TokenParser` parser type.
extension TokenParser {
    
    // Type aliases used internally to simplify the code.
    typealias StrParser = GenericParser<String, UserState, String>
    typealias CharacterParser = GenericParser<String, UserState, Character>
    typealias IntParser = GenericParser<String, UserState, Int>
    typealias DoubleParser = GenericParser<String, UserState, Double>
    typealias IntDoubleParser =
        GenericParser<String, UserState, Either<Int, Double>>
    typealias VoidParser = GenericParser<String, UserState, ()>
    
    //
    // Identifiers & Reserved words
    //
    
    /// This lexeme parser parses a legal identifier. Returns the identifier
    /// string. This parser will fail on identifiers that are reserved words.
    /// Legal identifier (start) characters and reserved words are defined in
    /// the `LanguageDefinition` that is passed to the initializer of this token
    /// parser. An `identifier` is treated as a single token using
    /// `GenericParser.attempt`.
    public var identifier: GenericParser<String, UserState, String> {
        
        let langDef = languageDefinition
        
        let ident: StrParser = langDef.identifierStart >>- { char in
            
            langDef.identifierLetter(char).many >>- { chars in
                
                let cs = chars.prepending(char)
                return GenericParser(result: String(cs))
                
            }
            
        } <?> LocalizedString("identifier")
        
        let identCheck: StrParser = ident >>- { name in
            
            let reservedNames: Set<String>
            let n: String
            
            if langDef.isCaseSensitive {
                
                reservedNames = langDef.reservedNames
                n = name
                
            } else {
                
                reservedNames = langDef.reservedNames.map { $0.lowercased() }
                n = name.lowercased()
                
            }
            
            guard !reservedNames.contains(n) else {
                
                let reservedWordMsg = LocalizedString("reserved word ")
                return GenericParser.unexpected(reservedWordMsg + name)
                
            }
            
            return GenericParser(result: name)
            
        }
        
        return lexeme(identCheck.attempt)
        
    }
    
    /// The lexeme parser `reservedName(name)` parses `symbol(name)`, but it
    /// also checks that the `name` is not a prefix of a valid identifier. A
    /// _reserved_ word is treated as a single token using
    /// `GenericParser.attempt`.
    ///
    /// - parameter name: The reserved name to parse.
    /// - returns: A parser returning nothing.
    public func reservedName(
        _ name: String
    ) -> GenericParser<String, UserState, ()> {
        
        let lastChar = name.last!
        let reserved = caseString(name) *>
            languageDefinition.identifierLetter(lastChar).noOccurence <?>
            LocalizedString("end of ") + name
        
        return lexeme(reserved.attempt)
        
    }
    
    //
    // Operators & reserved operators
    //
    
    /// This lexeme parser parses a legal operator and returns the name of the
    /// operator. This parser will fail on any operators that are reserved
    /// operators. Legal operator (start) characters and reserved operators are
    /// defined in the `LanguageDefinition` that is passed to the initializer of
    /// this token parser. An 'operator' is treated as a single token using
    /// `GenericParser.attempt`.
    public var legalOperator: GenericParser<String, UserState, String> {
        
        let langDef = languageDefinition
        
        let op: StrParser = langDef.operatorStart >>- { char in
            
            langDef.operatorLetter.many >>- { chars in
                
                let cs = chars.prepending(char)
                return GenericParser(result: String(cs))
                
            }
            
        } <?> LocalizedString("operator")
        
        let opCheck: StrParser = op >>- { name in
            
            guard !langDef.reservedOperators.contains(name) else {
                
                let reservedOperatorMsg = LocalizedString("reserved operator ")
                return GenericParser.unexpected(reservedOperatorMsg + name)
                
            }
            
            return GenericParser(result: name)
            
        }
        
        return lexeme(opCheck.attempt)
        
    }
    
    /// The lexeme parser `reservedOperator(name)` parses `symbol(name)`, but it
    /// also checks that the `name` is not a prefix of a valid operator. A
    /// 'reservedOperator' is treated as a single token using
    /// `GenericParser.attempt`.
    ///
    /// - parameter name: The operator name.
    /// - returns: A parser returning nothing.
    public func reservedOperator(
        _ name: String
    ) -> GenericParser<String, UserState, ()> {
        
        let op = VoidParser.string(name) *>
            languageDefinition.operatorLetter.noOccurence <?>
            LocalizedString("end of ") + name
        
        return lexeme(op.attempt)
        
    }
    
    //
    // Characters & Strings
    //
    
    /// This lexeme parser parses a single literal character and returns the
    /// literal character value. This parser deals correctly with escape
    /// sequences.
    public var characterLiteral: GenericParser<String, UserState, Character> {
        
        let characterLetter = CharacterParser.satisfy { char in
            
            char != "'" && char != "\\" && char != substituteCharacter
            
        }
        
        let defaultCharEscape = GenericParser.character("\\") *>
            GenericTokenParser<UserState>.escapeCode
        let characterEscape =
            languageDefinition.characterEscape ?? defaultCharEscape
        
        let character = characterLetter <|> characterEscape <?>
            LocalizedString("literal character")
        
        let quote = CharacterParser.character("'")
        
        let endOfCharMsg = LocalizedString("end of character")
        
        return lexeme(character.between(quote, quote <?> endOfCharMsg)) <?>
            LocalizedString("character")
        
    }
    
    /// This lexeme parser parses a literal string and returns the literal
    /// string value. This parser deals correctly with escape sequences and
    /// gaps.
    public var stringLiteral: GenericParser<String, UserState, String> {
        
        let stringLetter = CharacterParser.satisfy { char in
            
            char != "\"" && char != "\\" && char != substituteCharacter
            
        }
        
        let escapeGap: GenericParser<String, UserState, Character?> =
            GenericParser.space.many1 *> GenericParser.character("\\") *>
                GenericParser(result: nil) <?>
                LocalizedString("end of string gap")
        
        let escapeEmpty: GenericParser<String, UserState, Character?> =
            GenericParser.character("&") *> GenericParser(result: nil)
        
        let characterEscape: GenericParser<String, UserState, Character?> =
            GenericParser.character("\\") *>
                (escapeGap <|> escapeEmpty <|>
                GenericTokenParser.escapeCode.map { $0 })
        
        let stringEscape =
            languageDefinition.characterEscape?.map { $0 } ?? characterEscape
        
        let stringChar = stringLetter.map { $0 } <|> stringEscape
        
        let doubleQuote = CharacterParser.character("\"")
        let endOfStringMsg = LocalizedString("end of string")
        let string = stringChar.many.between(
            doubleQuote, doubleQuote <?> endOfStringMsg
        )
        
        let literalString = string.map({ str in
            
            str.reduce("") { (acc, char) in
                
                guard let c = char else { return acc }
                
                return acc.appending(c)
                
            }
            
        }) <?> LocalizedString("literal string")
        
        return lexeme(literalString)
        
    }
    
    //
    // Numbers
    //
    
    /// This lexeme parser parses a natural number (a positive whole number) and
    /// returns the value of the number. The number can be specified in
    /// 'decimal', 'hexadecimal' or 'octal'.
    public var natural: GenericParser<String, UserState, Int> {
        
        return lexeme(GenericTokenParser.naturalNumber) <?>
            LocalizedString("natural")
        
    }
    
    /// This lexeme parser parses an integer (a whole number). This parser is
    /// like `natural` except that it can be prefixed with sign (i.e. "-" or
    /// "+"). It returns the value of the number. The number can be specified in
    /// 'decimal', 'hexadecimal' or 'octal'.
    public var integer: GenericParser<String, UserState, Int> {
        
        let int = lexeme(GenericTokenParser.sign()) >>- { f in
            
            GenericTokenParser.naturalNumber >>- {
                
                GenericParser(result: f($0))
                
            }
            
        }
        
        return lexeme(int) <?> LocalizedString("integer")
        
    }
    
    /// This lexeme parser parses an integer (a whole number). It is like
    /// `integer` except that it can parse bigger numbers. Returns the value of
    /// the number as a `Double`.
    public var integerAsFloat: GenericParser<String, UserState, Double> {
        
        let hexaPrefix = CharacterParser.oneOf(hexadecimalPrefixes)
        let hexa = hexaPrefix *> GenericTokenParser.doubleWithBase(
            16,
            parser: GenericParser.hexadecimalDigit
        )
        
        let octPrefix = CharacterParser.oneOf(octalPrefixes)
        let oct = octPrefix *> GenericTokenParser.doubleWithBase(
            8,
            parser: GenericParser.octalDigit
        )
        
        let decDigit = CharacterParser.decimalDigit
        let dec = GenericTokenParser.doubleWithBase(10, parser: decDigit)
        
        let zeroNumber = (GenericParser.character("0") *>
            (hexa <|> oct <|> dec <|> GenericParser(result: 0))) <?> ""
        
        let nat = zeroNumber <|> dec
        
        let double = lexeme(GenericTokenParser.sign()) >>- { sign in
            
            nat >>- { GenericParser(result: sign($0)) }
            
        }
        
        return lexeme(double) <?> LocalizedString("integer")
        
    }
    
    /// This lexeme parser parses a floating point value and returns the value
    /// of the number.
    public var float: GenericParser<String, UserState, Double> {
        
        let intPart = GenericTokenParser<UserState>.doubleIntegerPart
        let expPart = GenericTokenParser<UserState>.fractionalExponent
        let f = intPart >>- { expPart($0) }
        
        let double = lexeme(GenericTokenParser.sign()) >>- { sign in
            
            f >>- { GenericParser(result: sign($0)) }
            
        }
        
        return lexeme(double) <?> LocalizedString("float")
        
    }
    
    /// This lexeme parser parses either `integer` or a `float` and returns the
    /// value of the number. This parser deals with any overlap in the grammar
    /// rules for integers and floats.
    public var number: GenericParser<String, UserState, Either<Int, Double>> {
        
        let intDouble = float.map({ Either.right($0) }).attempt <|>
            integer.map({ Either.left($0) })
        
        return lexeme(intDouble) <?> LocalizedString("number")
    }
    
    /// Parses a positive whole number in the decimal system. Returns the value
    /// of the number.
    public static var decimal: GenericParser<String, UserState, Int> {
        
        return numberWithBase(10, parser: GenericParser.decimalDigit)
        
    }
    
    /// Parses a positive whole number in the hexadecimal system. The number
    /// should be prefixed with "x" or "X". Returns the value of the number.
    public static var hexadecimal: GenericParser<String, UserState, Int> {
        
        return GenericParser.oneOf(hexadecimalPrefixes) *>
            numberWithBase(16, parser: GenericParser.hexadecimalDigit)
        
    }
    
    /// Parses a positive whole number in the octal system. The number should be
    /// prefixed with "o" or "O". Returns the value of the number.
    public static var octal: GenericParser<String, UserState, Int> {
        
        return GenericParser.oneOf(octalPrefixes) *>
            numberWithBase(8, parser: GenericParser.octalDigit)
        
    }
    
    //
    // White space & symbols
    //
    
    /// Lexeme parser `symbol(str)` parses `str` and skips trailing white space.
    ///
    /// - parameter name: The name of the symbol to parse.
    /// - returns: `name`.
    public func symbol(
        _ name: String
    ) -> GenericParser<String, UserState, String> {
        
        return lexeme(StrParser.string(name))
        
    }
    
    /// `lexeme(parser)` first applies `parser` and than the `whiteSpace`
    /// parser, returning the value of `parser`. Every lexical token (lexeme) is
    /// defined using `lexeme`, this way every parse starts at a point without
    /// white space. Parsers that use `lexeme` are called _lexeme_ parsers in
    /// this document.
    ///
    /// The only point where the 'whiteSpace' parser should be called explicitly
    /// is the start of the main parser in order to skip any leading white
    /// space.
    ///
    ///     let mainParser = sum <^> whiteSpace *> lexeme(digit) <* eof
    ///
    /// - parameter parser: The parser to transform in a 'lexeme'.
    /// - returns: The value of `parser`.
    public func lexeme<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result> {
        
        return parser <* whiteSpace
        
    }
    
    /// Parses any white space. White space consists of _zero_ or more
    /// occurrences of a 'space', a line comment or a block (multiline) comment.
    /// Block comments may be nested. How comments are started and ended is
    /// defined in the `LanguageDefinition` that is passed to the initializer of
    /// this token parser.
    public var whiteSpace: GenericParser<String, UserState, ()> {
        
        let simpleSpace = CharacterParser.satisfy({ $0.isSpace }).skipMany1
        
        let commentLineEmpty = languageDefinition.commentLine.isEmpty
        let commentStartEmpty = languageDefinition.commentStart.isEmpty
        
        if commentLineEmpty && commentStartEmpty {
            
            return (simpleSpace <?> "").skipMany
            
        }
        
        if commentLineEmpty {
            
            return (simpleSpace <|> multiLineComment <?> "").skipMany
            
        }
        
        if commentStartEmpty {
            
            return (simpleSpace <|> oneLineComment <?> "").skipMany
            
        }
        
        return (
            simpleSpace <|> oneLineComment <|> multiLineComment <?> ""
        ).skipMany
        
    }
    
    //
    // Bracketing
    //
    
    /// Lexeme parser `parentheses(parser)` parses `parser` enclosed in
    /// parentheses, returning the value of `parser`.
    ///
    /// - parameter parser: The parser applied between the parentheses.
    /// - returns: The value of `parser`.
    public func parentheses<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result> {
        
        return parser.between(symbol("("), symbol(")"))
        
    }
    
    /// Lexeme parser `braces(parser)` parses `parser` enclosed in braces "{"
    /// and "}", returning the value of `parser`.
    ///
    /// - parameter parser: The parser applied between the braces.
    /// - returns: The value of `parser`.
    public func braces<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result> {
        
        return parser.between(symbol("{"), symbol("}"))
        
    }
    
    /// Lexeme parser `angles(parser)` parses `parser` enclosed in angle
    /// brackets "<" and ">", returning the value of `parser`.
    ///
    /// - parameter parser: The parser applied between the angles.
    /// - returns: The value of `parser`.
    public func angles<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result> {
        
        return parser.between(symbol("<"), symbol(">"))
        
    }
    
    /// Lexeme parser `brackets(parser)` parses `parser` enclosed in brackets
    /// "[" and "]", returning the value of `parser`.
    ///
    /// - parameter parser: The parser applied between the brackets.
    /// - returns: The value of `parser`.
    public func brackets<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, Result> {
        
        return parser.between(symbol("["), symbol("]"))
        
    }
    
    /// Lexeme parser `semicolon` parses the character ";" and skips any
    /// trailing white space. Returns the string ";".
    public var semicolon: GenericParser<String, UserState, String> {
        
        return symbol(";")
        
    }
    
    /// Lexeme parser `comma` parses the character "," and skips any trailing
    /// white space. Returns the string ",".
    public var comma: GenericParser<String, UserState, String> {
        
        return symbol(",")
        
    }
    
    /// Lexeme parser `colon` parses the character ":" and skips any trailing
    /// white space. Returns the string ":".
    public var colon: GenericParser<String, UserState, String> {
        
        return symbol(":")
        
    }
    
    /// Lexeme parser `dot` parses the character "." and skips any trailing
    /// white space. Returns the string ".".
    public var dot: GenericParser<String, UserState, String> {
        
        return symbol(".")
        
    }
    
    /// Lexeme parser `semicolonSeperated(parser)` parses _zero_ or more
    /// occurrences of `parser` separated by `semicolon`. Returns an array of
    /// values returned by `parser`.
    ///
    /// - parameter parser: The parser applied between semicolons.
    /// - returns: An array of values returned by `parser`.
    public func semicolonSeparated<Result>(
        _ parser: GenericParser<String, UserState, Result>
    ) -> GenericParser<String, UserState, [Result]> {
        
        return parser.separatedBy(semicolon)
        
    }
    
    /// Lexeme parser `semicolonSeperated1(parser)` parses _one_ or more
    /// occurrences of `parser` separated by `semicolon`. Returns an array of
    /// values returned by `parser`.
    ///
    /// - parameter parser: The parser applied between semicolons.
    /// - returns: An array of
Download .txt
gitextract_e2cprpt0/

├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── LICENSE
├── Package.swift
├── README.md
├── Sources/
│   └── SwiftParsec/
│       ├── CharacterConversion.swift
│       ├── CharacterMembership.swift
│       ├── CharacterParsers.swift
│       ├── CharacterSet.swift
│       ├── CollectionAggregation.swift
│       ├── CombinatorParsers.swift
│       ├── Configuration.swift
│       ├── Either.swift
│       ├── ExpressionParser.swift
│       ├── GenericParser.swift
│       ├── GenericTokenParser.swift
│       ├── Info.plist
│       ├── LanguageDefinition.swift
│       ├── ParseError.swift
│       ├── Parsec.swift
│       ├── Permutation.swift
│       ├── Position.swift
│       ├── RangeReplaceableCollectionInsertion.swift
│       ├── SequenceAggregation.swift
│       ├── SequenceConversion.swift
│       ├── SetAggregation.swift
│       ├── String.swift
│       ├── TokenParser.swift
│       ├── UInt16.swift
│       ├── UnicodeScalar.swift
│       └── en.lproj/
│           └── Localizable.strings
├── SubmittingPatches.md
├── SwiftParsec.podspec
├── SwiftParsec.xcodeproj/
│   ├── project.pbxproj
│   ├── project.xcworkspace/
│   │   ├── contents.xcworkspacedata
│   │   └── xcshareddata/
│   │       └── IDEWorkspaceChecks.plist
│   └── xcshareddata/
│       └── xcschemes/
│           └── SwiftParsec.xcscheme
├── Tests/
│   ├── LinuxMain.swift
│   └── SwiftParsecTests/
│       ├── CharacterParsersTests.swift
│       ├── CharacterSetTests.swift
│       ├── CombinatorParsersTests.swift
│       ├── ErrorMessageTest.swift
│       ├── ExpressionParserTests.swift
│       ├── GenericParserTests.swift
│       ├── GenericTokenParserTests.swift
│       ├── Info.plist
│       ├── JSONBenchmarkTests.swift
│       ├── PermutationTests.swift
│       ├── PositionTests.swift
│       ├── SampleJSON.json
│       ├── StringTests.swift
│       ├── TestUtilities.swift
│       └── UnicodeScalarTests.swift
└── scripts/
    ├── install-swift.sh
    └── run-tests.sh
Condensed preview — 56 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (648K chars).
[
  {
    "path": ".gitignore",
    "chars": 1203,
    "preview": "# Xcode\n#\n# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore\n\n"
  },
  {
    "path": ".travis.yml",
    "chars": 850,
    "preview": "os: osx\nosx_image: xcode8\nlanguage: objective-c\n\n\nmatrix:\n  include:\n    - env: XCODE_DESTINATION=\"platform=iOS Simulato"
  },
  {
    "path": "CHANGELOG.md",
    "chars": 2224,
    "preview": "# Release 4.0.1\n\nFix compilation error in Xcode 12.5\n\n# Release 4.0.0\n\nMigrated source code to Swift 5.0\n\n# Release 3.0."
  },
  {
    "path": "LICENSE",
    "chars": 1290,
    "preview": "Copyright (c) 2015, David Dufresne\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or with"
  },
  {
    "path": "Package.swift",
    "chars": 661,
    "preview": "// swift-tools-version:4.2\n// The swift-tools-version declares the minimum version of Swift required to build this packa"
  },
  {
    "path": "README.md",
    "chars": 789,
    "preview": "# SwiftParsec\nSwiftParsec is a Swift port of the [Parsec](https://github.com/aslatter/parsec) parser combinator library."
  },
  {
    "path": "Sources/SwiftParsec/CharacterConversion.swift",
    "chars": 1060,
    "preview": "//==============================================================================\n// CharacterConversion.swift\n// SwiftPa"
  },
  {
    "path": "Sources/SwiftParsec/CharacterMembership.swift",
    "chars": 5332,
    "preview": "//==============================================================================\n// CharacterMembership.swift\n// SwiftPa"
  },
  {
    "path": "Sources/SwiftParsec/CharacterParsers.swift",
    "chars": 11747,
    "preview": "//==============================================================================\n// CharacterParsers.swift\n// SwiftParse"
  },
  {
    "path": "Sources/SwiftParsec/CharacterSet.swift",
    "chars": 2641,
    "preview": "//==============================================================================\n// CharacterSet.swift\n// SwiftParsec\n//"
  },
  {
    "path": "Sources/SwiftParsec/CollectionAggregation.swift",
    "chars": 1298,
    "preview": "//==============================================================================\n// CollectionAggregation.swift\n// Swift"
  },
  {
    "path": "Sources/SwiftParsec/CombinatorParsers.swift",
    "chars": 18390,
    "preview": "//==============================================================================\n// CombinatorParsers.swift\n// SwiftPars"
  },
  {
    "path": "Sources/SwiftParsec/Configuration.swift",
    "chars": 1377,
    "preview": "//==============================================================================\n// Configuration.swift\n// SwiftParsec\n/"
  },
  {
    "path": "Sources/SwiftParsec/Either.swift",
    "chars": 666,
    "preview": "//==============================================================================\n// Either.swift\n// SwiftParsec\n//\n// Cr"
  },
  {
    "path": "Sources/SwiftParsec/ExpressionParser.swift",
    "chars": 13626,
    "preview": "//==============================================================================\n// Expression.swift\n// SwiftParsec\n//\n/"
  },
  {
    "path": "Sources/SwiftParsec/GenericParser.swift",
    "chars": 42353,
    "preview": "//==============================================================================\n// Primitive.swift\n// SwiftParsec\n//\n//"
  },
  {
    "path": "Sources/SwiftParsec/GenericTokenParser.swift",
    "chars": 1518,
    "preview": "//==============================================================================\n// GenericTokenParser.swift\n// SwiftPar"
  },
  {
    "path": "Sources/SwiftParsec/Info.plist",
    "chars": 823,
    "preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/P"
  },
  {
    "path": "Sources/SwiftParsec/LanguageDefinition.swift",
    "chars": 14922,
    "preview": "//==============================================================================\n// LanguageDefinition.swift\n// SwiftPar"
  },
  {
    "path": "Sources/SwiftParsec/ParseError.swift",
    "chars": 9272,
    "preview": "//==============================================================================\n// Error.swift\n// SwiftParsec\n//\n// Cre"
  },
  {
    "path": "Sources/SwiftParsec/Parsec.swift",
    "chars": 16045,
    "preview": "//==============================================================================\n// Parsec.swift\n// SwiftParsec\n//\n// Cr"
  },
  {
    "path": "Sources/SwiftParsec/Permutation.swift",
    "chars": 7006,
    "preview": "//==============================================================================\n// Permutation.swift\n// SwiftParsec\n//\n"
  },
  {
    "path": "Sources/SwiftParsec/Position.swift",
    "chars": 2998,
    "preview": "//==============================================================================\n// Position.swift\n// SwiftParsec\n//\n// "
  },
  {
    "path": "Sources/SwiftParsec/RangeReplaceableCollectionInsertion.swift",
    "chars": 1690,
    "preview": "//==============================================================================\n// RangeReplaceableCollectionInsertion."
  },
  {
    "path": "Sources/SwiftParsec/SequenceAggregation.swift",
    "chars": 2309,
    "preview": "//==============================================================================\n// SequenceAggregation.swift\n// SwiftPa"
  },
  {
    "path": "Sources/SwiftParsec/SequenceConversion.swift",
    "chars": 775,
    "preview": "//==============================================================================\n// SequenceConversion.swift\n// SwiftPar"
  },
  {
    "path": "Sources/SwiftParsec/SetAggregation.swift",
    "chars": 1026,
    "preview": "//==============================================================================\n// SetAggregation.swift\n// SwiftParsec\n"
  },
  {
    "path": "Sources/SwiftParsec/String.swift",
    "chars": 1937,
    "preview": "//==============================================================================\n// String.swift\n// SwiftParsec\n//\n// Cr"
  },
  {
    "path": "Sources/SwiftParsec/TokenParser.swift",
    "chars": 42545,
    "preview": "//==============================================================================\n// TokenParser.swift\n// SwiftParsec\n//\n"
  },
  {
    "path": "Sources/SwiftParsec/UInt16.swift",
    "chars": 705,
    "preview": "//==============================================================================\n// UInt16.swift\n// SwiftParsec\n//\n// Cr"
  },
  {
    "path": "Sources/SwiftParsec/UnicodeScalar.swift",
    "chars": 1942,
    "preview": "//==============================================================================\n// UnicodeScalar.swift\n// SwiftParsec\n/"
  },
  {
    "path": "SubmittingPatches.md",
    "chars": 5159,
    "preview": "# Git Commit\n\nSome wise advice gathered from multiple sources on submitting patches.\n\n## Describe your changes\n\nDescribe"
  },
  {
    "path": "SwiftParsec.podspec",
    "chars": 633,
    "preview": "Pod::Spec.new do |s|\n  s.name = 'SwiftParsec'\n  s.version = '4.0.0'\n  s.license = '2-clause BSD'\n  s.summary = 'SwiftPar"
  },
  {
    "path": "SwiftParsec.xcodeproj/project.pbxproj",
    "chars": 37140,
    "preview": "// !$*UTF8*$!\n{\n\tarchiveVersion = 1;\n\tclasses = {\n\t};\n\tobjectVersion = 51;\n\tobjects = {\n\n/* Begin PBXBuildFile section *"
  },
  {
    "path": "SwiftParsec.xcodeproj/project.xcworkspace/contents.xcworkspacedata",
    "chars": 156,
    "preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Workspace\n   version = \"1.0\">\n   <FileRef\n      location = \"self:SwiftParsec.xco"
  },
  {
    "path": "SwiftParsec.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist",
    "chars": 238,
    "preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/P"
  },
  {
    "path": "SwiftParsec.xcodeproj/xcshareddata/xcschemes/SwiftParsec.xcscheme",
    "chars": 3631,
    "preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Scheme\n   LastUpgradeVersion = \"1210\"\n   version = \"1.3\">\n   <BuildAction\n      "
  },
  {
    "path": "Tests/LinuxMain.swift",
    "chars": 576,
    "preview": "import XCTest\n@testable import SwiftParsecTests\n\nXCTMain([\n    testCase(CharacterParsersTests.allTests),\n    testCase(Ch"
  },
  {
    "path": "Tests/SwiftParsecTests/CharacterParsersTests.swift",
    "chars": 28214,
    "preview": "//==============================================================================\n// CharacterParsersTests.swift\n// Swift"
  },
  {
    "path": "Tests/SwiftParsecTests/CharacterSetTests.swift",
    "chars": 1564,
    "preview": "//==============================================================================\n// CharacterSetTests.swift\n// SwiftPars"
  },
  {
    "path": "Tests/SwiftParsecTests/CombinatorParsersTests.swift",
    "chars": 36209,
    "preview": "//==============================================================================\n// CombinatorParsersTests.swift\n// Swif"
  },
  {
    "path": "Tests/SwiftParsecTests/ErrorMessageTest.swift",
    "chars": 10585,
    "preview": "//==============================================================================\n// ErrorMessageTest.swift\n// SwiftParse"
  },
  {
    "path": "Tests/SwiftParsecTests/ExpressionParserTests.swift",
    "chars": 4294,
    "preview": "//==============================================================================\n// ExpressionParserTests.swift\n// Swift"
  },
  {
    "path": "Tests/SwiftParsecTests/GenericParserTests.swift",
    "chars": 18807,
    "preview": "//==============================================================================\n// GenericParserTests.swift\n// SwiftPar"
  },
  {
    "path": "Tests/SwiftParsecTests/GenericTokenParserTests.swift",
    "chars": 43895,
    "preview": "//==============================================================================\n// DefaultTokenParserTests.swift\n// Swi"
  },
  {
    "path": "Tests/SwiftParsecTests/Info.plist",
    "chars": 733,
    "preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/P"
  },
  {
    "path": "Tests/SwiftParsecTests/JSONBenchmarkTests.swift",
    "chars": 6190,
    "preview": "//==============================================================================\n// BenchmarkTests.swift\n// SwiftParsec\n"
  },
  {
    "path": "Tests/SwiftParsecTests/PermutationTests.swift",
    "chars": 12200,
    "preview": "//==============================================================================\n// PermutationTests.swift\n// SwiftParse"
  },
  {
    "path": "Tests/SwiftParsecTests/PositionTests.swift",
    "chars": 4037,
    "preview": "//==============================================================================\n// PositionTests.swift\n// SwiftParsec\n/"
  },
  {
    "path": "Tests/SwiftParsecTests/SampleJSON.json",
    "chars": 169269,
    "preview": "{\n    \"name\":\"Product\",\n    \"properties\":\n    {\n        \"id\":\n        {\n            \"type\":\"number\",\n            \"descri"
  },
  {
    "path": "Tests/SwiftParsecTests/StringTests.swift",
    "chars": 866,
    "preview": "//==============================================================================\n// StringTests.swift\n// SwiftParsec\n//\n"
  },
  {
    "path": "Tests/SwiftParsecTests/TestUtilities.swift",
    "chars": 2889,
    "preview": "//==============================================================================\n// TestUtilities.swift\n// SwiftParsec\n/"
  },
  {
    "path": "Tests/SwiftParsecTests/UnicodeScalarTests.swift",
    "chars": 2560,
    "preview": "//==============================================================================\n// UnicodeScalarTests.swift\n// SwiftPar"
  },
  {
    "path": "scripts/install-swift.sh",
    "chars": 410,
    "preview": "#!/bin/bash\n\nset -ev\n\nSWIFT_VERSION=\"3.0.2\"\nSWIFT_URL=\"https://swift.org/builds/swift-${SWIFT_VERSION}-release/ubuntu140"
  },
  {
    "path": "scripts/run-tests.sh",
    "chars": 225,
    "preview": "#!/bin/bash\n\nset -ev\n\nif [ ! -z \"${XCODE_DESTINATION}\" ]; then\n  PARSEC=\"-project SwiftParsec.xcodeproj -scheme SwiftPar"
  }
]

// ... and 1 more files (download for full content)

About this extraction

This page contains the full source code of the davedufresne/SwiftParsec GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 56 files (587.4 KB), approximately 133.6k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!