more mention progress

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-05-07 13:50:19 -07:00
parent 73652513d9
commit 0eb1372937
21 changed files with 611 additions and 144 deletions

View File

@@ -7,7 +7,41 @@
import Foundation
class Parser {
var pos: Int
var str: String
init(pos: Int, str: String) {
self.pos = pos
self.str = str
}
}
func consume_until(_ p: Parser, match: (Character) -> Bool) -> Bool {
var i: Int = 0
let sub = substring(p.str, start: p.pos, end: p.str.count)
for c in sub {
if match(c) {
p.pos += i
return true
}
i += 1
}
return false
}
func substring(_ s: String, start: Int, end: Int) -> Substring {
let ind = s.index(s.startIndex, offsetBy: start)
let end = s.index(s.startIndex, offsetBy: end)
return s[ind..<end]
}
func parse_str(_ p: Parser, _ s: String) -> Bool {
if p.pos + s.count > p.str.count {
return false
}
let sub = substring(p.str, start: p.pos, end: p.pos + s.count)
if sub == s {
p.pos += s.count
@@ -16,7 +50,7 @@ func parse_str(_ p: Parser, _ s: String) -> Bool {
return false
}
func parse_char(_ p: Parser, _ c: Character) -> Bool{
func parse_char(_ p: Parser, _ c: Character) -> Bool {
let ind = p.str.index(p.str.startIndex, offsetBy: p.pos)
if p.str[ind] == c {
@@ -40,3 +74,19 @@ func parse_digit(_ p: Parser) -> Int? {
return 0
}
func parse_hex_char(_ p: Parser) -> Character? {
let ind = p.str.index(p.str.startIndex, offsetBy: p.pos)
if let c = p.str[ind].unicodeScalars.first {
// hex chars
let d = c.value
if (d >= 48 && d <= 57) || (d >= 97 && d <= 102) || (d >= 65 && d <= 70) {
p.pos += 1
return p.str[ind]
}
}
return nil
}