Line data Source code
1 : /*
2 : * Package : Cbor
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 12/12/2016
5 : * Copyright : S.Hamblett
6 : */
7 :
8 : part of cbor;
9 :
10 : /// A Dart item linked list entry for use by the stack
11 : class ItemEntry<DartItem> extends LinkedListEntry {
12 : DartItem value;
13 :
14 3 : ItemEntry(this.value);
15 :
16 0 : String toString() => "${super.toString()} : value.toString()";
17 : }
18 :
19 : /// The decoded Dart item stack class.
20 : class ItemStack {
21 : LinkedList _stack = new LinkedList();
22 :
23 : /// Push an item.
24 : void push(DartItem item) {
25 3 : final ItemEntry entry = new ItemEntry(item);
26 6 : _stack.addFirst(entry);
27 : }
28 :
29 : /// Pop an item from the stack top.
30 : DartItem pop() {
31 6 : final ItemEntry entry = _stack.first;
32 6 : _stack.remove(entry);
33 3 : return entry.value;
34 : }
35 :
36 : /// Peek the top stack item.
37 : DartItem peek() {
38 6 : final ItemEntry entry = _stack.first;
39 3 : return entry.value;
40 : }
41 :
42 : /// Size.
43 : int size() {
44 6 : return _stack.length;
45 : }
46 :
47 : /// Clear.
48 : void clear() {
49 6 : _stack.clear();
50 : }
51 :
52 : /// Stack walker, returns the stack from bottom to
53 : /// top as a list of Dart types.
54 : /// Returns null if the stack is empty.
55 : List<dynamic> walk() {
56 9 : if (_stack.length == 0) return null;
57 3 : final List<dynamic> ret = new List<dynamic>();
58 6 : final List<ItemEntry> stackList = _stack.toList();
59 6 : stackList.reversed.forEach((item) {
60 6 : if (!item.value.ignore) {
61 9 : ret.add(item.value.data);
62 : }
63 : });
64 : return ret;
65 : }
66 :
67 : /// Gets item hints. Returns item hints for stack items.
68 : /// If used with the walk stack the returned list can
69 : /// be used on a per index basis.
70 : List<dataHints> hints() {
71 6 : if (_stack.length == 0) return null;
72 2 : final List<dataHints> ret = new List<dataHints>();
73 4 : final List<ItemEntry> stackList = _stack.toList();
74 4 : stackList.reversed.forEach((item) {
75 4 : if (!item.value.ignore) {
76 6 : ret.add(item.value.hint);
77 : }
78 : });
79 : return ret;
80 : }
81 :
82 : /// Check if any error entries are present in the stack.
83 : /// Returns a list of error strings if any are found, null
84 : /// if none are found.
85 : List<String> errors() {
86 1 : final List<String> text = new List<String>();
87 3 : if (_stack.length == 0) return null;
88 2 : ItemEntry entry = _stack.last;
89 3 : if (entry.value.hint == dataHints.error) {
90 3 : text.add(entry.value.data);
91 : }
92 1 : while (entry.next != null) {
93 0 : final ItemEntry inner = entry.next;
94 0 : if (inner.value.hint == dataHints.error) {
95 0 : text.add(entry.value.data);
96 : }
97 : entry = inner;
98 : }
99 : return text;
100 : }
101 :
102 : /// Quick check if the stack contains any errors.
103 : bool hasErrors() {
104 1 : if (errors() == null) {
105 : return false;
106 : } else {
107 : return true;
108 : }
109 : }
110 : }
|