From b1e3a15c0853ca573df47ab36f2e9fd97c3a0b50 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 22 Feb 2020 13:06:12 +1100 Subject: [PATCH] Add endwhile syntax --- doc/changelog.md | 1 + doc/wuffs-the-language.md | 3 ++- lang/parse/parse.go | 20 ++++++++++++++ lang/token/list.go | 38 ++++++++++++++------------- std/deflate/decode_deflate.wuffs | 2 +- std/deflate/decode_huffman_fast.wuffs | 2 +- std/deflate/decode_huffman_slow.wuffs | 2 +- std/gif/decode_gif.wuffs | 4 +-- std/json/decode_json.wuffs | 10 +++---- 9 files changed, 53 insertions(+), 29 deletions(-) diff --git a/doc/changelog.md b/doc/changelog.md index d8fb1935..d86fa041 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -3,6 +3,7 @@ ## Work In Progress +- Added `endwhile` syntax. - Added `example/imageviewer`. - Added `example/jsonptr`. - Added `std/bmp`. diff --git a/doc/wuffs-the-language.md b/doc/wuffs-the-language.md index cf226f42..54c7cb3b 100644 --- a/doc/wuffs-the-language.md +++ b/doc/wuffs-the-language.md @@ -31,7 +31,8 @@ Minor features or restrictions include: an `if` or `while` condition are optional, but the `{}` curly braces are mandatory. There is no 'dangling else' ambiguity. - Labeled jumps look like `break.loopname` and `continue.loopname`, for a - matching `while.loopname`. + matching `while.loopname`. The `while.loopname`'s closing curly brace must be + followed by `endwhile.loopname`. Wuffs code is formatted by the [`wuffsfmt`](https://godoc.org/github.com/google/wuffs/cmd/wuffsfmt) program. diff --git a/lang/parse/parse.go b/lang/parse/parse.go index dd59a718..ab54a693 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -644,6 +644,22 @@ func (p *parser) parseLabel() (t.ID, error) { return 0, nil } +func (p *parser) parseEndwhile(label t.ID) bool { + if p.peek1() != t.IDEndwhile { + return false + } + p.src = p.src[1:] + if p.peek1() != t.IDDot { + return false + } + p.src = p.src[1:] + if p.peek1() != label { + return false + } + p.src = p.src[1:] + return true +} + func (p *parser) parseStatement1() (*a.Node, error) { x := p.peek1() if x == t.IDVar { @@ -721,6 +737,10 @@ func (p *parser) parseStatement1() (*a.Node, error) { if err != nil { return nil, err } + if (label != 0) && !p.parseEndwhile(label) { + return nil, fmt.Errorf(`parse: expected endwhile.%s at %s:%d`, + label.Str(p.tm), p.filename, p.line()) + } return a.NewWhile(label, condition, asserts, body).AsNode(), nil } return p.parseAssignNode() diff --git a/lang/token/list.go b/lang/token/list.go index 3ade9bca..ea2e069c 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -366,24 +366,25 @@ const ( IDConst = ID(0xA2) IDContinue = ID(0xA3) IDElse = ID(0xA4) - IDFunc = ID(0xA5) - IDIOBind = ID(0xA6) - IDIOLimit = ID(0xA7) - IDIf = ID(0xA8) - IDImplements = ID(0xA9) - IDInv = ID(0xAA) - IDIterate = ID(0xAB) - IDPost = ID(0xAC) - IDPre = ID(0xAD) - IDPri = ID(0xAE) - IDPub = ID(0xAF) - IDReturn = ID(0xB0) - IDStruct = ID(0xB1) - IDUse = ID(0xB2) - IDVar = ID(0xB3) - IDVia = ID(0xB4) - IDWhile = ID(0xB5) - IDYield = ID(0xB6) + IDEndwhile = ID(0xA5) + IDFunc = ID(0xA6) + IDIOBind = ID(0xA7) + IDIOLimit = ID(0xA8) + IDIf = ID(0xA9) + IDImplements = ID(0xAA) + IDInv = ID(0xAB) + IDIterate = ID(0xAC) + IDPost = ID(0xAD) + IDPre = ID(0xAE) + IDPri = ID(0xAF) + IDPub = ID(0xB0) + IDReturn = ID(0xB1) + IDStruct = ID(0xB2) + IDUse = ID(0xB3) + IDVar = ID(0xB4) + IDVia = ID(0xB5) + IDWhile = ID(0xB6) + IDYield = ID(0xB7) ) const ( @@ -723,6 +724,7 @@ var builtInsByID = [nBuiltInIDs]string{ IDConst: "const", IDContinue: "continue", IDElse: "else", + IDEndwhile: "endwhile", IDFunc: "func", IDIOBind: "io_bind", IDIOLimit: "io_limit", diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index db10b88a..4e2dcb14 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -257,7 +257,7 @@ pri func decoder.decode_blocks?(dst: base.io_writer, src: base.io_reader) { continue.outer } } - } + } endwhile.outer } // decode_uncompressed decodes an uncompresed block as per the RFC section diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index 9d7f460d..7f7bd0f3 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -362,7 +362,7 @@ pri func decoder.decode_huffman_fast!(dst: base.io_writer, src: base.io_reader) args.dst.copy_n32_from_history_fast!(n: length, distance: (dist_minus_1 + 1)) break } - } + } endwhile.loop // Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its // bytes into the bits variable. diff --git a/std/deflate/decode_huffman_slow.wuffs b/std/deflate/decode_huffman_slow.wuffs index 73ede6de..a6b07d23 100644 --- a/std/deflate/decode_huffman_slow.wuffs +++ b/std/deflate/decode_huffman_slow.wuffs @@ -272,7 +272,7 @@ pri func decoder.decode_huffman_slow?(dst: base.io_writer, src: base.io_reader) length -= n_copied yield? base."$short write" } - } + } endwhile.loop // TODO: "assert n_bits < 8"? What about (bits >> n_bits)? diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 69fba952..62f3a7e4 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -992,8 +992,8 @@ pri func decoder.decode_id_part2?(dst: ptr base.pixel_buffer, src: base.io_reade continue.inner } return lzw_status - } - } + } endwhile.inner + } endwhile.outer this.compressed_ri = 0 this.compressed_wi = 0 diff --git a/std/json/decode_json.wuffs b/std/json/decode_json.wuffs index 3cfd4ce3..a3ed1923 100644 --- a/std/json/decode_json.wuffs +++ b/std/json/decode_json.wuffs @@ -119,7 +119,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) { continue.outer } whitespace_length += 1 - } // while.ws + } endwhile.ws // Emit whitespace. if whitespace_length > 0 { @@ -286,7 +286,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) { } string_length += 1 } - } // while.string_loop + } endwhile.string_loop // The trailing '"' is filler. while true { @@ -523,7 +523,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) { } return "#bad input" - } // while.goto_parsed_a_leaf_value + } endwhile.goto_parsed_a_leaf_value // We've just parsed a leaf (non-container) value: literal (null, // false, true), number or string. @@ -531,7 +531,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) { break.outer } expect = expect_after_value - } // while.outer + } endwhile.outer } pri func decoder.decode_number!(src: base.io_reader) base.u32[..= 0x3FF] { @@ -654,7 +654,7 @@ pri func decoder.decode_number!(src: base.io_reader) base.u32[..= 0x3FF] { n = this.decode_digits!(src: args.src, n: n) break.goto_done - } // while.goto_done + } endwhile.goto_done return n | floating_point }