Line | Branch | Exec | Source |
---|---|---|---|
1 | |||
2 | #include "lexer/unlex.h" | ||
3 | |||
4 | namespace lython { | ||
5 | |||
6 | std::ostream& Unlex::format(std::ostream& out, Array<Token> const& tokens) { | ||
7 |
2/2✓ Branch 5 taken 2497 times.
✓ Branch 6 taken 566 times.
|
3063 | for (auto& tok: tokens) { |
8 |
1/1✓ Branch 1 taken 2497 times.
|
2497 | format(out, tok); |
9 | |||
10 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2497 times.
|
2497 | if (should_stop) { |
11 | ✗ | should_stop = false; | |
12 | ✗ | break; | |
13 | } | ||
14 | } | ||
15 | 566 | return out; | |
16 | } | ||
17 | |||
18 | std::ostream& Unlex::format(std::ostream& out, Token const& token, int indent) { | ||
19 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2852 times.
|
2852 | if (indent > 0) |
20 | ✗ | indent_level = indent; | |
21 | |||
22 | // because indent_level is static we need a token to reset the value | ||
23 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 2 taken 2827 times.
|
2852 | if (token.type() == tok_eof) { |
24 | 25 | indent_level = 0; | |
25 | 25 | emptyline = true; | |
26 | 25 | open_parens = false; | |
27 | 25 | prev_is_op = false; | |
28 | 25 | return out; | |
29 | } | ||
30 | |||
31 | // Invisible Token | ||
32 |
2/2✓ Branch 1 taken 83 times.
✓ Branch 2 taken 2744 times.
|
2827 | if (token.type() == tok_indent) { |
33 | 83 | indent_level += 1; | |
34 | 83 | return out; | |
35 | } | ||
36 | |||
37 |
2/2✓ Branch 1 taken 73 times.
✓ Branch 2 taken 2671 times.
|
2744 | if (token.type() == tok_desindent) { |
38 | 73 | indent_level -= 1; | |
39 | 73 | return out; | |
40 | } | ||
41 | |||
42 |
2/2✓ Branch 1 taken 55 times.
✓ Branch 2 taken 2616 times.
|
2671 | if (token.type() == tok_newline) { |
43 | 55 | emptyline = true; | |
44 | |||
45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
|
55 | if (stop_on_newline) { |
46 | ✗ | should_stop = stop_on_newline; | |
47 | ✗ | return out; | |
48 | } | ||
49 | |||
50 | 55 | out << std::endl; | |
51 | 55 | return out; | |
52 | } | ||
53 | |||
54 |
10/10✓ Branch 1 taken 2536 times.
✓ Branch 2 taken 80 times.
✓ Branch 4 taken 2505 times.
✓ Branch 5 taken 31 times.
✓ Branch 7 taken 2472 times.
✓ Branch 8 taken 33 times.
✓ Branch 9 taken 148 times.
✓ Branch 10 taken 2324 times.
✓ Branch 11 taken 292 times.
✓ Branch 12 taken 2324 times.
|
5088 | if (token.type() == tok_operator || token.type() == tok_dot || token.type() == tok_in || |
55 | 2472 | token.type() == tok_assign) { | |
56 |
5/6✓ Branch 2 taken 261 times.
✓ Branch 3 taken 31 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 261 times.
✓ Branch 8 taken 31 times.
✓ Branch 9 taken 261 times.
|
292 | if (token.operator_name() == "." || token.operator_name() == "@") { |
57 | 31 | out << token.operator_name(); | |
58 | } else { | ||
59 | 261 | out << " " << token.operator_name() << " "; | |
60 | } | ||
61 | |||
62 | 292 | prev_is_op = true; | |
63 | 292 | return out; | |
64 | } | ||
65 | |||
66 | // Indentation | ||
67 |
4/4✓ Branch 0 taken 530 times.
✓ Branch 1 taken 1794 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 476 times.
|
2324 | if (emptyline && indent_level > 0) |
68 |
2/2✓ Branch 2 taken 54 times.
✓ Branch 5 taken 54 times.
|
54 | out << String(std::size_t(indent_level * LYTHON_INDENT), ' '); |
69 | |||
70 |
1/1✓ Branch 3 taken 2324 times.
|
2324 | String const& str = keyword_as_string()[token.type()]; |
71 | |||
72 |
2/2✓ Branch 1 taken 440 times.
✓ Branch 2 taken 1884 times.
|
2324 | if (str.size() > 0) { |
73 | 440 | emptyline = false; | |
74 | |||
75 |
3/3✓ Branch 1 taken 76 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 336 times.
|
440 | switch (token.type()) { |
76 | 76 | case tok_arrow: | |
77 | 76 | case tok_as: out << " "; break; | |
78 | |||
79 | 28 | case tok_import: | |
80 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (!emptyline) { |
81 | 28 | out << " "; | |
82 | } | ||
83 | 28 | break; | |
84 | } | ||
85 | |||
86 | 440 | return out << str; | |
87 | } | ||
88 | |||
89 | // Single Char | ||
90 |
2/2✓ Branch 1 taken 767 times.
✓ Branch 2 taken 1117 times.
|
1884 | if (token.type() > 0) { |
91 | |||
92 |
6/6✓ Branch 1 taken 637 times.
✓ Branch 2 taken 130 times.
✓ Branch 4 taken 53 times.
✓ Branch 5 taken 584 times.
✓ Branch 6 taken 183 times.
✓ Branch 7 taken 584 times.
|
767 | if (token.type() == '(' || token.type() == '[') |
93 | 183 | open_parens = true; | |
94 | else | ||
95 | 584 | open_parens = false; | |
96 | |||
97 | 767 | emptyline = false; | |
98 | 767 | out << token.type(); | |
99 | 767 | return out; | |
100 | } | ||
101 | |||
102 | // Everything else | ||
103 | // we printout what the lexer read in identifier (no risk of error) | ||
104 | |||
105 | // no space if the line is empty and no space if it is just after | ||
106 | // a open parens | ||
107 |
6/6✓ Branch 0 taken 930 times.
✓ Branch 1 taken 187 times.
✓ Branch 2 taken 767 times.
✓ Branch 3 taken 163 times.
✓ Branch 4 taken 628 times.
✓ Branch 5 taken 139 times.
|
1117 | if (!prev_is_op && !emptyline && !open_parens) { |
108 | 628 | out << ' '; | |
109 | } | ||
110 | 1117 | prev_is_op = false; | |
111 | |||
112 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1099 times.
|
1117 | if (token.type() == tok_string) |
113 | 18 | out << '"' << token.identifier() << '"'; | |
114 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1096 times.
|
1099 | else if (token.type() == tok_docstring) |
115 | 3 | out << "\"\"\"" << token.identifier() << "\"\"\""; | |
116 |
6/6✓ Branch 1 taken 1016 times.
✓ Branch 2 taken 80 times.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 1000 times.
✓ Branch 6 taken 96 times.
✓ Branch 7 taken 1000 times.
|
1096 | else if (token.type() == tok_int || token.type() == tok_float) |
117 | 96 | out << token.identifier(); | |
118 | else | ||
119 | 1000 | out << token.identifier(); | |
120 | |||
121 | 1117 | open_parens = false; | |
122 | 1117 | emptyline = false; | |
123 | 1117 | prev_is_op = false; | |
124 | |||
125 | 1117 | return out; | |
126 | } | ||
127 | |||
128 | void Unlex::reset() { | ||
129 | ✗ | indent_level = 0; | |
130 | ✗ | emptyline = true; | |
131 | ✗ | open_parens = false; | |
132 | ✗ | prev_is_op = false; | |
133 | ✗ | } | |
134 | } // namespace lython | ||
135 |