1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
extension PEG {
struct Grammar {
var environment: Dictionary<String, Pattern>
var start: Pattern
}
struct Interpreter<C: Collection> where C.Element == Element {
var grammar: Grammar
func consume(_ c: C, from: C.Index) -> C.Index? {
fatalError()
}
func consume(_ c: C) -> C.Index? {
consume(c, from: c.startIndex)
}
func find(_ c: C) -> Range<C.Index>? {
// TODO: more efficient to have native support
var idx = c.startIndex
while idx != c.endIndex {
if let endIdx = consume(c, from: idx) {
return idx ..< endIdx
}
c.formIndex(after: &idx)
}
return nil
}
}
}
extension PEG.Program {
func consume<C: Collection>(
_ c: C
) -> C.Index? where C.Element == Element {
_consume(c, from: c.startIndex, environment[start]!, environment)
}
}
func _consume<C: Collection>(
_ c: C,
from idx: C.Index,
_ pattern: PEG<C.Element>.Pattern,
_ environment: PEG<C.Element>.Environment
) -> C.Index? {
var captures = Array<Range<C.Index>>()
return _consume(c, from: idx, pattern, environment, &captures)
}
func _consume<C: Collection>(
_ c: C,
from idx: C.Index,
_ pattern: PEG<C.Element>.Pattern,
_ environment: PEG<C.Element>.Environment,
_ captures: inout Array<Range<C.Index>>
) -> C.Index? {
typealias Pattern = PEG<C.Element>.Pattern
var idx = idx
func consume(_ p: Pattern, from idx: C.Index) -> C.Index? {
_consume(c, from: idx, p, environment, &captures)
}
func consumeIf(_ p: (C.Element) -> Bool) -> C.Index? {
guard idx < c.endIndex, p(c[idx]) else { return nil }
return c.index(after: idx)
}
func consumePrefix<Prefix: Sequence>(
_ p: Prefix
) -> C.Index? where Prefix.Element == C.Element {
var idx = idx
for e in p {
guard idx < c.endIndex, e == c[idx] else { return nil }
c.formIndex(after: &idx)
}
return idx
}
func consumeMany(_ p: Pattern, atLeast: Int) -> C.Index? {
var counter = 0
var idx = idx
while let nextIdx = consume(p, from: idx) {
counter += 1
idx = nextIdx
}
guard counter >= atLeast else { return nil }
return idx
}
func peek(_ p: Pattern) -> Bool {
nil != consume(p, from: idx)
}
switch pattern {
// Terminals
case .success: return idx
case .failure: return nil
case .end: return idx == c.endIndex ? idx : nil
// Consume a single unit of input
case .any: return consumeIf { _ in true }
case .element(let e): return consumeIf { e == $0 }
case .charactetSet(let p): return consumeIf(p)
// Consume many inputs
case .literal(let lit): return consumePrefix(lit)
case .repeat(let e, atLeast: let atLeast):
return consumeMany(e, atLeast: atLeast)
case .repeatRange(_, atLeast: _, atMost: _):
fatalError()
// Assertions (does not consume)
case .and(let p):
return peek(p) ? idx : nil
case .not(let p):
return peek(p) ? nil : idx
// Combinations of patterns
case .orderedChoice(let p1, let p2):
return consume(p1, from: idx) ?? consume(p2, from: idx)
case .concat(let ps):
for p in ps {
guard let resume = consume(p, from: idx) else {
return nil
}
idx = resume
}
return idx
case .difference(let p1, let p2):
guard !peek(p2) else { return nil }
return consume(p1, from: idx)
case .variable(let name):
return consume(environment[name]!, from: idx)
case .capture(let p):
// TODO: Are captured pre-order or post-order?
guard let endIdx = consume(p, from: idx) else { return nil }
captures.append(idx ..< endIdx)
return endIdx
}
}
// Match some subsequence within the collection
func _find<C: Collection>(
_ c: C,
from idx: C.Index,
_ pattern: PEG<C.Element>.Pattern,
_ environment: PEG<C.Element>.Environment,
_ captures: inout Array<Range<C.Index>>
) -> Range<C.Index>? {
// TODO: more efficient to have native support
var idx = c.startIndex
while idx != c.endIndex {
if let endIdx = _consume(c, from: idx, pattern, environment, &captures) {
return idx ..< endIdx
}
c.formIndex(after: &idx)
}
return nil
}
// Convenience: gather up the result of repeated consumption
func _gather<C: Collection>(
_ c: C,
_ pattern: PEG<C.Element>.Pattern,
_ environment: PEG<C.Element>.Environment
) -> [C.SubSequence] {
var idx = c.startIndex
var result = Array<C.SubSequence>()
while let end = _consume(c, from: idx, pattern, environment) {
result.append(c[idx ..< end])
idx = end
}
return result
}
|