1 # The YAML 1.2 Core schema: how a scalar's text and tag become a value. 2 # 3 # An untagged plain scalar is null (null, Null, NULL, ~ or nothing), a 4 # boolean (true/True/TRUE, false/False/FALSE), an integer (decimal, 0o 5 # octal, 0x hex), a float (decimal, .inf, -.inf, .nan) or else a string. 6 # A quoted or block scalar, or one tagged "!", is a string. A standard tag 7 # (!!str, !!null, !!bool, !!int, !!float) reads the text as that type, and 8 # fails when it can't; any other tag is kept as a Tagged value. 9 10 import Base 11 12 import ./value.bend as V 13 14 import ./re.bend as R 15 16 import ./num.bend as N 17 18 def yaml.tag(name: String) -> String: 19 "tag:yaml.org,2002:" ++ name 20 21 def when(-A: Type, hit: Bool, yes: Unit -> A, no: Unit -> A) -> A: 22 match hit: 23 case True{}: 24 yes(Unit{}) 25 case False{}: 26 no(Unit{}) 27 28 def is_null(+s: String) -> Bool: 29 Bool.or(Bool.or(String.is_empty(s), String.eq(s, "~")), Bool.or(String.eq(s, "null"), Bool.or(String.eq(s, "Null"), String.eq(s, "NULL")))) 30 31 def is_true(+s: String) -> Bool: 32 Bool.or(String.eq(s, "true"), Bool.or(String.eq(s, "True"), String.eq(s, "TRUE"))) 33 34 def is_false(+s: String) -> Bool: 35 Bool.or(String.eq(s, "false"), Bool.or(String.eq(s, "False"), String.eq(s, "FALSE"))) 36 37 def dec.pat() -> List<&2, R.Pat>: 38 [R.sign(), R.digits()] 39 40 def oct.pat() -> List<&2, R.Pat>: 41 [R.c('0'), R.c('o'), R.Plus{R.One{R.Range{'0', '7'}}}] 42 43 def hex.pat() -> List<&2, R.Pat>: 44 [R.c('0'), R.c('x'), R.Plus{R.One{R.Hex{}}}] 45 46 # [-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)? 47 def float.pat() -> List<&2, R.Pat>: 48 [R.sign(), R.Alt{[[R.c('.'), R.digits()], [R.digits(), R.Opt{R.seq([R.c('.'), R.Star{R.digit()}])}]]}, 49 R.Opt{R.seq([R.of("eE"), R.sign(), R.digits()])}] 50 51 def is_inf(+s: String) -> Bool: 52 Bool.or(String.eq(s, ".inf"), Bool.or(String.eq(s, ".Inf"), String.eq(s, ".INF"))) 53 54 def is_nan(+s: String) -> Bool: 55 Bool.or(String.eq(s, ".nan"), Bool.or(String.eq(s, ".NaN"), String.eq(s, ".NAN"))) 56 57 def unsigned(s: String) -> String: 58 match s: 59 case SCon{c, t}: 60 match c: 61 case '-': 62 t 63 case '+': 64 t 65 case _: 66 SCon{c, t} 67 case SNil{}: 68 "" 69 70 def is_neg(s: String) -> Bool: 71 String.starts_with(s, "-") 72 73 # An integer's decimal text, if +s+ is one. 74 def int.of(+s: String) -> Maybe<&2, String>: 75 when(Maybe<&2, String>, R.matches(dec.pat(), s), u => Some{N.int.text(is_neg(s), unsigned(s), 10n)}, 76 u => when(Maybe<&2, String>, R.matches(oct.pat(), s), u => Some{N.int.text(False{}, String.drop(s, 2n), 8n)}, 77 u => when(Maybe<&2, String>, R.matches(hex.pat(), s), u => Some{N.int.text(False{}, String.drop(s, 2n), 16n)}, u => None{}))) 78 79 # A float's text, if +s+ is one: as written, or ".inf", "-.inf", ".nan". 80 def float.of(+s: String) -> Maybe<&2, String>: 81 +u = unsigned(s) 82 Bool.pick(Maybe<&2, String>, is_inf(u), Some{Bool.pick(String, is_neg(s), "-.inf", ".inf")}, 83 Bool.pick(Maybe<&2, String>, is_nan(s), Some{".nan"}, 84 Bool.pick(Maybe<&2, String>, R.matches(float.pat(), s), Some{s}, None{}))) 85 86 def or.int(m: Maybe<&2, String>, s: String, rest: Unit -> V.Yaml) -> V.Yaml: 87 match m: 88 case Some{t}: 89 V.Int{t} 90 case None{}: 91 rest(Unit{}) 92 93 def or.float(m: Maybe<&2, String>, rest: Unit -> V.Yaml) -> V.Yaml: 94 match m: 95 case Some{t}: 96 V.Float{t} 97 case None{}: 98 rest(Unit{}) 99 100 # An untagged plain scalar. 101 def plain(+s: String) -> V.Yaml: 102 when(V.Yaml, is_null(s), u => V.Null{}, 103 u => when(V.Yaml, is_true(s), u => V.Boolean{True{}}, 104 u => when(V.Yaml, is_false(s), u => V.Boolean{False{}}, 105 u => or.int(int.of(s), s, u => or.float(float.of(s), u => V.Str{s}))))) 106 107 def must(m: Maybe<&2, V.Yaml>, +tag: String, +s: String) -> Result<&2, &2, String, V.Yaml>: 108 match m: 109 case Some{v}: 110 Done{v} 111 case None{}: 112 Fail{"found a value that isn't a " ++ tag ++ ": " ++ s} 113 114 def int.yaml(m: Maybe<&2, String>) -> Maybe<&2, V.Yaml>: 115 match m: 116 case Some{t}: 117 Some{V.Int{t}} 118 case None{}: 119 None{} 120 121 # !!float: a float, or an integer written as one ("3" is 3.0). 122 def float.yaml(m: Maybe<&2, String>, i: Maybe<&2, String>) -> Maybe<&2, V.Yaml>: 123 match m: 124 case Some{t}: 125 Some{V.Float{t}} 126 case None{}: 127 match i: 128 case Some{t}: 129 Some{V.Float{t ++ ".0"}} 130 case None{}: 131 None{} 132 133 # A scalar with a tag: !!str, !!null, !!bool, !!int and !!float read the 134 # text; "!" is a string; another tag is kept. 135 def tagged(+tag: String, +s: String) -> Result<&2, &2, String, V.Yaml>: 136 when(Result<&2, &2, String, V.Yaml>, Bool.or(String.eq(tag, "!"), String.eq(tag, yaml.tag("str"))), u => Done{V.Str{s}}, 137 u => when(Result<&2, &2, String, V.Yaml>, String.eq(tag, yaml.tag("null")), u => must(Bool.pick(Maybe<&2, V.Yaml>, is_null(s), Some{V.Null{}}, None{}), "!!null", s), 138 u => when(Result<&2, &2, String, V.Yaml>, String.eq(tag, yaml.tag("bool")), 139 u => must(Bool.pick(Maybe<&2, V.Yaml>, is_true(s), Some{V.Boolean{True{}}}, Bool.pick(Maybe<&2, V.Yaml>, is_false(s), Some{V.Boolean{False{}}}, None{})), "!!bool", s), 140 u => when(Result<&2, &2, String, V.Yaml>, String.eq(tag, yaml.tag("int")), u => must(int.yaml(int.of(s)), "!!int", s), 141 u => when(Result<&2, &2, String, V.Yaml>, String.eq(tag, yaml.tag("float")), u => must(float.yaml(float.of(s), int.of(s)), "!!float", s), 142 u => Done{V.Tagged{tag, V.Str{s}}}))))) 143 144 # A collection with a tag: "!", !!seq and !!map are plain; another is kept. 145 def collection(+tag: String, +v: V.Yaml) -> V.Yaml: 146 Bool.pick(V.Yaml, Bool.or(String.eq(tag, "!"), Bool.or(String.eq(tag, yaml.tag("seq")), String.eq(tag, yaml.tag("map")))), v, V.Tagged{tag, v})