bend-lint 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -143,8 +143,15 @@ Term files_real_run(Env e, Term* f, IoWork* w) {
143
143
  #endif
144
144
  }
145
145
 
146
+ // A program that does not use an effect gives it no id.
146
147
  static void __attribute__((constructor)) files_use(void) {
148
+ #ifdef CID(list.raw)
147
149
  io_eff(CID(list.raw), files_list_run);
150
+ #endif
151
+ #ifdef CID(kind)
148
152
  io_eff(CID(kind), files_kind_run);
153
+ #endif
154
+ #ifdef CID(real)
149
155
  io_eff(CID(real), files_real_run);
156
+ #endif
150
157
  }
@@ -1,6 +1,6 @@
1
1
  import Base
2
- import bend-lint-api@0.2.1.0/shared/lists.bend as Lists
3
- import bend-lint-api@0.2.1.0/shared/strings.bend as Strings
2
+ import bend-lint-api@0.3.0.0/shared.bend as Shared
3
+ import ./spec.bend as Spec
4
4
 
5
5
  # Types
6
6
  # =====
@@ -80,11 +80,13 @@ def kind(path: String) -> IO(U32):
80
80
  import "./effs/files.c"
81
81
  import "./effs/files.js"
82
82
 
83
- # The real path of `path`: absolute, links resolved, "/" between its parts.
84
83
  def real(path: String) -> IO(Result<&1, &1, U32 & String, String>):
85
84
  import "./effs/files.c"
86
85
  import "./effs/files.js"
87
86
 
87
+ def Spec.real(path):
88
+ real(path)
89
+
88
90
  # The names in `text`, each ended by a NUL.
89
91
  def names(text: String) -> List<&2, String>:
90
92
  List.filter(~String, ~(n => Bool.not(String.is_empty(n))), String.split(text, Chr{0}))
@@ -135,8 +137,7 @@ def read.go(
135
137
  File.close(f)
136
138
  return Fail{"too long"}
137
139
 
138
- # The whole text of `file`, which it closes; or why it could not be read.
139
- def read(file: File) -> IO(Result<&2, &2, String, String>):
140
+ def Spec.read(file):
140
141
  IO.bind(
141
142
  File & Result<&1, &1, U32 & String, String>,
142
143
  Result<&2, &2, String, String>,
@@ -167,8 +168,7 @@ def write.opened(text: String, opened: Result<&1, &1, U32 & String, File>) -> IO
167
168
  case Fail{e}:
168
169
  IO.pure(Bool, False{})
169
170
 
170
- # Whether `text` was written to the file at `path`, in place of its text.
171
- def write(path: String, text: String) -> IO(Bool):
171
+ def Spec.write(path, text):
172
172
  IO.bind(
173
173
  Result<&1, &1, U32 & String, File>,
174
174
  Bool,
@@ -370,7 +370,7 @@ def matches.if(shown: Bool, name: String, pattern: String) -> Bool:
370
370
  # (`[!...]` or `[^...]`, one outside it). A name that starts with "." matches only a pattern that does.
371
371
  def matches(+name: String, +pattern: String) -> Bool:
372
372
  matches.if(
373
- Bool.not(Strings.starts_with(name, '.')) || Strings.starts_with(pattern, '.'),
373
+ Bool.not(Shared.Strings.starts_with(name, '.')) || Shared.Strings.starts_with(pattern, '.'),
374
374
  name,
375
375
  pattern
376
376
  )
@@ -483,11 +483,11 @@ def Walk.deep(
483
483
  rest,
484
484
  path,
485
485
  more,
486
- Lists.push(
486
+ Shared.Lists.push(
487
487
  Walk,
488
- Lists.when(
488
+ Shared.Lists.when(
489
489
  Walk,
490
- Bool.not(Strings.starts_with(name, '.')),
490
+ Bool.not(Shared.Strings.starts_with(name, '.')),
491
491
  Walk{join(path, name), "**" <> more}
492
492
  ),
493
493
  out
@@ -511,9 +511,9 @@ def Walk.matching(
511
511
  pattern,
512
512
  path,
513
513
  more,
514
- Lists.push(
514
+ Shared.Lists.push(
515
515
  Walk,
516
- Lists.when(Walk, matches(name, pattern), Walk{join(path, name), more}),
516
+ Shared.Lists.when(Walk, matches(name, pattern), Walk{join(path, name), more}),
517
517
  out
518
518
  )
519
519
  )
@@ -559,7 +559,7 @@ def Walk.go(fuel: Nat, todo: List<&2, Walk>, out: List<&2, String>) -> IO(List<&
559
559
  List<&2, Walk>,
560
560
  List<&2, String>,
561
561
  Walk.step(path, segments),
562
- more => Walk.go(k, Lists.onto(&2, Walk, more, rest), out)
562
+ more => Walk.go(k, Shared.Lists.onto(&2, Walk, more, rest), out)
563
563
  )
564
564
  case _ _:
565
565
  IO.pure(List<&2, String>, out)
@@ -578,7 +578,7 @@ def glob(glob: String) -> IO(List<&2, String>):
578
578
  List<&2, String>,
579
579
  List<&2, String>,
580
580
  Walk.go(4294967295n, Walk.starts(expand(4294967295n, Expanding{[glob], []}), []), []),
581
- found => IO.pure(List<&2, String>, Lists.sort(~String, ~String.is_le, found))
581
+ found => IO.pure(List<&2, String>, Shared.Lists.sort(~String, ~String.is_le, found))
582
582
  )
583
583
 
584
584
  # Inputs
@@ -608,7 +608,7 @@ def named.check(
608
608
  case Con{Named{input, Nil{}}, rest}:
609
609
  Fail{input ++ " matches no file"}
610
610
  case Con{Named{input, found}, rest}:
611
- named.check(rest, Lists.onto(&2, String, found, out))
611
+ named.check(rest, Shared.Lists.onto(&2, String, found, out))
612
612
 
613
613
  # The paths seen, with `path` after `paths` unless `found` (the set of those seen, and whether it holds `path`) says it
614
614
  # is seen.
@@ -637,8 +637,7 @@ def Seen.found(found: Result<&1, &1, U32 & String, String>, seen: Seen) -> Seen:
637
637
  case Fail{why}:
638
638
  seen
639
639
 
640
- # The paths of `paths` that name no file, onto `out`.
641
- def missing(paths: List<&2, String>, out: List<&2, String>) -> IO(List<&2, String>):
640
+ def Spec.missing(paths, out):
642
641
  match paths:
643
642
  case Nil{}:
644
643
  IO.pure(List<&2, String>, out)
@@ -647,9 +646,9 @@ def missing(paths: List<&2, String>, out: List<&2, String>) -> IO(List<&2, Strin
647
646
  U32,
648
647
  List<&2, String>,
649
648
  kind(path),
650
- k => missing(
649
+ k => Spec.missing(
651
650
  rest,
652
- Lists.push(String, Lists.when(String, Bool.not(U32.is_eq(k, 1)), path), out)
651
+ Shared.Lists.push(String, Shared.Lists.when(String, Bool.not(U32.is_eq(k, 1)), path), out)
653
652
  )
654
653
  )
655
654
 
@@ -681,10 +680,7 @@ def named.real(
681
680
  case Fail{why}:
682
681
  IO.pure(Result<&2, &2, String, List<&2, String>>, Fail{why})
683
682
 
684
- # The files `inputs` name, each once, in order, as real paths; or why an input names none. An input is a path, or a
685
- # glob with "/" between its parts, whose files come sorted: in a segment, `*` matches any characters, `?` one, and
686
- # `[...]` one of a class; `**` is any folders; `{a,b}` is either.
687
- def named(inputs: List<&2, String>) -> IO(Result<&2, &2, String, List<&2, String>>):
683
+ def Spec.named(inputs):
688
684
  IO.bind(
689
685
  List<&2, Named>,
690
686
  Result<&2, &2, String, List<&2, String>>,
@@ -704,19 +700,19 @@ def git.listed(
704
700
 
705
701
  # Whether a part of a path is node_modules, or starts with ".".
706
702
  def listed.is_hidden(+part: String) -> Bool:
707
- String.eq(part, "node_modules") || Strings.starts_with(part, '.')
703
+ String.eq(part, "node_modules") || Shared.Strings.starts_with(part, '.')
708
704
 
709
705
  # The files of `paths` with no part that is node_modules or starts with ".", sorted, each once, as real paths.
710
706
  def listed.kept(paths: List<&2, String>) -> IO(List<&2, String>):
711
707
  reals(
712
- Lists.sort(
708
+ Shared.Lists.sort(
713
709
  ~String,
714
710
  ~String.is_le,
715
711
  List.filter(
716
712
  ~String,
717
713
  ~(
718
714
  path => Bool.not(
719
- Lists.any(~String, ~(part => listed.is_hidden(part)), String.split(path, '/'))
715
+ Shared.Lists.any(~String, ~(part => listed.is_hidden(part)), String.split(path, '/'))
720
716
  )
721
717
  ),
722
718
  paths
@@ -737,11 +733,7 @@ def listed.found(found: Maybe<&2, List<&2, String>>, pattern: String) -> IO(List
737
733
  paths => listed.kept(paths)
738
734
  )
739
735
 
740
- # The files under the current folder whose names match the glob segment `pattern`, sorted, each once, as real paths:
741
- # those git lists (tracked, or untracked and not ignored, by .gitignore files and git's other ignore files; at most 64
742
- # MiB of paths, in a minute), or all if git does not list them (outside a repository, for one); but none that
743
- # `listed.kept` drops.
744
- def listed(+pattern: String) -> IO(List<&2, String>):
736
+ def Spec.listed(pattern):
745
737
  IO.bind(
746
738
  Result<&1, &1, U32 & String, U32 & (String & String)>,
747
739
  List<&2, String>,
@@ -0,0 +1,39 @@
1
+ # Files: their text, folders and globs.
2
+ import Base
3
+
4
+ # The whole text of `file`, which it closes; or why it could not be read.
5
+ law read:
6
+ for file: File
7
+ IO(Result<&2, &2, String, String>)
8
+
9
+ # The real path of `path`: absolute, links resolved, "/" between its parts.
10
+ law real:
11
+ for path: String
12
+ IO(Result<&1, &1, U32 & String, String>)
13
+
14
+ # Whether `text` was written to the file at `path`, in place of its text.
15
+ law write:
16
+ for path: String
17
+ for text: String
18
+ IO(Bool)
19
+
20
+ # The paths of `paths` that name no file, onto `out`.
21
+ law missing:
22
+ for paths: List<&2, String>
23
+ for out: List<&2, String>
24
+ IO(List<&2, String>)
25
+
26
+ # The files `inputs` name, each once, in order, as real paths; or why an input names none. An input is a path, or a
27
+ # glob with "/" between its parts, whose files come sorted: in a segment, `*` matches any characters, `?` one, and
28
+ # `[...]` one of a class; `**` is any folders; `{a,b}` is either.
29
+ law named:
30
+ for inputs: List<&2, String>
31
+ IO(Result<&2, &2, String, List<&2, String>>)
32
+
33
+ # The files under the current folder whose names match the glob segment `pattern`, sorted, each once, as real paths:
34
+ # those git lists (tracked, or untracked and not ignored, by .gitignore files and git's other ignore files; at most 64
35
+ # MiB of paths, in a minute), or all if git does not list them (outside a repository, for one); but none with a part
36
+ # that is node_modules or starts with ".".
37
+ law listed:
38
+ for +pattern: String
39
+ IO(List<&2, String>)
@@ -1,12 +1,12 @@
1
- # The fixes a run makes with `--fix-dangerously`: no fix is proven to keep the program until Bend's checker is Bend.
2
1
  import Base
3
- import ../files/main.bend as Files
4
- import bend-lint-api@0.2.1.0/shared/lists.bend as Lists
5
- import bend-lint-api@0.2.1.0/shared/names.bend as Names
6
- import bend-lint-api@0.2.1.0/shared/sets.bend as Sets
7
- import bend-lint-api@0.2.1.0/shared/strings.bend as Strings
8
- import ../text/main.bend as Text
9
- import bend-lint-api@0.2.1.0/linter/lint.bend as Lint
2
+ import bend-lint-api@0.3.0.0/shared.bend as Shared
3
+ import bend-lint-api@0.3.0.0/linter/api/impl.bend as LintImpl
4
+ import bend-lint-api@0.3.0.0/linter/api/spec.bend as Lint
5
+ import ../files/impl.bend as FilesImpl
6
+ import ../files/spec.bend as Files
7
+ import ../text/impl.bend as TextImpl
8
+ import ../text/spec.bend as Text
9
+ import ./spec.bend as Spec
10
10
 
11
11
  # Types
12
12
  # =====
@@ -55,15 +55,15 @@ def Syntax.is_alias(syntax: Lint.Syntax) -> Bool:
55
55
  def Part.of(alias: Bool, +name: String, +old: String) -> Maybe<&2, Part>:
56
56
  match alias:
57
57
  case True{}:
58
- Lists.when(
58
+ Shared.Lists.when(
59
59
  Part,
60
60
  String.eq(name, old) || String.starts_with(name, old ++ "."),
61
61
  Part{0, U32.from_nat(String.length(old))}
62
62
  )
63
63
  case False{}:
64
- Lists.when(
64
+ Shared.Lists.when(
65
65
  Part,
66
- String.eq(Strings.after_last(name, '.'), old),
66
+ String.eq(Shared.Strings.after_last(name, '.'), old),
67
67
  Part{
68
68
  U32.from_nat(Nat.sub(String.length(name), String.length(old))),
69
69
  U32.from_nat(String.length(name))
@@ -121,10 +121,10 @@ def Taken.node(+path: String, +node: Lint.Node, out: Set()) -> Set():
121
121
  case Lint.Node{Lint.Import{t}, name, sp, k, f}:
122
122
  Set.add(
123
123
  Set.add(out, Taken.key(path, True{}, name)),
124
- Taken.key(path, False{}, Strings.after_last(name, '.'))
124
+ Taken.key(path, False{}, Shared.Strings.after_last(name, '.'))
125
125
  )
126
126
  case Lint.Node{s, name, sp, k, f}:
127
- Set.add(out, Taken.key(path, False{}, Strings.after_last(name, '.')))
127
+ Set.add(out, Taken.key(path, False{}, Shared.Strings.after_last(name, '.')))
128
128
 
129
129
  def Taken.files(sources: List<&2, Lint.Source>, out: Set()) -> Set():
130
130
  match sources:
@@ -150,7 +150,7 @@ def Rename.is_allowed(
150
150
  case True{} True{} _:
151
151
  False{}
152
152
  case True{} False{} Some{Lint.Span{path, beg, end}}:
153
- Bool.not(Sets.has(taken, Taken.key(path, alias, to)))
153
+ Bool.not(Shared.Sets.has(taken, Taken.key(path, alias, to)))
154
154
  case True{} False{} None{}:
155
155
  False{}
156
156
 
@@ -196,7 +196,7 @@ def Rename.plan(
196
196
  Rename.nodes(whole, Lint.Node{s, name, span, k, f}, uses),
197
197
  fix,
198
198
  alias,
199
- Bool.pick(String, alias, name, Strings.after_last(name, '.')),
199
+ Bool.pick(String, alias, name, Shared.Strings.after_last(name, '.')),
200
200
  to,
201
201
  Some{[]}
202
202
  )
@@ -354,7 +354,7 @@ def Spot.file(
354
354
  ) -> List<&2, Spotted>:
355
355
  match text:
356
356
  case Some{t}:
357
- Spot.walk(Lists.sort(~Spot, ~Spot.is_le, spots), t, 0, out)
357
+ Spot.walk(Shared.Lists.sort(~Spot, ~Spot.is_le, spots), t, 0, out)
358
358
  case None{}:
359
359
  Spot.missed(spots, out)
360
360
 
@@ -383,7 +383,7 @@ def Spot.read_all(
383
383
  texts,
384
384
  Spot.file(
385
385
  Spot.text(Map.has(&2, String, texts, path), path),
386
- Names.Group.get(Spot, groups, path),
386
+ Shared.Names.Group.get(Spot, groups, path),
387
387
  out
388
388
  )
389
389
  )
@@ -416,7 +416,7 @@ def Fix.changes(
416
416
  do Maybe<&2, List<&2, Change>>:
417
417
  changes: List<&2, Change> <- out
418
418
  more: List<&2, Change> <- Operation.changes(op, fix, lookup)
419
- return Lists.onto(&2, Change, more, changes))
419
+ return Shared.Lists.onto(&2, Change, more, changes))
420
420
 
421
421
  def Plan.firsts(founds: List<&2, Lint.Found>, out: List<&2, Lint.Fix>) -> List<&2, Lint.Fix>:
422
422
  match founds:
@@ -462,7 +462,7 @@ def Fix.any(~f: Lint.Operation -> Bool, fixes: List<&2, Lint.Fix>, found: Bool)
462
462
  case Nil{} False{}:
463
463
  False{}
464
464
  case Con{Lint.Fix{title, ops}, rest} False{}:
465
- Fix.any(~f, rest, Lists.any(~Lint.Operation, ~f, ops))
465
+ Fix.any(~f, rest, Shared.Lists.any(~Lint.Operation, ~f, ops))
466
466
 
467
467
  def Lookup.index(
468
468
  need: Bool,
@@ -579,7 +579,7 @@ def Spotted.take(
579
579
  # Spans
580
580
  # -----
581
581
  def Spans.of(spans: Map<&2, Text.Spans>, path: String) -> Text.Spans:
582
- Pair.snd(Map<&2, Text.Spans>, Text.Spans, Map.get(Text.Spans, Text.SLeaf{}, spans, path))
582
+ Pair.snd(Map<&2, Text.Spans>, Text.Spans, Map.get(Text.Spans, Text.Spans.empty, spans, path))
583
583
 
584
584
  def Spans.clashes(edits: List<&2, Text.Edit>, spans: Map<&2, Text.Spans>) -> Bool:
585
585
  match edits:
@@ -620,7 +620,7 @@ def Plan.add(spans: Maybe<&2, Map<&2, Text.Spans>>, edits: List<&2, Text.Edit>,
620
620
  case None{} _:
621
621
  plan
622
622
  case Some{taken} Plan{kept, n, old}:
623
- Plan{Lists.onto(&2, Text.Edit, edits, kept), (n + 1: U32), taken}
623
+ Plan{Shared.Lists.onto(&2, Text.Edit, edits, kept), (n + 1: U32), taken}
624
624
 
625
625
  # The plan with a fix's edits, if the linter can make it, and none of its edits touches a planned one or another.
626
626
  def Plan.fix(edits: Maybe<&2, List<&2, Text.Edit>>, +plan: Plan) -> Plan:
@@ -667,30 +667,28 @@ def Works.go(
667
667
  )
668
668
  )
669
669
 
670
- def texts(sources: List<&2, Lint.Source>, out: Map<&2, String>) -> Map<&2, String>:
670
+ def Spec.texts(sources, out):
671
671
  match sources:
672
672
  case Nil{}:
673
673
  out
674
674
  case Con{Lint.Source{path, namespace, text, nodes}, rest}:
675
- texts(rest, Map.set(&2, String, out, path, text))
675
+ Spec.texts(rest, Map.set(&2, String, out, path, text))
676
676
 
677
- # The edits of each of `fixes`, in order; None for a fix the linter cannot make: an operation it refuses, an edit in a
678
- # file a fix may not edit, or a rename of a name the text does not write where a use is. Each file a rename changes is
679
- # read once, for all the fixes.
680
- def edits(
681
- +fixes: List<&2, Lint.Fix>,
682
- declarations: List<&2, Lint.Declaration>,
683
- +sources: List<&2, Lint.Source>
684
- ) -> List<&2, Maybe<&2, List<&2, Text.Edit>>>:
677
+ def Spec.edits(fixes, declarations, sources):
685
678
  +works = Fix.works(fixes, Lookup.of(fixes, declarations, sources), 0, [])
686
- +own = texts(Lint.Program.own(sources), Map.new(&2, String))
687
- +groups = Names.Group.of(~Spot, ~Spot.path, Fix.spots(works, []), Map.new(&2, List<&2, Spot>))
679
+ +own = Spec.texts(Lint.Program.own(sources), Map.new(&2, String))
680
+ +groups = Shared.Names.Group.of(
681
+ ~Spot,
682
+ ~Spot.path,
683
+ Fix.spots(works, []),
684
+ Map.new(&2, List<&2, Spot>)
685
+ )
688
686
  Works.go(
689
687
  works,
690
688
  0,
691
689
  own,
692
690
  (
693
- Lists.sort(
691
+ Shared.Lists.sort(
694
692
  ~Spotted,
695
693
  ~Spotted.is_le,
696
694
  Spot.read_all(Map.keys(&2, List<&2, Spot>, groups), groups, own, [])
@@ -715,7 +713,7 @@ def Plan.of(
715
713
  sources: List<&2, Lint.Source>
716
714
  ) -> Plan:
717
715
  Plan.fold(
718
- edits(Plan.firsts(founds, []), declarations, sources),
716
+ Spec.edits(Plan.firsts(founds, []), declarations, sources),
719
717
  Plan{[], 0, Map.new(&2, Text.Spans)}
720
718
  )
721
719
 
@@ -810,7 +808,7 @@ def write.all(sources: List<&2, Lint.Source>, +groups: Map<&2, List<&2, Text.Edi
810
808
  IO.pure(Unit, Unit{})
811
809
  case Con{Lint.Source{+path, namespace, text, nodes}, rest}:
812
810
  do IO<Unit>:
813
- write.edited(path, text, Names.Group.get(Text.Edit, groups, path))
811
+ write.edited(path, text, Shared.Names.Group.get(Text.Edit, groups, path))
814
812
  write.all(rest, groups)
815
813
 
816
814
  # Fixes
@@ -821,18 +819,16 @@ def made(sources: List<&2, Lint.Source>, plan: Plan) -> IO(U32):
821
819
  do IO<U32>:
822
820
  write.all(
823
821
  sources,
824
- Names.Group.of(~Text.Edit, ~Text.Edit.path, edits, Map.new(&2, List<&2, Text.Edit>))
822
+ Shared.Names.Group.of(
823
+ ~Text.Edit,
824
+ ~Text.Edit.path,
825
+ edits,
826
+ Map.new(&2, List<&2, Text.Edit>)
827
+ )
825
828
  )
826
829
  return n
827
830
 
828
- # Makes the fixes of `founds` (in order), if `fixing`, in the program's files (`sources`, whose declarations are
829
- # `declarations`); how many.
830
- def apply(
831
- fixing: Bool,
832
- founds: List<&2, Lint.Found>,
833
- declarations: List<&2, Lint.Declaration>,
834
- +sources: List<&2, Lint.Source>
835
- ) -> IO(U32):
831
+ def Spec.apply(fixing, founds, declarations, sources):
836
832
  match fixing:
837
833
  case False{}:
838
834
  IO.pure(U32, 0)
@@ -0,0 +1,28 @@
1
+ # The fixes a run makes with `--fix-dangerously`: no fix is proven to keep the program until Bend's checker is Bend.
2
+ import Base
3
+ import bend-lint-api@0.3.0.0/linter/api/spec.bend as Lint
4
+ import ../text/spec.bend as Text
5
+
6
+ # The text of each of `sources`, by its path, onto `out`.
7
+ law texts:
8
+ for sources: List<&2, Lint.Source>
9
+ for out: Map<&2, String>
10
+ Map<&2, String>
11
+
12
+ # The edits of each of `fixes`, in order; None for a fix the linter cannot make: an operation it refuses, an edit in a
13
+ # file a fix may not edit, or a rename of a name the text does not write where a use is. Each file a rename changes is
14
+ # read once, for all the fixes.
15
+ law edits:
16
+ for +fixes: List<&2, Lint.Fix>
17
+ for declarations: List<&2, Lint.Declaration>
18
+ for +sources: List<&2, Lint.Source>
19
+ List<&2, Maybe<&2, List<&2, Text.Edit>>>
20
+
21
+ # Makes the fixes of `founds` (in order), if `fixing`, in the program's files (`sources`, whose declarations are
22
+ # `declarations`); how many.
23
+ law apply:
24
+ for fixing: Bool
25
+ for founds: List<&2, Lint.Found>
26
+ for declarations: List<&2, Lint.Declaration>
27
+ for +sources: List<&2, Lint.Source>
28
+ IO(U32)
@@ -1,20 +1,9 @@
1
1
  import Base
2
- import bend-lint-api@0.2.1.0/shared/names.bend as Names
3
- import bend-lint-api@0.2.1.0/shared/strings.bend as Strings
2
+ import bend-lint-api@0.3.0.0/shared.bend as Shared
3
+ import ./spec.bend as Spec
4
4
 
5
5
  # Types
6
6
  # =====
7
- type Json is Data:
8
- JNull{}
9
- JTrue{}
10
- JFalse{}
11
- JNumber{text: String}
12
- JString{text: String}
13
- JArray{items: List<&2, Json>}
14
- JObject{fields: List<&2, JField>}
15
-
16
- type JField is Data:
17
- JField{key: String, value: Json}
18
7
 
19
8
  # Reading
20
9
  # =======
@@ -256,36 +245,40 @@ def Lex.done(lex: Lex) -> List<&2, Token>:
256
245
 
257
246
  # A list or an object being read: its items, or its fields and the key read for the next one (all last first).
258
247
  type Frame is Data:
259
- InList{items: List<&2, Json>}
260
- InObject{fields: List<&2, JField>, key: Maybe<&2, String>}
248
+ InList{items: List<&2, Spec.Json>}
249
+ InObject{fields: List<&2, Spec.JField>, key: Maybe<&2, String>}
261
250
 
262
251
  # What is read so far: the open lists and objects (innermost first), and the value read whole, if any; None if the
263
252
  # text is not JSON.
264
253
  type Read is Data:
265
- Read{stack: List<&2, Frame>, value: Maybe<&2, Json>}
254
+ Read{stack: List<&2, Frame>, value: Maybe<&2, Spec.Json>}
266
255
  Unreadable{}
267
256
 
268
- def Json.word(+w: String) -> Json:
257
+ def Json.word(+w: String) -> Spec.Json:
269
258
  Maybe.default(
270
259
  &2,
271
- Json,
272
- Names.lookup(
273
- Json,
260
+ Spec.Json,
261
+ Shared.Names.lookup(
262
+ Spec.Json,
274
263
  w,
275
- [Names.Named{"true", JTrue{}}, Names.Named{"false", JFalse{}}, Names.Named{"null", JNull{}}]
264
+ [
265
+ Shared.Names.Named{"true", Spec.JTrue{}},
266
+ Shared.Names.Named{"false", Spec.JFalse{}},
267
+ Shared.Names.Named{"null", Spec.JNull{}}
268
+ ]
276
269
  ),
277
- JNumber{w}
270
+ Spec.JNumber{w}
278
271
  )
279
272
 
280
273
  # A value read whole: into the innermost list or object, or as the text's value.
281
- def Read.value(v: Json, stack: List<&2, Frame>, value: Maybe<&2, Json>) -> Read:
274
+ def Read.value(v: Spec.Json, stack: List<&2, Frame>, value: Maybe<&2, Spec.Json>) -> Read:
282
275
  match stack value:
283
276
  case Nil{} None{}:
284
277
  Read{[], Some{v}}
285
278
  case Con{InList{items}, rest} _:
286
279
  Read{InList{v <> items} <> rest, value}
287
280
  case Con{InObject{fields, Some{k}}, rest} _:
288
- Read{InObject{JField{k, v} <> fields, None{}} <> rest, value}
281
+ Read{InObject{Spec.JField{k, v} <> fields, None{}} <> rest, value}
289
282
  case _ _:
290
283
  Unreadable{}
291
284
 
@@ -300,13 +293,13 @@ def Read.step(token: Token, r: Read) -> Read:
300
293
  case TList{} Read{stack, value}:
301
294
  Read{InList{[]} <> stack, value}
302
295
  case TEnd{} Read{Con{InList{items}, rest}, value}:
303
- Read.value(JArray{List.reverse(&2, Json, items)}, rest, value)
296
+ Read.value(Spec.JArray{List.reverse(&2, Spec.Json, items)}, rest, value)
304
297
  case TClose{} Read{Con{InObject{fields, None{}}, rest}, value}:
305
- Read.value(JObject{List.reverse(&2, JField, fields)}, rest, value)
298
+ Read.value(Spec.JObject{List.reverse(&2, Spec.JField, fields)}, rest, value)
306
299
  case TString{text} Read{Con{InObject{fields, None{}}, rest}, value}:
307
300
  Read{InObject{fields, Some{text}} <> rest, value}
308
301
  case TString{text} Read{stack, value}:
309
- Read.value(JString{text}, stack, value)
302
+ Read.value(Spec.JString{text}, stack, value)
310
303
  case TWord{w} Read{stack, value}:
311
304
  Read.value(Json.word(w), stack, value)
312
305
  case _ _:
@@ -319,14 +312,14 @@ def Read.go(tokens: List<&2, Token>, r: Read) -> Read:
319
312
  case Con{t, rest}:
320
313
  Read.go(rest, Read.step(t, r))
321
314
 
322
- def Read.done(r: Read) -> Maybe<&2, Json>:
315
+ def Read.done(r: Read) -> Maybe<&2, Spec.Json>:
323
316
  match r:
324
317
  case Read{Nil{}, value}:
325
318
  value
326
319
  case _:
327
320
  None{}
328
321
 
329
- def read(text: String) -> Maybe<&2, Json>:
322
+ def Spec.read(text):
330
323
  Read.done(Read.go(Lex.done(Lex.go(text, Lex{Between{}, "", []})), Read{[], None{}}))
331
324
 
332
325
  # Writing
@@ -334,7 +327,7 @@ def read(text: String) -> Maybe<&2, Json>:
334
327
 
335
328
  # What is left to write: a value, or text as it is.
336
329
  type Piece is Data:
337
- PValue{value: Json}
330
+ PValue{value: Spec.Json}
338
331
  PText{text: String}
339
332
 
340
333
  def hex(+n: U32) -> Char:
@@ -369,7 +362,7 @@ def quote(s: String) -> String:
369
362
  "\"" ++ quote.go(s, "") ++ "\""
370
363
 
371
364
  # The pieces of `items` (last first), with a comma between each two, onto `out`.
372
- def items(reversed: List<&2, Json>, out: List<&2, Piece>) -> List<&2, Piece>:
365
+ def items(reversed: List<&2, Spec.Json>, out: List<&2, Piece>) -> List<&2, Piece>:
373
366
  match reversed:
374
367
  case Nil{}:
375
368
  out
@@ -379,32 +372,32 @@ def items(reversed: List<&2, Json>, out: List<&2, Piece>) -> List<&2, Piece>:
379
372
  items(rest, PText{","} <> PValue{v} <> out)
380
373
 
381
374
  # The pieces of `fields` (last first), with a comma between each two, onto `out`.
382
- def fields(reversed: List<&2, JField>, out: List<&2, Piece>) -> List<&2, Piece>:
375
+ def fields(reversed: List<&2, Spec.JField>, out: List<&2, Piece>) -> List<&2, Piece>:
383
376
  match reversed:
384
377
  case Nil{}:
385
378
  out
386
- case Con{JField{k, v}, Nil{}}:
379
+ case Con{Spec.JField{k, v}, Nil{}}:
387
380
  PText{quote(k) ++ ":"} <> PValue{v} <> out
388
- case Con{JField{k, v}, rest}:
381
+ case Con{Spec.JField{k, v}, rest}:
389
382
  fields(rest, PText{","} <> PText{quote(k) ++ ":"} <> PValue{v} <> out)
390
383
 
391
384
  # A value as pieces, onto `rest`: its lists and objects open one level.
392
- def pieces(v: Json, rest: List<&2, Piece>) -> List<&2, Piece>:
385
+ def pieces(v: Spec.Json, rest: List<&2, Piece>) -> List<&2, Piece>:
393
386
  match v:
394
- case JNull{}:
387
+ case Spec.JNull{}:
395
388
  PText{"null"} <> rest
396
- case JTrue{}:
389
+ case Spec.JTrue{}:
397
390
  PText{"true"} <> rest
398
- case JFalse{}:
391
+ case Spec.JFalse{}:
399
392
  PText{"false"} <> rest
400
- case JNumber{text}:
393
+ case Spec.JNumber{text}:
401
394
  PText{text} <> rest
402
- case JString{text}:
395
+ case Spec.JString{text}:
403
396
  PText{quote(text)} <> rest
404
- case JArray{xs}:
405
- PText{"["} <> items(List.reverse(&2, Json, xs), PText{"]"} <> rest)
406
- case JObject{fs}:
407
- PText{"{"} <> fields(List.reverse(&2, JField, fs), PText{"}"} <> rest)
397
+ case Spec.JArray{xs}:
398
+ PText{"["} <> items(List.reverse(&2, Spec.Json, xs), PText{"]"} <> rest)
399
+ case Spec.JObject{fs}:
400
+ PText{"{"} <> fields(List.reverse(&2, Spec.JField, fs), PText{"}"} <> rest)
408
401
 
409
402
  # The text of `todo`, chunk by chunk, onto `out` (last first). `fuel` bounds the pieces.
410
403
  def write(fuel: Nat, todo: List<&2, Piece>, out: List<&2, String>) -> List<&2, String>:
@@ -418,5 +411,5 @@ def write(fuel: Nat, todo: List<&2, Piece>, out: List<&2, String>) -> List<&2, S
418
411
  case 1n+f Con{PValue{v}, rest}:
419
412
  write(f, pieces(v, rest), out)
420
413
 
421
- def show(v: Json) -> String:
422
- Strings.onto(write(4294967295n, [PValue{v}], []), "")
414
+ def Spec.show(v):
415
+ Shared.Strings.onto(write(4294967295n, [PValue{v}], []), "")
@@ -0,0 +1,24 @@
1
+ # JSON: read from text, and shown as text.
2
+ import Base
3
+
4
+ type Json is Data:
5
+ JNull{}
6
+ JTrue{}
7
+ JFalse{}
8
+ JNumber{text: String}
9
+ JString{text: String}
10
+ JArray{items: List<&2, Json>}
11
+ JObject{fields: List<&2, JField>}
12
+
13
+ type JField is Data:
14
+ JField{key: String, value: Json}
15
+
16
+ # The JSON value that `text` is, if it is one.
17
+ law read:
18
+ for text: String
19
+ Maybe<&2, Json>
20
+
21
+ # `v` as JSON text, on one line.
22
+ law show:
23
+ for v: Json
24
+ String