Add double-curly blocks

This commit is contained in:
Nigel Tao committed 2020-04-02 21:56:53 +11:00
1 parent 4cfc367a9b
commit 3fe3f84106
8 files changed
+1243 -1192

No files matched your search

+1
View File
@@ -14,6 +14,7 @@
- Added `std/wbmp`. - Added `std/wbmp`.
- Added `WUFFS_BASE__PIXEL_BLEND__SRC_OVER`. - Added `WUFFS_BASE__PIXEL_BLEND__SRC_OVER`.
- Added `WUFFS_BASE__PIXEL_FORMAT__BGR_565`. - Added `WUFFS_BASE__PIXEL_FORMAT__BGR_565`.
- Added double-curly blocks.
- Added interfaces. - Added interfaces.
- Added preprocessor. - Added preprocessor.
- Added single-quoted strings. - Added single-quoted strings.
+4
View File
@@ -30,6 +30,10 @@ Minor features or restrictions include:
- Labeled jumps look like `break.loopname` and `continue.loopname`, for a - Labeled jumps look like `break.loopname` and `continue.loopname`, for a
matching `while.loopname`. The `while.loopname`'s closing curly brace must be matching `while.loopname`. The `while.loopname`'s closing curly brace must be
followed by `endwhile.loopname`. followed by `endwhile.loopname`.
- A `while true` loop's block may use double-curly braces: `{{` and `}}`. The
formatter will not add an indent to the code inside the block. This is useful
when using `while true {{ etc; break; etc; break }}` to simulate what would
be a (forwards) `goto` in other languages' straight-line code.
Wuffs code is formatted by the Wuffs code is formatted by the
[`wuffsfmt`](https://godoc.org/github.com/google/wuffs/cmd/wuffsfmt) program. [`wuffsfmt`](https://godoc.org/github.com/google/wuffs/cmd/wuffsfmt) program.
+46 -17
View File
@@ -205,7 +205,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
} }
p.allowVar = true p.allowVar = true
body, err := p.parseBlock() body, err := p.parseBlock(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -341,11 +341,13 @@ func (p *parser) parseList(stop t.ID, parseElem func(*parser) (*a.Node, error))
ret := []*a.Node(nil) ret := []*a.Node(nil)
for len(p.src) > 0 { for len(p.src) > 0 {
if p.src[0].ID == stop { if id := p.src[0].ID; id == stop {
if stop == t.IDCloseParen || stop == t.IDCloseBracket { if stop == t.IDCloseParen || stop == t.IDCloseBracket {
p.src = p.src[1:] p.src = p.src[1:]
} }
return ret, nil return ret, nil
} else if (stop == t.IDOpenDoubleCurly) && (id == t.IDOpenCurly) {
return ret, nil
} }
elem, err := parseElem(p) elem, err := parseElem(p)
@@ -531,18 +533,34 @@ func (p *parser) parseBracket(sep t.ID) (op t.ID, ei *a.Expr, ej *a.Expr, err er
return sep, ei, ej, nil return sep, ei, ej, nil
} }
func (p *parser) parseBlock() ([]*a.Node, error) { func (p *parser) parseBlock(doubleCurly bool) ([]*a.Node, error) {
if x := p.peek1(); x != t.IDOpenCurly { if doubleCurly {
got := p.tm.ByID(x) if x := p.peek1(); x != t.IDOpenDoubleCurly {
return nil, fmt.Errorf(`parse: expected "{", got %q at %s:%d`, got, p.filename, p.line()) got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected "{{", got %q at %s:%d`, got, p.filename, p.line())
}
} else {
if x := p.peek1(); x != t.IDOpenCurly {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected "{", got %q at %s:%d`, got, p.filename, p.line())
}
} }
p.src = p.src[1:] p.src = p.src[1:]
block := []*a.Node(nil) block := []*a.Node(nil)
for len(p.src) > 0 { for {
if p.src[0].ID == t.IDCloseCurly { if len(p.src) == 0 {
p.src = p.src[1:] return nil, fmt.Errorf(`parse: expected "}" or "}}" at %s:%d`, p.filename, p.line())
return block, nil }
if doubleCurly {
if p.src[0].ID == t.IDCloseDoubleCurly {
break
}
} else {
if p.src[0].ID == t.IDCloseCurly {
break
}
} }
s, err := p.parseStatement() s, err := p.parseStatement()
@@ -557,7 +575,9 @@ func (p *parser) parseBlock() ([]*a.Node, error) {
} }
p.src = p.src[1:] p.src = p.src[1:]
} }
return nil, fmt.Errorf(`parse: expected "}" at %s:%d`, p.filename, p.line())
p.src = p.src[1:]
return block, nil
} }
func (p *parser) assertsSorted(asserts []*a.Node) error { func (p *parser) assertsSorted(asserts []*a.Node) error {
@@ -735,7 +755,12 @@ func (p *parser) parseStatement1() (*a.Node, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
body, err := p.parseBlock() doubleCurly := p.peek1() == t.IDOpenDoubleCurly
if doubleCurly && ((condition.Operator() != 0) || (condition.Ident() != t.IDTrue)) {
return nil, fmt.Errorf(`parse: double {{ }} while loop condition isn't "true" at %s:%d`,
p.filename, p.line())
}
body, err := p.parseBlock(doubleCurly)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -747,6 +772,10 @@ func (p *parser) parseStatement1() (*a.Node, error) {
return nil, fmt.Errorf(`parse: expected endwhile%s at %s:%d`, return nil, fmt.Errorf(`parse: expected endwhile%s at %s:%d`,
dotLabel, p.filename, p.line()) dotLabel, p.filename, p.line())
} }
if doubleCurly && !a.Terminates(body) {
return nil, fmt.Errorf(`parse: double {{ }} while loop doesn't terminate at %s:%d`,
p.filename, p.line())
}
return a.NewWhile(label, condition, asserts, body).AsNode(), nil return a.NewWhile(label, condition, asserts, body).AsNode(), nil
} }
return p.parseAssignNode() return p.parseAssignNode()
@@ -838,7 +867,7 @@ func (p *parser) parseAsserts() ([]*a.Node, error) {
if p.peek1() == t.IDComma { if p.peek1() == t.IDComma {
p.src = p.src[1:] p.src = p.src[1:]
var err error var err error
if asserts, err = p.parseList(t.IDOpenCurly, (*parser).parseAssertNode); err != nil { if asserts, err = p.parseList(t.IDOpenDoubleCurly, (*parser).parseAssertNode); err != nil {
return nil, err return nil, err
} }
if err := p.assertsSorted(asserts); err != nil { if err := p.assertsSorted(asserts); err != nil {
@@ -916,7 +945,7 @@ func (p *parser) parseIOBindNode() (*a.Node, error) {
} }
p.src = p.src[1:] p.src = p.src[1:]
body, err := p.parseBlock() body, err := p.parseBlock(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -938,7 +967,7 @@ func (p *parser) parseIf() (*a.If, error) {
return nil, fmt.Errorf(`parse: if-condition %q is not effect-free at %s:%d`, return nil, fmt.Errorf(`parse: if-condition %q is not effect-free at %s:%d`,
condition.Str(p.tm), p.filename, p.line()) condition.Str(p.tm), p.filename, p.line())
} }
bodyIfTrue, err := p.parseBlock() bodyIfTrue, err := p.parseBlock(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -951,7 +980,7 @@ func (p *parser) parseIf() (*a.If, error) {
return nil, err return nil, err
} }
} else { } else {
bodyIfFalse, err = p.parseBlock() bodyIfFalse, err = p.parseBlock(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -1042,7 +1071,7 @@ func (p *parser) parseIterateBlock(label t.ID, assigns []*a.Node) (*a.Iterate, e
if err != nil { if err != nil {
return nil, err return nil, err
} }
body, err := p.parseBlock() body, err := p.parseBlock(false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+7 -3
View File
@@ -110,9 +110,11 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error
// line containing the matching open token. // line containing the matching open token.
buf = buf[:0] buf = buf[:0]
indentAdjustment := 0 indentAdjustment := 0
if lineTokens[0].ID.IsClose() { if id := lineTokens[0].ID; id == t.IDCloseDoubleCurly {
// No-op.
} else if id.IsClose() {
indentAdjustment-- indentAdjustment--
} else if hanging && lineTokens[0].ID != t.IDOpenCurly { } else if hanging && ((id != t.IDOpenCurly) && (id != t.IDOpenDoubleCurly)) {
indentAdjustment++ indentAdjustment++
} }
buf = appendTabs(buf, indent+indentAdjustment) buf = appendTabs(buf, indent+indentAdjustment)
@@ -189,7 +191,9 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error
} }
commentLine = line + 1 commentLine = line + 1
prevLine = line prevLine = line
prevLineHanging = prevLineHanging && lineTokens[len(lineTokens)-1].ID != t.IDOpenCurly lastID := lineTokens[len(lineTokens)-1].ID
prevLineHanging = prevLineHanging &&
(lastID != t.IDOpenCurly) && (lastID != t.IDOpenDoubleCurly)
} }
// Print any trailing comments. // Print any trailing comments.
+24 -14
View File
@@ -242,16 +242,18 @@ const (
minOpen = 0x10 minOpen = 0x10
maxOpen = 0x17 maxOpen = 0x17
IDOpenParen = ID(0x10) IDOpenParen = ID(0x10)
IDOpenBracket = ID(0x11) IDOpenBracket = ID(0x11)
IDOpenCurly = ID(0x12) IDOpenCurly = ID(0x12)
IDOpenDoubleCurly = ID(0x13)
minClose = 0x18 minClose = 0x18
maxClose = 0x1F maxClose = 0x1F
IDCloseParen = ID(0x18) IDCloseParen = ID(0x18)
IDCloseBracket = ID(0x19) IDCloseBracket = ID(0x19)
IDCloseCurly = ID(0x1A) IDCloseCurly = ID(0x1A)
IDCloseDoubleCurly = ID(0x1B)
) )
const ( const (
@@ -670,13 +672,15 @@ var builtInsByID = [nBuiltInIDs]string{
IDQuestion: "?", IDQuestion: "?",
IDColon: ":", IDColon: ":",
IDOpenParen: "(", IDOpenParen: "(",
IDCloseParen: ")", IDOpenBracket: "[",
IDOpenBracket: "[", IDOpenCurly: "{",
IDOpenDoubleCurly: "{{",
IDCloseBracket: "]", IDCloseParen: ")",
IDOpenCurly: "{", IDCloseBracket: "]",
IDCloseCurly: "}", IDCloseCurly: "}",
IDCloseDoubleCurly: "}}",
IDPlusEq: "+=", IDPlusEq: "+=",
IDMinusEq: "-=", IDMinusEq: "-=",
@@ -1029,8 +1033,6 @@ var squiggles = [256]ID{
')': IDCloseParen, ')': IDCloseParen,
'[': IDOpenBracket, '[': IDOpenBracket,
']': IDCloseBracket, ']': IDCloseBracket,
'{': IDOpenCurly,
'}': IDCloseCurly,
',': IDComma, ',': IDComma,
'!': IDExclam, '!': IDExclam,
@@ -1105,6 +1107,14 @@ var lexers = [256][]suffixLexer{
{"=", IDGreaterEq}, {"=", IDGreaterEq},
{"", IDGreaterThan}, {"", IDGreaterThan},
}, },
'{': {
{"{", IDOpenDoubleCurly},
{"", IDOpenCurly},
},
'}': {
{"}", IDCloseDoubleCurly},
{"", IDCloseCurly},
},
'~': { '~': {
{"mod<<=", IDTildeModShiftLEq}, {"mod<<=", IDTildeModShiftLEq},
{"mod<<", IDTildeModShiftL}, {"mod<<", IDTildeModShiftL},
+93 -92
View File
@@ -544,105 +544,106 @@ pri func config_decoder.decode_ae?(src: base.io_reader) {
var is_iccp : base.bool var is_iccp : base.bool
var is_xmp : base.bool var is_xmp : base.bool
while.goto_done true { while.goto_done true {{
block_size = args.src.read_u8?() block_size = args.src.read_u8?()
if block_size == 0 { if block_size == 0 {
return ok return ok
} }
// Look only for an 11 byte "ANIMEXTS1.0", "NETSCAPE2.0" or other // Look only for an 11 byte "ANIMEXTS1.0", "NETSCAPE2.0" or other
// extension, as per: // extension, as per:
// - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension // - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
// - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension // - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
// //
// Other extensions include XMP metadata. // Other extensions include XMP metadata.
if block_size <> 11 { if block_size <> 11 {
args.src.skip32?(n: block_size as base.u32)
break.goto_done
}
is_animexts = true
is_netscape = true
is_iccp = true
is_xmp = true
block_size = 0 // Re-purpose the block_size variable as a counter.
while block_size < 11 {
c = args.src.read_u8?()
is_animexts = is_animexts and (c == animexts1dot0[block_size])
is_netscape = is_netscape and (c == netscape2dot0[block_size])
is_iccp = is_iccp and (c == iccrgbg1012[block_size])
is_xmp = is_xmp and (c == xmpdataxmp[block_size])
block_size += 1
} endwhile
if is_animexts or is_netscape {
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop
// count.
block_size = args.src.read_u8?()
if block_size <> 3 {
args.src.skip32?(n: block_size as base.u32) args.src.skip32?(n: block_size as base.u32)
break.goto_done break.goto_done
} }
is_animexts = true c = args.src.read_u8?()
is_netscape = true if c <> 0x01 {
is_iccp = true args.src.skip32?(n: 2)
is_xmp = true break.goto_done
block_size = 0 // Re-purpose the block_size variable as a counter. }
while block_size < 11 { this.num_loops = args.src.read_u16le_as_u32?()
c = args.src.read_u8?() this.seen_num_loops = true
is_animexts = is_animexts and (c == animexts1dot0[block_size])
is_netscape = is_netscape and (c == netscape2dot0[block_size])
is_iccp = is_iccp and (c == iccrgbg1012[block_size])
is_xmp = is_xmp and (c == xmpdataxmp[block_size])
block_size += 1
} endwhile
if is_animexts or is_netscape { // A loop count of N, in the wire format, actually means "repeat N
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop // times after the first play", if N is positive. A zero N means to
// count. // loop forever. Playing the frames exactly once is denoted by the
block_size = args.src.read_u8?() // *absence* of this NETSCAPE2.0 application extension.
if block_size <> 3 { //
args.src.skip32?(n: block_size as base.u32) // For example, if there are four frames: A, B, C, D, and N is 2, then
break.goto_done // each frame is actually played N+1 or 3 times: ABCDABCDABCD.
} //
c = args.src.read_u8?() // Thus, we increment N if it is positive. The comparison against
if c <> 0x01 { // 0xFFFF will never fail, but is necessary for the overflow checker.
args.src.skip32?(n: 2) if (0 < this.num_loops) and (this.num_loops <= 0xFFFF) {
break.goto_done this.num_loops += 1
}
this.num_loops = args.src.read_u16le_as_u32?()
this.seen_num_loops = true
// A loop count of N, in the wire format, actually means "repeat N
// times after the first play", if N is positive. A zero N means to
// loop forever. Playing the frames exactly once is denoted by the
// *absence* of this NETSCAPE2.0 application extension.
//
// For example, if there are four frames: A, B, C, D, and N is 2, then
// each frame is actually played N+1 or 3 times: ABCDABCDABCD.
//
// Thus, we increment N if it is positive. The comparison against
// 0xFFFF will never fail, but is necessary for the overflow checker.
if (0 < this.num_loops) and (this.num_loops <= 0xFFFF) {
this.num_loops += 1
}
} else if this.ignore_metadata {
// No-op.
} else if is_iccp and this.report_metadata_iccp {
while args.src.available() <= 0,
post args.src.available() > 0,
{
yield? base."$short read"
} endwhile
this.metadata_chunk_length_value = args.src.peek_u8_as_u64()
args.src.skip32_fast!(actual: 1, worst_case: 1)
this.metadata_fourcc_value = 'ICCP'be
this.metadata_io_position = args.src.position() ~sat+ this.metadata_chunk_length_value
this.call_sequence = 1
return base."@metadata reported"
} else if is_xmp and this.report_metadata_xmp {
while args.src.available() <= 0,
post args.src.available() > 0,
{
yield? base."$short read"
} endwhile
this.metadata_chunk_length_value = args.src.peek_u8_as_u64()
if this.metadata_chunk_length_value > 0 {
// The +1 is because XMP metadata's encoding includes each
// block's leading byte (the block size) as part of the
// metadata passed to the caller.
this.metadata_chunk_length_value += 1
} else {
args.src.skip32_fast!(actual: 1, worst_case: 1)
}
this.metadata_fourcc_value = 'XMP 'be
this.metadata_io_position = args.src.position() ~sat+ this.metadata_chunk_length_value
this.call_sequence = 1
return base."@metadata reported"
} }
break.goto_done } else if this.ignore_metadata {
} endwhile.goto_done // No-op.
} else if is_iccp and this.report_metadata_iccp {
while args.src.available() <= 0,
post args.src.available() > 0,
{
yield? base."$short read"
} endwhile
this.metadata_chunk_length_value = args.src.peek_u8_as_u64()
args.src.skip32_fast!(actual: 1, worst_case: 1)
this.metadata_fourcc_value = 'ICCP'be
this.metadata_io_position = args.src.position() ~sat+ this.metadata_chunk_length_value
this.call_sequence = 1
return base."@metadata reported"
} else if is_xmp and this.report_metadata_xmp {
while args.src.available() <= 0,
post args.src.available() > 0,
{
yield? base."$short read"
} endwhile
this.metadata_chunk_length_value = args.src.peek_u8_as_u64()
if this.metadata_chunk_length_value > 0 {
// The +1 is because XMP metadata's encoding includes each
// block's leading byte (the block size) as part of the
// metadata passed to the caller.
this.metadata_chunk_length_value += 1
} else {
args.src.skip32_fast!(actual: 1, worst_case: 1)
}
this.metadata_fourcc_value = 'XMP 'be
this.metadata_io_position = args.src.position() ~sat+ this.metadata_chunk_length_value
this.call_sequence = 1
return base."@metadata reported"
}
break.goto_done
}} endwhile.goto_done
this.skip_blocks?(src: args.src) this.skip_blocks?(src: args.src)
} }
+93 -92
View File
@@ -619,105 +619,106 @@ pri func decoder.decode_ae?(src: base.io_reader) {
var is_iccp : base.bool var is_iccp : base.bool
var is_xmp : base.bool var is_xmp : base.bool
while.goto_done true { while.goto_done true {{
block_size = args.src.read_u8?() block_size = args.src.read_u8?()
if block_size == 0 { if block_size == 0 {
return ok return ok
} }
// Look only for an 11 byte "ANIMEXTS1.0", "NETSCAPE2.0" or other // Look only for an 11 byte "ANIMEXTS1.0", "NETSCAPE2.0" or other
// extension, as per: // extension, as per:
// - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension // - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
// - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension // - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
// //
// Other extensions include XMP metadata. // Other extensions include XMP metadata.
if block_size <> 11 { if block_size <> 11 {
args.src.skip32?(n: block_size as base.u32)
break.goto_done
}
is_animexts = true
is_netscape = true
is_iccp = true
is_xmp = true
block_size = 0 // Re-purpose the block_size variable as a counter.
while block_size < 11 {
c = args.src.read_u8?()
is_animexts = is_animexts and (c == animexts1dot0[block_size])
is_netscape = is_netscape and (c == netscape2dot0[block_size])
is_iccp = is_iccp and (c == iccrgbg1012[block_size])
is_xmp = is_xmp and (c == xmpdataxmp[block_size])
block_size += 1
} endwhile
if is_animexts or is_netscape {
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop
// count.
block_size = args.src.read_u8?()
if block_size <> 3 {
args.src.skip32?(n: block_size as base.u32) args.src.skip32?(n: block_size as base.u32)
break.goto_done break.goto_done
} }
is_animexts = true c = args.src.read_u8?()
is_netscape = true if c <> 0x01 {
is_iccp = true args.src.skip32?(n: 2)
is_xmp = true break.goto_done
block_size = 0 // Re-purpose the block_size variable as a counter. }
while block_size < 11 { this.num_loops = args.src.read_u16le_as_u32?()
c = args.src.read_u8?() this.seen_num_loops = true
is_animexts = is_animexts and (c == animexts1dot0[block_size])
is_netscape = is_netscape and (c == netscape2dot0[block_size])
is_iccp = is_iccp and (c == iccrgbg1012[block_size])
is_xmp = is_xmp and (c == xmpdataxmp[block_size])
block_size += 1
} endwhile
if is_animexts or is_netscape { // A loop count of N, in the wire format, actually means "repeat N
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop // times after the first play", if N is positive. A zero N means to
// count. // loop forever. Playing the frames exactly once is denoted by the
block_size = args.src.read_u8?() // *absence* of this NETSCAPE2.0 application extension.
if block_size <> 3 { //
args.src.skip32?(n: block_size as base.u32) // For example, if there are four frames: A, B, C, D, and N is 2, then
break.goto_done // each frame is actually played N+1 or 3 times: ABCDABCDABCD.
} //
c = args.src.read_u8?() // Thus, we increment N if it is positive. The comparison against
if c <> 0x01 { // 0xFFFF will never fail, but is necessary for the overflow checker.
args.src.skip32?(n: 2) if (0 < this.num_loops) and (this.num_loops <= 0xFFFF) {
break.goto_done this.num_loops += 1
}
this.num_loops = args.src.read_u16le_as_u32?()
this.seen_num_loops = true
// A loop count of N, in the wire format, actually means "repeat N
// times after the first play", if N is positive. A zero N means to
// loop forever. Playing the frames exactly once is denoted by the
// *absence* of this NETSCAPE2.0 application extension.
//
// For example, if there are four frames: A, B, C, D, and N is 2, then
// each frame is actually played N+1 or 3 times: ABCDABCDABCD.
//
// Thus, we increment N if it is positive. The comparison against
// 0xFFFF will never fail, but is necessary for the overflow checker.
if (0 < this.num_loops) and (this.num_loops <= 0xFFFF) {
this.num_loops += 1
}
} else if this.ignore_metadata {
// No-op.
} else if is_iccp and this.report_metadata_iccp {
while args.src.available() <= 0,
post args.src.available() > 0,
{
yield? base."$short read"
} endwhile
this.metadata_chunk_length_value = args.src.peek_u8_as_u64()
args.src.skip32_fast!(actual: 1, worst_case: 1)
this.metadata_fourcc_value = 'ICCP'be
this.metadata_io_position = args.src.position() ~sat+ this.metadata_chunk_length_value
this.call_sequence = 1
return base."@metadata reported"
} else if is_xmp and this.report_metadata_xmp {
while args.src.available() <= 0,
post args.src.available() > 0,
{
yield? base."$short read"
} endwhile
this.metadata_chunk_length_value = args.src.peek_u8_as_u64()
if this.metadata_chunk_length_value > 0 {
// The +1 is because XMP metadata's encoding includes each
// block's leading byte (the block size) as part of the
// metadata passed to the caller.
this.metadata_chunk_length_value += 1
} else {
args.src.skip32_fast!(actual: 1, worst_case: 1)
}
this.metadata_fourcc_value = 'XMP 'be
this.metadata_io_position = args.src.position() ~sat+ this.metadata_chunk_length_value
this.call_sequence = 1
return base."@metadata reported"
} }
break.goto_done } else if this.ignore_metadata {
} endwhile.goto_done // No-op.
} else if is_iccp and this.report_metadata_iccp {
while args.src.available() <= 0,
post args.src.available() > 0,
{
yield? base."$short read"
} endwhile
this.metadata_chunk_length_value = args.src.peek_u8_as_u64()
args.src.skip32_fast!(actual: 1, worst_case: 1)
this.metadata_fourcc_value = 'ICCP'be
this.metadata_io_position = args.src.position() ~sat+ this.metadata_chunk_length_value
this.call_sequence = 1
return base."@metadata reported"
} else if is_xmp and this.report_metadata_xmp {
while args.src.available() <= 0,
post args.src.available() > 0,
{
yield? base."$short read"
} endwhile
this.metadata_chunk_length_value = args.src.peek_u8_as_u64()
if this.metadata_chunk_length_value > 0 {
// The +1 is because XMP metadata's encoding includes each
// block's leading byte (the block size) as part of the
// metadata passed to the caller.
this.metadata_chunk_length_value += 1
} else {
args.src.skip32_fast!(actual: 1, worst_case: 1)
}
this.metadata_fourcc_value = 'XMP 'be
this.metadata_io_position = args.src.position() ~sat+ this.metadata_chunk_length_value
this.call_sequence = 1
return base."@metadata reported"
}
break.goto_done
}} endwhile.goto_done
this.skip_blocks?(src: args.src) this.skip_blocks?(src: args.src)
} }
+975 -974
View File
@@ -157,617 +157,173 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader, wor
expect = 0x1EB2 // EXPECT_VALUE expect = 0x1EB2 // EXPECT_VALUE
while.outer true { while.outer true {
while.goto_parsed_a_leaf_value true { while.goto_parsed_a_leaf_value true {{
if args.dst.available() <= 0 { if args.dst.available() <= 0 {
yield? base."$short write" yield? base."$short write"
continue.outer continue.outer
} }
// Consume whitespace. // Consume whitespace.
whitespace_length = 0 whitespace_length = 0
c = 0 c = 0
class = 0 class = 0
while.ws true, while.ws true,
inv args.dst.available() > 0, inv args.dst.available() > 0,
post args.src.available() > 0, post args.src.available() > 0,
{ {
if args.src.available() <= 0 { if args.src.available() <= 0 {
if whitespace_length > 0 { if whitespace_length > 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0,
link: 0x0,
length: whitespace_length)
whitespace_length = 0
}
if args.src.is_closed() {
return "#bad input"
}
yield? base."$short read"
whitespace_length = 0
continue.outer
}
c = args.src.peek_u8()
class = lut_classes[c]
if class <> 0x00 { // 0x00 is CLASS_WHITESPACE.
break.ws
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
if whitespace_length >= 0xFFFE {
args.dst.write_simple_token_fast!( args.dst.write_simple_token_fast!(
value_major: 0, value_major: 0,
value_minor: 0, value_minor: 0,
link: 0x0, link: 0x0,
length: 0xFFFF) length: whitespace_length)
whitespace_length = 0 whitespace_length = 0
continue.outer
} }
whitespace_length += 1 if args.src.is_closed() {
} endwhile.ws return "#bad input"
}
yield? base."$short read"
whitespace_length = 0
continue.outer
}
// Emit whitespace. c = args.src.peek_u8()
if whitespace_length > 0 { class = lut_classes[c]
if class <> 0x00 { // 0x00 is CLASS_WHITESPACE.
break.ws
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
if whitespace_length >= 0xFFFE {
args.dst.write_simple_token_fast!( args.dst.write_simple_token_fast!(
value_major: 0, value_major: 0,
value_minor: 0, value_minor: 0,
link: 0x0, link: 0x0,
length: whitespace_length) length: 0xFFFF)
whitespace_length = 0 whitespace_length = 0
continue.outer
}
whitespace_length += 1
} endwhile.ws
// Emit whitespace.
if whitespace_length > 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0,
link: 0x0,
length: whitespace_length)
whitespace_length = 0
if args.dst.available() <= 0 {
continue.outer
}
}
// Check expected character classes.
if 0 == (expect & ((1 as base.u32) << class)) {
return "#bad input"
}
// These assertions are redundant (the Wuffs compiler should already
// know these facts; deleting these assertions should still compile)
// but are listed explicitly to guard against future edits to the code
// above inadvertently invalidating these assertions.
assert args.dst.available() > 0
assert args.src.available() > 0
if class == 0x01 { // 0x01 is CLASS_STRING.
// -------- BEGIN parse strings.
// Emit the leading '"'.
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0013,
link: 0x1,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
while.string_loop_outer true {
if args.dst.available() <= 0 { if args.dst.available() <= 0 {
continue.outer yield? base."$short write"
continue.string_loop_outer
} }
}
// Check expected character classes. string_length = 0
if 0 == (expect & ((1 as base.u32) << class)) { while.string_loop_inner true,
return "#bad input" pre args.dst.available() > 0,
} {
if args.src.available() <= 0 {
// These assertions are redundant (the Wuffs compiler should if string_length > 0 {
// already know these facts; deleting these assertions should still args.dst.write_simple_token_fast!(
// compile) but are listed explicitly to guard against future edits value_major: 0,
// to the code above inadvertently invalidating these assertions. value_minor: 0x40_0021,
assert args.dst.available() > 0 link: 0x3,
assert args.src.available() > 0 length: string_length)
string_length = 0
if class == 0x01 { // 0x01 is CLASS_STRING. }
// -------- BEGIN parse strings. if args.src.is_closed() {
// Emit the leading '"'. return "#bad input"
args.dst.write_simple_token_fast!( }
value_major: 0, yield? base."$short read"
value_minor: 0x40_0013, string_length = 0
link: 0x1,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
while.string_loop_outer true {
if args.dst.available() <= 0 {
yield? base."$short write"
continue.string_loop_outer continue.string_loop_outer
} }
string_length = 0 // As an optimization, consume non-special ASCII 4 bytes at
while.string_loop_inner true, // a time.
pre args.dst.available() > 0, while args.src.available() > 4,
inv args.dst.available() > 0,
inv args.src.available() > 0,
{ {
if args.src.available() <= 0 { c4 = args.src.peek_u32le()
if string_length > 0 { if 0x00 <> (lut_chars[0xFF & (c4 >> 0)] |
args.dst.write_simple_token_fast!( lut_chars[0xFF & (c4 >> 8)] |
value_major: 0, lut_chars[0xFF & (c4 >> 16)] |
value_minor: 0x40_0021, lut_chars[0xFF & (c4 >> 24)]) {
link: 0x3, break
length: string_length) }
string_length = 0 args.src.skip32_fast!(actual: 4, worst_case: 4)
} if string_length > (0xFFFB - 4) {
if args.src.is_closed() { args.dst.write_simple_token_fast!(
return "#bad input" value_major: 0,
} value_minor: 0x40_0021,
yield? base."$short read" link: 0x3,
length: string_length + 4)
string_length = 0 string_length = 0
continue.string_loop_outer continue.string_loop_outer
} }
string_length += 4
} endwhile
// As an optimization, consume non-special ASCII 4 bytes at a time. c = args.src.peek_u8()
while args.src.available() > 4, char = lut_chars[c]
inv args.dst.available() > 0,
inv args.src.available() > 0,
{
c4 = args.src.peek_u32le()
if 0x00 <> (lut_chars[0xFF & (c4 >> 0)] |
lut_chars[0xFF & (c4 >> 8)] |
lut_chars[0xFF & (c4 >> 16)] |
lut_chars[0xFF & (c4 >> 24)]) {
break
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
if string_length > (0xFFFB - 4) {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length + 4)
string_length = 0
continue.string_loop_outer
}
string_length += 4
} endwhile
c = args.src.peek_u8() if char == 0x00 { // Non-special ASCII.
char = lut_chars[c] args.src.skip32_fast!(actual: 1, worst_case: 1)
if string_length >= 0xFFFB {
if char == 0x00 { // Non-special ASCII. args.dst.write_simple_token_fast!(
args.src.skip32_fast!(actual: 1, worst_case: 1) value_major: 0,
if string_length >= 0xFFFB { value_minor: 0x40_0021,
args.dst.write_simple_token_fast!( link: 0x3,
value_major: 0, length: 0xFFFC)
value_minor: 0x40_0021, string_length = 0
link: 0x3, continue.string_loop_outer
length: 0xFFFC)
string_length = 0
continue.string_loop_outer
}
string_length += 1
continue.string_loop_inner
} else if char == 0x01 { // '"'
if string_length <> 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length)
string_length = 0
}
break.string_loop_outer
} else if char == 0x02 { // '\\'.
if string_length > 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length)
string_length = 0
if args.dst.available() <= 0 {
continue.string_loop_outer
}
}
assert args.dst.available() > 0
if args.src.available() < 2 {
if args.src.is_closed() {
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
c = (args.src.peek_u16le() >> 8) as base.u8
backslash = lut_backslashes[c]
if (backslash & 0x80) <> 0 {
args.src.skip32_fast!(actual: 2, worst_case: 2)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | ((backslash & 0x7F) as base.u32),
link: 0x3,
length: 2)
continue.string_loop_outer
} else if backslash <> 0 {
if this.quirk_enabled_allow_backslash_etc[backslash & 7] {
args.src.skip32_fast!(actual: 2, worst_case: 2)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | (lut_quirky_backslashes[backslash & 7] as base.u32),
link: 0x3,
length: 2)
continue.string_loop_outer
}
} else if c == 'u' {
// -------- BEGIN backslash-u.
if args.src.available() < 6 {
if args.src.is_closed() {
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
uni4_string = args.src.peek_u48le_as_u64() >> 16
uni4_value = 0
uni4_ok = 0x80
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 0)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 12
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 8)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 8
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 16)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 4
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 24)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 0
if uni4_ok == 0 {
// It wasn't 4 hexadecimal digits. No-op
// (and fall through to "#bad
// backslash-escape").
} else if (uni4_value < 0xD800) or (0xDFFF < uni4_value) {
// Not a Unicode surrogate. We're good.
args.src.skip32_fast!(actual: 6, worst_case: 6)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | uni4_value,
link: 0x3,
length: 6)
continue.string_loop_outer
} else if uni4_value >= 0xDC00 {
// Low surrogate. No-op (and fall through
// to "#bad backslash-escape").
} else {
// High surrogate, which needs to be
// followed by a "\\u1234" low surrogate.
// We've already peeked 6 bytes for the
// high surrogate. We need 12 in total:
// another 8 bytes at an offset of 4.
if args.src.available() < 12 {
if args.src.is_closed() {
if this.quirk_enabled_replace_invalid_unicode {
args.src.skip32_fast!(actual: 6, worst_case: 6)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 6)
continue.string_loop_outer
}
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
uni4_value = 0
char = 0
continue.string_loop_outer
}
uni4_string = args.src.peek_u64le_at(offset: 4) >> 16
// Look for the low surrogate's "\\u".
if ((0xFF & (uni4_string >> 0)) <> '\\') or
((0xFF & (uni4_string >> 8)) <> 'u') {
uni4_high_surrogate = 0
uni4_value = 0
uni4_ok = 0
} else {
uni4_high_surrogate =
0x1_0000 + ((uni4_value - 0xD800) << 10)
uni4_value = 0
uni4_ok = 0x80
uni4_string >>= 16
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 0)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 12
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 8)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 8
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 16)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 4
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 24)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 0
}
if (uni4_ok <> 0) and
(0xDC00 <= uni4_value) and (uni4_value <= 0xDFFF) {
// Emit a single token for the surrogate pair.
uni4_value -= 0xDC00
args.src.skip32_fast!(actual: 12, worst_case: 12)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | uni4_high_surrogate | uni4_value,
link: 0x3,
length: 12)
continue.string_loop_outer
}
}
if this.quirk_enabled_replace_invalid_unicode {
if args.src.available() < 6 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 6, worst_case: 6)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 6)
continue.string_loop_outer
}
// -------- END backslash-u.
} else if (c == 'U') and
this.quirk_enabled_allow_backslash_capital_u {
// -------- BEGIN backslash-capital-u.
if args.src.available() < 10 {
if args.src.is_closed() {
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
uni8_string = args.src.peek_u64le_at(offset: 2)
uni8_value = 0
uni8_ok = 0x80
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 0)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 28
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 8)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 24
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 16)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 20
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 24)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 16
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 32)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 12
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 40)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 8
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 48)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 4
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 56)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 0
if uni8_ok == 0 {
// It wasn't 8 hexadecimal digits. No-op
// (and fall through to "#bad
// backslash-escape").
} else if (uni8_value < 0xD800) or (
(0xDFFF < uni8_value) and (uni8_value <= 0x10_FFFF)) {
// Not a Unicode surrogate. We're good.
args.src.skip32_fast!(actual: 10, worst_case: 10)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | (uni8_value & 0x1F_FFFF),
link: 0x3,
length: 10)
continue.string_loop_outer
} else if this.quirk_enabled_replace_invalid_unicode {
args.src.skip32_fast!(actual: 10, worst_case: 10)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 10)
continue.string_loop_outer
}
// -------- END backslash-capital-u.
} else if (c == 'x') and
this.quirk_enabled_allow_backslash_x {
// -------- BEGIN backslash-x
if args.src.available() < 4 {
if args.src.is_closed() {
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
backslash_x_length = 0
while (backslash_x_length <= 0xFFFB) and (args.src.available() >= 4),
inv args.dst.available() > 0,
{
backslash_x_string = args.src.peek_u32le()
backslash_x_ok = 0x80
c = lut_hexadecimal_digits[0xFF & (backslash_x_string >> 16)]
backslash_x_ok &= c
c = lut_hexadecimal_digits[0xFF & (backslash_x_string >> 24)]
backslash_x_ok &= c
if (backslash_x_ok == 0) or
((backslash_x_string & 0xFFFF) <> 0x785C) {
// It wasn't "\\x34", for some
// hexadecimal digits "34".
break
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
backslash_x_length += 4
} endwhile
if backslash_x_length == 0 {
return "#bad backslash-escape"
}
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0080,
link: 0x3,
length: backslash_x_length)
continue.string_loop_outer
// -------- END backslash-x
}
return "#bad backslash-escape"
} else if char == 0x03 { // 2-byte UTF-8.
if args.src.available() < 2 {
if string_length > 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length)
string_length = 0
if args.dst.available() <= 0 {
continue.string_loop_outer
}
}
if args.src.is_closed() {
if this.quirk_enabled_replace_invalid_unicode {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
continue.string_loop_outer
}
return "#bad UTF-8"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
multi_byte_utf8 = args.src.peek_u16le_as_u32()
if (multi_byte_utf8 & 0xC000) == 0x8000 {
multi_byte_utf8 = (0x00_07C0 & (multi_byte_utf8 ~mod<< 6)) |
(0x00_003F & (multi_byte_utf8 >> 8))
args.src.skip32_fast!(actual: 2, worst_case: 2)
if string_length >= 0xFFF8 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length + 2)
string_length = 0
continue.string_loop_outer
}
string_length += 2
continue.string_loop_inner
}
} else if char == 0x04 { // 3-byte UTF-8.
if args.src.available() < 3 {
if string_length > 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length)
string_length = 0
if args.dst.available() <= 0 {
continue.string_loop_outer
}
}
if args.src.is_closed() {
if this.quirk_enabled_replace_invalid_unicode {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
continue.string_loop_outer
}
return "#bad UTF-8"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
multi_byte_utf8 = args.src.peek_u24le_as_u32()
if (multi_byte_utf8 & 0xC0_C000) == 0x80_8000 {
multi_byte_utf8 = (0x00_F000 & (multi_byte_utf8 ~mod<< 12)) |
(0x00_0FC0 & (multi_byte_utf8 >> 2)) |
(0x00_003F & (multi_byte_utf8 >> 16))
if (0x07FF < multi_byte_utf8) and
((multi_byte_utf8 < 0xD800) or (0xDFFF < multi_byte_utf8)) {
args.src.skip32_fast!(actual: 3, worst_case: 3)
if string_length >= 0xFFF8 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length + 3)
string_length = 0
continue.string_loop_outer
}
string_length += 3
continue.string_loop_inner
}
}
} else if char == 0x05 { // 4-byte UTF-8.
if args.src.available() < 4 {
if string_length > 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length)
string_length = 0
if args.dst.available() <= 0 {
continue.string_loop_outer
}
}
if args.src.is_closed() {
if this.quirk_enabled_replace_invalid_unicode {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
continue.string_loop_outer
}
return "#bad UTF-8"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
multi_byte_utf8 = args.src.peek_u32le()
if (multi_byte_utf8 & 0xC0C0_C000) == 0x8080_8000 {
multi_byte_utf8 = (0x1C_0000 & (multi_byte_utf8 ~mod<< 18)) |
(0x03_F000 & (multi_byte_utf8 ~mod<< 4)) |
(0x00_0FC0 & (multi_byte_utf8 >> 10)) |
(0x00_003F & (multi_byte_utf8 >> 24))
if (0xFFFF < multi_byte_utf8) and (multi_byte_utf8 <= 0x10_FFFF) {
args.src.skip32_fast!(actual: 4, worst_case: 4)
if string_length >= 0xFFF8 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length + 4)
string_length = 0
continue.string_loop_outer
}
string_length += 4
continue.string_loop_inner
}
}
} }
string_length += 1
continue.string_loop_inner
} else if char == 0x01 { // '"'
if string_length <> 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length)
string_length = 0
}
break.string_loop_outer
} else if char == 0x02 { // '\\'.
if string_length > 0 { if string_length > 0 {
args.dst.write_simple_token_fast!( args.dst.write_simple_token_fast!(
value_major: 0, value_major: 0,
@@ -779,330 +335,774 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader, wor
continue.string_loop_outer continue.string_loop_outer
} }
} }
if char == 0x80 { assert args.dst.available() > 0
return "#bad C0 control code"
} if args.src.available() < 2 {
if this.quirk_enabled_replace_invalid_unicode { if args.src.is_closed() {
args.dst.write_simple_token_fast!( return "#bad backslash-escape"
value_major: 0, }
value_minor: 0x60_FFFD, yield? base."$short read"
link: 0x3, string_length = 0
length: 1) char = 0
args.src.skip32_fast!(actual: 1, worst_case: 1)
continue.string_loop_outer continue.string_loop_outer
} }
return "#bad UTF-8" c = (args.src.peek_u16le() >> 8) as base.u8
} endwhile.string_loop_inner backslash = lut_backslashes[c]
} endwhile.string_loop_outer if (backslash & 0x80) <> 0 {
args.src.skip32_fast!(actual: 2, worst_case: 2)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | ((backslash & 0x7F) as base.u32),
link: 0x3,
length: 2)
continue.string_loop_outer
// Emit the trailing '"'. } else if backslash <> 0 {
while true { if this.quirk_enabled_allow_backslash_etc[backslash & 7] {
if args.src.available() <= 0 { args.src.skip32_fast!(actual: 2, worst_case: 2)
if args.src.is_closed() { args.dst.write_simple_token_fast!(
return "#bad input" value_major: 0,
value_minor: 0x60_0000 | (lut_quirky_backslashes[backslash & 7] as base.u32),
link: 0x3,
length: 2)
continue.string_loop_outer
}
} else if c == 'u' {
// -------- BEGIN backslash-u.
if args.src.available() < 6 {
if args.src.is_closed() {
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
uni4_string = args.src.peek_u48le_as_u64() >> 16
uni4_value = 0
uni4_ok = 0x80
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 0)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 12
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 8)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 8
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 16)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 4
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 24)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 0
if uni4_ok == 0 {
// It wasn't 4 hexadecimal digits. No-op (and
// fall through to "#bad backslash-escape").
} else if (uni4_value < 0xD800) or (0xDFFF < uni4_value) {
// Not a Unicode surrogate. We're good.
args.src.skip32_fast!(actual: 6, worst_case: 6)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | uni4_value,
link: 0x3,
length: 6)
continue.string_loop_outer
} else if uni4_value >= 0xDC00 {
// Low surrogate. No-op (and fall through to
// "#bad backslash-escape").
} else {
// High surrogate, which needs to be followed
// by a "\\u1234" low surrogate. We've already
// peeked 6 bytes for the high surrogate. We
// need 12 in total: another 8 bytes at an
// offset of 4.
if args.src.available() < 12 {
if args.src.is_closed() {
if this.quirk_enabled_replace_invalid_unicode {
args.src.skip32_fast!(actual: 6, worst_case: 6)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 6)
continue.string_loop_outer
}
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
uni4_value = 0
char = 0
continue.string_loop_outer
}
uni4_string = args.src.peek_u64le_at(offset: 4) >> 16
// Look for the low surrogate's "\\u".
if ((0xFF & (uni4_string >> 0)) <> '\\') or
((0xFF & (uni4_string >> 8)) <> 'u') {
uni4_high_surrogate = 0
uni4_value = 0
uni4_ok = 0
} else {
uni4_high_surrogate =
0x1_0000 + ((uni4_value - 0xD800) << 10)
uni4_value = 0
uni4_ok = 0x80
uni4_string >>= 16
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 0)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 12
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 8)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 8
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 16)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 4
c = lut_hexadecimal_digits[0xFF & (uni4_string >> 24)]
uni4_ok &= c
uni4_value |= ((c & 0x0F) as base.u32) << 0
}
if (uni4_ok <> 0) and
(0xDC00 <= uni4_value) and (uni4_value <= 0xDFFF) {
// Emit a single token for the surrogate
// pair.
uni4_value -= 0xDC00
args.src.skip32_fast!(actual: 12, worst_case: 12)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | uni4_high_surrogate | uni4_value,
link: 0x3,
length: 12)
continue.string_loop_outer
}
}
if this.quirk_enabled_replace_invalid_unicode {
if args.src.available() < 6 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 6, worst_case: 6)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 6)
continue.string_loop_outer
}
// -------- END backslash-u.
} else if (c == 'U') and
this.quirk_enabled_allow_backslash_capital_u {
// -------- BEGIN backslash-capital-u.
if args.src.available() < 10 {
if args.src.is_closed() {
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
uni8_string = args.src.peek_u64le_at(offset: 2)
uni8_value = 0
uni8_ok = 0x80
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 0)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 28
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 8)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 24
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 16)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 20
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 24)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 16
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 32)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 12
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 40)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 8
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 48)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 4
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 56)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 0
if uni8_ok == 0 {
// It wasn't 8 hexadecimal digits. No-op (and
// fall through to "#bad backslash-escape").
} else if (uni8_value < 0xD800) or (
(0xDFFF < uni8_value) and (uni8_value <= 0x10_FFFF)) {
// Not a Unicode surrogate. We're good.
args.src.skip32_fast!(actual: 10, worst_case: 10)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_0000 | (uni8_value & 0x1F_FFFF),
link: 0x3,
length: 10)
continue.string_loop_outer
} else if this.quirk_enabled_replace_invalid_unicode {
args.src.skip32_fast!(actual: 10, worst_case: 10)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 10)
continue.string_loop_outer
}
// -------- END backslash-capital-u.
} else if (c == 'x') and
this.quirk_enabled_allow_backslash_x {
// -------- BEGIN backslash-x
if args.src.available() < 4 {
if args.src.is_closed() {
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
backslash_x_length = 0
while (backslash_x_length <= 0xFFFB) and (args.src.available() >= 4),
inv args.dst.available() > 0,
{
backslash_x_string = args.src.peek_u32le()
backslash_x_ok = 0x80
c = lut_hexadecimal_digits[0xFF & (backslash_x_string >> 16)]
backslash_x_ok &= c
c = lut_hexadecimal_digits[0xFF & (backslash_x_string >> 24)]
backslash_x_ok &= c
if (backslash_x_ok == 0) or
((backslash_x_string & 0xFFFF) <> 0x785C) {
// It wasn't "\\x34", for some hexadecimal
// digits "34".
break
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
backslash_x_length += 4
} endwhile
if backslash_x_length == 0 {
return "#bad backslash-escape"
}
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0080,
link: 0x3,
length: backslash_x_length)
continue.string_loop_outer
// -------- END backslash-x
} }
yield? base."$short read"
continue
}
if args.dst.available() <= 0 {
yield? base."$short write"
continue
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0013,
link: 0x2,
length: 1)
break
} endwhile
// As above, expect must have contained EXPECT_STRING. If it return "#bad backslash-escape"
// didn't also contain EXPECT_NUMBER then we were parsing an
// object key and the next token should be ':'.
if 0 == (expect & 0x0010) { // 0x0010 is (1 << CLASS_NUMBER).
expect = 0x1008 // 0x1008 is EXPECT_COLON.
continue.outer
}
break.goto_parsed_a_leaf_value
// -------- END parse strings.
} else if class == 0x02 { // 0x02 is CLASS_COMMA. } else if char == 0x03 { // 2-byte UTF-8.
args.src.skip32_fast!(actual: 1, worst_case: 1) if args.src.available() < 2 {
// The ',' is filler. if string_length > 0 {
args.dst.write_simple_token_fast!( args.dst.write_simple_token_fast!(
value_major: 0, value_major: 0,
value_minor: 0, value_minor: 0x40_0021,
link: 0x0, link: 0x3,
length: 1) length: string_length)
// What's valid after a comma depends on whether or not we're string_length = 0
// in an array or an object. if args.dst.available() <= 0 {
if 0 == (expect & 0x0100) { // 0x0100 is (1 << CLASS_CLOSE_SQUARE_BRACKET). continue.string_loop_outer
if this.quirk_enabled_allow_extra_comma { }
expect = 0x1042 // 0x1042 is EXPECT_STRING | EXPECT_CLOSE_CURLY_BRACE. }
} else { if args.src.is_closed() {
expect = 0x1002 // 0x1002 is EXPECT_STRING. if this.quirk_enabled_replace_invalid_unicode {
} args.dst.write_simple_token_fast!(
} else { value_major: 0,
if this.quirk_enabled_allow_extra_comma { value_minor: 0x60_FFFD,
expect = 0x1FB2 // 0x0FB2 is EXPECT_VALUE | EXPECT_CLOSE_SQUARE_BRACKET. link: 0x3,
} else { length: 1)
expect = 0x1EB2 // 0x0EB2 is EXPECT_VALUE. args.src.skip32_fast!(actual: 1, worst_case: 1)
} continue.string_loop_outer
} }
continue.outer return "#bad UTF-8"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
multi_byte_utf8 = args.src.peek_u16le_as_u32()
if (multi_byte_utf8 & 0xC000) == 0x8000 {
multi_byte_utf8 = (0x00_07C0 & (multi_byte_utf8 ~mod<< 6)) |
(0x00_003F & (multi_byte_utf8 >> 8))
args.src.skip32_fast!(actual: 2, worst_case: 2)
if string_length >= 0xFFF8 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length + 2)
string_length = 0
continue.string_loop_outer
}
string_length += 2
continue.string_loop_inner
}
} else if class == 0x03 { // 0x03 is CLASS_COLON. } else if char == 0x04 { // 3-byte UTF-8.
args.src.skip32_fast!(actual: 1, worst_case: 1) if args.src.available() < 3 {
// The ':' is filler. if string_length > 0 {
args.dst.write_simple_token_fast!( args.dst.write_simple_token_fast!(
value_major: 0, value_major: 0,
value_minor: 0, value_minor: 0x40_0021,
link: 0x0, link: 0x3,
length: 1) length: string_length)
expect = 0x1EB2 // 0x1EB2 is EXPECT_VALUE. string_length = 0
continue.outer if args.dst.available() <= 0 {
continue.string_loop_outer
}
}
if args.src.is_closed() {
if this.quirk_enabled_replace_invalid_unicode {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
continue.string_loop_outer
}
return "#bad UTF-8"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
multi_byte_utf8 = args.src.peek_u24le_as_u32()
if (multi_byte_utf8 & 0xC0_C000) == 0x80_8000 {
multi_byte_utf8 = (0x00_F000 & (multi_byte_utf8 ~mod<< 12)) |
(0x00_0FC0 & (multi_byte_utf8 >> 2)) |
(0x00_003F & (multi_byte_utf8 >> 16))
if (0x07FF < multi_byte_utf8) and
((multi_byte_utf8 < 0xD800) or (0xDFFF < multi_byte_utf8)) {
} else if class == 0x04 { // 0x04 is CLASS_NUMBER. args.src.skip32_fast!(actual: 3, worst_case: 3)
// -------- BEGIN parse numbers. if string_length >= 0xFFF8 {
while true, args.dst.write_simple_token_fast!(
pre args.dst.available() > 0, value_major: 0,
{ value_minor: 0x40_0021,
number_length = this.decode_number!(src: args.src) link: 0x3,
number_status = number_length >> 8 length: string_length + 3)
vminor = 0xA0_0403 string_length = 0
if (number_length & 0x80) <> 0 { continue.string_loop_outer
vminor = 0xA0_0401 }
string_length += 3
continue.string_loop_inner
}
}
} else if char == 0x05 { // 4-byte UTF-8.
if args.src.available() < 4 {
if string_length > 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length)
string_length = 0
if args.dst.available() <= 0 {
continue.string_loop_outer
}
}
if args.src.is_closed() {
if this.quirk_enabled_replace_invalid_unicode {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
continue.string_loop_outer
}
return "#bad UTF-8"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
multi_byte_utf8 = args.src.peek_u32le()
if (multi_byte_utf8 & 0xC0C0_C000) == 0x8080_8000 {
multi_byte_utf8 = (0x1C_0000 & (multi_byte_utf8 ~mod<< 18)) |
(0x03_F000 & (multi_byte_utf8 ~mod<< 4)) |
(0x00_0FC0 & (multi_byte_utf8 >> 10)) |
(0x00_003F & (multi_byte_utf8 >> 24))
if (0xFFFF < multi_byte_utf8) and (multi_byte_utf8 <= 0x10_FFFF) {
args.src.skip32_fast!(actual: 4, worst_case: 4)
if string_length >= 0xFFF8 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x40_0021,
link: 0x3,
length: string_length + 4)
string_length = 0
continue.string_loop_outer
}
string_length += 4
continue.string_loop_inner
}
}
} }
number_length = number_length & 0x7F
if number_status == 0 { if string_length > 0 {
args.dst.write_simple_token_fast!( args.dst.write_simple_token_fast!(
value_major: 0, value_major: 0,
value_minor: vminor, value_minor: 0x40_0021,
link: 0x0, link: 0x3,
length: number_length) length: string_length)
break string_length = 0
if args.dst.available() <= 0 {
continue.string_loop_outer
}
} }
if char == 0x80 {
return "#bad C0 control code"
}
if this.quirk_enabled_replace_invalid_unicode {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 1)
args.src.skip32_fast!(actual: 1, worst_case: 1)
continue.string_loop_outer
}
return "#bad UTF-8"
} endwhile.string_loop_inner
} endwhile.string_loop_outer
while number_length > 0 { // Emit the trailing '"'.
number_length -= 1 while true {
if args.src.can_undo_byte() { if args.src.available() <= 0 {
args.src.undo_byte!() if args.src.is_closed() {
} else {
return "#internal error: inconsistent I/O"
}
} endwhile
if number_status == 1 {
if this.quirk_enabled_allow_inf_nan_numbers {
this.decode_inf_nan?(dst: args.dst, src: args.src)
break
}
return "#bad input" return "#bad input"
} else if number_status == 2 {
return "#unsupported number length"
} else {
yield? base."$short read"
while args.dst.available() <= 0,
post args.dst.available() > 0,
{
yield? base."$short write"
} endwhile
} }
} endwhile yield? base."$short read"
break.goto_parsed_a_leaf_value continue
// -------- END parse numbers.
} else if class == 0x05 { // 0x05 is CLASS_OPEN_CURLY_BRACE.
vminor = 0x20_4011
if depth == 0 {
// No-op.
} else if 0 <> (expect_after_value & 0x0040) { // 0x0040 is (1 << CLASS_CLOSE_CURLY_BRACE).
vminor = 0x20_4041
} else {
vminor = 0x20_4021
} }
if depth >= 1024 { if args.dst.available() <= 0 {
return "#unsupported recursion depth" yield? base."$short write"
continue
} }
stack_byte = depth / 32
stack_bit = depth & 31
this.stack[stack_byte] |= (1 as base.u32) << stack_bit
depth += 1
args.src.skip32_fast!(actual: 1, worst_case: 1) args.src.skip32_fast!(actual: 1, worst_case: 1)
args.dst.write_simple_token_fast!( args.dst.write_simple_token_fast!(
value_major: 0, value_major: 0,
value_minor: vminor, value_minor: 0x40_0013,
link: 0x0, link: 0x2,
length: 1) length: 1)
expect = 0x1042 // 0x1042 is (EXPECT_CLOSE_CURLY_BRACE | EXPECT_STRING). break
expect_after_value = 0x1044 // 0x1044 is (EXPECT_CURLY_CLOSE_BRACE | EXPECT_COMMA). } endwhile
continue.outer
} else if class == 0x06 { // 0x06 is CLASS_CLOSE_CURLY_BRACE. // As above, expect must have contained EXPECT_STRING. If it didn't
args.src.skip32_fast!(actual: 1, worst_case: 1) // also contain EXPECT_NUMBER then we were parsing an object key
if depth <= 1 { // and the next token should be ':'.
args.dst.write_simple_token_fast!( if 0 == (expect & 0x0010) { // 0x0010 is (1 << CLASS_NUMBER).
value_major: 0, expect = 0x1008 // 0x1008 is EXPECT_COLON.
value_minor: 0x20_1042, continue.outer
link: 0x0, }
length: 1) break.goto_parsed_a_leaf_value
break.outer // -------- END parse strings.
}
depth -= 1 } else if class == 0x02 { // 0x02 is CLASS_COMMA.
stack_byte = (depth - 1) / 32 args.src.skip32_fast!(actual: 1, worst_case: 1)
stack_bit = (depth - 1) & 31 // The ',' is filler.
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) { args.dst.write_simple_token_fast!(
args.dst.write_simple_token_fast!( value_major: 0,
value_major: 0, value_minor: 0,
value_minor: 0x20_2042, link: 0x0,
link: 0x0, length: 1)
length: 1) // What's valid after a comma depends on whether or not we're in an
// 0x1104 is (EXPECT_SQUARE_CLOSE_BRACKET | EXPECT_COMMA). // array or an object.
expect = 0x1104 if 0 == (expect & 0x0100) { // 0x0100 is (1 << CLASS_CLOSE_SQUARE_BRACKET).
expect_after_value = 0x1104 if this.quirk_enabled_allow_extra_comma {
expect = 0x1042 // 0x1042 is EXPECT_STRING | EXPECT_CLOSE_CURLY_BRACE.
} else { } else {
args.dst.write_simple_token_fast!( expect = 0x1002 // 0x1002 is EXPECT_STRING.
value_major: 0,
value_minor: 0x20_4042,
link: 0x0,
length: 1)
// 0x1044 is (EXPECT_CURLY_CLOSE_BRACE | EXPECT_COMMA).
expect = 0x1044
expect_after_value = 0x1044
} }
continue.outer } else {
if this.quirk_enabled_allow_extra_comma {
} else if class == 0x07 { // 0x07 is CLASS_OPEN_SQUARE_BRACKET. expect = 0x1FB2 // 0x0FB2 is EXPECT_VALUE | EXPECT_CLOSE_SQUARE_BRACKET.
vminor = 0x20_2011
if depth == 0 {
// No-op.
} else if 0 <> (expect_after_value & 0x0040) { // 0x0040 is (1 << CLASS_CLOSE_CURLY_BRACE).
vminor = 0x20_2041
} else { } else {
vminor = 0x20_2021 expect = 0x1EB2 // 0x0EB2 is EXPECT_VALUE.
}
if depth >= 1024 {
return "#unsupported recursion depth"
}
stack_byte = depth / 32
stack_bit = depth & 31
this.stack[stack_byte] &= 0xFFFF_FFFF ^ ((1 as base.u32) << stack_bit)
depth += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: vminor,
link: 0x0,
length: 1)
expect = 0x1FB2 // 0x1FB2 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_VALUE).
expect_after_value = 0x1104 // 0x1104 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA).
continue.outer
} else if class == 0x08 { // 0x08 is CLASS_CLOSE_SQUARE_BRACKET.
args.src.skip32_fast!(actual: 1, worst_case: 1)
if depth <= 1 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_1022,
link: 0x0,
length: 1)
break.outer
}
depth -= 1
stack_byte = (depth - 1) / 32
stack_bit = (depth - 1) & 31
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_2022,
link: 0x0,
length: 1)
// 0x1104 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA).
expect = 0x1104
expect_after_value = 0x1104
} else {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_4022,
link: 0x0,
length: 1)
// 0x1044 is (EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA).
expect = 0x1044
expect_after_value = 0x1044
}
continue.outer
} else if class == 0x09 { // 0x09 is CLASS_FALSE.
match = args.src.match7(a: '\x05false'le)
if match == 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x80_0004,
link: 0x0,
length: 5)
if args.src.available() < 5 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 5, worst_case: 5)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}
} else if class == 0x0A { // 0x0A is CLASS_TRUE.
match = args.src.match7(a: '\x04true'le)
if match == 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x80_0008,
link: 0x0,
length: 4)
if args.src.available() < 4 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}
} else if class == 0x0B { // 0x0B is CLASS_NULL_NAN_INF.
match = args.src.match7(a: '\x04null'le)
if match == 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x80_0002,
link: 0x0,
length: 4)
if args.src.available() < 4 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}
if this.quirk_enabled_allow_inf_nan_numbers {
this.decode_inf_nan?(dst: args.dst, src: args.src)
break.goto_parsed_a_leaf_value
}
} else if class == 0x0C { // 0x0C is CLASS_COMMENT.
if this.quirk_enabled_allow_comment_block or this.quirk_enabled_allow_comment_line {
this.decode_comment?(dst: args.dst, src: args.src)
continue.outer
} }
} }
continue.outer
return "#bad input" } else if class == 0x03 { // 0x03 is CLASS_COLON.
} endwhile.goto_parsed_a_leaf_value args.src.skip32_fast!(actual: 1, worst_case: 1)
// The ':' is filler.
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0,
link: 0x0,
length: 1)
expect = 0x1EB2 // 0x1EB2 is EXPECT_VALUE.
continue.outer
} else if class == 0x04 { // 0x04 is CLASS_NUMBER.
// -------- BEGIN parse numbers.
while true,
pre args.dst.available() > 0,
{
number_length = this.decode_number!(src: args.src)
number_status = number_length >> 8
vminor = 0xA0_0403
if (number_length & 0x80) <> 0 {
vminor = 0xA0_0401
}
number_length = number_length & 0x7F
if number_status == 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: vminor,
link: 0x0,
length: number_length)
break
}
while number_length > 0 {
number_length -= 1
if args.src.can_undo_byte() {
args.src.undo_byte!()
} else {
return "#internal error: inconsistent I/O"
}
} endwhile
if number_status == 1 {
if this.quirk_enabled_allow_inf_nan_numbers {
this.decode_inf_nan?(dst: args.dst, src: args.src)
break
}
return "#bad input"
} else if number_status == 2 {
return "#unsupported number length"
} else {
yield? base."$short read"
while args.dst.available() <= 0,
post args.dst.available() > 0,
{
yield? base."$short write"
} endwhile
}
} endwhile
break.goto_parsed_a_leaf_value
// -------- END parse numbers.
} else if class == 0x05 { // 0x05 is CLASS_OPEN_CURLY_BRACE.
vminor = 0x20_4011
if depth == 0 {
// No-op.
} else if 0 <> (expect_after_value & 0x0040) { // 0x0040 is (1 << CLASS_CLOSE_CURLY_BRACE).
vminor = 0x20_4041
} else {
vminor = 0x20_4021
}
if depth >= 1024 {
return "#unsupported recursion depth"
}
stack_byte = depth / 32
stack_bit = depth & 31
this.stack[stack_byte] |= (1 as base.u32) << stack_bit
depth += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: vminor,
link: 0x0,
length: 1)
expect = 0x1042 // 0x1042 is (EXPECT_CLOSE_CURLY_BRACE | EXPECT_STRING).
expect_after_value = 0x1044 // 0x1044 is (EXPECT_CURLY_CLOSE_BRACE | EXPECT_COMMA).
continue.outer
} else if class == 0x06 { // 0x06 is CLASS_CLOSE_CURLY_BRACE.
args.src.skip32_fast!(actual: 1, worst_case: 1)
if depth <= 1 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_1042,
link: 0x0,
length: 1)
break.outer
}
depth -= 1
stack_byte = (depth - 1) / 32
stack_bit = (depth - 1) & 31
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_2042,
link: 0x0,
length: 1)
// 0x1104 is (EXPECT_SQUARE_CLOSE_BRACKET | EXPECT_COMMA).
expect = 0x1104
expect_after_value = 0x1104
} else {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_4042,
link: 0x0,
length: 1)
// 0x1044 is (EXPECT_CURLY_CLOSE_BRACE | EXPECT_COMMA).
expect = 0x1044
expect_after_value = 0x1044
}
continue.outer
} else if class == 0x07 { // 0x07 is CLASS_OPEN_SQUARE_BRACKET.
vminor = 0x20_2011
if depth == 0 {
// No-op.
} else if 0 <> (expect_after_value & 0x0040) { // 0x0040 is (1 << CLASS_CLOSE_CURLY_BRACE).
vminor = 0x20_2041
} else {
vminor = 0x20_2021
}
if depth >= 1024 {
return "#unsupported recursion depth"
}
stack_byte = depth / 32
stack_bit = depth & 31
this.stack[stack_byte] &= 0xFFFF_FFFF ^ ((1 as base.u32) << stack_bit)
depth += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: vminor,
link: 0x0,
length: 1)
expect = 0x1FB2 // 0x1FB2 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_VALUE).
expect_after_value = 0x1104 // 0x1104 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA).
continue.outer
} else if class == 0x08 { // 0x08 is CLASS_CLOSE_SQUARE_BRACKET.
args.src.skip32_fast!(actual: 1, worst_case: 1)
if depth <= 1 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_1022,
link: 0x0,
length: 1)
break.outer
}
depth -= 1
stack_byte = (depth - 1) / 32
stack_bit = (depth - 1) & 31
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_2022,
link: 0x0,
length: 1)
// 0x1104 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA).
expect = 0x1104
expect_after_value = 0x1104
} else {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x20_4022,
link: 0x0,
length: 1)
// 0x1044 is (EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA).
expect = 0x1044
expect_after_value = 0x1044
}
continue.outer
} else if class == 0x09 { // 0x09 is CLASS_FALSE.
match = args.src.match7(a: '\x05false'le)
if match == 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x80_0004,
link: 0x0,
length: 5)
if args.src.available() < 5 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 5, worst_case: 5)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}
} else if class == 0x0A { // 0x0A is CLASS_TRUE.
match = args.src.match7(a: '\x04true'le)
if match == 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x80_0008,
link: 0x0,
length: 4)
if args.src.available() < 4 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}
} else if class == 0x0B { // 0x0B is CLASS_NULL_NAN_INF.
match = args.src.match7(a: '\x04null'le)
if match == 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: 0x80_0002,
link: 0x0,
length: 4)
if args.src.available() < 4 {
return "#internal error: inconsistent I/O"
}
args.src.skip32_fast!(actual: 4, worst_case: 4)
break.goto_parsed_a_leaf_value
} else if match == 1 {
yield? base."$short read"
continue.outer
}
if this.quirk_enabled_allow_inf_nan_numbers {
this.decode_inf_nan?(dst: args.dst, src: args.src)
break.goto_parsed_a_leaf_value
}
} else if class == 0x0C { // 0x0C is CLASS_COMMENT.
if this.quirk_enabled_allow_comment_block or this.quirk_enabled_allow_comment_line {
this.decode_comment?(dst: args.dst, src: args.src)
continue.outer
}
}
return "#bad input"
}} endwhile.goto_parsed_a_leaf_value
// We've just parsed a leaf (non-container) value: literal (null, // We've just parsed a leaf (non-container) value: literal (null,
// false, true), number or string. // false, true), number or string.
@@ -1124,97 +1124,67 @@ pri func decoder.decode_number!(src: base.io_reader) base.u32[..= 0x3FF] {
var n : base.u32[..= 0x3FF] var n : base.u32[..= 0x3FF]
var floating_point : base.u32[..= 0x80] var floating_point : base.u32[..= 0x80]
while.goto_done true { while.goto_done true {{
n = 0 n = 0
// Peek.
if args.src.available() <= 0 {
if not args.src.is_closed() {
n |= 0x300
}
break.goto_done
}
c = args.src.peek_u8()
// Scan the optional minus sign.
if c <> '-' {
assert args.src.available() > 0
assert n <= 1
} else {
n += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
// Peek. // Peek.
if args.src.available() <= 0 { if args.src.available() <= 0 {
if not args.src.is_closed() { if not args.src.is_closed() {
n |= 0x300 n |= 0x300
} }
n |= 0x100 // A '-' without digits is invalid.
break.goto_done break.goto_done
} }
c = args.src.peek_u8() c = args.src.peek_u8()
// Scan the optional minus sign. assert args.src.available() > 0
if c <> '-' { assert n <= 1
assert args.src.available() > 0 }
assert n <= 1
} else {
n += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
// Peek. // Scan the opening digits.
if args.src.available() <= 0 { if c == '0' {
if not args.src.is_closed() { n += 1
n |= 0x300 args.src.skip32_fast!(actual: 1, worst_case: 1)
} assert n <= 99
n |= 0x100 // A '-' without digits is invalid. } else {
break.goto_done n = this.decode_digits!(src: args.src, n: n)
} if n > 99 {
c = args.src.peek_u8()
assert args.src.available() > 0
assert n <= 1
}
// Scan the opening digits.
if c == '0' {
n += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
assert n <= 99
} else {
n = this.decode_digits!(src: args.src, n: n)
if n > 99 {
break.goto_done
}
assert n <= 99
}
// Peek.
if args.src.available() <= 0 {
if not args.src.is_closed() {
n |= 0x300
}
break.goto_done break.goto_done
} }
c = args.src.peek_u8() assert n <= 99
}
// Scan the optional fraction. // Peek.
if c <> '.' { if args.src.available() <= 0 {
assert args.src.available() > 0 if not args.src.is_closed() {
assert n <= 99 n |= 0x300
} else {
if n >= 99 {
n |= 0x200
break.goto_done
}
n += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
floating_point = 0x80
n = this.decode_digits!(src: args.src, n: n)
if n > 99 {
break.goto_done
}
// Peek.
if args.src.available() <= 0 {
if not args.src.is_closed() {
n |= 0x300
}
break.goto_done
}
c = args.src.peek_u8()
assert args.src.available() > 0
assert n <= 99
} }
break.goto_done
}
c = args.src.peek_u8()
// Scan the optional 'E' or 'e'. // Scan the optional fraction.
if (c <> 'E') and (c <> 'e') { if c <> '.' {
break.goto_done assert args.src.available() > 0
} assert n <= 99
} else {
if n >= 99 { if n >= 99 {
n |= 0x200 n |= 0x200
break.goto_done break.goto_done
@@ -1222,36 +1192,67 @@ pri func decoder.decode_number!(src: base.io_reader) base.u32[..= 0x3FF] {
n += 1 n += 1
args.src.skip32_fast!(actual: 1, worst_case: 1) args.src.skip32_fast!(actual: 1, worst_case: 1)
floating_point = 0x80 floating_point = 0x80
assert n <= 99
n = this.decode_digits!(src: args.src, n: n)
if n > 99 {
break.goto_done
}
// Peek. // Peek.
if args.src.available() <= 0 { if args.src.available() <= 0 {
if not args.src.is_closed() { if not args.src.is_closed() {
n |= 0x300 n |= 0x300
} }
n |= 0x100 // An 'E' or 'e' without digits is invalid.
break.goto_done break.goto_done
} }
c = args.src.peek_u8() c = args.src.peek_u8()
// Scan the optional '+' or '-'. assert args.src.available() > 0
if (c <> '+') and (c <> '-') { assert n <= 99
assert n <= 99 }
} else {
if n >= 99 {
n |= 0x200
break.goto_done
}
n += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
assert n <= 99
}
// Scan the exponent digits.
n = this.decode_digits!(src: args.src, n: n)
// Scan the optional 'E' or 'e'.
if (c <> 'E') and (c <> 'e') {
break.goto_done break.goto_done
} endwhile.goto_done }
if n >= 99 {
n |= 0x200
break.goto_done
}
n += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
floating_point = 0x80
assert n <= 99
// Peek.
if args.src.available() <= 0 {
if not args.src.is_closed() {
n |= 0x300
}
n |= 0x100 // An 'E' or 'e' without digits is invalid.
break.goto_done
}
c = args.src.peek_u8()
// Scan the optional '+' or '-'.
if (c <> '+') and (c <> '-') {
assert n <= 99
} else {
if n >= 99 {
n |= 0x200
break.goto_done
}
n += 1
args.src.skip32_fast!(actual: 1, worst_case: 1)
assert n <= 99
}
// Scan the exponent digits.
n = this.decode_digits!(src: args.src, n: n)
break.goto_done
}} endwhile.goto_done
return n | floating_point return n | floating_point
} }
@@ -1299,6 +1300,7 @@ pri func decoder.decode_leading?(dst: base.token_writer, src: base.io_reader) {
this.allow_leading_ars = this.quirk_enabled_allow_leading_ascii_record_separator this.allow_leading_ars = this.quirk_enabled_allow_leading_ascii_record_separator
this.allow_leading_ubom = this.quirk_enabled_allow_leading_unicode_byte_order_mark this.allow_leading_ubom = this.quirk_enabled_allow_leading_unicode_byte_order_mark
while this.allow_leading_ars or this.allow_leading_ubom { while this.allow_leading_ars or this.allow_leading_ubom {
if args.dst.available() <= 0 { if args.dst.available() <= 0 {
yield? base."$short write" yield? base."$short write"
@@ -1634,5 +1636,4 @@ pri func decoder.decode_trailing_new_line?(dst: base.token_writer, src: base.io_
whitespace_length += 1 whitespace_length += 1
} endwhile.inner } endwhile.inner
} endwhile.outer } endwhile.outer
} }