 /// A Parser using rules made of composable functions.
 class Parser
 {
-    typealias Value  = AnyObject
+    /// —— Public Types ——
+
+    // Values that occur during parsing.  Those are terminal values
+    // from the lexer as well as parse results of nonterminal rules
+    // and return values from your action code blocks.
+    typealias Value  = NSObject
+
+    // Actual rule result type includes the possibility of failure.
     typealias Result = Value?
+
+    // Interface that lets your action code blocks access the parse
+    // results of previous elements in the current nonterminal rule.
+    // Naming the Getter as `$`, you access the first value with
+    // `$(1)`.  `$(0)` would access the last value that was parsed
+    // before the current rule became active.  It is your responsibility
+    // to keep the index argument to the Getter in a sensible range.
     typealias Getter = (Int) -> Value
+
+    // Your action code blocks.  Note that you may return nil to signal failure.
     typealias Action = (Getter) -> Result
 
+    /// —— Public Methods ——
+
+    /// Constructs a parser for a grammar with the given terminal symbols.
+    init (terminals: String[])
+    {
+        _tsRule = Dictionary()
+        _ntRule = Dictionary()
+        _values = []
+        _stkptr = 0
+        _ntbase = 0
+
+        for name in terminals
+        {
+            _tsRule[name] = true
+        }
+    }
+
+    /// Defines a parsing rule for the named nonterminal in the grammar.
+    func rule (name: String, parses rule: Rule)
+    {
+        _ntRule[name] = rule
+    }
+
+    /// Defines a parsing rule for the named nonterminal in the grammar.
+    func rule (name: String, parses symbol: String)
+    {
+        rule(name, parses: .RulePromise(symbol))
+    }
+
+    /// Parses the defined grammar starting with the given rule.
+    func start (rule: Rule, _ lexer: Lexer)
+    -> Result
+    {
+        _values = []
+        _stkptr = 0
+        _ntbase = 0
+
+        return execute(compile(rule), lexer)
+    }
+
+    /// Parses the defined grammar with the given start symbol.
+    func start (symbol: String, _ lexer: Lexer)
+    -> Result
+    {
+        return start(.RulePromise(symbol), lexer)
+    }
+
+    /// —— Private Parts ——
+
     // Abstraction for nonterminal elements.
-    enum Part
+    enum Rule
     {
         case RulePromise(String)
         case TerminalLex(String)
-        case NestedParse(Part[])    // Boxing Array
-        case Conjunction(Part[])    // Actual Array
-        case Disjunction(Part[])    // Actual Array
+        case NestedParse(Rule[])    // Boxing Array
+        case Conjunction(Rule[])    // Actual Array
+        case Disjunction(Rule[])    // Actual Array
         case Computation(Action[])  // Boxing Array
-        case Termination(Part[])    // Boxing Array
-    }
-
-    var _ruleMap: Dictionary<String, Bool>
-    var _partMap: Dictionary<String, Part>
-    var _values:  Value[]
-    var _stkptr:  Int
-    var _ntbase:  Int
+        case Termination(Rule[])    // Boxing Array
+    }
+
+    var _tsRule: Dictionary<String, Bool>
+    var _ntRule: Dictionary<String, Rule>
+    var _values: Value[]
+    var _stkptr: Int
+    var _ntbase: Int
+
+    // Getter for the current NT's parse results.
+    @lazy var getter: Getter = { [unowned self] index in self._values[self._ntbase + index - 1] }
 
     func store (value: Value)
     {

        ++_stkptr
    }

    // Rule interpreter.
    func execute (part: Part, _ lexer: Lexer)
    -> Result
    {
        func resolve (name: String) -> Part
        {
            if let _    = _ruleMap[name] { return .TerminalLex(name)   }
            if let part = _partMap[name] { return .NestedParse([part]) }

            println("Parser error: Undefined symbol “\(name)”")
            return .Disjunction([])     // Constant Failure.
        }
        
        func element (body: Part[], index: Int) -> Part
        {
            var part = body[index]
            switch part
            {
            case .RulePromise(let name) :
                part = resolve(name)
                body[index] = part
            default :
                nil
            }
            return part
        }

        switch part
        {
        case .RulePromise(let name) :
            return execute(resolve(name), lexer)
            
        case .TerminalLex(let name) :
            if let (term, value: Value) = lexer.next()
            {
                if term == name
                {
                    store(value)
                    return value

/// =<`value`   (an Unconditional Success rule)
/// Successfully parses an empty slice of input and returns the given value.
@prefix func =< (value: Parser.Value)
-> Parser.Rule
{
    return .Computation([{ _ in value }])
}

/// =!`action`   (an Unconditional Success rule)
/// Successfully parses an empty slice of input and returns the action's value.
@prefix func =! (block: Parser.Action)
-> Parser.Rule
{
    return .Computation([block])
}

/// `rule` &> `rule`
/// Attempts to parse the conjunction of LHS and RHS sequentially.
@infix func &> (lhsRule: Parser.Rule, rhsRule: Parser.Rule)
-> Parser.Rule
{
    switch lhsRule
    {
    case .Conjunction(let lhsBody) :
        var body = lhsBody.copy()
        switch rhsRule
        {
        case .Conjunction(let rhsBody) :
            body.extend(rhsBody)
            return .Conjunction(body)

        default :
            body.append(rhsRule)
            return .Conjunction(body)
        }

    default :
        switch rhsRule
        {
        case .Conjunction(let rhsBody) :
            var body = rhsBody.copy()
            body.insert(lhsRule, atIndex: 0)
            return .Conjunction(body)

        default :
            return .Conjunction([lhsRule, rhsRule])
        }
    }
}

@infix func &> (lhsRule: Parser.Rule, rhsBlock: Parser.Action)
-> Parser.Rule
{
    return (lhsRule &> .Computation([rhsBlock]))
}

@infix func &> (lhsRule: Parser.Rule, rhsName: String)
-> Parser.Rule
{
    return (lhsRule &> .RulePromise(rhsName))
}

@infix func &> (lhsBlock: Parser.Action, rhsRule: Parser.Rule)
-> Parser.Rule
{
    return (.Computation([lhsBlock]) &> rhsRule)
}

@infix func &> (lhsBlock: Parser.Action, rhsName: String)
-> Parser.Rule
{
    return (.Computation([lhsBlock]) &> .RulePromise(rhsName))
}

@infix func &> (lhsName: String, rhsRule: Parser.Rule)
-> Parser.Rule
{
    return (.RulePromise(lhsName) &> rhsRule)
}

@infix func &> (lhsName: String, rhsBlock: Parser.Action)
-> Parser.Rule
{
    return (.RulePromise(lhsName) &> .Computation([rhsBlock]))
}

@infix func &> (lhsName: String, rhsName: String)
-> Parser.Rule
{
    return .Conjunction([.RulePromise(lhsName), .RulePromise(rhsName)])
}

/// `rule` |> `rule`
/// Attempts to parse LHS first and RHS afterwards, if LHS fails.
@infix func |> (lhsRule: Parser.Rule, rhsRule: Parser.Rule)
-> Parser.Rule
{
    switch lhsRule
    {
    case .Disjunction(let lhsBody) :
        var body = lhsBody.copy()
        switch rhsRule
        {
        case .Disjunction(let rhsBody) :
            body.extend(rhsBody)
            return .Disjunction(body)

        default :
            body.append(rhsRule)
            return .Disjunction(body)
        }

    default :
        switch rhsRule
        {
        case .Disjunction(let rhsBody) :
            var body = rhsBody.copy()
            body.insert(lhsRule, atIndex: 0)
            return .Disjunction(body)

        default :
            return .Disjunction([lhsRule, rhsRule])
        }
    }
}

@infix func |> (lhsRule: Parser.Rule, rhsBlock: Parser.Action)
-> Parser.Rule
{
    return (lhsRule |> .Computation([rhsBlock]))
}

@infix func |> (lhsRule: Parser.Rule, rhsName: String)
-> Parser.Rule
{
    return (lhsRule |> .RulePromise(rhsName))
}

@infix func |> (lhsBlock: Parser.Action, rhsRule: Parser.Rule)
-> Parser.Rule
{
    return (.Computation([lhsBlock]) |> rhsRule)
}

@infix func |> (lhsBlock: Parser.Action, rhsName: String)
-> Parser.Rule
{
    return (.Computation([lhsBlock]) |> .RulePromise(rhsName))
}

@infix func |> (lhsName: String, rhsRule: Parser.Rule)
-> Parser.Rule
{
    return (.RulePromise(lhsName) |> rhsRule)
}

@infix func |> (lhsName: String, rhsBlock: Parser.Action)
-> Parser.Rule
{
    return (.RulePromise(lhsName) |> .Computation([rhsBlock]))
}

@infix func |> (lhsName: String, rhsName: String)
-> Parser.Rule
{
    return .Disjunction([.RulePromise(lhsName), .RulePromise(rhsName)])
}

/// `rule`<!
/// Asserts End-Of-Input (EOI) after parsing the preceding rule.
@postfix func <! (rule: Parser.Rule)
-> Parser.Rule
{
    return .Termination([rule])
}

@postfix func <! (name: String)
-> Parser.Rule
{
    return .Termination([.RulePromise(name)])
}

@postfix func <! (block: Parser.Action)
-> Parser.Rule
{
    return .Termination([.Computation([block])])
}

/// `rule` <! `action`
/// Asserts EOI after parsing the rule, then returns the action's value.
@infix func <! (condition: Parser.Rule, block: Parser.Action)
-> Parser.Rule
{
    return (.Termination([condition]) &> .Computation([block]))
}

@infix func <! (condName: String, block: Parser.Action)
-> Parser.Rule
{
    return (.Termination([.RulePromise(condName)]) &> .Computation([block]))
}

/// Sequence construction utility for use in rule actions.
