• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

peterjwest / yason / fe350948-1b8a-4ace-9331-a18394309013

01 Aug 2024 11:39AM UTC coverage: 96.748% (+0.5%) from 96.234%
fe350948-1b8a-4ace-9331-a18394309013

push

circleci

peterjwest
Add snippet config

217 of 226 branches covered (96.02%)

Branch coverage included in aggregate %.

735 of 758 relevant lines covered (96.97%)

131.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/src/parse.ts
1
import lodash from 'lodash';
1✔
2

3
import { Token } from './tokens';
4
import DocumentNode from './DocumentNode';
1✔
5
import MapNode from './MapNode';
6
import ListNode from './ListNode';
7

8
/** Parse a yason string and return node graph for the data */
9
export default function parse(tokens: Token[]) {
21✔
10
  const document = new DocumentNode();
21✔
11
  const stack: Array<DocumentNode | MapNode | ListNode> = [document];
21✔
12
  let startIndex = 0;
21✔
13
  let endIndex = 0;
21✔
14
  while (startIndex < tokens.length || stack.length) {
21✔
15
    const currentTokens = tokens.slice(startIndex, endIndex + 1);
580✔
16

17
    const current = lodash.last(stack);
580✔
18

19
    if (!current) {
580!
20
      // TODO: Better error message
21
      throw Error('Stack empty');
×
22
    }
×
23

24
    // TODO: Remove
25
    // console.log(current.constructor.name, current.state, currentTokens);
26

27
    const response = current.runNextAction(currentTokens, document.indent);
580✔
28

29
    if (response) {
580✔
30
      if (response.result.indent) document.indent = response.result.indent;
439✔
31
      if (response.result.pop) stack.pop();
439✔
32
      if (response.result.push) stack.push(response.result.push);
439✔
33
      if (response.result.consumed !== undefined) {
439✔
34
        if (response.length && response.result.consumed > response.length) {
301!
35
          throw Error(
×
36
            `Parse error in ${current.constructor.name} ${current.state},` +
×
37
            `attempted to consume more tokens (${response.result.consumed}) than available (${response.length})`,
×
38
          );
×
39
        }
×
40
        startIndex = startIndex + response.result.consumed || 0;
301✔
41
      }
301✔
42
      if (startIndex > endIndex) endIndex++;
439✔
43
    } else {
580✔
44
      // TODO: Better error message
45
      if (currentTokens.length >= current.matchableTokens()) {
141!
46
        throw Error(
×
47
          `Parse error in ${current.constructor.name} ${current.state}: ` +
×
48
          `'${JSON.stringify(currentTokens)}'`,
×
49
        );
×
50
      }
×
51
      endIndex++;
141✔
52
    }
141✔
53

54
    if (endIndex > tokens.length) {
580!
55
      throw Error(
×
56
        `Parse error in ${current.constructor.name} ${current.state}, unexpected end of document`,
×
57
      );
×
58
    }
×
59
  }
580✔
60

61
  return document;
21✔
62
}
21✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc