Skip to content

Commit

Permalink
[hay] Fix and test serialization of code blocks
Browse files Browse the repository at this point in the history
A big part of #951.

We are printing code blocks and have location info.

Still need to write an end-to-end demo, and possibly modify the rules
for '=' inside blocks.
  • Loading branch information
Andy C committed Jun 6, 2022
1 parent 5f6847e commit 1b2f7b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
22 changes: 13 additions & 9 deletions core/alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,25 @@ def GetCodeString(self, lbrace_spid, rbrace_spid):
right_id = right_span.line_id
assert left_id <= right_id

left_col = left_span.col
left_col = left_span.col # 0-based indices
right_col = right_span.col

parts = []
parts.append(' ' * (left_col+1)) # pad with spaces so column numbers are the same

# first incomplete line
parts.append(' ' * left_col) # pad with spaces so column numbers are the same
parts.append(self.line_vals[left_id][left_col+1:])
if left_id == right_id:
# the single line
parts.append(self.line_vals[left_id][left_col+1:right_col])
else:
# first incomplete line
parts.append(self.line_vals[left_id][left_col+1:])

# all the complete lines
for line_id in xrange(left_id + 1, right_id):
parts.append(self.line_vals[line_id])
# all the complete lines
for line_id in xrange(left_id + 1, right_id):
parts.append(self.line_vals[line_id])

# last incomplete line
parts.append(self.line_vals[right_id][:right_col])
# last incomplete line
parts.append(self.line_vals[right_id][:right_col])

return ''.join(parts)

Expand Down
5 changes: 3 additions & 2 deletions osh/builtin_pure.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,9 @@ def Run(self, cmd_val):
src = self.arena.GetLineSource(line_span.line_id)
line_num = self.arena.GetLineNumber(line_span.line_id)

result['location_file'] = ui.GetLineSourceString(self.arena,
line_span.line_id)
# for the user to pass back to --location-str
result['location_str'] = ui.GetLineSourceString(self.arena,
line_span.line_id)
result['location_start_line'] = line_num

# Between { and }
Expand Down
31 changes: 28 additions & 3 deletions spec/hay.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,38 @@ shopt --set parse_brace parse_equals
hay define TASK

hay eval :result {
TASK build {
echo hi
TASK { echo hi }

TASK {
echo one
echo two
}
}

#= result
json write (result)
json write (result) | jq . > out.txt

diff -u - out.txt <<'EOF'
{
"source": null,
"children": [
{
"type": "TASK",
"args": [],
"location_str": "[ stdin ]",
"location_start_line": 6,
"code_str": " echo hi "
},
{
"type": "TASK",
"args": [],
"location_str": "[ stdin ]",
"location_start_line": 8,
"code_str": " \n echo one\n echo two\n "
}
]
}
EOF

## STDOUT:
## END
Expand Down
2 changes: 1 addition & 1 deletion test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ oil-blocks() {
}

hay() {
sh-spec spec/hay.test.sh --osh-failures-allowed 10 \
sh-spec spec/hay.test.sh --osh-failures-allowed 9 \
$OSH_LIST "$@"
}

Expand Down

0 comments on commit 1b2f7b5

Please sign in to comment.