src/rules/tokens.bend
bolt@1.27.0.0 · 104 lines · 13 definitions
1
2
3
4import Base
5import bolt@1.27.0.0/src/lazy/lazy.bend as Lazy
6import bolt@1.27.0.0/src/syntax/lex.bend as Lex
7import bolt@1.27.0.0/src/syntax/tree.bend as Tree
8
9
10def sig(toks: List<&2, Lex.Tok>) -> List<&2, Lex.Tok>:
11 match toks:
12 case Nil{}:
13 Nil{}
14 case Con{Lex.Tok{+k, t, l, c}, rest}:
15 +more = sig(rest)
16 Bool.pick(List<&2, Lex.Tok>, Lex.significant(k), Lex.Tok{k, t, l, c} <> more, more)
17
18
19def code.cut(tt: String) -> String:
20 match tt:
21 case SNil{}:
22 SNil{}
23 case SCon{h, _t}:
24 SCon{h, SNil{}}
25
26
27def code.tok(tk: Lex.Tok) -> Lex.Tok:
28 match tk:
29 case Lex.Tok{+k, +t, l, c}:
30 Lex.Tok{k, Bool.pick(String, Lex.is_str(k), code.cut(t), t), l, c}
31
32
33def code.toks(ts: List<&2, Lex.Tok>) -> List<&2, Lex.Tok>:
34 match ts:
35 case Nil{}:
36 Nil{}
37 case Con{h, t}:
38 code.tok(h) <> code.toks(t)
39
40
41
42def code(+tt: String) -> String:
43 Lex.text(code.toks(Lex.tokens(tt)))
44
45
46def is_lit(kk: Lex.TokKind) -> Bool:
47 match kk:
48 case Lex.TNum{}:
49 True{}
50 case Lex.TChar{}:
51 True{}
52 case Lex.TStr{}:
53 True{}
54 case other:
55 False{}
56
57
58def nat_ok(cs: List<&2, Char>) -> Bool:
59 match cs:
60 case Con{'n', Nil{}}:
61 True{}
62 case Con{+c, t}:
63 Lazy.and_then(Char.is_digit(c), _u => nat_ok(t))
64 case Nil{}:
65 False{}
66
67
68def digits_ok(cs: List<&2, Char>) -> Bool:
69 match cs:
70 case Con{+c, Nil{}}:
71 Char.is_digit(c)
72 case Con{+c, t}:
73 Lazy.and_then(Char.is_digit(c), _u => digits_ok(t))
74 case Nil{}:
75 False{}
76
77
78def stem(+tt: String) -> String:
79 String.take(tt, Nat.sub(String.length(tt), 1n))
80
81
82def nat_value(+tt: String) -> Maybe<&2, U32>:
83 Bool.pick(Maybe<&2, U32>, nat_ok(String.to_list(tt)), U32.read(stem(tt)), None{})
84
85
86def width(tt: String) -> U32:
87 U32.from_nat(String.length(tt))
88
89
90def keyword(nn: Tree.Node, +word: String) -> Bool:
91 match nn:
92 case Tree.NCons{Tree.Leaf{Lex.Tok{Lex.TKey{}, +t, l, c}}, rest}:
93 String.eq(t, word)
94 case other:
95 False{}
96
97
98
99def pattern(kids: Tree.Node) -> Tree.Node:
100 match kids:
101 case Tree.NCons{h, rest}:
102 rest
103 case other:
104 other