summary refs log tree commit diff stats
path: root/raku/bob/bob.rakutest
blob: a540719166bba3faf6c0f5d9da12ac76236a849b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env raku
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname; #`[Look for the module inside the same directory as this test file.]
use Bob;
plan 26; #`[This is how many tests we expect to run.]

#`[Check that the class 'Bob' can use all of the methods
needed in the tests (only 'hey' for this one).]
subtest 'Class methods', {
  for <hey> -> $method {
    can-ok Bob, $method;
  }
} or bail-out 'Cannot run expected method(s).';

my @test-cases = from-json($=pod.pop.contents).List;
# Go through the cases and check that Bob gives us the correct responses.
for @test-cases -> %case {
  is Bob.hey(%case<input><heyBob>), |%case<expected description>;
}

=head2 Test Cases
=begin code
[
  {
    "description": "stating something",
    "expected": "Whatever.",
    "input": {
      "heyBob": "Tom-ay-to, tom-aaaah-to."
    },
    "property": "response"
  },
  {
    "description": "shouting",
    "expected": "Whoa, chill out!",
    "input": {
      "heyBob": "WATCH OUT!"
    },
    "property": "response"
  },
  {
    "description": "shouting gibberish",
    "expected": "Whoa, chill out!",
    "input": {
      "heyBob": "FCECDFCAAB"
    },
    "property": "response"
  },
  {
    "description": "asking a question",
    "expected": "Sure.",
    "input": {
      "heyBob": "Does this cryogenic chamber make me look fat?"
    },
    "property": "response"
  },
  {
    "description": "asking a numeric question",
    "expected": "Sure.",
    "input": {
      "heyBob": "You are, what, like 15?"
    },
    "property": "response"
  },
  {
    "description": "asking gibberish",
    "expected": "Sure.",
    "input": {
      "heyBob": "fffbbcbeab?"
    },
    "property": "response"
  },
  {
    "description": "talking forcefully",
    "expected": "Whatever.",
    "input": {
      "heyBob": "Hi there!"
    },
    "property": "response"
  },
  {
    "description": "using acronyms in regular speech",
    "expected": "Whatever.",
    "input": {
      "heyBob": "It's OK if you don't want to go work for NASA."
    },
    "property": "response"
  },
  {
    "description": "forceful question",
    "expected": "Calm down, I know what I'm doing!",
    "input": {
      "heyBob": "WHAT'S GOING ON?"
    },
    "property": "response"
  },
  {
    "description": "shouting numbers",
    "expected": "Whoa, chill out!",
    "input": {
      "heyBob": "1, 2, 3 GO!"
    },
    "property": "response"
  },
  {
    "description": "no letters",
    "expected": "Whatever.",
    "input": {
      "heyBob": "1, 2, 3"
    },
    "property": "response"
  },
  {
    "description": "question with no letters",
    "expected": "Sure.",
    "input": {
      "heyBob": "4?"
    },
    "property": "response"
  },
  {
    "description": "shouting with special characters",
    "expected": "Whoa, chill out!",
    "input": {
      "heyBob": "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"
    },
    "property": "response"
  },
  {
    "description": "shouting with no exclamation mark",
    "expected": "Whoa, chill out!",
    "input": {
      "heyBob": "I HATE THE DENTIST"
    },
    "property": "response"
  },
  {
    "description": "statement containing question mark",
    "expected": "Whatever.",
    "input": {
      "heyBob": "Ending with ? means a question."
    },
    "property": "response"
  },
  {
    "description": "non-letters with question",
    "expected": "Sure.",
    "input": {
      "heyBob": ":) ?"
    },
    "property": "response"
  },
  {
    "description": "prattling on",
    "expected": "Sure.",
    "input": {
      "heyBob": "Wait! Hang on. Are you going to be OK?"
    },
    "property": "response"
  },
  {
    "description": "silence",
    "expected": "Fine. Be that way!",
    "input": {
      "heyBob": ""
    },
    "property": "response"
  },
  {
    "description": "prolonged silence",
    "expected": "Fine. Be that way!",
    "input": {
      "heyBob": "          "
    },
    "property": "response"
  },
  {
    "description": "alternate silence",
    "expected": "Fine. Be that way!",
    "input": {
      "heyBob": "\t\t\t\t\t\t\t\t\t\t"
    },
    "property": "response"
  },
  {
    "description": "multiple line question",
    "expected": "Whatever.",
    "input": {
      "heyBob": "\nDoes this cryogenic chamber make me look fat?\nNo."
    },
    "property": "response"
  },
  {
    "description": "starting with whitespace",
    "expected": "Whatever.",
    "input": {
      "heyBob": "         hmmmmmmm..."
    },
    "property": "response"
  },
  {
    "description": "ending with whitespace",
    "expected": "Sure.",
    "input": {
      "heyBob": "Okay if like my  spacebar  quite a bit?   "
    },
    "property": "response"
  },
  {
    "description": "other whitespace",
    "expected": "Fine. Be that way!",
    "input": {
      "heyBob": "\n\r \t"
    },
    "property": "response"
  },
  {
    "description": "non-question ending with whitespace",
    "expected": "Whatever.",
    "input": {
      "heyBob": "This is a statement ending with whitespace      "
    },
    "property": "response"
  }
]
=end code
an class="p">; } .traceContains { color: #008000; } --> </style> <script type='text/javascript'> <!-- --> </script> </head> <body> <pre id='vimCodeElement'> <span class="Comment">//: Phase 1 of running mu code: load it from a textual representation.</span> <span class="Delimiter">:(scenarios load)</span> <span class="Comment">// use 'load' instead of 'run' in all scenarios in this layer</span> <span class="Delimiter">:(scenario first_recipe)</span> recipe main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(code)</span> vector&lt;recipe_ordinal&gt; load<span class="Delimiter">(</span>string form<span class="Delimiter">)</span> <span class="Delimiter">{</span> istringstream in<span class="Delimiter">(</span>form<span class="Delimiter">);</span> in &gt;&gt; std::noskipws<span class="Delimiter">;</span> <span class="Identifier">return</span> load<span class="Delimiter">(</span>in<span class="Delimiter">);</span> <span class="Delimiter">}</span> vector&lt;recipe_ordinal&gt; load<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> in &gt;&gt; std::noskipws<span class="Delimiter">;</span> vector&lt;recipe_ordinal&gt; result<span class="Delimiter">;</span> while <span class="Delimiter">(</span>!in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span> skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Identifier">break</span><span class="Delimiter">;</span> string command = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span> <span class="Comment">// Command Handlers</span> if <span class="Delimiter">(</span>command == <span class="Constant">&quot;recipe&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>slurp_recipe<span class="Delimiter">(</span>in<span class="Delimiter">));</span> <span class="Delimiter">}</span> else if <span class="Delimiter">(</span>command == <span class="Constant">&quot;recipe!&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> Disable_redefine_warnings = <span class="Constant">true</span><span class="Delimiter">;</span> result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>slurp_recipe<span class="Delimiter">(</span>in<span class="Delimiter">));</span> Disable_redefine_warnings = <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Comment">// End Command Handlers</span> else <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;unknown top-level command: &quot;</span> &lt;&lt; command &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Delimiter">}</span> <span class="Delimiter">}</span> <span class="Identifier">return</span> result<span class="Delimiter">;</span> <span class="Delimiter">}</span> long long int slurp_recipe<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> string recipe_name = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span> if <span class="Delimiter">(</span>recipe_name<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> raise &lt;&lt; <span class="Constant">&quot;empty recipe name</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> if <span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">.</span>find<span class="Delimiter">(</span>recipe_name<span class="Delimiter">)</span> == Recipe_ordinal<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> Recipe_ordinal[recipe_name] = Next_recipe_ordinal++<span class="Delimiter">;</span> <span class="Delimiter">}</span> if <span class="Delimiter">(</span>warn_on_redefine<span class="Delimiter">(</span>recipe_name<span class="Delimiter">)</span> &amp;&amp; Recipe<span class="Delimiter">.</span>find<span class="Delimiter">(</span>Recipe_ordinal[recipe_name]<span class="Delimiter">)</span> != Recipe<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;redefining recipe &quot;</span> &lt;&lt; Recipe[Recipe_ordinal[recipe_name]]<span class="Delimiter">.</span>name &lt;&lt; <span class="Constant">&quot;</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Delimiter">}</span> <span class="Comment">// todo: save user-defined recipes to mu's memory</span> Recipe[Recipe_ordinal[recipe_name]] = slurp_body<span class="Delimiter">(</span>in<span class="Delimiter">);</span> Recipe[Recipe_ordinal[recipe_name]]<span class="Delimiter">.</span>name = recipe_name<span class="Delimiter">;</span> <span class="Comment">// track added recipes because we may need to undo them in tests; see below</span> recently_added_recipes<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>Recipe_ordinal[recipe_name]<span class="Delimiter">);</span> <span class="Identifier">return</span> Recipe_ordinal[recipe_name]<span class="Delimiter">;</span> <span class="Delimiter">}</span> recipe slurp_body<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> recipe result<span class="Delimiter">;</span> skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>get<span class="Delimiter">()</span> != <span class="Constant">'['</span><span class="Delimiter">)</span> raise &lt;&lt; <span class="Constant">&quot;recipe body must begin with '['</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span> instruction curr<span class="Delimiter">;</span> while <span class="Delimiter">(</span>next_instruction<span class="Delimiter">(</span>in<span class="Delimiter">,</span> &amp;curr<span class="Delimiter">))</span> <span class="Delimiter">{</span> <span class="Comment">// End Rewrite Instruction(curr)</span> result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>curr<span class="Delimiter">);</span> <span class="Delimiter">}</span> <span class="Identifier">return</span> result<span class="Delimiter">;</span> <span class="Delimiter">}</span> bool next_instruction<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">,</span> instruction* curr<span class="Delimiter">)</span> <span class="Delimiter">{</span> in &gt;&gt; std::noskipws<span class="Delimiter">;</span> curr<span class="Delimiter">-&gt;</span>clear<span class="Delimiter">();</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;0: unbalanced '[' for recipe</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;1: unbalanced '[' for recipe</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;2: unbalanced '[' for recipe</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> vector&lt;string&gt; words<span class="Delimiter">;</span> while <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span> &amp;&amp; !in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span> skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;3: unbalanced '[' for recipe</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> string word = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span> words<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>word<span class="Delimiter">);</span> skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span> <span class="Delimiter">}</span> skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span> if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>words<span class="Delimiter">)</span> == <span class="Constant">1</span> &amp;&amp; words<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">&quot;]&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Comment">// end of recipe</span> <span class="Delimiter">}</span> if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>words<span class="Delimiter">)</span> == <span class="Constant">1</span> &amp;&amp; !isalnum<span class="Delimiter">(</span>words<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> &amp;&amp; words<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> != <span class="Constant">'$'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> curr<span class="Delimiter">-&gt;</span>is_label = <span class="Constant">true</span><span class="Delimiter">;</span> curr<span class="Delimiter">-&gt;</span>label = words<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> trace<span class="Delimiter">(</span><span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;label: &quot;</span> &lt;&lt; curr<span class="Delimiter">-&gt;</span>label &lt;&lt; end<span class="Delimiter">();</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;7: unbalanced '[' for recipe</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> vector&lt;string&gt;::iterator p = words<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> if <span class="Delimiter">(</span>find<span class="Delimiter">(</span>words<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> words<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> <span class="Constant">&quot;&lt;-&quot;</span><span class="Delimiter">)</span> != words<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> for <span class="Delimiter">(;</span> *p != <span class="Constant">&quot;&lt;-&quot;</span><span class="Delimiter">;</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>*p == <span class="Constant">&quot;,&quot;</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> curr<span class="Delimiter">-&gt;</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span>*p<span class="Delimiter">));</span> <span class="Delimiter">}</span> ++p<span class="Delimiter">;</span> <span class="Comment">// skip &lt;-</span> <span class="Delimiter">}</span> if <span class="Delimiter">(</span>p == words<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;instruction prematurely ended with '&lt;-'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">()</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> curr<span class="Delimiter">-&gt;</span>name = *p<span class="Delimiter">;</span> if <span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">.</span>find<span class="Delimiter">(</span>*p<span class="Delimiter">)</span> == Recipe_ordinal<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span> Recipe_ordinal[*p] = Next_recipe_ordinal++<span class="Delimiter">;</span> <span class="Delimiter">}</span> if <span class="Delimiter">(</span>Recipe_ordinal[*p] == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;Recipe &quot;</span> &lt;&lt; *p &lt;&lt; <span class="Constant">&quot; has number 0, which is reserved for IDLE.</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">()</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> curr<span class="Delimiter">-&gt;</span>operation = Recipe_ordinal[*p]<span class="Delimiter">;</span> ++p<span class="Delimiter">;</span> for <span class="Delimiter">(;</span> p != words<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>*p == <span class="Constant">&quot;,&quot;</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span> curr<span class="Delimiter">-&gt;</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span>*p<span class="Delimiter">));</span> <span class="Delimiter">}</span> trace<span class="Delimiter">(</span><span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;instruction: &quot;</span> &lt;&lt; curr<span class="Delimiter">-&gt;</span>name &lt;&lt; end<span class="Delimiter">();</span> for <span class="Delimiter">(</span>vector&lt;reagent&gt;::iterator p = curr<span class="Delimiter">-&gt;</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != curr<span class="Delimiter">-&gt;</span>ingredients<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span> trace<span class="Delimiter">(</span><span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot; ingredient: &quot;</span> &lt;&lt; p<span class="Delimiter">-&gt;</span>to_string<span class="Delimiter">()</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Delimiter">}</span> for <span class="Delimiter">(</span>vector&lt;reagent&gt;::iterator p = curr<span class="Delimiter">-&gt;</span>products<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != curr<span class="Delimiter">-&gt;</span>products<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span> trace<span class="Delimiter">(</span><span class="Constant">&quot;parse&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot; product: &quot;</span> &lt;&lt; p<span class="Delimiter">-&gt;</span>to_string<span class="Delimiter">()</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Delimiter">}</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span> raise &lt;&lt; <span class="Constant">&quot;9: unbalanced '[' for recipe</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> string next_word<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> ostringstream out<span class="Delimiter">;</span> skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span> slurp_word<span class="Delimiter">(</span>in<span class="Delimiter">,</span> out<span class="Delimiter">);</span> skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span> skip_comment<span class="Delimiter">(</span>in<span class="Delimiter">);</span> <span class="Identifier">return</span> out<span class="Delimiter">.</span>str<span class="Delimiter">();</span> <span class="Delimiter">}</span> void slurp_word<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">,</span> ostream&amp; out<span class="Delimiter">)</span> <span class="Delimiter">{</span> char c<span class="Delimiter">;</span> if <span class="Delimiter">(</span>!in<span class="Delimiter">.</span>eof<span class="Delimiter">()</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">','</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> in &gt;&gt; c<span class="Delimiter">;</span> out &lt;&lt; c<span class="Delimiter">;</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> while <span class="Delimiter">(</span>in &gt;&gt; c<span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>isspace<span class="Delimiter">(</span>c<span class="Delimiter">)</span> || c == <span class="Constant">','</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> in<span class="Delimiter">.</span>putback<span class="Delimiter">(</span>c<span class="Delimiter">);</span> <span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> out &lt;&lt; c<span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Delimiter">}</span> void skip_whitespace<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> while <span class="Delimiter">(</span>!in<span class="Delimiter">.</span>eof<span class="Delimiter">()</span> &amp;&amp; isspace<span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">())</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span> <span class="Delimiter">}</span> <span class="Delimiter">}</span> void skip_whitespace_and_comments<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> while <span class="Delimiter">(</span><span class="Constant">true</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Identifier">break</span><span class="Delimiter">;</span> if <span class="Delimiter">(</span>isspace<span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()))</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span> else if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">'#'</span><span class="Delimiter">)</span> skip_comment<span class="Delimiter">(</span>in<span class="Delimiter">);</span> else <span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Delimiter">}</span> void skip_comment<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>!in<span class="Delimiter">.</span>eof<span class="Delimiter">()</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">'#'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span> while <span class="Delimiter">(</span>!in<span class="Delimiter">.</span>eof<span class="Delimiter">()</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span> <span class="Delimiter">}</span> <span class="Delimiter">}</span> void skip_comma<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span> if <span class="Delimiter">(</span>!in<span class="Delimiter">.</span>eof<span class="Delimiter">()</span> &amp;&amp; in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">','</span><span class="Delimiter">)</span> in<span class="Delimiter">.</span>get<span class="Delimiter">();</span> skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span> <span class="Delimiter">}</span> <span class="Comment">//: Warn if a recipe gets redefined, because large codebases can accidentally</span> <span class="Comment">//: step on their own toes. But there'll be many occasions later where</span> <span class="Comment">//: we'll want to disable the warnings.</span> <span class="Delimiter">:(before &quot;End Globals&quot;)</span> bool Disable_redefine_warnings = <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">:(before &quot;End Setup&quot;)</span> Disable_redefine_warnings = <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">:(code)</span> bool warn_on_redefine<span class="Delimiter">(</span>const string&amp; recipe_name<span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>Disable_redefine_warnings<span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> <span class="Comment">// for debugging</span> <span class="Delimiter">:(before &quot;End Globals&quot;)</span> bool Show_rest_of_stream = <span class="Constant">false</span><span class="Delimiter">;</span> <span class="Delimiter">:(code)</span> void show_rest_of_stream<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>!Show_rest_of_stream<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span> cerr &lt;&lt; <span class="Constant">'^'</span><span class="Delimiter">;</span> char c<span class="Delimiter">;</span> while <span class="Delimiter">(</span>in &gt;&gt; c<span class="Delimiter">)</span> <span class="Delimiter">{</span> cerr &lt;&lt; c<span class="Delimiter">;</span> <span class="Delimiter">}</span> cerr &lt;&lt; <span class="Constant">&quot;$</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span> exit<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> <span class="Delimiter">}</span> <span class="Comment">//: Have tests clean up any recipes they added.</span> <span class="Delimiter">:(before &quot;End Globals&quot;)</span> vector&lt;recipe_ordinal&gt; recently_added_recipes<span class="Delimiter">;</span> long long int Reserved_for_tests = <span class="Constant">1000</span><span class="Delimiter">;</span> <span class="Delimiter">:(before &quot;End Setup&quot;)</span> for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>recently_added_recipes<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>recently_added_recipes<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span> &gt;= Reserved_for_tests<span class="Delimiter">)</span> <span class="Comment">// don't renumber existing recipes, like 'interactive'</span> Recipe_ordinal<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>Recipe[recently_added_recipes<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span>]<span class="Delimiter">.</span>name<span class="Delimiter">);</span> Recipe<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>recently_added_recipes<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span> <span class="Delimiter">}</span> <span class="Comment">// Clear Other State For recently_added_recipes</span> recently_added_recipes<span class="Delimiter">.</span>clear<span class="Delimiter">();</span> <span class="Delimiter">:(scenario parse_comment_outside_recipe)</span> <span class="Comment"># this comment will be dropped by the tangler, so we need a dummy recipe to stop that</span> recipe f1 [ ] <span class="Comment"># this comment will go through to 'load'</span> recipe main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_comment_amongst_instruction)</span> recipe main [ <span class="Comment"># comment</span> <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_comment_amongst_instruction_2)</span> recipe main [ <span class="Comment"># comment</span> <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> <span class="Comment"># comment</span> ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_comment_amongst_instruction_3)</span> recipe main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> <span class="Comment"># comment</span> <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;2&quot;, properties: [&quot;2&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_comment_after_instruction)</span> recipe main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> <span class="Comment"># comment</span> ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_label)</span> recipe main [ +foo ] <span class="traceContains">+parse: label: +foo</span> <span class="Delimiter">:(scenario parse_dollar_as_recipe_name)</span> recipe main [ $foo ] <span class="traceContains">+parse: instruction: $foo</span> <span class="Delimiter">:(scenario parse_multiple_properties)</span> recipe main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>/foo:bar:baz ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;, &quot;foo&quot;: &quot;bar&quot;:&quot;baz&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_multiple_products)</span> recipe main [ <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;2&quot;, properties: [&quot;2&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_multiple_ingredients)</span> recipe main [ <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span><span class="Delimiter">,</span> <span class="Constant">4</span>:number ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: ingredient: {name: &quot;4&quot;, properties: [&quot;4&quot;: &quot;number&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;2&quot;, properties: [&quot;2&quot;: &quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_multiple_types)</span> recipe main [ <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:address:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span><span class="Delimiter">,</span> <span class="Constant">4</span>:number ] <span class="traceContains">+parse: instruction: copy</span> <span class="traceContains">+parse: ingredient: {name: &quot;23&quot;, properties: [&quot;23&quot;: &quot;literal&quot;]}</span> <span class="traceContains">+parse: ingredient: {name: &quot;4&quot;, properties: [&quot;4&quot;: &quot;number&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;]}</span> <span class="traceContains">+parse: product: {name: &quot;2&quot;, properties: [&quot;2&quot;: &quot;address&quot;:&quot;number&quot;]}</span> <span class="Delimiter">:(scenario parse_properties)</span> recipe main [ <span class="Constant">1</span>:number:address/lookup<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> ] <span class="traceContains">+parse: product: {name: &quot;1&quot;, properties: [&quot;1&quot;: &quot;number&quot;:&quot;address&quot;, &quot;lookup&quot;: ]}</span> <span class="Comment">//: this test we can't represent with a scenario</span> <span class="Delimiter">:(code)</span> void test_parse_comment_terminated_by_eof<span class="Delimiter">()</span> <span class="Delimiter">{</span> Trace_file = <span class="Constant">&quot;parse_comment_terminated_by_eof&quot;</span><span class="Delimiter">;</span> load<span class="Delimiter">(</span><span class="Constant">&quot;recipe main [</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> <span class="Constant">&quot; a:number &lt;- copy 34</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> <span class="Constant">&quot;]</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> <span class="Constant">&quot;# abc&quot;</span><span class="Delimiter">);</span> <span class="Comment">// no newline after comment</span> cerr &lt;&lt; <span class="Constant">&quot;.&quot;</span><span class="Delimiter">;</span> <span class="Comment">// termination = success</span> <span class="Delimiter">}</span> <span class="Delimiter">:(scenario warn_on_redefine)</span> <span class="Special">% Hide_warnings = true;</span> recipe main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> ] recipe main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">24</span> ] <span class="traceContains">+warn: redefining recipe main</span> <span class="Delimiter">:(scenario redefine_without_warning)</span> <span class="Special">% Hide_warnings = true;</span> recipe main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">23</span> ] recipe! main [ <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">24</span> ] <span class="traceAbsent">-warn: redefining recipe main</span> $warn: <span class="Constant">0</span> </pre> </body> </html> <!-- vim: set foldmethod=manual : -->