scan.bend checks
raw source on the hub · import gralha-text-scan@0.1.0.1/scan.bend as Scan
Text scanning, number parsing (the SVG/CSS number grammar, units, hex) and small helpers over strings, U32 and F32.
1 import
import Base
Types
type Scan source · line 62 · raw
Data
Scan@head:String -> @tail:String -> Scan
type Number source · line 135 · raw
Data
Number@value:F32 -> @tail:String -> @valid:Bool -> Number
Definitions
def choose source · line 5 · raw
@-A:Type -> @yes:Bool -> @a:A -> @b:A -> A
def max.u32 source · line 10 · raw
@+a:U32 -> @+b:U32 -> U32
def min.u32 source · line 13 · raw
@+a:U32 -> @+b:U32 -> U32
def min source · line 16 · raw
@+a:F32 -> @+b:F32 -> F32
def max source · line 19 · raw
@+a:F32 -> @+b:F32 -> F32
def clamp source · line 22 · raw
@x:F32 -> @lo:F32 -> @hi:F32 -> F32
def code source · line 25 · raw
@c:Char -> U32
def space source · line 29 · raw
@+c:U32 -> Bool
def digit source · line 32 · raw
@+c:U32 -> Bool
def rev source · line 35 · raw
@s:String -> @acc:String -> String
def string.equal.go source · line 42 · raw
@a:String -> @b:String -> @same:Bool -> Bool
same says whether the characters before a and b matched: deciding
it one step ahead keeps the loop a tail call on the shrinking a.
def string.equal source · line 48 · raw
@a:String -> @b:String -> Bool
def branch source · line 51 · raw
@-A:Type -> @yes:Bool -> @a:(@_:Unit -> A) -> @b:(@_:Unit -> A) -> A
def trim source · line 56 · raw
@+s:String -> String
def until.go source · line 69 · raw
@t:String -> @hit:Bool -> @+c:U32 -> @+end:U32 -> @acc:String -> Scan
Scanning loops decide each character one step ahead (hit says whether
c, the character before t, ends the scan), so the rest of the text is
only ever held once: a shared tail would make every step pay for the
sharing. The rest comes first, as the argument that shrinks.
def until source · line 77 · raw
@s:String -> @+end:U32 -> @acc:String -> Scan
def prefix source · line 84 · raw
@s:String -> @p:String -> Bool
def drop source · line 91 · raw
@s:String -> @p:String -> String
def through source · line 97 · raw
@+s:String -> @+p:String -> String
def until.sequence source · line 103 · raw
@+s:String -> @+end:String -> @+acc:String -> Scan
def separator source · line 109 · raw
@+c:U32 -> Bool
SVG number grammar, including adjacent signs and scientific notation.
def separators.go source · line 112 · raw
@t:String -> @skip:Bool -> @+c:U32 -> String
def separators source · line 120 · raw
@s:String -> String
def number.exponent source · line 127 · raw
@+s:String -> Bool
def number.class source · line 147 · raw
@+c:U32 -> U32
A number is read straight into a float: digits accumulate the mantissa
(fraction digits also scale a divisor), an exponent accumulates its own
digits, and a sign is taken where it may appear. Nothing is copied into a
scratch string. Each character is classified one step ahead, so the loop
branches on parameters: class 0 digit, 1 dot, 2 exponent mark, 3 sign,
4 anything else; stage 0 start, 1 mantissa, 2 fraction, 3 after the
mark, 4 exponent digits. An exponent mark that no digit follows is not
part of the number (as in "1em"), so mark keeps what was read from the
mark on (the mark and any signs, reversed) to give back in that case.
def number.accepts source · line 152 · raw
@+k:U32 -> @+stage:U32 -> @+digits:Bool -> Bool
def number.stage source · line 158 · raw
@+k:U32 -> @+stage:U32 -> U32
def number.mark source · line 162 · raw
@keep:Bool -> @+c:U32 -> @mark:String -> String
def number.pow10 source · line 167 · raw
@n:Nat -> @+acc:F32 -> F32
def number.tail source · line 172 · raw
@dangling:Bool -> @mark:String -> @rest:String -> String
def number.finish source · line 178 · raw
@rest:String -> @mark:String -> @+stage:U32 -> @+sign:F32 -> @+mant:F32 -> @+div:F32 -> @+esign:F32 -> @+ev:U32 -> @+digits:Bool -> @+edigits:Bool -> Number
The value from the parts; rest is the string from the stop.
def number.go source · line 187 · raw
@t:String -> @ok:Bool -> @+c:U32 -> @mark:String -> @+stage:U32 -> @+sign:F32 -> @+mant:F32 -> @+div:F32 -> @+esign:F32 -> @+ev:U32 -> @+digits:Bool -> @+edigits:Bool -> Number
c is the character before t; ok says whether it was read, and the
parts are the ones after it. The next character is read when the stage
accepts it, and otherwise the parts are kept and the number stops there.
def number source · line 208 · raw
@s:String -> Number
The loop starts as if after a character that was read, with nothing yet.
def number.default source · line 211 · raw
@num:Number -> @fallback:F32 -> F32
def scalar source · line 215 · raw
@s:String -> @fallback:F32 -> F32
def numbers.next source · line 218 · raw
@num:Number -> @next:(@_:String -> List<&2, F32>) -> List<&2, F32>
def numbers.go source · line 224 · raw
@fuel:Nat -> @s:String -> List<&2, F32>
Every number read takes at least one character (a digit), so the text's length bounds how many there are.
def numbers source · line 229 · raw
@+s:String -> List<&2, F32>
def nth source · line 232 · raw
@xs:List<&2, F32> -> @i:Nat -> @fallback:F32 -> F32
def uints.nth source · line 238 · raw
@xs:List<&2, U32> -> @n:Nat -> @+fallback:U32 -> U32
def hex.digit source · line 244 · raw
@+c:U32 -> U32
def hex source · line 249 · raw
@s:String -> @acc:U32 -> U32
def trim.both source · line 254 · raw
@s:String -> String
def unit source · line 257 · raw
@num:Number -> @reference:F32 -> F32
def string.contains source · line 268 · raw
@+s:String -> @+part:String -> Bool
def scan.head source · line 273 · raw
@s:Scan -> String
def unquote source · line 277 · raw
@+s:String -> String
def strings.has source · line 281 · raw
@xs:List<&2, String> -> @+s:String -> Bool