Skip to content

Commit b551c16

Browse files
committed
Fix typo in source code. Improve documentation for cb_fail1.
1 parent f4d40a0 commit b551c16

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

JSON.awk

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,15 @@ function scream(msg) { #{{{
229229
NFAILS += (FILENAME in FAILS ? 0 : 1)
230230
FAILS[FILENAME] = FAILS[FILENAME] (FAILS[FILENAME]!="" ? "\n" : "") msg
231231
if(0 == STREAM) {
232-
# Call back the embedding program passing the error message
233-
cb_fail1(msg)
232+
# Call back the embedding program passing the error message,
233+
# which will be printed to stderr if the callback returns non-zero.
234+
if(cb_fail1(msg)) {
235+
print FILENAME ": " msg >"/dev/stderr"
236+
}
237+
} else {
238+
# Print error message when not not embedded.
239+
print FILENAME ": " msg >"/dev/stderr"
234240
}
235-
msg = FILENAME ": " msg
236-
print msg >"/dev/stderr"
237241
}
238242
#}}}
239243

callbacks.awk

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,30 @@ function cb_jpaths (ary, size, i) {
1111

1212
# Print ary - array of size jpaths.
1313
for(i=1; i <= size; i++) {
14-
print "callback", ary[i]
14+
print "cb_jpaths", ary[i]
1515
}
1616
}
1717

18-
# cb_fails - call back for processing errors after parsing has completed
19-
# Called in JSON.awk's END action when STREAM=0 only.
18+
# cb_fails - call back for processing all error messages at once after parsing
19+
# has completed. Called in JSON.awk's END action when STREAM=0 only.
2020
# This example illustrates printing parsing errors to stdout,
2121
function cb_fails (ary, size, k) {
2222

2323
# Print ary - associative array of parsing failures.
2424
# ary's keys are the size input file names that JSON.awk read.
2525
for(k in ary) {
26-
print "callback: invalid input file:", k
26+
print "cb_fails: invalid input file:", k
2727
print FAILS[k]
2828
}
2929
}
3030

31-
# cb_fail1 - call back for processing a parse error immediately
32-
# Called in JSON.awk's main loop when STREAM=0 only.
33-
# This example illustrates printing the error message to stdout.
34-
function cb_fails1 (message) {
31+
# cb_fail1 - call back for processing a single parse error as soon as it is
32+
# encountered. Called in JSON.awk's main loop when STREAM=0 only.
33+
# Return non-zero to let JSON.awk also print the message to stderr.
34+
# This example illustrates printing the error message to stdout only.
35+
function cb_fail1 (message) {
3536

36-
print "callback: invalid input file:", FILENAME
37+
print "cb_fail1: invalid input file:", FILENAME
3738
print message
3839
}
3940

doc/embed.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ another awk program to process the jpaths directly.
77

88
An awk program that embeds JSON.awk must satisfy two conditions:
99
* Set global variable `STREAM=0` before JSON.awk runs its main loop.
10-
* Define callback functions `cb_jpaths` and `cb_fails`.
10+
* Define callback functions `cb_jpaths`, `cb_fails` and `cb_fail1`.
1111

1212
When `STREAM=0` JSON.awk assumes that it is running embedded and modifies
1313
its behavior as follows:
@@ -19,9 +19,14 @@ its behavior as follows:
1919
`NJPATHS` is the array length. `JPATHS` will include non-leaf nodes when
2020
global variable `BRIEF=0`.
2121

22+
* When it encounters an error, JSON.awk's main loop will call function
23+
`cb_fail1(error_message)`, If `cb_fail1` returns a non-zero value then
24+
JSON.awk will print the error message to stderr, which is the default way
25+
to handle errors when JSON.awk isn't embedded.
26+
2227
* JSON.awk's END action will call function `cb_fails(FAILS, NFAILS)`, where
2328
`FAILS` is an associative array of error messages, if any, that JSON.awk
24-
prints to stderr while it parses JSON input, and `NFAILS` is the array length.
29+
accumulates while it parses JSON input, and `NFAILS` is the array length.
2530

2631
<a name="notes"></a>
2732
**Notes**

0 commit comments

Comments
 (0)