pax_global_header00006660000000000000000000000064146321230150014507gustar00rootroot0000000000000052 comment=bfd29f3ae377433cb810b51ff2b458eb04a19b46 parse-2.7.15/000077500000000000000000000000001463212301500127155ustar00rootroot00000000000000parse-2.7.15/.gitattributes000066400000000000000000000000441463212301500156060ustar00rootroot00000000000000tests/*/corpus/* linguist-generated parse-2.7.15/.github/000077500000000000000000000000001463212301500142555ustar00rootroot00000000000000parse-2.7.15/.github/dependabot.yml000066400000000000000000000001361463212301500171050ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: gomod directory: / schedule: interval: daily parse-2.7.15/.github/workflows/000077500000000000000000000000001463212301500163125ustar00rootroot00000000000000parse-2.7.15/.github/workflows/go.yml000066400000000000000000000015561463212301500174510ustar00rootroot00000000000000name: Go on: push: branches: - master pull_request: branches: - master jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Go uses: actions/setup-go@v2 with: go-version: 1.18 - name: Build run: go build -v ./... - name: Tests with coverage run: go test -race -v -count=1 ./... #run: go test -race -v -count=1 -coverprofile=coverage.out ./... #- name: Upload Code Coverage # uses: codecov/codecov-action@v1 # with: # name: codecov # fail_ci_if_error: true golangci: name: lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: golangci-lint uses: golangci/golangci-lint-action@v2 continue-on-error: true with: version: latest parse-2.7.15/.gitignore000066400000000000000000000001411463212301500147010ustar00rootroot00000000000000tests/*/fuzz-fuzz.zip tests/*/crashers tests/*/suppressions tests/*/corpus/* !tests/*/corpus/*.* parse-2.7.15/.golangci.yml000066400000000000000000000003031463212301500152750ustar00rootroot00000000000000linters: enable: - depguard - dogsled - gofmt - goimports - golint - gosec - govet - megacheck - misspell - nakedret - prealloc - unconvert - unparam - wastedassign parse-2.7.15/LICENSE.md000066400000000000000000000020621463212301500143210ustar00rootroot00000000000000Copyright (c) 2015 Taco de Wolff Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.parse-2.7.15/README.md000066400000000000000000000125541463212301500142030ustar00rootroot00000000000000# Parse [![API reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/tdewolff/parse/v2?tab=doc) [![Go Report Card](https://goreportcard.com/badge/github.com/tdewolff/parse)](https://goreportcard.com/report/github.com/tdewolff/parse) [![Coverage Status](https://coveralls.io/repos/github/tdewolff/parse/badge.svg?branch=master)](https://coveralls.io/github/tdewolff/parse?branch=master) [![Donate](https://img.shields.io/badge/patreon-donate-DFB317)](https://www.patreon.com/tdewolff) This package contains several lexers and parsers written in [Go][1]. All subpackages are built to be streaming, high performance and to be in accordance with the official (latest) specifications. The lexers are implemented using `buffer.Lexer` in https://github.com/tdewolff/parse/buffer and the parsers work on top of the lexers. Some subpackages have hashes defined (using [Hasher](https://github.com/tdewolff/hasher)) that speed up common byte-slice comparisons. ## Buffer ### Reader Reader is a wrapper around a `[]byte` that implements the `io.Reader` interface. It is comparable to `bytes.Reader` but has slightly different semantics (and a slightly smaller memory footprint). ### Writer Writer is a buffer that implements the `io.Writer` interface and expands the buffer as needed. The reset functionality allows for better memory reuse. After calling `Reset`, it will overwrite the current buffer and thus reduce allocations. ### Lexer Lexer is a read buffer specifically designed for building lexers. It keeps track of two positions: a start and end position. The start position is the beginning of the current token being parsed, the end position is being moved forward until a valid token is found. Calling `Shift` will collapse the positions to the end and return the parsed `[]byte`. Moving the end position can go through `Move(int)` which also accepts negative integers. One can also use `Pos() int` to try and parse a token, and if it fails rewind with `Rewind(int)`, passing the previously saved position. `Peek(int) byte` will peek forward (relative to the end position) and return the byte at that location. `PeekRune(int) (rune, int)` returns UTF-8 runes and its length at the given **byte** position. Upon an error `Peek` will return `0`, the **user must peek at every character** and not skip any, otherwise it may skip a `0` and panic on out-of-bounds indexing. `Lexeme() []byte` will return the currently selected bytes, `Skip()` will collapse the selection. `Shift() []byte` is a combination of `Lexeme() []byte` and `Skip()`. When the passed `io.Reader` returned an error, `Err() error` will return that error even if not at the end of the buffer. ### StreamLexer StreamLexer behaves like Lexer but uses a buffer pool to read in chunks from `io.Reader`, retaining old buffers in memory that are still in use, and re-using old buffers otherwise. Calling `Free(n int)` frees up `n` bytes from the internal buffer(s). It holds an array of buffers to accommodate for keeping everything in-memory. Calling `ShiftLen() int` returns the number of bytes that have been shifted since the previous call to `ShiftLen`, which can be used to specify how many bytes need to be freed up from the buffer. If you don't need to keep returned byte slices around, call `Free(ShiftLen())` after every `Shift` call. ## Strconv This package contains string conversion function much like the standard library's `strconv` package, but it is specifically tailored for the performance needs within the `minify` package. For example, the floating-point to string conversion function is approximately twice as fast as the standard library, but it is not as precise. ## CSS This package is a CSS3 lexer and parser. Both follow the specification at [CSS Syntax Module Level 3](http://www.w3.org/TR/css-syntax-3/). The lexer takes an io.Reader and converts it into tokens until the EOF. The parser returns a parse tree of the full io.Reader input stream, but the low-level `Next` function can be used for stream parsing to returns grammar units until the EOF. [See README here](https://github.com/tdewolff/parse/tree/master/css). ## HTML This package is an HTML5 lexer. It follows the specification at [The HTML syntax](http://www.w3.org/TR/html5/syntax.html). The lexer takes an io.Reader and converts it into tokens until the EOF. [See README here](https://github.com/tdewolff/parse/tree/master/html). ## JS This package is a JS lexer (ECMA-262, edition 6.0). It follows the specification at [ECMAScript Language Specification](http://www.ecma-international.org/ecma-262/6.0/). The lexer takes an io.Reader and converts it into tokens until the EOF. [See README here](https://github.com/tdewolff/parse/tree/master/js). ## JSON This package is a JSON parser (ECMA-404). It follows the specification at [JSON](http://json.org/). The parser takes an io.Reader and converts it into tokens until the EOF. [See README here](https://github.com/tdewolff/parse/tree/master/json). ## SVG This package contains common hashes for SVG1.1 tags and attributes. ## XML This package is an XML1.0 lexer. It follows the specification at [Extensible Markup Language (XML) 1.0 (Fifth Edition)](http://www.w3.org/TR/xml/). The lexer takes an io.Reader and converts it into tokens until the EOF. [See README here](https://github.com/tdewolff/parse/tree/master/xml). ## License Released under the [MIT license](LICENSE.md). [1]: http://golang.org/ "Go Language" parse-2.7.15/binary.go000066400000000000000000000241201463212301500145270ustar00rootroot00000000000000package parse import ( "encoding/binary" "fmt" "io" "math" "os" ) // BinaryReader is a binary big endian file format reader. type BinaryReader struct { Endianness binary.ByteOrder buf []byte pos uint32 eof bool } // NewBinaryReader returns a big endian binary file format reader. func NewBinaryReader(buf []byte) *BinaryReader { if math.MaxUint32 < uint(len(buf)) { return &BinaryReader{binary.BigEndian, nil, 0, true} } return &BinaryReader{binary.BigEndian, buf, 0, false} } // NewBinaryReaderLE returns a little endian binary file format reader. func NewBinaryReaderLE(buf []byte) *BinaryReader { r := NewBinaryReader(buf) r.Endianness = binary.LittleEndian return r } // Seek set the reader position in the buffer. func (r *BinaryReader) Seek(pos uint32) error { if uint32(len(r.buf)) < pos { r.eof = true return io.EOF } r.pos = pos r.eof = false return nil } // Pos returns the reader's position. func (r *BinaryReader) Pos() uint32 { return r.pos } // Len returns the remaining length of the buffer. func (r *BinaryReader) Len() uint32 { return uint32(len(r.buf)) - r.pos } // EOF returns true if we reached the end-of-file. func (r *BinaryReader) EOF() bool { return r.eof } // Read complies with io.Reader. func (r *BinaryReader) Read(b []byte) (int, error) { n := copy(b, r.buf[r.pos:]) r.pos += uint32(n) if r.pos == uint32(len(r.buf)) { r.eof = true return n, io.EOF } return n, nil } // ReadBytes reads n bytes. func (r *BinaryReader) ReadBytes(n uint32) []byte { if r.eof || uint32(len(r.buf))-r.pos < n { r.eof = true return nil } buf := r.buf[r.pos : r.pos+n : r.pos+n] r.pos += n return buf } // ReadString reads a string of length n. func (r *BinaryReader) ReadString(n uint32) string { return string(r.ReadBytes(n)) } // ReadByte reads a single byte. func (r *BinaryReader) ReadByte() byte { b := r.ReadBytes(1) if b == nil { return 0 } return b[0] } // ReadUint8 reads a uint8. func (r *BinaryReader) ReadUint8() uint8 { return r.ReadByte() } // ReadUint16 reads a uint16. func (r *BinaryReader) ReadUint16() uint16 { b := r.ReadBytes(2) if b == nil { return 0 } return r.Endianness.Uint16(b) } // ReadUint32 reads a uint32. func (r *BinaryReader) ReadUint32() uint32 { b := r.ReadBytes(4) if b == nil { return 0 } return r.Endianness.Uint32(b) } // ReadUint64 reads a uint64. func (r *BinaryReader) ReadUint64() uint64 { b := r.ReadBytes(8) if b == nil { return 0 } return r.Endianness.Uint64(b) } // ReadInt8 reads a int8. func (r *BinaryReader) ReadInt8() int8 { return int8(r.ReadByte()) } // ReadInt16 reads a int16. func (r *BinaryReader) ReadInt16() int16 { return int16(r.ReadUint16()) } // ReadInt32 reads a int32. func (r *BinaryReader) ReadInt32() int32 { return int32(r.ReadUint32()) } // ReadInt64 reads a int64. func (r *BinaryReader) ReadInt64() int64 { return int64(r.ReadUint64()) } type BinaryFileReader struct { f *os.File size uint64 offset uint64 Endianness binary.ByteOrder buf []byte pos int } func NewBinaryFileReader(f *os.File, chunk int) (*BinaryFileReader, error) { var buf []byte var size uint64 if chunk == 0 { var err error if buf, err = io.ReadAll(f); err != nil { return nil, err } } else { buf = make([]byte, 0, chunk) } if info, err := f.Stat(); err != nil { return nil, err } else { size = uint64(info.Size()) } return &BinaryFileReader{ f: f, size: size, Endianness: binary.BigEndian, buf: buf, }, nil } func (r *BinaryFileReader) buffer(pos, length uint64) error { if pos < r.offset || r.offset+uint64(len(r.buf)) < pos+length { if math.MaxInt64 < pos { return fmt.Errorf("seek position too large") } else if _, err := r.f.Seek(int64(pos), 0); err != nil { return err } else if n, err := r.f.Read(r.buf[:cap(r.buf)]); err != nil { return err } else { r.offset = pos r.buf = r.buf[:n] r.pos = 0 } } return nil } // Seek set the reader position in the buffer. func (r *BinaryFileReader) Seek(pos uint64) error { if r.size <= pos { return io.EOF } else if err := r.buffer(pos, 0); err != nil { return err } r.pos = int(pos - r.offset) return nil } // Pos returns the reader's position. func (r *BinaryFileReader) Pos() uint64 { return r.offset + uint64(r.pos) } // Len returns the remaining length of the buffer. func (r *BinaryFileReader) Len() uint64 { return r.size - r.Pos() } // Offset returns the offset of the buffer. func (r *BinaryFileReader) Offset() uint64 { return r.offset } // BufferLen returns the length of the buffer. func (r *BinaryFileReader) BufferLen() int { return len(r.buf) } // Read complies with io.Reader. func (r *BinaryFileReader) Read(b []byte) (int, error) { if len(b) <= cap(r.buf) { if err := r.buffer(r.offset+uint64(r.pos), uint64(len(b))); err != nil { return 0, err } n := copy(b, r.buf[r.pos:]) r.pos += n return n, nil } // read directly from file if _, err := r.f.Seek(int64(r.offset)+int64(r.pos), 0); err != nil { return 0, err } n, err := r.f.Read(b) r.offset += uint64(r.pos + n) r.pos = 0 r.buf = r.buf[:0] return n, err } // ReadBytes reads n bytes. func (r *BinaryFileReader) ReadBytes(n int) []byte { if n < len(r.buf)-r.pos { b := r.buf[r.pos : r.pos+n] r.pos += n return b } b := make([]byte, n) if _, err := r.Read(b); err != nil { return nil } return b } // ReadString reads a string of length n. func (r *BinaryFileReader) ReadString(n int) string { return string(r.ReadBytes(n)) } // ReadByte reads a single byte. func (r *BinaryFileReader) ReadByte() byte { b := r.ReadBytes(1) if b == nil { return 0 } return b[0] } // ReadUint8 reads a uint8. func (r *BinaryFileReader) ReadUint8() uint8 { return r.ReadByte() } // ReadUint16 reads a uint16. func (r *BinaryFileReader) ReadUint16() uint16 { b := r.ReadBytes(2) if b == nil { return 0 } return r.Endianness.Uint16(b) } // ReadUint32 reads a uint32. func (r *BinaryFileReader) ReadUint32() uint32 { b := r.ReadBytes(4) if b == nil { return 0 } return r.Endianness.Uint32(b) } // ReadUint64 reads a uint64. func (r *BinaryFileReader) ReadUint64() uint64 { b := r.ReadBytes(8) if b == nil { return 0 } return r.Endianness.Uint64(b) } // ReadInt8 reads a int8. func (r *BinaryFileReader) ReadInt8() int8 { return int8(r.ReadByte()) } // ReadInt16 reads a int16. func (r *BinaryFileReader) ReadInt16() int16 { return int16(r.ReadUint16()) } // ReadInt32 reads a int32. func (r *BinaryFileReader) ReadInt32() int32 { return int32(r.ReadUint32()) } // ReadInt64 reads a int64. func (r *BinaryFileReader) ReadInt64() int64 { return int64(r.ReadUint64()) } // BinaryWriter is a big endian binary file format writer. type BinaryWriter struct { buf []byte } // NewBinaryWriter returns a big endian binary file format writer. func NewBinaryWriter(buf []byte) *BinaryWriter { return &BinaryWriter{buf} } // Len returns the buffer's length in bytes. func (w *BinaryWriter) Len() uint32 { return uint32(len(w.buf)) } // Bytes returns the buffer's bytes. func (w *BinaryWriter) Bytes() []byte { return w.buf } // Write complies with io.Writer. func (w *BinaryWriter) Write(b []byte) (int, error) { w.buf = append(w.buf, b...) return len(b), nil } // WriteBytes writes the given bytes to the buffer. func (w *BinaryWriter) WriteBytes(v []byte) { w.buf = append(w.buf, v...) } // WriteString writes the given string to the buffer. func (w *BinaryWriter) WriteString(v string) { w.WriteBytes([]byte(v)) } // WriteByte writes the given byte to the buffer. func (w *BinaryWriter) WriteByte(v byte) { w.WriteBytes([]byte{v}) } // WriteUint8 writes the given uint8 to the buffer. func (w *BinaryWriter) WriteUint8(v uint8) { w.WriteByte(v) } // WriteUint16 writes the given uint16 to the buffer. func (w *BinaryWriter) WriteUint16(v uint16) { pos := len(w.buf) w.buf = append(w.buf, make([]byte, 2)...) binary.BigEndian.PutUint16(w.buf[pos:], v) } // WriteUint32 writes the given uint32 to the buffer. func (w *BinaryWriter) WriteUint32(v uint32) { pos := len(w.buf) w.buf = append(w.buf, make([]byte, 4)...) binary.BigEndian.PutUint32(w.buf[pos:], v) } // WriteUint64 writes the given uint64 to the buffer. func (w *BinaryWriter) WriteUint64(v uint64) { pos := len(w.buf) w.buf = append(w.buf, make([]byte, 8)...) binary.BigEndian.PutUint64(w.buf[pos:], v) } // WriteInt8 writes the given int8 to the buffer. func (w *BinaryWriter) WriteInt8(v int8) { w.WriteUint8(uint8(v)) } // WriteInt16 writes the given int16 to the buffer. func (w *BinaryWriter) WriteInt16(v int16) { w.WriteUint16(uint16(v)) } // WriteInt32 writes the given int32 to the buffer. func (w *BinaryWriter) WriteInt32(v int32) { w.WriteUint32(uint32(v)) } // WriteInt64 writes the given int64 to the buffer. func (w *BinaryWriter) WriteInt64(v int64) { w.WriteUint64(uint64(v)) } // BitmapReader is a binary bitmap reader. type BitmapReader struct { buf []byte pos uint32 // TODO: to uint64 eof bool } // NewBitmapReader returns a binary bitmap reader. func NewBitmapReader(buf []byte) *BitmapReader { return &BitmapReader{buf, 0, false} } // Pos returns the current bit position. func (r *BitmapReader) Pos() uint32 { return r.pos } // EOF returns if we reached the buffer's end-of-file. func (r *BitmapReader) EOF() bool { return r.eof } // Read reads the next bit. func (r *BitmapReader) Read() bool { if r.eof || uint32(len(r.buf)) <= (r.pos+1)/8 { r.eof = true return false } bit := r.buf[r.pos>>3]&(0x80>>(r.pos&7)) != 0 r.pos += 1 return bit } // BitmapWriter is a binary bitmap writer. type BitmapWriter struct { buf []byte pos uint32 } // NewBitmapWriter returns a binary bitmap writer. func NewBitmapWriter(buf []byte) *BitmapWriter { return &BitmapWriter{buf, 0} } // Len returns the buffer's length in bytes. func (w *BitmapWriter) Len() uint32 { return uint32(len(w.buf)) } // Bytes returns the buffer's bytes. func (w *BitmapWriter) Bytes() []byte { return w.buf } // Write writes the next bit. func (w *BitmapWriter) Write(bit bool) { if uint32(len(w.buf)) <= (w.pos+1)/8 { w.buf = append(w.buf, 0) } if bit { w.buf[w.pos>>3] = w.buf[w.pos>>3] | (0x80 >> (w.pos & 7)) } w.pos += 1 } parse-2.7.15/buffer/000077500000000000000000000000001463212301500141665ustar00rootroot00000000000000parse-2.7.15/buffer/buffer.go000066400000000000000000000015731463212301500157740ustar00rootroot00000000000000// Package buffer contains buffer and wrapper types for byte slices. It is useful for writing lexers or other high-performance byte slice handling. // The `Reader` and `Writer` types implement the `io.Reader` and `io.Writer` respectively and provide a thinner and faster interface than `bytes.Buffer`. // The `Lexer` type is useful for building lexers because it keeps track of the start and end position of a byte selection, and shifts the bytes whenever a valid token is found. // The `StreamLexer` does the same, but keeps a buffer pool so that it reads a limited amount at a time, allowing to parse from streaming sources. package buffer // defaultBufSize specifies the default initial length of internal buffers. var defaultBufSize = 4096 // MinBuf specifies the default initial length of internal buffers. // Solely here to support old versions of parse. var MinBuf = defaultBufSize parse-2.7.15/buffer/lexer.go000066400000000000000000000074711463212301500156450ustar00rootroot00000000000000package buffer import ( "io" "io/ioutil" ) var nullBuffer = []byte{0} // Lexer is a buffered reader that allows peeking forward and shifting, taking an io.Reader. // It keeps data in-memory until Free, taking a byte length, is called to move beyond the data. type Lexer struct { buf []byte pos int // index in buf start int // index in buf err error restore func() } // NewLexer returns a new Lexer for a given io.Reader, and uses ioutil.ReadAll to read it into a byte slice. // If the io.Reader implements Bytes, that is used instead. // It will append a NULL at the end of the buffer. func NewLexer(r io.Reader) *Lexer { var b []byte if r != nil { if buffer, ok := r.(interface { Bytes() []byte }); ok { b = buffer.Bytes() } else { var err error b, err = ioutil.ReadAll(r) if err != nil { return &Lexer{ buf: nullBuffer, err: err, } } } } return NewLexerBytes(b) } // NewLexerBytes returns a new Lexer for a given byte slice, and appends NULL at the end. // To avoid reallocation, make sure the capacity has room for one more byte. func NewLexerBytes(b []byte) *Lexer { z := &Lexer{ buf: b, } n := len(b) if n == 0 { z.buf = nullBuffer } else { // Append NULL to buffer, but try to avoid reallocation if cap(b) > n { // Overwrite next byte but restore when done b = b[:n+1] c := b[n] b[n] = 0 z.buf = b z.restore = func() { b[n] = c } } else { z.buf = append(b, 0) } } return z } // Restore restores the replaced byte past the end of the buffer by NULL. func (z *Lexer) Restore() { if z.restore != nil { z.restore() z.restore = nil } } // Err returns the error returned from io.Reader or io.EOF when the end has been reached. func (z *Lexer) Err() error { return z.PeekErr(0) } // PeekErr returns the error at position pos. When pos is zero, this is the same as calling Err(). func (z *Lexer) PeekErr(pos int) error { if z.err != nil { return z.err } else if z.pos+pos >= len(z.buf)-1 { return io.EOF } return nil } // Peek returns the ith byte relative to the end position. // Peek returns 0 when an error has occurred, Err returns the error. func (z *Lexer) Peek(pos int) byte { pos += z.pos return z.buf[pos] } // PeekRune returns the rune and rune length of the ith byte relative to the end position. func (z *Lexer) PeekRune(pos int) (rune, int) { // from unicode/utf8 c := z.Peek(pos) if c < 0xC0 || z.Peek(pos+1) == 0 { return rune(c), 1 } else if c < 0xE0 || z.Peek(pos+2) == 0 { return rune(c&0x1F)<<6 | rune(z.Peek(pos+1)&0x3F), 2 } else if c < 0xF0 || z.Peek(pos+3) == 0 { return rune(c&0x0F)<<12 | rune(z.Peek(pos+1)&0x3F)<<6 | rune(z.Peek(pos+2)&0x3F), 3 } return rune(c&0x07)<<18 | rune(z.Peek(pos+1)&0x3F)<<12 | rune(z.Peek(pos+2)&0x3F)<<6 | rune(z.Peek(pos+3)&0x3F), 4 } // Move advances the position. func (z *Lexer) Move(n int) { z.pos += n } // Pos returns a mark to which can be rewinded. func (z *Lexer) Pos() int { return z.pos - z.start } // Rewind rewinds the position to the given position. func (z *Lexer) Rewind(pos int) { z.pos = z.start + pos } // Lexeme returns the bytes of the current selection. func (z *Lexer) Lexeme() []byte { return z.buf[z.start:z.pos:z.pos] } // Skip collapses the position to the end of the selection. func (z *Lexer) Skip() { z.start = z.pos } // Shift returns the bytes of the current selection and collapses the position to the end of the selection. func (z *Lexer) Shift() []byte { b := z.buf[z.start:z.pos:z.pos] z.start = z.pos return b } // Offset returns the character position in the buffer. func (z *Lexer) Offset() int { return z.pos } // Bytes returns the underlying buffer. func (z *Lexer) Bytes() []byte { return z.buf[: len(z.buf)-1 : len(z.buf)-1] } // Reset resets position to the underlying buffer. func (z *Lexer) Reset() { z.start = 0 z.pos = 0 } parse-2.7.15/buffer/lexer_test.go000066400000000000000000000101051463212301500166700ustar00rootroot00000000000000package buffer import ( "bytes" "io" "testing" "github.com/tdewolff/test" ) func TestLexer(t *testing.T) { s := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` z := NewLexer(bytes.NewBufferString(s)) test.Bytes(t, z.Bytes(), []byte(s), "bytes match original buffer") test.T(t, z.err, nil, "buffer has no error") test.T(t, z.Err(), nil, "buffer is at EOF but must not return EOF until we reach that") test.That(t, z.Pos() == 0, "buffer must start at position 0") test.That(t, z.Peek(0) == 'L', "first character must be 'L'") test.That(t, z.Peek(1) == 'o', "second character must be 'o'") z.Move(1) test.That(t, z.Peek(0) == 'o', "must be 'o' at position 1") test.That(t, z.Peek(1) == 'r', "must be 'r' at position 1") z.Rewind(6) test.That(t, z.Peek(0) == 'i', "must be 'i' at position 6") test.That(t, z.Peek(1) == 'p', "must be 'p' at position 7") test.T(t, z.Offset(), 6, "offset") test.Bytes(t, z.Lexeme(), []byte("Lorem "), "buffered string must now read 'Lorem ' when at position 6") test.Bytes(t, z.Shift(), []byte("Lorem "), "shift must return the buffered string") test.That(t, z.Pos() == 0, "after shifting position must be 0") test.That(t, z.Peek(0) == 'i', "must be 'i' at position 0 after shifting") test.That(t, z.Peek(1) == 'p', "must be 'p' at position 1 after shifting") test.T(t, z.Err(), nil, "error must be nil at this point") z.Move(len(s) - len("Lorem ") - 1) test.T(t, z.Err(), nil, "error must be nil just before the end of the buffer") z.Skip() test.That(t, z.Pos() == 0, "after skipping position must be 0") z.Move(1) test.T(t, z.Err(), io.EOF, "error must be EOF when past the buffer") z.Move(-1) test.T(t, z.Err(), nil, "error must be nil just before the end of the buffer, even when it has been past the buffer") z.Reset() test.That(t, z.Peek(0) == 'L', "must be 'L' at position 0") test.That(t, z.Peek(1) == 'o', "must be 'o' at position 1") } func TestLexerRunes(t *testing.T) { z := NewLexer(bytes.NewBufferString("aæ†\U00100000")) r, n := z.PeekRune(0) test.That(t, n == 1, "first character must be length 1") test.That(t, r == 'a', "first character must be rune 'a'") r, n = z.PeekRune(1) test.That(t, n == 2, "second character must be length 2") test.That(t, r == 'æ', "second character must be rune 'æ'") r, n = z.PeekRune(3) test.That(t, n == 3, "fourth character must be length 3") test.That(t, r == '†', "fourth character must be rune '†'") r, n = z.PeekRune(6) test.That(t, n == 4, "seventh character must be length 4") test.That(t, r == '\U00100000', "seventh character must be rune '\U00100000'") } func TestLexerBadRune(t *testing.T) { z := NewLexer(bytes.NewBufferString("\xF0")) // expect four byte rune r, n := z.PeekRune(0) test.T(t, n, 1, "length") test.T(t, r, rune(0xF0), "rune") } func TestLexerZeroLen(t *testing.T) { z := NewLexer(test.NewPlainReader(bytes.NewBufferString(""))) test.That(t, z.Peek(0) == 0, "first character must yield error") test.Bytes(t, z.Bytes(), []byte{}, "bytes match original buffer") } func TestLexerEmptyReader(t *testing.T) { z := NewLexer(test.NewEmptyReader()) test.That(t, z.Peek(0) == 0, "first character must yield error") test.T(t, z.Err(), io.EOF, "error must be EOF") test.That(t, z.Peek(0) == 0, "second peek must also yield error") } func TestLexerErrorReader(t *testing.T) { z := NewLexer(test.NewErrorReader(0)) test.That(t, z.Peek(0) == 0, "first character must yield error") test.T(t, z.Err(), test.ErrPlain, "error must be ErrPlain") test.That(t, z.Peek(0) == 0, "second peek must also yield error") } func TestLexerBytes(t *testing.T) { b := []byte{'t', 'e', 's', 't'} z := NewLexerBytes(b) test.That(t, z.Peek(4) == 0, "fifth character must yield NULL") } func TestLexerRestore(t *testing.T) { b := []byte{'a', 'b', 'c', 'd'} z := NewLexerBytes(b[:2]) test.T(t, len(z.buf), 3, "must have terminating NULL") test.T(t, z.buf[2], byte(0), "must have terminating NULL") test.Bytes(t, b, []byte{'a', 'b', 0, 'd'}, "terminating NULL overwrites underlying buffer") z.Restore() test.Bytes(t, b, []byte{'a', 'b', 'c', 'd'}, "terminating NULL has been restored") } parse-2.7.15/buffer/reader.go000066400000000000000000000015561463212301500157660ustar00rootroot00000000000000package buffer import "io" // Reader implements an io.Reader over a byte slice. type Reader struct { buf []byte pos int } // NewReader returns a new Reader for a given byte slice. func NewReader(buf []byte) *Reader { return &Reader{ buf: buf, } } // Read reads bytes into the given byte slice and returns the number of bytes read and an error if occurred. func (r *Reader) Read(b []byte) (n int, err error) { if len(b) == 0 { return 0, nil } if r.pos >= len(r.buf) { return 0, io.EOF } n = copy(b, r.buf[r.pos:]) r.pos += n return } // Bytes returns the underlying byte slice. func (r *Reader) Bytes() []byte { return r.buf } // Reset resets the position of the read pointer to the beginning of the underlying byte slice. func (r *Reader) Reset() { r.pos = 0 } // Len returns the length of the buffer. func (r *Reader) Len() int { return len(r.buf) } parse-2.7.15/buffer/reader_test.go000066400000000000000000000023311463212301500170150ustar00rootroot00000000000000package buffer import ( "bytes" "fmt" "io" "testing" "github.com/tdewolff/test" ) func TestReader(t *testing.T) { s := []byte("abcde") r := NewReader(s) test.T(t, r.Len(), 5, "len") test.Bytes(t, r.Bytes(), s, "reader must return bytes stored") buf := make([]byte, 3) n, err := r.Read(buf) test.T(t, err, nil, "error") test.That(t, n == 3, "first read must read 3 characters") test.Bytes(t, buf, []byte("abc"), "first read must match 'abc'") n, err = r.Read(buf) test.T(t, err, nil, "error") test.That(t, n == 2, "second read must read 2 characters") test.Bytes(t, buf[:n], []byte("de"), "second read must match 'de'") n, err = r.Read(buf) test.T(t, err, io.EOF, "error") test.That(t, n == 0, "third read must read 0 characters") n, err = r.Read(nil) test.T(t, err, nil, "error") test.That(t, n == 0, "read to nil buffer must return 0 characters read") r.Reset() n, err = r.Read(buf) test.T(t, err, nil, "error") test.That(t, n == 3, "read after reset must read 3 characters") test.Bytes(t, buf, []byte("abc"), "read after reset must match 'abc'") } func ExampleNewReader() { r := NewReader([]byte("Lorem ipsum")) w := &bytes.Buffer{} io.Copy(w, r) fmt.Println(w.String()) // Output: Lorem ipsum } parse-2.7.15/buffer/streamlexer.go000066400000000000000000000133341463212301500170540ustar00rootroot00000000000000package buffer import ( "io" ) type block struct { buf []byte next int // index in pool plus one active bool } type bufferPool struct { pool []block head int // index in pool plus one tail int // index in pool plus one pos int // byte pos in tail } func (z *bufferPool) swap(oldBuf []byte, size int) []byte { // find new buffer that can be reused swap := -1 for i := 0; i < len(z.pool); i++ { if !z.pool[i].active && size <= cap(z.pool[i].buf) { swap = i break } } if swap == -1 { // no free buffer found for reuse if z.tail == 0 && z.pos >= len(oldBuf) && size <= cap(oldBuf) { // but we can reuse the current buffer! z.pos -= len(oldBuf) return oldBuf[:0] } // allocate new z.pool = append(z.pool, block{make([]byte, 0, size), 0, true}) swap = len(z.pool) - 1 } newBuf := z.pool[swap].buf // put current buffer into pool z.pool[swap] = block{oldBuf, 0, true} if z.head != 0 { z.pool[z.head-1].next = swap + 1 } z.head = swap + 1 if z.tail == 0 { z.tail = swap + 1 } return newBuf[:0] } func (z *bufferPool) free(n int) { z.pos += n // move the tail over to next buffers for z.tail != 0 && z.pos >= len(z.pool[z.tail-1].buf) { z.pos -= len(z.pool[z.tail-1].buf) newTail := z.pool[z.tail-1].next z.pool[z.tail-1].active = false // after this, any thread may pick up the inactive buffer, so it can't be used anymore z.tail = newTail } if z.tail == 0 { z.head = 0 } } // StreamLexer is a buffered reader that allows peeking forward and shifting, taking an io.Reader. // It keeps data in-memory until Free, taking a byte length, is called to move beyond the data. type StreamLexer struct { r io.Reader err error pool bufferPool buf []byte start int // index in buf pos int // index in buf prevStart int free int } // NewStreamLexer returns a new StreamLexer for a given io.Reader with a 4kB estimated buffer size. // If the io.Reader implements Bytes, that buffer is used instead. func NewStreamLexer(r io.Reader) *StreamLexer { return NewStreamLexerSize(r, defaultBufSize) } // NewStreamLexerSize returns a new StreamLexer for a given io.Reader and estimated required buffer size. // If the io.Reader implements Bytes, that buffer is used instead. func NewStreamLexerSize(r io.Reader, size int) *StreamLexer { // if reader has the bytes in memory already, use that instead if buffer, ok := r.(interface { Bytes() []byte }); ok { return &StreamLexer{ err: io.EOF, buf: buffer.Bytes(), } } return &StreamLexer{ r: r, buf: make([]byte, 0, size), } } func (z *StreamLexer) read(pos int) byte { if z.err != nil { return 0 } // free unused bytes z.pool.free(z.free) z.free = 0 // get new buffer c := cap(z.buf) p := pos - z.start + 1 if 2*p > c { // if the token is larger than half the buffer, increase buffer size c = 2*c + p } d := len(z.buf) - z.start buf := z.pool.swap(z.buf[:z.start], c) copy(buf[:d], z.buf[z.start:]) // copy the left-overs (unfinished token) from the old buffer // read in new data for the rest of the buffer var n int for pos-z.start >= d && z.err == nil { n, z.err = z.r.Read(buf[d:cap(buf)]) d += n } pos -= z.start z.pos -= z.start z.start, z.buf = 0, buf[:d] if pos >= d { return 0 } return z.buf[pos] } // Err returns the error returned from io.Reader. It may still return valid bytes for a while though. func (z *StreamLexer) Err() error { if z.err == io.EOF && z.pos < len(z.buf) { return nil } return z.err } // Free frees up bytes of length n from previously shifted tokens. // Each call to Shift should at one point be followed by a call to Free with a length returned by ShiftLen. func (z *StreamLexer) Free(n int) { z.free += n } // Peek returns the ith byte relative to the end position and possibly does an allocation. // Peek returns zero when an error has occurred, Err returns the error. // TODO: inline function func (z *StreamLexer) Peek(pos int) byte { pos += z.pos if uint(pos) < uint(len(z.buf)) { // uint for BCE return z.buf[pos] } return z.read(pos) } // PeekRune returns the rune and rune length of the ith byte relative to the end position. func (z *StreamLexer) PeekRune(pos int) (rune, int) { // from unicode/utf8 c := z.Peek(pos) if c < 0xC0 { return rune(c), 1 } else if c < 0xE0 { return rune(c&0x1F)<<6 | rune(z.Peek(pos+1)&0x3F), 2 } else if c < 0xF0 { return rune(c&0x0F)<<12 | rune(z.Peek(pos+1)&0x3F)<<6 | rune(z.Peek(pos+2)&0x3F), 3 } return rune(c&0x07)<<18 | rune(z.Peek(pos+1)&0x3F)<<12 | rune(z.Peek(pos+2)&0x3F)<<6 | rune(z.Peek(pos+3)&0x3F), 4 } // Move advances the position. func (z *StreamLexer) Move(n int) { z.pos += n } // Pos returns a mark to which can be rewinded. func (z *StreamLexer) Pos() int { return z.pos - z.start } // Rewind rewinds the position to the given position. func (z *StreamLexer) Rewind(pos int) { z.pos = z.start + pos } // Lexeme returns the bytes of the current selection. func (z *StreamLexer) Lexeme() []byte { return z.buf[z.start:z.pos] } // Skip collapses the position to the end of the selection. func (z *StreamLexer) Skip() { z.start = z.pos } // Shift returns the bytes of the current selection and collapses the position to the end of the selection. // It also returns the number of bytes we moved since the last call to Shift. This can be used in calls to Free. func (z *StreamLexer) Shift() []byte { if z.pos > len(z.buf) { // make sure we peeked at least as much as we shift z.read(z.pos - 1) } b := z.buf[z.start:z.pos] z.start = z.pos return b } // ShiftLen returns the number of bytes moved since the last call to ShiftLen. This can be used in calls to Free because it takes into account multiple Shifts or Skips. func (z *StreamLexer) ShiftLen() int { n := z.start - z.prevStart z.prevStart = z.start return n } parse-2.7.15/buffer/streamlexer_test.go000066400000000000000000000125111463212301500201070ustar00rootroot00000000000000package buffer import ( "bytes" "io" "testing" "github.com/tdewolff/test" ) func TestBufferPool(t *testing.T) { z := &bufferPool{} lorem := []byte("Lorem ipsum") dolor := []byte("dolor sit amet") consectetur := []byte("consectetur adipiscing elit") // set lorem as first buffer and get new dolor buffer b := z.swap(lorem, len(dolor)) test.That(t, len(b) == 0) test.That(t, cap(b) == len(dolor)) b = append(b, dolor...) // free first buffer so it will be reused z.free(len(lorem)) b = z.swap(b, len(lorem)) b = b[:len(lorem)] test.Bytes(t, b, lorem) b = z.swap(b, len(consectetur)) b = append(b, consectetur...) // free in advance to reuse the same buffer z.free(len(dolor) + len(lorem) + len(consectetur)) test.That(t, z.head == 0) b = z.swap(b, len(consectetur)) b = b[:len(consectetur)] test.Bytes(t, b, consectetur) // free in advance but request larger buffer z.free(len(consectetur)) b = z.swap(b, len(consectetur)+1) b = append(b, consectetur...) b = append(b, '.') test.That(t, cap(b) == len(consectetur)+1) } func TestStreamLexer(t *testing.T) { s := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` z := NewStreamLexer(bytes.NewBufferString(s)) test.T(t, z.err, io.EOF, "buffer must be fully in memory") test.T(t, z.Err(), nil, "buffer is at EOF but must not return EOF until we reach that") test.That(t, z.Pos() == 0, "buffer must start at position 0") test.That(t, z.Peek(0) == 'L', "first character must be 'L'") test.That(t, z.Peek(1) == 'o', "second character must be 'o'") z.Move(1) test.That(t, z.Peek(0) == 'o', "must be 'o' at position 1") test.That(t, z.Peek(1) == 'r', "must be 'r' at position 1") z.Rewind(6) test.That(t, z.Peek(0) == 'i', "must be 'i' at position 6") test.That(t, z.Peek(1) == 'p', "must be 'p' at position 7") test.Bytes(t, z.Lexeme(), []byte("Lorem "), "buffered string must now read 'Lorem ' when at position 6") test.Bytes(t, z.Shift(), []byte("Lorem "), "shift must return the buffered string") test.That(t, z.ShiftLen() == len("Lorem "), "shifted length must equal last shift") test.That(t, z.Pos() == 0, "after shifting position must be 0") test.That(t, z.Peek(0) == 'i', "must be 'i' at position 0 after shifting") test.That(t, z.Peek(1) == 'p', "must be 'p' at position 1 after shifting") test.T(t, z.Err(), nil, "error must be nil at this point") z.Move(len(s) - len("Lorem ") - 1) test.T(t, z.Err(), nil, "error must be nil just before the end of the buffer") z.Skip() test.That(t, z.Pos() == 0, "after skipping position must be 0") z.Move(1) test.T(t, z.Err(), io.EOF, "error must be EOF when past the buffer") z.Move(-1) test.T(t, z.Err(), nil, "error must be nil just before the end of the buffer, even when it has been past the buffer") z.Free(0) // has already been tested } func TestStreamLexerShift(t *testing.T) { s := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` z := NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 5) z.Move(len("Lorem ")) test.Bytes(t, z.Shift(), []byte("Lorem "), "shift must return the buffered string") test.That(t, z.ShiftLen() == len("Lorem "), "shifted length must equal last shift") } func TestStreamLexerSmall(t *testing.T) { s := `abcdefghijklm` z := NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 4) test.That(t, z.Peek(8) == 'i', "first character must be 'i' at position 8") z = NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 4) test.That(t, z.Peek(12) == 'm', "first character must be 'm' at position 12") z = NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 0) test.That(t, z.Peek(4) == 'e', "first character must be 'e' at position 4") z = NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 13) test.That(t, z.Peek(13) == 0, "must yield error at position 13") } func TestStreamLexerSingle(t *testing.T) { z := NewStreamLexer(test.NewInfiniteReader()) test.That(t, z.Peek(0) == '.') test.That(t, z.Peek(1) == '.') test.That(t, z.Peek(3) == '.', "required two successful reads") } func TestStreamLexerRunes(t *testing.T) { z := NewStreamLexer(bytes.NewBufferString("aæ†\U00100000")) r, n := z.PeekRune(0) test.That(t, n == 1, "first character must be length 1") test.That(t, r == 'a', "first character must be rune 'a'") r, n = z.PeekRune(1) test.That(t, n == 2, "second character must be length 2") test.That(t, r == 'æ', "second character must be rune 'æ'") r, n = z.PeekRune(3) test.That(t, n == 3, "fourth character must be length 3") test.That(t, r == '†', "fourth character must be rune '†'") r, n = z.PeekRune(6) test.That(t, n == 4, "seventh character must be length 4") test.That(t, r == '\U00100000', "seventh character must be rune '\U00100000'") } func TestStreamLexerBadRune(t *testing.T) { z := NewStreamLexer(bytes.NewBufferString("\xF0")) // expect four byte rune r, n := z.PeekRune(0) test.T(t, n, 4, "length") test.T(t, r, rune(0), "rune") } func TestStreamLexerZeroLen(t *testing.T) { z := NewStreamLexer(test.NewPlainReader(bytes.NewBufferString(""))) test.That(t, z.Peek(0) == 0, "first character must yield error") } func TestStreamLexerEmptyReader(t *testing.T) { z := NewStreamLexer(test.NewEmptyReader()) test.That(t, z.Peek(0) == 0, "first character must yield error") test.T(t, z.Err(), io.EOF, "error must be EOF") test.That(t, z.Peek(0) == 0, "second peek must also yield error") } parse-2.7.15/buffer/writer.go000066400000000000000000000026571463212301500160430ustar00rootroot00000000000000package buffer import ( "io" ) // Writer implements an io.Writer over a byte slice. type Writer struct { buf []byte err error expand bool } // NewWriter returns a new Writer for a given byte slice. func NewWriter(buf []byte) *Writer { return &Writer{ buf: buf, expand: true, } } // NewStaticWriter returns a new Writer for a given byte slice. It does not reallocate and expand the byte-slice. func NewStaticWriter(buf []byte) *Writer { return &Writer{ buf: buf, expand: false, } } // Write writes bytes from the given byte slice and returns the number of bytes written and an error if occurred. When err != nil, n == 0. func (w *Writer) Write(b []byte) (int, error) { n := len(b) end := len(w.buf) if end+n > cap(w.buf) { if !w.expand { w.err = io.EOF return 0, io.EOF } buf := make([]byte, end, 2*cap(w.buf)+n) copy(buf, w.buf) w.buf = buf } w.buf = w.buf[:end+n] return copy(w.buf[end:], b), nil } // Len returns the length of the underlying byte slice. func (w *Writer) Len() int { return len(w.buf) } // Bytes returns the underlying byte slice. func (w *Writer) Bytes() []byte { return w.buf } // Reset empties and reuses the current buffer. Subsequent writes will overwrite the buffer, so any reference to the underlying slice is invalidated after this call. func (w *Writer) Reset() { w.buf = w.buf[:0] } // Close returns the last error. func (w *Writer) Close() error { return w.err } parse-2.7.15/buffer/writer_test.go000066400000000000000000000025121463212301500170700ustar00rootroot00000000000000package buffer import ( "fmt" "testing" "github.com/tdewolff/test" ) func TestWriter(t *testing.T) { w := NewWriter(make([]byte, 0, 3)) test.That(t, w.Len() == 0, "buffer must initially have zero length") n, _ := w.Write([]byte("abc")) test.That(t, n == 3, "first write must write 3 characters") test.Bytes(t, w.Bytes(), []byte("abc"), "first write must match 'abc'") test.That(t, w.Len() == 3, "buffer must have length 3 after first write") n, _ = w.Write([]byte("def")) test.That(t, n == 3, "second write must write 3 characters") test.Bytes(t, w.Bytes(), []byte("abcdef"), "second write must match 'abcdef'") w.Reset() test.Bytes(t, w.Bytes(), []byte(""), "reset must match ''") n, _ = w.Write([]byte("ghijkl")) test.That(t, n == 6, "third write must write 6 characters") test.Bytes(t, w.Bytes(), []byte("ghijkl"), "third write must match 'ghijkl'") } func ExampleNewWriter() { w := NewWriter(make([]byte, 0, 11)) // initial buffer length is 11 w.Write([]byte("Lorem ipsum")) fmt.Println(string(w.Bytes())) // Output: Lorem ipsum } func ExampleWriter_Reset() { w := NewWriter(make([]byte, 0, 11)) // initial buffer length is 10 w.Write([]byte("garbage that will be overwritten")) // does reallocation w.Reset() w.Write([]byte("Lorem ipsum")) fmt.Println(string(w.Bytes())) // Output: Lorem ipsum } parse-2.7.15/common.go000066400000000000000000000351251463212301500145420ustar00rootroot00000000000000// Package parse contains a collection of parsers for various formats in its subpackages. package parse import ( "bytes" "encoding/base64" "errors" "strconv" ) var ( dataSchemeBytes = []byte("data:") base64Bytes = []byte("base64") textMimeBytes = []byte("text/plain") ) // ErrBadDataURI is returned by DataURI when the byte slice does not start with 'data:' or is too short. var ErrBadDataURI = errors.New("not a data URI") // Number returns the number of bytes that parse as a number of the regex format (+|-)?([0-9]+(\.[0-9]+)?|\.[0-9]+)((e|E)(+|-)?[0-9]+)?. func Number(b []byte) int { if len(b) == 0 { return 0 } i := 0 if b[i] == '+' || b[i] == '-' { i++ if i >= len(b) { return 0 } } firstDigit := (b[i] >= '0' && b[i] <= '9') if firstDigit { i++ for i < len(b) && b[i] >= '0' && b[i] <= '9' { i++ } } if i < len(b) && b[i] == '.' { i++ if i < len(b) && b[i] >= '0' && b[i] <= '9' { i++ for i < len(b) && b[i] >= '0' && b[i] <= '9' { i++ } } else if firstDigit { // . could belong to the next token i-- return i } else { return 0 } } else if !firstDigit { return 0 } iOld := i if i < len(b) && (b[i] == 'e' || b[i] == 'E') { i++ if i < len(b) && (b[i] == '+' || b[i] == '-') { i++ } if i >= len(b) || b[i] < '0' || b[i] > '9' { // e could belong to next token return iOld } for i < len(b) && b[i] >= '0' && b[i] <= '9' { i++ } } return i } // Dimension parses a byte-slice and returns the length of the number and its unit. func Dimension(b []byte) (int, int) { num := Number(b) if num == 0 || num == len(b) { return num, 0 } else if b[num] == '%' { return num, 1 } else if b[num] >= 'a' && b[num] <= 'z' || b[num] >= 'A' && b[num] <= 'Z' { i := num + 1 for i < len(b) && (b[i] >= 'a' && b[i] <= 'z' || b[i] >= 'A' && b[i] <= 'Z') { i++ } return num, i - num } return num, 0 } // Mediatype parses a given mediatype and splits the mimetype from the parameters. // It works similar to mime.ParseMediaType but is faster. func Mediatype(b []byte) ([]byte, map[string]string) { i := 0 for i < len(b) && b[i] == ' ' { i++ } b = b[i:] n := len(b) mimetype := b var params map[string]string for i := 3; i < n; i++ { // mimetype is at least three characters long if b[i] == ';' || b[i] == ' ' { mimetype = b[:i] if b[i] == ' ' { i++ // space for i < n && b[i] == ' ' { i++ } if n <= i || b[i] != ';' { break } } params = map[string]string{} s := string(b) PARAM: i++ // semicolon for i < n && s[i] == ' ' { i++ } start := i for i < n && s[i] != '=' && s[i] != ';' && s[i] != ' ' { i++ } key := s[start:i] for i < n && s[i] == ' ' { i++ } if i < n && s[i] == '=' { i++ for i < n && s[i] == ' ' { i++ } start = i for i < n && s[i] != ';' && s[i] != ' ' { i++ } } else { start = i } params[key] = s[start:i] for i < n && s[i] == ' ' { i++ } if i < n && s[i] == ';' { goto PARAM } break } } return mimetype, params } // DataURI parses the given data URI and returns the mediatype, data and ok. func DataURI(dataURI []byte) ([]byte, []byte, error) { if len(dataURI) > 5 && bytes.Equal(dataURI[:5], dataSchemeBytes) { dataURI = dataURI[5:] inBase64 := false var mediatype []byte i := 0 for j := 0; j < len(dataURI); j++ { c := dataURI[j] if c == '=' || c == ';' || c == ',' { if c != '=' && bytes.Equal(TrimWhitespace(dataURI[i:j]), base64Bytes) { if len(mediatype) > 0 { mediatype = mediatype[:len(mediatype)-1] } inBase64 = true i = j } else if c != ',' { mediatype = append(append(mediatype, TrimWhitespace(dataURI[i:j])...), c) i = j + 1 } else { mediatype = append(mediatype, TrimWhitespace(dataURI[i:j])...) } if c == ',' { if len(mediatype) == 0 || mediatype[0] == ';' { mediatype = textMimeBytes } data := dataURI[j+1:] if inBase64 { decoded := make([]byte, base64.StdEncoding.DecodedLen(len(data))) n, err := base64.StdEncoding.Decode(decoded, data) if err != nil { return nil, nil, err } data = decoded[:n] } else { data = DecodeURL(data) } return mediatype, data, nil } } } } return nil, nil, ErrBadDataURI } // QuoteEntity parses the given byte slice and returns the quote that got matched (' or ") and its entity length. // TODO: deprecated func QuoteEntity(b []byte) (quote byte, n int) { if len(b) < 5 || b[0] != '&' { return 0, 0 } if b[1] == '#' { if b[2] == 'x' { i := 3 for i < len(b) && b[i] == '0' { i++ } if i+2 < len(b) && b[i] == '2' && b[i+2] == ';' { if b[i+1] == '2' { return '"', i + 3 // " } else if b[i+1] == '7' { return '\'', i + 3 // ' } } } else { i := 2 for i < len(b) && b[i] == '0' { i++ } if i+2 < len(b) && b[i] == '3' && b[i+2] == ';' { if b[i+1] == '4' { return '"', i + 3 // " } else if b[i+1] == '9' { return '\'', i + 3 // ' } } } } else if len(b) >= 6 && b[5] == ';' { if bytes.Equal(b[1:5], []byte{'q', 'u', 'o', 't'}) { return '"', 6 // " } else if bytes.Equal(b[1:5], []byte{'a', 'p', 'o', 's'}) { return '\'', 6 // ' } } return 0, 0 } // ReplaceMultipleWhitespace replaces character series of space, \n, \t, \f, \r into a single space or newline (when the serie contained a \n or \r). func ReplaceMultipleWhitespace(b []byte) []byte { j, k := 0, 0 // j is write position, k is start of next text section for i := 0; i < len(b); i++ { if IsWhitespace(b[i]) { start := i newline := IsNewline(b[i]) i++ for ; i < len(b) && IsWhitespace(b[i]); i++ { if IsNewline(b[i]) { newline = true } } if newline { b[start] = '\n' } else { b[start] = ' ' } if 1 < i-start { // more than one whitespace if j == 0 { j = start + 1 } else { j += copy(b[j:], b[k:start+1]) } k = i } } } if j == 0 { return b } else if j == 1 { // only if starts with whitespace b[k-1] = b[0] return b[k-1:] } else if k < len(b) { j += copy(b[j:], b[k:]) } return b[:j] } // replaceEntities will replace in b at index i, assuming that b[i] == '&' and that i+3= '0' && b[j] <= '9' || b[j] >= 'a' && b[j] <= 'f' || b[j] >= 'A' && b[j] <= 'F'); j++ { if b[j] <= '9' { c = c<<4 + int(b[j]-'0') } else if b[j] <= 'F' { c = c<<4 + int(b[j]-'A') + 10 } else if b[j] <= 'f' { c = c<<4 + int(b[j]-'a') + 10 } } if j <= i+3 || 10000 <= c { return b, j - 1 } if c < 128 { r = []byte{byte(c)} } else { r = append(r, '&', '#') r = strconv.AppendInt(r, int64(c), 10) r = append(r, ';') } } else { c := 0 for ; j < len(b) && c < 128 && b[j] >= '0' && b[j] <= '9'; j++ { c = c*10 + int(b[j]-'0') } if j <= i+2 || 128 <= c { return b, j - 1 } r = []byte{byte(c)} } } else { for ; j < len(b) && j-i-1 <= MaxEntityLength && b[j] != ';'; j++ { } if j <= i+1 || len(b) <= j { return b, j - 1 } var ok bool r, ok = entitiesMap[string(b[i+1:j])] if !ok { return b, j } } // j is at semicolon n := j + 1 - i if j < len(b) && b[j] == ';' && 2 < n { if len(r) == 1 { if q, ok := revEntitiesMap[r[0]]; ok { if len(q) == len(b[i:j+1]) && bytes.Equal(q, b[i:j+1]) { return b, j } r = q } else if r[0] == '&' { // check if for example & is followed by something that could potentially be an entity k := j + 1 if k < len(b) && (b[k] >= '0' && b[k] <= '9' || b[k] >= 'a' && b[k] <= 'z' || b[k] >= 'A' && b[k] <= 'Z' || b[k] == '#') { return b, k } } } copy(b[i:], r) copy(b[i+len(r):], b[j+1:]) b = b[:len(b)-n+len(r)] return b, i + len(r) - 1 } return b, i } // ReplaceEntities replaces all occurrences of entites (such as ") to their respective unencoded bytes. func ReplaceEntities(b []byte, entitiesMap map[string][]byte, revEntitiesMap map[byte][]byte) []byte { for i := 0; i < len(b); i++ { if b[i] == '&' && i+3 < len(b) { b, i = replaceEntities(b, i, entitiesMap, revEntitiesMap) } } return b } // ReplaceMultipleWhitespaceAndEntities is a combination of ReplaceMultipleWhitespace and ReplaceEntities. It is faster than executing both sequentially. func ReplaceMultipleWhitespaceAndEntities(b []byte, entitiesMap map[string][]byte, revEntitiesMap map[byte][]byte) []byte { j, k := 0, 0 // j is write position, k is start of next text section for i := 0; i < len(b); i++ { if IsWhitespace(b[i]) { start := i newline := IsNewline(b[i]) i++ for ; i < len(b) && IsWhitespace(b[i]); i++ { if IsNewline(b[i]) { newline = true } } if newline { b[start] = '\n' } else { b[start] = ' ' } if 1 < i-start { // more than one whitespace if j == 0 { j = start + 1 } else { j += copy(b[j:], b[k:start+1]) } k = i } } if i+3 < len(b) && b[i] == '&' { b, i = replaceEntities(b, i, entitiesMap, revEntitiesMap) } } if j == 0 { return b } else if j == 1 { // only if starts with whitespace b[k-1] = b[0] return b[k-1:] } else if k < len(b) { j += copy(b[j:], b[k:]) } return b[:j] } // URLEncodingTable is a charmap for which characters need escaping in the URL encoding scheme var URLEncodingTable = [256]bool{ // ASCII true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, false, // space, ", #, $, %, & false, false, false, true, true, false, false, true, // +, comma, / false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, // :, ;, <, =, >, ? true, false, false, false, false, false, false, false, // @ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, false, // [, \, ], ^ true, false, false, false, false, false, false, false, // ` false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, true, // {, |, }, DEL // non-ASCII true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, } // DataURIEncodingTable is a charmap for which characters need escaping in the Data URI encoding scheme // Escape only non-printable characters, unicode and %, #, &. // IE11 additionally requires encoding of \, [, ], ", <, >, `, {, }, |, ^ which is not required by Chrome, Firefox, Opera, Edge, Safari, Yandex // To pass the HTML validator, restricted URL characters must be escaped: non-printable characters, space, <, >, #, %, " var DataURIEncodingTable = [256]bool{ // ASCII true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, false, true, true, false, // space, ", #, %, & false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, true, false, // <, > false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, false, // [, \, ], ^ true, false, false, false, false, false, false, false, // ` false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, true, // {, |, }, DEL // non-ASCII true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, } // EncodeURL encodes bytes using the URL encoding scheme func EncodeURL(b []byte, table [256]bool) []byte { for i := 0; i < len(b); i++ { c := b[i] if table[c] { b = append(b, 0, 0) copy(b[i+3:], b[i+1:]) b[i+0] = '%' b[i+1] = "0123456789ABCDEF"[c>>4] b[i+2] = "0123456789ABCDEF"[c&15] } } return b } // DecodeURL decodes an URL encoded using the URL encoding scheme func DecodeURL(b []byte) []byte { for i := 0; i < len(b); i++ { if b[i] == '%' && i+2 < len(b) { j := i + 1 c := 0 for ; j < i+3 && (b[j] >= '0' && b[j] <= '9' || b[j] >= 'a' && b[j] <= 'f' || b[j] >= 'A' && b[j] <= 'F'); j++ { if b[j] <= '9' { c = c<<4 + int(b[j]-'0') } else if b[j] <= 'F' { c = c<<4 + int(b[j]-'A') + 10 } else if b[j] <= 'f' { c = c<<4 + int(b[j]-'a') + 10 } } if j == i+3 && c < 128 { b[i] = byte(c) b = append(b[:i+1], b[i+3:]...) } } else if b[i] == '+' { b[i] = ' ' } } return b } parse-2.7.15/common_test.go000066400000000000000000000253141463212301500156000ustar00rootroot00000000000000package parse import ( "encoding/base64" "mime" "net/url" "regexp" "testing" "github.com/tdewolff/test" ) var entitySlices [][]byte var encodedURLSlices [][]byte var urlSlices [][]byte func init() { entitySlices = helperRandStrings(100, 5, []string{""", "'", "'", " ", " ", "test"}) encodedURLSlices = helperRandStrings(100, 5, []string{"%20", "%3D", "test"}) urlSlices = helperRandStrings(100, 5, []string{"~", "\"", "<", "test"}) } func TestParseNumber(t *testing.T) { var numberTests = []struct { number string expected int }{ {"5", 1}, {"0.51", 4}, {"0.5e-99", 7}, {"0.5e-", 3}, {"+50.0", 5}, {".0", 2}, {"0.", 1}, {"", 0}, {"+", 0}, {".", 0}, {"a", 0}, } for _, tt := range numberTests { t.Run(tt.number, func(t *testing.T) { n := Number([]byte(tt.number)) test.T(t, n, tt.expected) }) } } func TestParseDimension(t *testing.T) { var dimensionTests = []struct { dimension string expectedNum int expectedUnit int }{ {"5px", 1, 2}, {"5px ", 1, 2}, {"5%", 1, 1}, {"5em", 1, 2}, {"px", 0, 0}, {"1", 1, 0}, {"1~", 1, 0}, } for _, tt := range dimensionTests { t.Run(tt.dimension, func(t *testing.T) { num, unit := Dimension([]byte(tt.dimension)) test.T(t, num, tt.expectedNum, "number") test.T(t, unit, tt.expectedUnit, "unit") }) } } func TestMediatype(t *testing.T) { var mediatypeTests = []struct { mediatype string expectedMimetype string expectedParams map[string]string }{ {"text/plain", "text/plain", nil}, {"text/plain;charset=US-ASCII", "text/plain", map[string]string{"charset": "US-ASCII"}}, {" text/plain ; charset = US-ASCII ", "text/plain", map[string]string{"charset": "US-ASCII"}}, {" text/plain a", "text/plain", nil}, {"text/plain;base64", "text/plain", map[string]string{"base64": ""}}, {"text/plain;inline=;base64", "text/plain", map[string]string{"inline": "", "base64": ""}}, {"ÿ ", "ÿ ", nil}, // OSS-Fuzz; ÿ is two bytes in UTF8 {"ÿ ;", "ÿ ", map[string]string{"": ""}}, } for _, tt := range mediatypeTests { t.Run(tt.mediatype, func(t *testing.T) { mimetype, params := Mediatype([]byte(tt.mediatype)) test.String(t, string(mimetype), tt.expectedMimetype, "mimetype") test.T(t, params, tt.expectedParams, "parameters") // TODO }) } } func TestParseDataURI(t *testing.T) { var dataURITests = []struct { dataURI string expectedMimetype string expectedData string expectedErr error }{ {"www.domain.com", "", "", ErrBadDataURI}, {"data:,", "text/plain", "", nil}, {"data:text/xml,", "text/xml", "", nil}, {"data:,text", "text/plain", "text", nil}, {"data:;base64,dGV4dA==", "text/plain", "text", nil}, {"data:image/svg+xml,", "image/svg+xml", "", nil}, {"data:;base64,()", "", "", base64.CorruptInputError(0)}, {"data:image/svg+xml,%3Cpath%20stroke-width='9.38%'/%3E", "image/svg+xml", "", nil}, {"data:,%ii", "text/plain", "%ii", nil}, } for _, tt := range dataURITests { t.Run(tt.dataURI, func(t *testing.T) { mimetype, data, err := DataURI([]byte(tt.dataURI)) test.T(t, err, tt.expectedErr) test.String(t, string(mimetype), tt.expectedMimetype, "mimetype") test.String(t, string(data), tt.expectedData, "data") }) } } func TestParseQuoteEntity(t *testing.T) { var quoteEntityTests = []struct { quoteEntity string expectedQuote byte expectedN int }{ {""", '"', 5}, {"'", '\'', 6}, {""", '"', 8}, {"'", '\'', 6}, {""", '"', 6}, {"'", '\'', 6}, {">", 0x00, 0}, {"&", 0x00, 0}, } for _, tt := range quoteEntityTests { t.Run(tt.quoteEntity, func(t *testing.T) { quote, n := QuoteEntity([]byte(tt.quoteEntity)) test.T(t, quote, tt.expectedQuote, "quote") test.T(t, n, tt.expectedN, "quote length") }) } } func TestReplaceMultipleWhitespace(t *testing.T) { test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a")), []byte(" a")) test.Bytes(t, ReplaceMultipleWhitespace([]byte("a ")), []byte("a ")) test.Bytes(t, ReplaceMultipleWhitespace([]byte("a b ")), []byte("a b ")) test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a b ")), []byte(" a b ")) test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a b ")), []byte(" a b ")) test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a b ")), []byte(" a b ")) test.Bytes(t, ReplaceMultipleWhitespace([]byte(" a")), []byte(" a")) test.Bytes(t, ReplaceMultipleWhitespace([]byte("a b")), []byte("a b")) } func TestReplaceMultipleWhitespaceRandom(t *testing.T) { wsRegexp := regexp.MustCompile("[ \t\f]+") wsNewlinesRegexp := regexp.MustCompile("[ ]*[\r\n][ \r\n]*") for _, e := range wsSlices { reference := wsRegexp.ReplaceAll(e, []byte(" ")) reference = wsNewlinesRegexp.ReplaceAll(reference, []byte("\n")) test.Bytes(t, ReplaceMultipleWhitespace(Copy(e)), reference, "in '"+string(e)+"'") } } func TestReplaceEntities(t *testing.T) { entitiesMap := map[string][]byte{ "varphi": []byte("ϕ"), "varpi": []byte("ϖ"), "quot": []byte("\""), "apos": []byte("'"), "amp": []byte("&"), } revEntitiesMap := map[byte][]byte{ '\'': []byte("'"), } var entityTests = []struct { entity string expected string }{ {""", `"`}, {"'", `'`}, {""", `"`}, {"'", `'`}, {" ", ` `}, {""", `"`}, {"'", `'`}, {"⏧", `⏧`}, {"⏧", `⏧`}, {"⏧", `⏧`}, {"⏧", `⏧`}, {"✏", `✏`}, {"✐", `✐`}, {"'"", `'"`}, {""", `"`}, {""", `"`}, {"&apos", `&apos`}, {"&", `&`}, {"'", `'`}, {"&amp;", `&amp;`}, {"&#34;", `&#34;`}, //{"&a mp;", `&a mp;`}, {"&DiacriticalAcute;", `&DiacriticalAcute;`}, {"&CounterClockwiseContourIntegral;", `&CounterClockwiseContourIntegral;`}, //{"&CounterClockwiseContourIntegralL;", `&CounterClockwiseContourIntegralL;`}, {"&parameterize", `&parameterize`}, {"ϕ", "ϕ"}, {"ϖ", "ϖ"}, {"&varnone;", "&varnone;"}, } for _, tt := range entityTests { t.Run(tt.entity, func(t *testing.T) { b := ReplaceEntities([]byte(tt.entity), entitiesMap, revEntitiesMap) test.T(t, string(b), tt.expected, "in '"+tt.entity+"'") }) } } func TestReplaceEntitiesRandom(t *testing.T) { entitiesMap := map[string][]byte{ "quot": []byte("\""), "apos": []byte("'"), } revEntitiesMap := map[byte][]byte{ '\'': []byte("'"), } quotRegexp := regexp.MustCompile(""") aposRegexp := regexp.MustCompile("('|')") for _, e := range entitySlices { reference := quotRegexp.ReplaceAll(e, []byte("\"")) reference = aposRegexp.ReplaceAll(reference, []byte("'")) test.Bytes(t, ReplaceEntities(Copy(e), entitiesMap, revEntitiesMap), reference, "in '"+string(e)+"'") } } func TestReplaceMultipleWhitespaceAndEntities(t *testing.T) { entitiesMap := map[string][]byte{ "varphi": []byte("ϕ"), } var entityTests = []struct { entity string expected string }{ {" ϕ " \n ", " ϕ \"\n"}, } for _, tt := range entityTests { t.Run(tt.entity, func(t *testing.T) { b := ReplaceMultipleWhitespaceAndEntities([]byte(tt.entity), entitiesMap, nil) test.T(t, string(b), tt.expected, "in '"+tt.entity+"'") }) } } func TestReplaceMultipleWhitespaceAndEntitiesRandom(t *testing.T) { entitiesMap := map[string][]byte{ "quot": []byte("\""), "apos": []byte("'"), } revEntitiesMap := map[byte][]byte{ '\'': []byte("'"), } wsRegexp := regexp.MustCompile("[ ]+") quotRegexp := regexp.MustCompile(""") aposRegexp := regexp.MustCompile("('|')") for _, e := range entitySlices { reference := wsRegexp.ReplaceAll(e, []byte(" ")) reference = quotRegexp.ReplaceAll(reference, []byte("\"")) reference = aposRegexp.ReplaceAll(reference, []byte("'")) test.Bytes(t, ReplaceMultipleWhitespaceAndEntities(Copy(e), entitiesMap, revEntitiesMap), reference, "in '"+string(e)+"'") } } func TestDecodeURL(t *testing.T) { var urlTests = []struct { url string expected string }{ {"%20%3F%7E", " ?~"}, {"%80", "%80"}, {"%2B%2b", "++"}, {"%' ", "%' "}, {"a+b", "a b"}, } for _, tt := range urlTests { t.Run(tt.url, func(t *testing.T) { b := DecodeURL([]byte(tt.url)) test.T(t, string(b), tt.expected, "in '"+tt.url+"'") }) } } func TestDecodeURLRandom(t *testing.T) { for _, e := range encodedURLSlices { reference, _ := url.QueryUnescape(string(e)) test.Bytes(t, DecodeURL(Copy(e)), []byte(reference), "in '"+string(e)+"'") } } func TestEncodeURL(t *testing.T) { var urlTests = []struct { url string expected string }{ {"AZaz09-_.!~*'()", "AZaz09-_.!~*'()"}, {"<>", "%3C%3E"}, {"\u2318", "%E2%8C%98"}, {"a b", "a%20b"}, } for _, tt := range urlTests { t.Run(tt.url, func(t *testing.T) { b := EncodeURL([]byte(tt.url), URLEncodingTable) test.T(t, string(b), tt.expected, "in '"+tt.url+"'") }) } } func TestEncodeDataURI(t *testing.T) { var urlTests = []struct { url string expected string }{ {``, `%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3C/svg%3E`}, } for _, tt := range urlTests { t.Run(tt.url, func(t *testing.T) { b := EncodeURL([]byte(tt.url), DataURIEncodingTable) test.T(t, string(b), tt.expected, "in '"+tt.url+"'") }) } } func TestEncodeURLRandom(t *testing.T) { for _, e := range urlSlices { reference := url.QueryEscape(string(e)) test.Bytes(t, EncodeURL(Copy(e), URLEncodingTable), []byte(reference), "in '"+string(e)+"'") } } //////////////////////////////////////////////////////////////// func BenchmarkParseMediatypeStd(b *testing.B) { mediatype := "text/plain" for i := 0; i < b.N; i++ { mime.ParseMediaType(mediatype) } } func BenchmarkParseMediatypeParamStd(b *testing.B) { mediatype := "text/plain;inline=1" for i := 0; i < b.N; i++ { mime.ParseMediaType(mediatype) } } func BenchmarkParseMediatypeParamsStd(b *testing.B) { mediatype := "text/plain;charset=US-ASCII;language=US-EN;compression=gzip;base64" for i := 0; i < b.N; i++ { mime.ParseMediaType(mediatype) } } func BenchmarkParseMediatypeParse(b *testing.B) { mediatype := []byte("text/plain") for i := 0; i < b.N; i++ { Mediatype(mediatype) } } func BenchmarkParseMediatypeParamParse(b *testing.B) { mediatype := []byte("text/plain;inline=1") for i := 0; i < b.N; i++ { Mediatype(mediatype) } } func BenchmarkParseMediatypeParamsParse(b *testing.B) { mediatype := []byte("text/plain;charset=US-ASCII;language=US-EN;compression=gzip;base64") for i := 0; i < b.N; i++ { Mediatype(mediatype) } } func BenchmarkReplaceMultipleWhitespace(b *testing.B) { for i := 0; i < b.N; i++ { for _, e := range wsSlices { ReplaceMultipleWhitespace(e) } } } parse-2.7.15/css/000077500000000000000000000000001463212301500135055ustar00rootroot00000000000000parse-2.7.15/css/README.md000066400000000000000000000074061463212301500147730ustar00rootroot00000000000000# CSS [![API reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/tdewolff/parse/v2/css?tab=doc) This package is a CSS3 lexer and parser written in [Go][1]. Both follow the specification at [CSS Syntax Module Level 3](http://www.w3.org/TR/css-syntax-3/). The lexer takes an io.Reader and converts it into tokens until the EOF. The parser returns a parse tree of the full io.Reader input stream, but the low-level `Next` function can be used for stream parsing to returns grammar units until the EOF. ## Installation Run the following command go get -u github.com/tdewolff/parse/v2/css or add the following import and run project with `go get` import "github.com/tdewolff/parse/v2/css" ## Lexer ### Usage The following initializes a new Lexer with io.Reader `r`: ``` go l := css.NewLexer(parse.NewInput(r)) ``` To tokenize until EOF an error, use: ``` go for { tt, text := l.Next() switch tt { case css.ErrorToken: // error or EOF set in l.Err() return // ... } } ``` All tokens (see [CSS Syntax Module Level 3](http://www.w3.org/TR/css3-syntax/)): ``` go ErrorToken // non-official token, returned when errors occur IdentToken FunctionToken // rgb( rgba( ... AtKeywordToken // @abc HashToken // #abc StringToken BadStringToken URLToken // url( BadURLToken DelimToken // any unmatched character NumberToken // 5 PercentageToken // 5% DimensionToken // 5em UnicodeRangeToken IncludeMatchToken // ~= DashMatchToken // |= PrefixMatchToken // ^= SuffixMatchToken // $= SubstringMatchToken // *= ColumnToken // || WhitespaceToken CDOToken // ColonToken SemicolonToken CommaToken BracketToken // ( ) [ ] { }, all bracket tokens use this, Data() can distinguish between the brackets CommentToken // non-official token ``` ### Examples ``` go package main import ( "os" "github.com/tdewolff/parse/v2/css" ) // Tokenize CSS3 from stdin. func main() { l := css.NewLexer(parse.NewInput(os.Stdin)) for { tt, text := l.Next() switch tt { case css.ErrorToken: if l.Err() != io.EOF { fmt.Println("Error on line", l.Line(), ":", l.Err()) } return case css.IdentToken: fmt.Println("Identifier", string(text)) case css.NumberToken: fmt.Println("Number", string(text)) // ... } } } ``` ## Parser ### Usage The following creates a new Parser. ``` go // true because this is the content of an inline style attribute p := css.NewParser(parse.NewInput(bytes.NewBufferString("color: red;")), true) ``` To iterate over the stylesheet, use: ``` go for { gt, _, data := p.Next() if gt == css.ErrorGrammar { break } // ... } ``` All grammar units returned by `Next`: ``` go ErrorGrammar AtRuleGrammar EndAtRuleGrammar RulesetGrammar EndRulesetGrammar DeclarationGrammar TokenGrammar ``` ### Examples ``` go package main import ( "bytes" "fmt" "github.com/tdewolff/parse/v2/css" ) func main() { // true because this is the content of an inline style attribute p := css.NewParser(parse.NewInput(bytes.NewBufferString("color: red;")), true) out := "" for { gt, _, data := p.Next() if gt == css.ErrorGrammar { break } else if gt == css.AtRuleGrammar || gt == css.BeginAtRuleGrammar || gt == css.BeginRulesetGrammar || gt == css.DeclarationGrammar { out += string(data) if gt == css.DeclarationGrammar { out += ":" } for _, val := range p.Values() { out += string(val.Data) } if gt == css.BeginAtRuleGrammar || gt == css.BeginRulesetGrammar { out += "{" } else if gt == css.AtRuleGrammar || gt == css.DeclarationGrammar { out += ";" } } else { out += string(data) } } fmt.Println(out) } ``` ## License Released under the [MIT license](https://github.com/tdewolff/parse/blob/master/LICENSE.md). [1]: http://golang.org/ "Go Language" parse-2.7.15/css/hash.go000066400000000000000000000034451463212301500147650ustar00rootroot00000000000000package css // generated by hasher -type=Hash -file=hash.go; DO NOT EDIT, except for adding more constants to the list and rerun go generate // uses github.com/tdewolff/hasher //go:generate hasher -type=Hash -file=hash.go // Hash defines perfect hashes for a predefined list of strings type Hash uint32 // Unique hash definitions to be used instead of strings const ( Document Hash = 0x8 // document Font_Face Hash = 0x809 // font-face Keyframes Hash = 0x1109 // keyframes Media Hash = 0x2105 // media Page Hash = 0x2604 // page Supports Hash = 0x1908 // supports ) // String returns the hash' name. func (i Hash) String() string { start := uint32(i >> 8) n := uint32(i & 0xff) if start+n > uint32(len(_Hash_text)) { return "" } return _Hash_text[start : start+n] } // ToHash returns the hash whose name is s. It returns zero if there is no // such hash. It is case sensitive. func ToHash(s []byte) Hash { if len(s) == 0 || len(s) > _Hash_maxLen { return 0 } h := uint32(_Hash_hash0) for i := 0; i < len(s); i++ { h ^= uint32(s[i]) h *= 16777619 } if i := _Hash_table[h&uint32(len(_Hash_table)-1)]; int(i&0xff) == len(s) { t := _Hash_text[i>>8 : i>>8+i&0xff] for i := 0; i < len(s); i++ { if t[i] != s[i] { goto NEXT } } return i } NEXT: if i := _Hash_table[(h>>16)&uint32(len(_Hash_table)-1)]; int(i&0xff) == len(s) { t := _Hash_text[i>>8 : i>>8+i&0xff] for i := 0; i < len(s); i++ { if t[i] != s[i] { return 0 } } return i } return 0 } const _Hash_hash0 = 0x9acb0442 const _Hash_maxLen = 9 const _Hash_text = "documentfont-facekeyframesupportsmediapage" var _Hash_table = [1 << 3]Hash{ 0x1: 0x2604, // page 0x2: 0x2105, // media 0x3: 0x809, // font-face 0x5: 0x1109, // keyframes 0x6: 0x1908, // supports 0x7: 0x8, // document } parse-2.7.15/css/lex.go000066400000000000000000000347751463212301500146440ustar00rootroot00000000000000// Package css is a CSS3 lexer and parser following the specifications at http://www.w3.org/TR/css-syntax-3/. package css // TODO: \uFFFD replacement character for NULL bytes in strings for example, or atleast don't end the string early import ( "bytes" "io" "strconv" "github.com/tdewolff/parse/v2" ) // TokenType determines the type of token, eg. a number or a semicolon. type TokenType uint32 // TokenType values. const ( ErrorToken TokenType = iota // extra token when errors occur IdentToken FunctionToken // rgb( rgba( ... AtKeywordToken // @abc HashToken // #abc StringToken BadStringToken URLToken BadURLToken DelimToken // any unmatched character NumberToken // 5 PercentageToken // 5% DimensionToken // 5em UnicodeRangeToken // U+554A IncludeMatchToken // ~= DashMatchToken // |= PrefixMatchToken // ^= SuffixMatchToken // $= SubstringMatchToken // *= ColumnToken // || WhitespaceToken // space \t \r \n \f CDOToken // ColonToken // : SemicolonToken // ; CommaToken // , LeftBracketToken // [ RightBracketToken // ] LeftParenthesisToken // ( RightParenthesisToken // ) LeftBraceToken // { RightBraceToken // } CommentToken // extra token for comments EmptyToken CustomPropertyNameToken CustomPropertyValueToken ) // String returns the string representation of a TokenType. func (tt TokenType) String() string { switch tt { case ErrorToken: return "Error" case IdentToken: return "Ident" case FunctionToken: return "Function" case AtKeywordToken: return "AtKeyword" case HashToken: return "Hash" case StringToken: return "String" case BadStringToken: return "BadString" case URLToken: return "URL" case BadURLToken: return "BadURL" case DelimToken: return "Delim" case NumberToken: return "Number" case PercentageToken: return "Percentage" case DimensionToken: return "Dimension" case UnicodeRangeToken: return "UnicodeRange" case IncludeMatchToken: return "IncludeMatch" case DashMatchToken: return "DashMatch" case PrefixMatchToken: return "PrefixMatch" case SuffixMatchToken: return "SuffixMatch" case SubstringMatchToken: return "SubstringMatch" case ColumnToken: return "Column" case WhitespaceToken: return "Whitespace" case CDOToken: return "CDO" case CDCToken: return "CDC" case ColonToken: return "Colon" case SemicolonToken: return "Semicolon" case CommaToken: return "Comma" case LeftBracketToken: return "LeftBracket" case RightBracketToken: return "RightBracket" case LeftParenthesisToken: return "LeftParenthesis" case RightParenthesisToken: return "RightParenthesis" case LeftBraceToken: return "LeftBrace" case RightBraceToken: return "RightBrace" case CommentToken: return "Comment" case EmptyToken: return "Empty" case CustomPropertyNameToken: return "CustomPropertyName" case CustomPropertyValueToken: return "CustomPropertyValue" } return "Invalid(" + strconv.Itoa(int(tt)) + ")" } //////////////////////////////////////////////////////////////// // Lexer is the state for the lexer. type Lexer struct { r *parse.Input } // NewLexer returns a new Lexer for a given io.Reader. func NewLexer(r *parse.Input) *Lexer { return &Lexer{ r: r, } } // Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned. func (l *Lexer) Err() error { return l.r.Err() } // Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message. func (l *Lexer) Next() (TokenType, []byte) { switch l.r.Peek(0) { case ' ', '\t', '\n', '\r', '\f': l.r.Move(1) for l.consumeWhitespace() { } return WhitespaceToken, l.r.Shift() case ':': l.r.Move(1) return ColonToken, l.r.Shift() case ';': l.r.Move(1) return SemicolonToken, l.r.Shift() case ',': l.r.Move(1) return CommaToken, l.r.Shift() case '(', ')', '[', ']', '{', '}': if t := l.consumeBracket(); t != ErrorToken { return t, l.r.Shift() } case '#': if l.consumeHashToken() { return HashToken, l.r.Shift() } case '"', '\'': if t := l.consumeString(); t != ErrorToken { return t, l.r.Shift() } case '.', '+': if t := l.consumeNumeric(); t != ErrorToken { return t, l.r.Shift() } case '-': if t := l.consumeNumeric(); t != ErrorToken { return t, l.r.Shift() } else if t := l.consumeIdentlike(); t != ErrorToken { return t, l.r.Shift() } else if l.consumeCDCToken() { return CDCToken, l.r.Shift() } else if l.consumeCustomVariableToken() { return CustomPropertyNameToken, l.r.Shift() } case '@': if l.consumeAtKeywordToken() { return AtKeywordToken, l.r.Shift() } case '$', '*', '^', '~': if t := l.consumeMatch(); t != ErrorToken { return t, l.r.Shift() } case '/': if l.consumeComment() { return CommentToken, l.r.Shift() } case '<': if l.consumeCDOToken() { return CDOToken, l.r.Shift() } case '\\': if t := l.consumeIdentlike(); t != ErrorToken { return t, l.r.Shift() } case 'u', 'U': if l.consumeUnicodeRangeToken() { return UnicodeRangeToken, l.r.Shift() } else if t := l.consumeIdentlike(); t != ErrorToken { return t, l.r.Shift() } case '|': if t := l.consumeMatch(); t != ErrorToken { return t, l.r.Shift() } else if l.consumeColumnToken() { return ColumnToken, l.r.Shift() } case 0: if l.r.Err() != nil { return ErrorToken, nil } default: if t := l.consumeNumeric(); t != ErrorToken { return t, l.r.Shift() } else if t := l.consumeIdentlike(); t != ErrorToken { return t, l.r.Shift() } } // can't be rune because consumeIdentlike consumes that as an identifier l.r.Move(1) return DelimToken, l.r.Shift() } //////////////////////////////////////////////////////////////// /* The following functions follow the railroad diagrams in http://www.w3.org/TR/css3-syntax/ */ func (l *Lexer) consumeByte(c byte) bool { if l.r.Peek(0) == c { l.r.Move(1) return true } return false } func (l *Lexer) consumeComment() bool { if l.r.Peek(0) != '/' || l.r.Peek(1) != '*' { return false } l.r.Move(2) for { c := l.r.Peek(0) if c == 0 && l.r.Err() != nil { break } else if c == '*' && l.r.Peek(1) == '/' { l.r.Move(2) return true } l.r.Move(1) } return true } func (l *Lexer) consumeNewline() bool { c := l.r.Peek(0) if c == '\n' || c == '\f' { l.r.Move(1) return true } else if c == '\r' { if l.r.Peek(1) == '\n' { l.r.Move(2) } else { l.r.Move(1) } return true } return false } func (l *Lexer) consumeWhitespace() bool { c := l.r.Peek(0) if c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' { l.r.Move(1) return true } return false } func (l *Lexer) consumeDigit() bool { c := l.r.Peek(0) if c >= '0' && c <= '9' { l.r.Move(1) return true } return false } func (l *Lexer) consumeHexDigit() bool { c := l.r.Peek(0) if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') { l.r.Move(1) return true } return false } func (l *Lexer) consumeEscape() bool { if l.r.Peek(0) != '\\' { return false } mark := l.r.Pos() l.r.Move(1) if l.consumeNewline() { l.r.Rewind(mark) return false } else if l.consumeHexDigit() { for k := 1; k < 6; k++ { if !l.consumeHexDigit() { break } } l.consumeWhitespace() return true } else { c := l.r.Peek(0) if c >= 0xC0 { _, n := l.r.PeekRune(0) l.r.Move(n) return true } else if c == 0 && l.r.Err() != nil { l.r.Rewind(mark) return false } } l.r.Move(1) return true } func (l *Lexer) consumeIdentToken() bool { mark := l.r.Pos() if l.r.Peek(0) == '-' { l.r.Move(1) } c := l.r.Peek(0) if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c >= 0x80) { if c != '\\' || !l.consumeEscape() { l.r.Rewind(mark) return false } } else { l.r.Move(1) } for { c := l.r.Peek(0) if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c >= 0x80) { if c != '\\' || !l.consumeEscape() { break } } else { l.r.Move(1) } } return true } // support custom variables, https://www.w3.org/TR/css-variables-1/ func (l *Lexer) consumeCustomVariableToken() bool { // expect to be on a '-' l.r.Move(1) if l.r.Peek(0) != '-' { l.r.Move(-1) return false } if !l.consumeIdentToken() { l.r.Move(-1) return false } return true } func (l *Lexer) consumeAtKeywordToken() bool { // expect to be on an '@' l.r.Move(1) if !l.consumeIdentToken() { l.r.Move(-1) return false } return true } func (l *Lexer) consumeHashToken() bool { // expect to be on a '#' mark := l.r.Pos() l.r.Move(1) c := l.r.Peek(0) if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c >= 0x80) { if c != '\\' || !l.consumeEscape() { l.r.Rewind(mark) return false } } else { l.r.Move(1) } for { c := l.r.Peek(0) if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c >= 0x80) { if c != '\\' || !l.consumeEscape() { break } } else { l.r.Move(1) } } return true } func (l *Lexer) consumeNumberToken() bool { mark := l.r.Pos() c := l.r.Peek(0) if c == '+' || c == '-' { l.r.Move(1) } firstDigit := l.consumeDigit() if firstDigit { for l.consumeDigit() { } } if l.r.Peek(0) == '.' { l.r.Move(1) if l.consumeDigit() { for l.consumeDigit() { } } else if firstDigit { // . could belong to the next token l.r.Move(-1) return true } else { l.r.Rewind(mark) return false } } else if !firstDigit { l.r.Rewind(mark) return false } mark = l.r.Pos() c = l.r.Peek(0) if c == 'e' || c == 'E' { l.r.Move(1) c = l.r.Peek(0) if c == '+' || c == '-' { l.r.Move(1) } if !l.consumeDigit() { // e could belong to next token l.r.Rewind(mark) return true } for l.consumeDigit() { } } return true } func (l *Lexer) consumeUnicodeRangeToken() bool { c := l.r.Peek(0) if (c != 'u' && c != 'U') || l.r.Peek(1) != '+' { return false } mark := l.r.Pos() l.r.Move(2) // consume up to 6 hexDigits k := 0 for l.consumeHexDigit() { k++ } // either a minus or a question mark or the end is expected if l.consumeByte('-') { if k == 0 || 6 < k { l.r.Rewind(mark) return false } // consume another up to 6 hexDigits if l.consumeHexDigit() { k = 1 for l.consumeHexDigit() { k++ } } else { l.r.Rewind(mark) return false } } else if l.consumeByte('?') { // could be filled up to 6 characters with question marks or else regular hexDigits k++ for l.consumeByte('?') { k++ } } if k == 0 || 6 < k { l.r.Rewind(mark) return false } return true } func (l *Lexer) consumeColumnToken() bool { if l.r.Peek(0) == '|' && l.r.Peek(1) == '|' { l.r.Move(2) return true } return false } func (l *Lexer) consumeCDOToken() bool { if l.r.Peek(0) == '<' && l.r.Peek(1) == '!' && l.r.Peek(2) == '-' && l.r.Peek(3) == '-' { l.r.Move(4) return true } return false } func (l *Lexer) consumeCDCToken() bool { if l.r.Peek(0) == '-' && l.r.Peek(1) == '-' && l.r.Peek(2) == '>' { l.r.Move(3) return true } return false } //////////////////////////////////////////////////////////////// // consumeMatch consumes any MatchToken. func (l *Lexer) consumeMatch() TokenType { if l.r.Peek(1) == '=' { switch l.r.Peek(0) { case '~': l.r.Move(2) return IncludeMatchToken case '|': l.r.Move(2) return DashMatchToken case '^': l.r.Move(2) return PrefixMatchToken case '$': l.r.Move(2) return SuffixMatchToken case '*': l.r.Move(2) return SubstringMatchToken } } return ErrorToken } // consumeBracket consumes any bracket token. func (l *Lexer) consumeBracket() TokenType { switch l.r.Peek(0) { case '(': l.r.Move(1) return LeftParenthesisToken case ')': l.r.Move(1) return RightParenthesisToken case '[': l.r.Move(1) return LeftBracketToken case ']': l.r.Move(1) return RightBracketToken case '{': l.r.Move(1) return LeftBraceToken case '}': l.r.Move(1) return RightBraceToken } return ErrorToken } // consumeNumeric consumes NumberToken, PercentageToken or DimensionToken. func (l *Lexer) consumeNumeric() TokenType { if l.consumeNumberToken() { if l.consumeByte('%') { return PercentageToken } else if l.consumeIdentToken() { return DimensionToken } return NumberToken } return ErrorToken } // consumeString consumes a string and may return BadStringToken when a newline is encountered. func (l *Lexer) consumeString() TokenType { // assume to be on " or ' delim := l.r.Peek(0) l.r.Move(1) for { c := l.r.Peek(0) if c == 0 && l.r.Err() != nil { break } else if c == '\n' || c == '\r' || c == '\f' { l.r.Move(1) return BadStringToken } else if c == delim { l.r.Move(1) break } else if c == '\\' { if !l.consumeEscape() { // either newline or EOF after backslash l.r.Move(1) l.consumeNewline() } } else { l.r.Move(1) } } return StringToken } func (l *Lexer) consumeUnquotedURL() bool { for { c := l.r.Peek(0) if c == 0 && l.r.Err() != nil || c == ')' { break } else if c == '"' || c == '\'' || c == '(' || c == '\\' || c == ' ' || c <= 0x1F || c == 0x7F { if c != '\\' || !l.consumeEscape() { return false } } else { l.r.Move(1) } } return true } // consumeRemnantsBadUrl consumes bytes of a BadUrlToken so that normal tokenization may continue. func (l *Lexer) consumeRemnantsBadURL() { for { if l.consumeByte(')') || l.r.Err() != nil { break } else if !l.consumeEscape() { l.r.Move(1) } } } // consumeIdentlike consumes IdentToken, FunctionToken or UrlToken. func (l *Lexer) consumeIdentlike() TokenType { if l.consumeIdentToken() { if l.r.Peek(0) != '(' { return IdentToken } else if !parse.EqualFold(bytes.Replace(l.r.Lexeme(), []byte{'\\'}, nil, -1), []byte{'u', 'r', 'l'}) { l.r.Move(1) return FunctionToken } l.r.Move(1) // consume url for l.consumeWhitespace() { } if c := l.r.Peek(0); c == '"' || c == '\'' { if l.consumeString() == BadStringToken { l.consumeRemnantsBadURL() return BadURLToken } } else if !l.consumeUnquotedURL() && !l.consumeWhitespace() { // if unquoted URL fails due to encountering whitespace, continue l.consumeRemnantsBadURL() return BadURLToken } for l.consumeWhitespace() { } if !l.consumeByte(')') && l.r.Err() != io.EOF { l.consumeRemnantsBadURL() return BadURLToken } return URLToken } return ErrorToken } parse-2.7.15/css/lex_test.go000066400000000000000000000205741463212301500156730ustar00rootroot00000000000000package css import ( "fmt" "io" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) type TTs []TokenType func TestTokens(t *testing.T) { var tokenTests = []struct { css string ttypes []TokenType lexemes []string }{ {" ", TTs{}, []string{}}, {"5.2 .4", TTs{NumberToken, NumberToken}, []string{"5.2", ".4"}}, {"color: red;", TTs{IdentToken, ColonToken, IdentToken, SemicolonToken}, []string{"color", ":", "red", ";"}}, {"background: url(\"http://x\");", TTs{IdentToken, ColonToken, URLToken, SemicolonToken}, []string{"background", ":", `url("http://x")`, ";"}}, {"background: URL(x.png);", TTs{IdentToken, ColonToken, URLToken, SemicolonToken}, []string{"background", ":", "URL(x.png)", ";"}}, {"color: rgb(4, 0%, 5em);", TTs{IdentToken, ColonToken, FunctionToken, NumberToken, CommaToken, PercentageToken, CommaToken, DimensionToken, RightParenthesisToken, SemicolonToken}, []string{"color", ":", "rgb(", "4", ",", "0%", ",", "5em", ")", ";"}}, {"body { \"string\" }", TTs{IdentToken, LeftBraceToken, StringToken, RightBraceToken}, []string{"body", "{", `"string"`, "}"}}, {"body { \"str\\\"ing\" }", TTs{IdentToken, LeftBraceToken, StringToken, RightBraceToken}, []string{"body", "{", `"str\"ing"`, "}"}}, {".class { }", TTs{DelimToken, IdentToken, LeftBraceToken, RightBraceToken}, []string{".", "class", "{", "}"}}, {"#class { }", TTs{HashToken, LeftBraceToken, RightBraceToken}, []string{"#class", "{", "}"}}, {"#class\\#withhash { }", TTs{HashToken, LeftBraceToken, RightBraceToken}, []string{`#class\#withhash`, "{", "}"}}, {"@media print { }", TTs{AtKeywordToken, IdentToken, LeftBraceToken, RightBraceToken}, []string{"@media", "print", "{", "}"}}, {"/*comment*/", TTs{CommentToken}, []string{"/*comment*/"}}, {"/*com* /ment*/", TTs{CommentToken}, []string{"/*com* /ment*/"}}, {"~= |= ^= $= *=", TTs{IncludeMatchToken, DashMatchToken, PrefixMatchToken, SuffixMatchToken, SubstringMatchToken}, []string{"~=", "|=", "^=", "$=", "*="}}, {"||", TTs{ColumnToken}, []string{"||"}}, {"", TTs{CDOToken, CDCToken}, []string{""}}, {"U+1234", TTs{UnicodeRangeToken}, []string{"U+1234"}}, {"5.2 .4 4e-22", TTs{NumberToken, NumberToken, NumberToken}, []string{"5.2", ".4", "4e-22"}}, {"--custom-variable", TTs{CustomPropertyNameToken}, []string{"--custom-variable"}}, // unexpected ending {"ident", TTs{IdentToken}, []string{"ident"}}, {"123.", TTs{NumberToken, DelimToken}, []string{"123", "."}}, {"\"string", TTs{StringToken}, []string{`"string`}}, {"123/*comment", TTs{NumberToken, CommentToken}, []string{"123", "/*comment"}}, {"U+1-", TTs{IdentToken, NumberToken, DelimToken}, []string{"U", "+1", "-"}}, // unicode {"fooδbar􀀀", TTs{IdentToken}, []string{"fooδbar􀀀"}}, {"foo\\æ\\†", TTs{IdentToken}, []string{"foo\\æ\\†"}}, {"'foo\u554abar'", TTs{StringToken}, []string{"'foo\u554abar'"}}, {"\\000026B", TTs{IdentToken}, []string{"\\000026B"}}, {"\\26 B", TTs{IdentToken}, []string{"\\26 B"}}, // hacks {`\-\mo\z\-b\i\nd\in\g:\url(//business\i\nfo.co.uk\/labs\/xbl\/xbl\.xml\#xss);`, TTs{IdentToken, ColonToken, URLToken, SemicolonToken}, []string{`\-\mo\z\-b\i\nd\in\g`, ":", `\url(//business\i\nfo.co.uk\/labs\/xbl\/xbl\.xml\#xss)`, ";"}}, {"width/**/:/**/ 40em;", TTs{IdentToken, CommentToken, ColonToken, CommentToken, DimensionToken, SemicolonToken}, []string{"width", "/**/", ":", "/**/", "40em", ";"}}, {":root *> #quince", TTs{ColonToken, IdentToken, DelimToken, DelimToken, HashToken}, []string{":", "root", "*", ">", "#quince"}}, {"html[xmlns*=\"\"]:root", TTs{IdentToken, LeftBracketToken, IdentToken, SubstringMatchToken, StringToken, RightBracketToken, ColonToken, IdentToken}, []string{"html", "[", "xmlns", "*=", `""`, "]", ":", "root"}}, {"body:nth-of-type(1)", TTs{IdentToken, ColonToken, FunctionToken, NumberToken, RightParenthesisToken}, []string{"body", ":", "nth-of-type(", "1", ")"}}, {"color/*\\**/: blue\\9;", TTs{IdentToken, CommentToken, ColonToken, IdentToken, SemicolonToken}, []string{"color", `/*\**/`, ":", `blue\9`, ";"}}, {"color: blue !ie;", TTs{IdentToken, ColonToken, IdentToken, DelimToken, IdentToken, SemicolonToken}, []string{"color", ":", "blue", "!", "ie", ";"}}, // escapes, null and replacement character {"c\\\x00olor: white;", TTs{IdentToken, ColonToken, IdentToken, SemicolonToken}, []string{"c\\\x00olor", ":", "white", ";"}}, {"null\\0", TTs{IdentToken}, []string{`null\0`}}, {"\\", TTs{DelimToken}, []string{"\\"}}, {"abc\\", TTs{IdentToken, DelimToken}, []string{"abc", "\\"}}, {"#\\", TTs{DelimToken, DelimToken}, []string{"#", "\\"}}, {"#abc\\", TTs{HashToken, DelimToken}, []string{"#abc", "\\"}}, {"\"abc\\", TTs{StringToken}, []string{"\"abc\\"}}, // should officially not include backslash, but no biggie {"url(abc\\", TTs{BadURLToken}, []string{"url(abc\\"}}, {"\"a\x00b\"", TTs{StringToken}, []string{"\"a\x00b\""}}, {"a\\\x00b", TTs{IdentToken}, []string{"a\\\x00b"}}, {"url(a\x00b)", TTs{BadURLToken}, []string{"url(a\x00b)"}}, // null character cannot be unquoted {"/*a\x00b*/", TTs{CommentToken}, []string{"/*a\x00b*/"}}, // coverage {" \n\r\n\r\"\\\r\n\\\r\"", TTs{StringToken}, []string{"\"\\\r\n\\\r\""}}, {"U+?????? U+ABCD?? U+ABC-DEF", TTs{UnicodeRangeToken, UnicodeRangeToken, UnicodeRangeToken}, []string{"U+??????", "U+ABCD??", "U+ABC-DEF"}}, {"U+? U+A?", TTs{UnicodeRangeToken, UnicodeRangeToken}, []string{"U+?", "U+A?"}}, {"U+ U+ABCDEF?", TTs{IdentToken, DelimToken, IdentToken, DelimToken, IdentToken, DelimToken}, []string{"U", "+", "U", "+", "ABCDEF", "?"}}, {"-5.23 -moz", TTs{NumberToken, IdentToken}, []string{"-5.23", "-moz"}}, {"()", TTs{LeftParenthesisToken, RightParenthesisToken}, []string{"(", ")"}}, {"url( //url\n )", TTs{URLToken}, []string{"url( //url\n )"}}, {"url( ", TTs{URLToken}, []string{"url( "}}, {"url( //url ", TTs{URLToken}, []string{"url( //url "}}, {"url(\")a", TTs{URLToken}, []string{"url(\")a"}}, {"url(a'\\\n)a", TTs{BadURLToken, IdentToken}, []string{"url(a'\\\n)", "a"}}, {"url(\"\n)a", TTs{BadURLToken, IdentToken}, []string{"url(\"\n)", "a"}}, {"url(a h)a", TTs{BadURLToken, IdentToken}, []string{"url(a h)", "a"}}, {" 0 && atRuleName[1] == '-' { if i := bytes.IndexByte(atRuleName[2:], '-'); i != -1 { atRuleName = atRuleName[i+2:] // skip vendor specific prefix } } atRule := ToHash(atRuleName[1:]) first := true skipWS := false for { tt, data := p.popToken(false) if tt == LeftBraceToken && p.level == 0 { if atRule == Font_Face || atRule == Page { p.state = append(p.state, (*Parser).parseAtRuleDeclarationList) } else if atRule == Document || atRule == Keyframes || atRule == Media || atRule == Supports { p.state = append(p.state, (*Parser).parseAtRuleRuleList) } else { p.state = append(p.state, (*Parser).parseAtRuleUnknown) } return BeginAtRuleGrammar } else if (tt == SemicolonToken || tt == RightBraceToken) && p.level == 0 || tt == ErrorToken { p.prevEnd = (tt == RightBraceToken) return AtRuleGrammar } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken { p.level++ } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken { if p.level == 0 { // TODO: buggy p.pushBuf(tt, data) if 1 < len(p.state) { p.state = p.state[:len(p.state)-1] } p.err, p.errPos = "unexpected ending in at rule", p.l.r.Offset() return ErrorGrammar } p.level-- } if first { if tt == LeftParenthesisToken || tt == LeftBracketToken { p.prevWS = false } first = false } if len(data) == 1 && (data[0] == ',' || data[0] == ':') { skipWS = true } else if p.prevWS && !skipWS && tt != RightParenthesisToken { p.pushBuf(WhitespaceToken, wsBytes) } else { skipWS = false } if tt == LeftParenthesisToken { skipWS = true } p.pushBuf(tt, data) } } func (p *Parser) parseAtRuleRuleList() GrammarType { if p.tt == RightBraceToken || p.tt == ErrorToken { p.state = p.state[:len(p.state)-1] return EndAtRuleGrammar } else if p.tt == AtKeywordToken { return p.parseAtRule() } else { return p.parseQualifiedRule() } } func (p *Parser) parseAtRuleDeclarationList() GrammarType { for p.tt == SemicolonToken { p.tt, p.data = p.popToken(false) } if p.tt == RightBraceToken || p.tt == ErrorToken { p.state = p.state[:len(p.state)-1] return EndAtRuleGrammar } return p.parseDeclarationList() } func (p *Parser) parseAtRuleUnknown() GrammarType { p.keepWS = true if p.tt == RightBraceToken && p.level == 0 || p.tt == ErrorToken { p.state = p.state[:len(p.state)-1] p.keepWS = false return EndAtRuleGrammar } if p.tt == LeftParenthesisToken || p.tt == LeftBraceToken || p.tt == LeftBracketToken || p.tt == FunctionToken { p.level++ } else if p.tt == RightParenthesisToken || p.tt == RightBraceToken || p.tt == RightBracketToken { p.level-- } return TokenGrammar } func (p *Parser) parseQualifiedRule() GrammarType { p.initBuf() first := true inAttrSel := false skipWS := true var tt TokenType var data []byte for { if first { tt, data = p.tt, p.data p.tt = WhitespaceToken p.data = emptyBytes first = false } else { tt, data = p.popToken(false) } if tt == LeftBraceToken && p.level == 0 { p.state = append(p.state, (*Parser).parseQualifiedRuleDeclarationList) return BeginRulesetGrammar } else if tt == ErrorToken { p.err, p.errPos = "unexpected ending in qualified rule", p.l.r.Offset() return ErrorGrammar } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken { p.level++ } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken { if p.level == 0 { // TODO: buggy p.pushBuf(tt, data) if 1 < len(p.state) { p.state = p.state[:len(p.state)-1] } p.err, p.errPos = "unexpected ending in qualified rule", p.l.r.Offset() return ErrorGrammar } p.level-- } if len(data) == 1 && (data[0] == ',' || data[0] == '>' || data[0] == '+' || data[0] == '~') { if data[0] == ',' { return QualifiedRuleGrammar } skipWS = true } else if p.prevWS && !skipWS && !inAttrSel { p.pushBuf(WhitespaceToken, wsBytes) } else { skipWS = false } if tt == LeftBracketToken { inAttrSel = true } else if tt == RightBracketToken { inAttrSel = false } p.pushBuf(tt, data) } } func (p *Parser) parseQualifiedRuleDeclarationList() GrammarType { for p.tt == SemicolonToken { p.tt, p.data = p.popToken(false) } if p.tt == RightBraceToken || p.tt == ErrorToken { p.state = p.state[:len(p.state)-1] return EndRulesetGrammar } return p.parseDeclarationList() } func (p *Parser) parseDeclaration() GrammarType { p.initBuf() p.data = parse.ToLower(parse.Copy(p.data)) ttName, dataName := p.tt, p.data tt, data := p.popToken(false) if tt != ColonToken { p.l.r.Move(-len(data)) p.err, p.errPos = "expected colon in declaration", p.l.r.Offset() p.l.r.Move(len(data)) p.pushBuf(ttName, dataName) return p.parseDeclarationError(tt, data) } skipWS := true for { tt, data := p.popToken(false) if (tt == SemicolonToken || tt == RightBraceToken) && p.level == 0 || tt == ErrorToken { p.prevEnd = (tt == RightBraceToken) return DeclarationGrammar } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken { p.level++ } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken { if p.level == 0 { // TODO: buggy p.err, p.errPos = "unexpected ending in declaration", p.l.r.Offset() p.pushBuf(ttName, dataName) p.pushBuf(ColonToken, []byte{':'}) return p.parseDeclarationError(tt, data) } p.level-- } if len(data) == 1 && (data[0] == ',' || data[0] == '/' || data[0] == ':' || data[0] == '!' || data[0] == '=') { skipWS = true } else if (p.prevWS || p.prevComment) && !skipWS { p.pushBuf(WhitespaceToken, wsBytes) } else { skipWS = false } p.pushBuf(tt, data) } } func (p *Parser) parseDeclarationError(tt TokenType, data []byte) GrammarType { // we're on the offending (tt,data), keep popping tokens till we reach ;, }, or EOF p.tt, p.data = tt, data for { if (tt == SemicolonToken || tt == RightBraceToken) && p.level == 0 || tt == ErrorToken { p.prevEnd = (tt == RightBraceToken) if tt == SemicolonToken { p.pushBuf(tt, data) } return ErrorGrammar } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken { p.level++ } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken { p.level-- } if p.prevWS { p.pushBuf(WhitespaceToken, wsBytes) } p.pushBuf(tt, data) tt, data = p.popToken(false) } } func (p *Parser) parseCustomProperty() GrammarType { p.initBuf() if tt, data := p.popToken(false); tt != ColonToken { p.l.r.Move(-len(data)) p.err, p.errPos = "expected colon in custom property", p.l.r.Offset() p.l.r.Move(len(data)) return ErrorGrammar } val := []byte{} for { tt, data := p.l.Next() if (tt == SemicolonToken || tt == RightBraceToken) && p.level == 0 || tt == ErrorToken { p.prevEnd = (tt == RightBraceToken) p.pushBuf(CustomPropertyValueToken, val) return CustomPropertyGrammar } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken { p.level++ } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken { if p.level == 0 { // TODO: buggy p.pushBuf(tt, data) p.err, p.errPos = "unexpected ending in custom property", p.l.r.Offset() return ErrorGrammar } p.level-- } val = append(val, data...) } } parse-2.7.15/css/parse_test.go000066400000000000000000000227031463212301500162110ustar00rootroot00000000000000package css import ( "fmt" "io" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) //////////////////////////////////////////////////////////////// func TestParse(t *testing.T) { var parseTests = []struct { inline bool css string expected string }{ {true, " x : y ; ", "x:y;"}, {true, "color: red;", "color:red;"}, {true, "color : red;", "color:red;"}, {true, "color: red; border: 0;", "color:red;border:0;"}, {true, "color: red !important;", "color:red!important;"}, {true, "color: red ! important;", "color:red!important;"}, {true, "white-space: -moz-pre-wrap;", "white-space:-moz-pre-wrap;"}, {true, "display: -moz-inline-stack;", "display:-moz-inline-stack;"}, {true, "x: 10px / 1em;", "x:10px/1em;"}, {true, "x: 1em/1.5em \"Times New Roman\", Times, serif;", "x:1em/1.5em \"Times New Roman\",Times,serif;"}, {true, "x: hsla(100,50%, 75%, 0.5);", "x:hsla(100,50%,75%,0.5);"}, {true, "x: hsl(100,50%, 75%);", "x:hsl(100,50%,75%);"}, {true, "x: rgba(255, 238 , 221, 0.3);", "x:rgba(255,238,221,0.3);"}, {true, "x: 50vmax;", "x:50vmax;"}, {true, "color: linear-gradient(to right, black, white);", "color:linear-gradient(to right,black,white);"}, {true, "color: calc(100%/2 - 1em);", "color:calc(100%/2 - 1em);"}, {true, "color: calc(100%/2--1em);", "color:calc(100%/2--1em);"}, {false, "", ""}, {false, "@media print, screen { }", "@media print,screen{}"}, {false, "@media { @viewport ; }", "@media{@viewport;}"}, {false, "@keyframes 'diagonal-slide' { from { left: 0; top: 0; } to { left: 100px; top: 100px; } }", "@keyframes 'diagonal-slide'{from{left:0;top:0;}to{left:100px;top:100px;}}"}, {false, "@keyframes movingbox{0%{left:90%;}50%{left:10%;}100%{left:90%;}}", "@keyframes movingbox{0%{left:90%;}50%{left:10%;}100%{left:90%;}}"}, {false, ".foo { color: #fff;}", ".foo{color:#fff;}"}, {false, ".foo { ; _color: #fff;}", ".foo{_color:#fff;}"}, {false, "a { color: red; border: 0; }", "a{color:red;border:0;}"}, {false, "a { color: red; border: 0; } b { padding: 0; }", "a{color:red;border:0;}b{padding:0;}"}, {false, "/* comment */", "/* comment */"}, // extraordinary {true, "color: red;;", "color:red;"}, {true, "margin: 10px/*comment*/50px;", "margin:10px 50px;"}, {true, "color:#c0c0c0", "color:#c0c0c0;"}, {true, "background:URL(x.png);", "background:URL(x.png);"}, {true, "filter: progid : DXImageTransform.Microsoft.BasicImage(rotation=1);", "filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=1);"}, {true, "/*a*/\n/*c*/\nkey: value;", "key:value;"}, {true, "@-moz-charset;", "@-moz-charset;"}, {true, "--custom-variable: (0;) ;", "--custom-variable: (0;) ;"}, {false, "@import;@import;", "@import;@import;"}, {false, ".a .b#c, .d<.e { x:y; }", ".a .b#c,.d<.e{x:y;}"}, {false, ".a[b~=c]d { x:y; }", ".a[b~=c]d{x:y;}"}, // {false, "{x:y;}", "{x:y;}"}, {false, "a{}", "a{}"}, {false, "a,.b/*comment*/ {x:y;}", "a,.b{x:y;}"}, {false, "a,.b/*comment*/.c {x:y;}", "a,.b.c{x:y;}"}, {false, "a{x:; z:q;}", "a{x:;z:q;}"}, {false, "@font-face { x:y; }", "@font-face{x:y;}"}, {false, "a:not([controls]){x:y;}", "a:not([controls]){x:y;}"}, {false, "@document regexp('https:.*') { p { color: red; } }", "@document regexp('https:.*'){p{color:red;}}"}, {false, "@media all and ( max-width:400px ) { }", "@media all and (max-width:400px){}"}, {false, "@media (max-width:400px) { }", "@media(max-width:400px){}"}, {false, "@media (max-width:400px)", "@media(max-width:400px);"}, {false, "@font-face { ; font:x; }", "@font-face{font:x;}"}, {false, "@-moz-font-face { ; font:x; }", "@-moz-font-face{font:x;}"}, {false, "@unknown abc { {} lala }", "@unknown abc{{} lala }"}, {false, "a[x={}]{x:y;}", "a[x={}]{x:y;}"}, {false, "a[x=,]{x:y;}", "a[x=,]{x:y;}"}, {false, "a[x=+]{x:y;}", "a[x=+]{x:y;}"}, {false, ".cla .ss > #id { x:y; }", ".cla .ss>#id{x:y;}"}, {false, ".cla /*a*/ /*b*/ .ss{}", ".cla .ss{}"}, {false, "a{x:f(a(),b);}", "a{x:f(a(),b);}"}, {false, "a{x:y!z;}", "a{x:y!z;}"}, {false, "[class*=\"column\"]+[class*=\"column\"]:last-child{a:b;}", "[class*=\"column\"]+[class*=\"column\"]:last-child{a:b;}"}, {false, "@media { @viewport }", "@media{@viewport;}"}, {false, "table { @unknown }", "table{@unknown;}"}, {false, "a{@media{width:70%;} b{width:60%;}}", "a{@media{ERROR(width:70%;})ERROR(b{width:60%;})}"}, // early endings {false, "selector{", "selector{"}, {false, "@media{selector{", "@media{selector{"}, // bad grammar {false, "}", "ERROR(})"}, {true, "}", "ERROR(})"}, {true, "~color:red", "ERROR(~color:red)"}, {true, "(color;red)", "ERROR((color;red))"}, {true, "color(;red)", "ERROR(color(;red))"}, {false, ".foo { *color: #fff;}", ".foo{*color:#fff;}"}, {true, "*color: red; font-size: 12pt;", "*color:red;font-size:12pt;"}, {true, "*--custom: red;", "*--custom: red;"}, {true, "_color: red; font-size: 12pt;", "_color:red;font-size:12pt;"}, {false, ".foo { baddecl } .bar { color:red; }", ".foo{ERROR(baddecl)}.bar{color:red;}"}, {false, ".foo { baddecl baddecl baddecl; height:100px } .bar { color:red; }", ".foo{ERROR(baddecl baddecl baddecl;)height:100px;}.bar{color:red;}"}, {false, ".foo { visibility: hidden;” } .bar { color:red; }", ".foo{visibility:hidden;ERROR(”)}.bar{color:red;}"}, {false, ".foo { baddecl (; color:red; }", ".foo{ERROR(baddecl (; color:red; })"}, // issues {false, "@media print {.class{width:5px;}}", "@media print{.class{width:5px;}}"}, // #6 {false, ".class{width:calc((50% + 2em)/2 + 14px);}", ".class{width:calc((50% + 2em)/2 + 14px);}"}, // #7 {false, ".class [c=y]{}", ".class [c=y]{}"}, // tdewolff/minify#16 {false, "table{font-family:Verdana}", "table{font-family:Verdana;}"}, // tdewolff/minify#22 // go-fuzz {false, "@-webkit-", "@-webkit-;"}, } for _, tt := range parseTests { t.Run(tt.css, func(t *testing.T) { output := "" p := NewParser(parse.NewInputString(tt.css), tt.inline) for { grammar, _, data := p.Next() data = parse.Copy(data) if grammar == ErrorGrammar { if err := p.Err(); err != io.EOF { data = []byte("ERROR(") for _, val := range p.Values() { data = append(data, val.Data...) } data = append(data, ")"...) } else { break } } else if grammar == AtRuleGrammar || grammar == BeginAtRuleGrammar || grammar == QualifiedRuleGrammar || grammar == BeginRulesetGrammar || grammar == DeclarationGrammar || grammar == CustomPropertyGrammar { if grammar == DeclarationGrammar || grammar == CustomPropertyGrammar { data = append(data, ":"...) } for _, val := range p.Values() { data = append(data, val.Data...) } if grammar == BeginAtRuleGrammar || grammar == BeginRulesetGrammar { data = append(data, "{"...) } else if grammar == AtRuleGrammar || grammar == DeclarationGrammar || grammar == CustomPropertyGrammar { data = append(data, ";"...) } else if grammar == QualifiedRuleGrammar { data = append(data, ","...) } } output += string(data) } test.String(t, output, tt.expected) }) } // coverage for i := 0; ; i++ { if GrammarType(i).String() == fmt.Sprintf("Invalid(%d)", i) { break } } test.T(t, Token{IdentToken, []byte("data")}.String(), "Ident('data')") } func TestParseError(t *testing.T) { var parseErrorTests = []struct { inline bool css string col int }{ {false, "}", 2}, {true, "}", 1}, {false, "selector", 9}, {true, "color 0", 7}, {true, "--color 0", 9}, {true, "--custom-variable:0", 0}, } for _, tt := range parseErrorTests { t.Run(tt.css, func(t *testing.T) { p := NewParser(parse.NewInputString(tt.css), tt.inline) for { grammar, _, _ := p.Next() if grammar == ErrorGrammar { if tt.col == 0 { test.T(t, p.Err(), io.EOF) } else if perr, ok := p.Err().(*parse.Error); ok { test.That(t, p.HasParseError()) _, col, _ := perr.Position() test.T(t, col, tt.col) } else { test.Fail(t, "bad error:", p.Err()) } break } } }) } } func TestParseOffset(t *testing.T) { z := parse.NewInputString(`div{background:url(link);}`) p := NewParser(z, false) test.T(t, z.Offset(), 0) _, _, _ = p.Next() test.T(t, z.Offset(), 4) // div{ _, _, _ = p.Next() test.T(t, z.Offset(), 25) // background:url(link); _, _, _ = p.Next() test.T(t, z.Offset(), 26) // } } //////////////////////////////////////////////////////////////// type Obj struct{} func (*Obj) F() {} var f1 func(*Obj) func BenchmarkFuncPtr(b *testing.B) { for i := 0; i < b.N; i++ { f1 = (*Obj).F } } var f2 func() func BenchmarkMemFuncPtr(b *testing.B) { obj := &Obj{} for i := 0; i < b.N; i++ { f2 = obj.F } } func ExampleNewParser() { p := NewParser(parse.NewInputString("color: red;"), true) // false because this is the content of an inline style attribute out := "" for { gt, _, data := p.Next() if gt == ErrorGrammar { break } else if gt == AtRuleGrammar || gt == BeginAtRuleGrammar || gt == BeginRulesetGrammar || gt == DeclarationGrammar { out += string(data) if gt == DeclarationGrammar { out += ":" } for _, val := range p.Values() { out += string(val.Data) } if gt == BeginAtRuleGrammar || gt == BeginRulesetGrammar { out += "{" } else if gt == AtRuleGrammar || gt == DeclarationGrammar { out += ";" } } else { out += string(data) } } fmt.Println(out) // Output: color:red; } parse-2.7.15/css/util.go000066400000000000000000000020301463212301500150040ustar00rootroot00000000000000package css import "github.com/tdewolff/parse/v2" // IsIdent returns true if the bytes are a valid identifier. func IsIdent(b []byte) bool { l := NewLexer(parse.NewInputBytes(b)) l.consumeIdentToken() l.r.Restore() return l.r.Pos() == len(b) } // IsURLUnquoted returns true if the bytes are a valid unquoted URL. func IsURLUnquoted(b []byte) bool { l := NewLexer(parse.NewInputBytes(b)) l.consumeUnquotedURL() l.r.Restore() return l.r.Pos() == len(b) } // HSL2RGB converts HSL to RGB with all of range [0,1] // from http://www.w3.org/TR/css3-color/#hsl-color func HSL2RGB(h, s, l float64) (float64, float64, float64) { m2 := l * (s + 1) if l > 0.5 { m2 = l + s - l*s } m1 := l*2 - m2 return hue2rgb(m1, m2, h+1.0/3.0), hue2rgb(m1, m2, h), hue2rgb(m1, m2, h-1.0/3.0) } func hue2rgb(m1, m2, h float64) float64 { if h < 0.0 { h += 1.0 } if h > 1.0 { h -= 1.0 } if h*6.0 < 1.0 { return m1 + (m2-m1)*h*6.0 } else if h*2.0 < 1.0 { return m2 } else if h*3.0 < 2.0 { return m1 + (m2-m1)*(2.0/3.0-h)*6.0 } return m1 } parse-2.7.15/css/util_test.go000066400000000000000000000011621463212301500160500ustar00rootroot00000000000000package css import ( "testing" "github.com/tdewolff/test" ) func TestIsIdent(t *testing.T) { test.That(t, IsIdent([]byte("color"))) test.That(t, !IsIdent([]byte("4.5"))) } func TestIsURLUnquoted(t *testing.T) { test.That(t, IsURLUnquoted([]byte("http://x"))) test.That(t, !IsURLUnquoted([]byte(")"))) } func TestHsl2Rgb(t *testing.T) { r, g, b := HSL2RGB(0.0, 1.0, 0.5) test.T(t, r, 1.0) test.T(t, g, 0.0) test.T(t, b, 0.0) r, g, b = HSL2RGB(1.0, 1.0, 0.5) test.T(t, r, 1.0) test.T(t, g, 0.0) test.T(t, b, 0.0) r, g, b = HSL2RGB(0.66, 0.0, 1.0) test.T(t, r, 1.0) test.T(t, g, 1.0) test.T(t, b, 1.0) } parse-2.7.15/error.go000066400000000000000000000023001463212301500143700ustar00rootroot00000000000000package parse import ( "bytes" "fmt" "io" ) // Error is a parsing error returned by parser. It contains a message and an offset at which the error occurred. type Error struct { Message string Line int Column int Context string } // NewError creates a new error func NewError(r io.Reader, offset int, message string, a ...interface{}) *Error { line, column, context := Position(r, offset) if 0 < len(a) { message = fmt.Sprintf(message, a...) } return &Error{ Message: message, Line: line, Column: column, Context: context, } } // NewErrorLexer creates a new error from an active Lexer. func NewErrorLexer(l *Input, message string, a ...interface{}) *Error { r := bytes.NewBuffer(l.Bytes()) offset := l.Offset() return NewError(r, offset, message, a...) } // Position returns the line, column, and context of the error. // Context is the entire line at which the error occurred. func (e *Error) Position() (int, int, string) { return e.Line, e.Column, e.Context } // Error returns the error string, containing the context and line + column number. func (e *Error) Error() string { return fmt.Sprintf("%s on line %d and column %d\n%s", e.Message, e.Line, e.Column, e.Context) } parse-2.7.15/error_test.go000066400000000000000000000020071463212301500154330ustar00rootroot00000000000000package parse import ( "bytes" "testing" "github.com/tdewolff/test" ) func TestError(t *testing.T) { err := NewError(bytes.NewBufferString("buffer"), 3, "message") line, column, context := err.Position() test.T(t, line, 1, "line") test.T(t, column, 4, "column") test.T(t, "\n"+context, "\n 1: buffer\n ^", "context") test.T(t, err.Error(), "message on line 1 and column 4\n 1: buffer\n ^", "error") } func TestErrorLexer(t *testing.T) { l := NewInputString("buffer") l.Move(3) err := NewErrorLexer(l, "message") line, column, context := err.Position() test.T(t, line, 1, "line") test.T(t, column, 4, "column") test.T(t, "\n"+context, "\n 1: buffer\n ^", "context") test.T(t, err.Error(), "message on line 1 and column 4\n 1: buffer\n ^", "error") } func TestErrorMessages(t *testing.T) { err := NewError(bytes.NewBufferString("buffer"), 3, "message %d", 5) test.T(t, err.Error(), "message 5 on line 1 and column 4\n 1: buffer\n ^", "error") } parse-2.7.15/go.mod000066400000000000000000000001651463212301500140250ustar00rootroot00000000000000module github.com/tdewolff/parse/v2 go 1.13 require github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52 parse-2.7.15/go.sum000066400000000000000000000003451463212301500140520ustar00rootroot00000000000000github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52 h1:gAQliwn+zJrkjAHVcBEYW/RFvd2St4yYimisvozAYlA= github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= parse-2.7.15/html/000077500000000000000000000000001463212301500136615ustar00rootroot00000000000000parse-2.7.15/html/README.md000066400000000000000000000036561463212301500151520ustar00rootroot00000000000000# HTML [![API reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/tdewolff/parse/v2/html?tab=doc) This package is an HTML5 lexer written in [Go][1]. It follows the specification at [The HTML syntax](http://www.w3.org/TR/html5/syntax.html). The lexer takes an io.Reader and converts it into tokens until the EOF. ## Installation Run the following command go get -u github.com/tdewolff/parse/v2/html or add the following import and run project with `go get` import "github.com/tdewolff/parse/v2/html" ## Lexer ### Usage The following initializes a new Lexer with io.Reader `r`: ``` go l := html.NewLexer(parse.NewInput(r)) ``` To tokenize until EOF an error, use: ``` go for { tt, data := l.Next() switch tt { case html.ErrorToken: // error or EOF set in l.Err() return case html.StartTagToken: // ... for { ttAttr, dataAttr := l.Next() if ttAttr != html.AttributeToken { break } // ... } // ... } } ``` All tokens: ``` go ErrorToken TokenType = iota // extra token when errors occur CommentToken DoctypeToken StartTagToken StartTagCloseToken StartTagVoidToken EndTagToken AttributeToken TextToken ``` ### Examples ``` go package main import ( "os" "github.com/tdewolff/parse/v2/html" ) // Tokenize HTML from stdin. func main() { l := html.NewLexer(parse.NewInput(os.Stdin)) for { tt, data := l.Next() switch tt { case html.ErrorToken: if l.Err() != io.EOF { fmt.Println("Error on line", l.Line(), ":", l.Err()) } return case html.StartTagToken: fmt.Println("Tag", string(data)) for { ttAttr, dataAttr := l.Next() if ttAttr != html.AttributeToken { break } key := dataAttr val := l.AttrVal() fmt.Println("Attribute", string(key), "=", string(val)) } // ... } } } ``` ## License Released under the [MIT license](https://github.com/tdewolff/parse/blob/master/LICENSE.md). [1]: http://golang.org/ "Go Language" parse-2.7.15/html/hash.go000066400000000000000000000037001463212301500151330ustar00rootroot00000000000000package html // generated by hasher -type=Hash -file=hash.go; DO NOT EDIT, except for adding more constants to the list and rerun go generate // uses github.com/tdewolff/hasher //go:generate hasher -type=Hash -file=hash.go // Hash defines perfect hashes for a predefined list of strings type Hash uint32 // Unique hash definitions to be used instead of strings const ( Iframe Hash = 0x6 // iframe Math Hash = 0x604 // math Plaintext Hash = 0x1e09 // plaintext Script Hash = 0xa06 // script Style Hash = 0x1405 // style Svg Hash = 0x1903 // svg Textarea Hash = 0x2308 // textarea Title Hash = 0xf05 // title Xmp Hash = 0x1c03 // xmp ) // String returns the hash' name. func (i Hash) String() string { start := uint32(i >> 8) n := uint32(i & 0xff) if start+n > uint32(len(_Hash_text)) { return "" } return _Hash_text[start : start+n] } // ToHash returns the hash whose name is s. It returns zero if there is no // such hash. It is case sensitive. func ToHash(s []byte) Hash { if len(s) == 0 || len(s) > _Hash_maxLen { return 0 } h := uint32(_Hash_hash0) for i := 0; i < len(s); i++ { h ^= uint32(s[i]) h *= 16777619 } if i := _Hash_table[h&uint32(len(_Hash_table)-1)]; int(i&0xff) == len(s) { t := _Hash_text[i>>8 : i>>8+i&0xff] for i := 0; i < len(s); i++ { if t[i] != s[i] { goto NEXT } } return i } NEXT: if i := _Hash_table[(h>>16)&uint32(len(_Hash_table)-1)]; int(i&0xff) == len(s) { t := _Hash_text[i>>8 : i>>8+i&0xff] for i := 0; i < len(s); i++ { if t[i] != s[i] { return 0 } } return i } return 0 } const _Hash_hash0 = 0x9acb0442 const _Hash_maxLen = 9 const _Hash_text = "iframemathscriptitlestylesvgxmplaintextarea" var _Hash_table = [1 << 4]Hash{ 0x0: 0x2308, // textarea 0x2: 0x6, // iframe 0x4: 0xf05, // title 0x5: 0x1e09, // plaintext 0x7: 0x1405, // style 0x8: 0x604, // math 0x9: 0xa06, // script 0xa: 0x1903, // svg 0xb: 0x1c03, // xmp } parse-2.7.15/html/lex.go000066400000000000000000000330461463212301500150060ustar00rootroot00000000000000// Package html is an HTML5 lexer following the specifications at http://www.w3.org/TR/html5/syntax.html. package html import ( "strconv" "github.com/tdewolff/parse/v2" ) // TokenType determines the type of token, eg. a number or a semicolon. type TokenType uint32 // TokenType values. const ( ErrorToken TokenType = iota // extra token when errors occur CommentToken DoctypeToken StartTagToken StartTagCloseToken StartTagVoidToken EndTagToken AttributeToken TextToken SvgToken MathToken ) // String returns the string representation of a TokenType. func (tt TokenType) String() string { switch tt { case ErrorToken: return "Error" case CommentToken: return "Comment" case DoctypeToken: return "Doctype" case StartTagToken: return "StartTag" case StartTagCloseToken: return "StartTagClose" case StartTagVoidToken: return "StartTagVoid" case EndTagToken: return "EndTag" case AttributeToken: return "Attribute" case TextToken: return "Text" case SvgToken: return "Svg" case MathToken: return "Math" } return "Invalid(" + strconv.Itoa(int(tt)) + ")" } //////////////////////////////////////////////////////////////// var GoTemplate = [2]string{"{{", "}}"} var HandlebarsTemplate = [2]string{"{{", "}}"} var MustacheTemplate = [2]string{"{{", "}}"} var EJSTemplate = [2]string{"<%", "%>"} var ASPTemplate = [2]string{"<%", "%>"} var PHPTemplate = [2]string{""} // Lexer is the state for the lexer. type Lexer struct { r *parse.Input tmplBegin []byte tmplEnd []byte err error rawTag Hash inTag bool text []byte attrVal []byte hasTmpl bool } // NewLexer returns a new Lexer for a given io.Reader. func NewLexer(r *parse.Input) *Lexer { return &Lexer{ r: r, } } func NewTemplateLexer(r *parse.Input, tmpl [2]string) *Lexer { return &Lexer{ r: r, tmplBegin: []byte(tmpl[0]), tmplEnd: []byte(tmpl[1]), } } // Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned. func (l *Lexer) Err() error { if l.err != nil { return l.err } return l.r.Err() } // Text returns the textual representation of a token. This excludes delimiters and additional leading/trailing characters. func (l *Lexer) Text() []byte { return l.text } // AttrKey returns the attribute key when an AttributeToken was returned from Next. func (l *Lexer) AttrKey() []byte { return l.text } // AttrVal returns the attribute value when an AttributeToken was returned from Next. func (l *Lexer) AttrVal() []byte { return l.attrVal } // HasTemplate returns the true if the token value contains a template. func (l *Lexer) HasTemplate() bool { return l.hasTmpl } // Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message. func (l *Lexer) Next() (TokenType, []byte) { l.text = nil l.hasTmpl = false var c byte if l.inTag { l.attrVal = nil for { // before attribute name state if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' { l.r.Move(1) continue } break } if c == 0 && l.r.Err() != nil { return ErrorToken, nil } else if c != '>' && (c != '/' || l.r.Peek(1) != '>') { return AttributeToken, l.shiftAttribute() } l.r.Skip() l.inTag = false if c == '/' { l.r.Move(2) return StartTagVoidToken, l.r.Shift() } l.r.Move(1) return StartTagCloseToken, l.r.Shift() } if l.rawTag != 0 { if rawText := l.shiftRawText(); 0 < len(rawText) { l.text = rawText l.rawTag = 0 return TextToken, rawText } l.rawTag = 0 } for { c = l.r.Peek(0) if c == '<' { c = l.r.Peek(1) isEndTag := c == '/' && l.r.Peek(2) != '>' && (l.r.Peek(2) != 0 || l.r.PeekErr(2) == nil) if !isEndTag && (c < 'a' || 'z' < c) && (c < 'A' || 'Z' < c) && c != '!' && c != '?' { // not a tag l.r.Move(1) } else if 0 < l.r.Pos() { // return currently buffered texttoken so that we can return tag next iteration l.text = l.r.Shift() return TextToken, l.text } else if isEndTag { l.r.Move(2) // only endtags that are not followed by > or EOF arrive here if c = l.r.Peek(0); !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') { return CommentToken, l.shiftBogusComment() } return EndTagToken, l.shiftEndTag() } else if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' { l.r.Move(1) l.inTag = true return l.shiftStartTag() } else if c == '!' { l.r.Move(2) return l.readMarkup() } else if 0 < len(l.tmplBegin) && l.at(l.tmplBegin...) { l.r.Move(len(l.tmplBegin)) l.moveTemplate() l.hasTmpl = true } else if c == '?' { l.r.Move(1) return CommentToken, l.shiftBogusComment() } } else if 0 < len(l.tmplBegin) && l.at(l.tmplBegin...) { l.r.Move(len(l.tmplBegin)) l.moveTemplate() l.hasTmpl = true } else if c == 0 && l.r.Err() != nil { if 0 < l.r.Pos() { l.text = l.r.Shift() return TextToken, l.text } return ErrorToken, nil } else { l.r.Move(1) } } } //////////////////////////////////////////////////////////////// // The following functions follow the specifications at https://html.spec.whatwg.org/multipage/parsing.html func (l *Lexer) shiftRawText() []byte { if l.rawTag == Plaintext { for { if l.r.Peek(0) == 0 && l.r.Err() != nil { return l.r.Shift() } l.r.Move(1) } } else { // RCDATA, RAWTEXT and SCRIPT for { c := l.r.Peek(0) if c == '<' { if l.r.Peek(1) == '/' { mark := l.r.Pos() l.r.Move(2) for { if c = l.r.Peek(0); !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') { break } l.r.Move(1) } if h := ToHash(parse.ToLower(parse.Copy(l.r.Lexeme()[mark+2:]))); h == l.rawTag { // copy so that ToLower doesn't change the case of the underlying slice l.r.Rewind(mark) return l.r.Shift() } } else if l.rawTag == Script && l.r.Peek(1) == '!' && l.r.Peek(2) == '-' && l.r.Peek(3) == '-' { l.r.Move(4) inScript := false for { c := l.r.Peek(0) if c == '-' && l.r.Peek(1) == '-' && l.r.Peek(2) == '>' { l.r.Move(3) break } else if c == '<' { isEnd := l.r.Peek(1) == '/' if isEnd { l.r.Move(2) } else { l.r.Move(1) } mark := l.r.Pos() for { if c = l.r.Peek(0); !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') { break } l.r.Move(1) } if h := ToHash(parse.ToLower(parse.Copy(l.r.Lexeme()[mark:]))); h == Script { // copy so that ToLower doesn't change the case of the underlying slice if !isEnd { inScript = true } else { if !inScript { l.r.Rewind(mark - 2) return l.r.Shift() } inScript = false } } } else if c == 0 && l.r.Err() != nil { return l.r.Shift() } else { l.r.Move(1) } } } else { l.r.Move(1) } } else if 0 < len(l.tmplBegin) && l.at(l.tmplBegin...) { l.r.Move(len(l.tmplBegin)) l.moveTemplate() l.hasTmpl = true } else if c == 0 && l.r.Err() != nil { return l.r.Shift() } else { l.r.Move(1) } } } } func (l *Lexer) readMarkup() (TokenType, []byte) { if l.at('-', '-') { l.r.Move(2) for { if l.r.Peek(0) == 0 && l.r.Err() != nil { l.text = l.r.Lexeme()[4:] return CommentToken, l.r.Shift() } else if l.at('-', '-', '>') { l.text = l.r.Lexeme()[4:] l.r.Move(3) return CommentToken, l.r.Shift() } else if l.at('-', '-', '!', '>') { l.text = l.r.Lexeme()[4:] l.r.Move(4) return CommentToken, l.r.Shift() } l.r.Move(1) } } else if l.at('[', 'C', 'D', 'A', 'T', 'A', '[') { l.r.Move(7) for { if l.r.Peek(0) == 0 && l.r.Err() != nil { l.text = l.r.Lexeme()[9:] return TextToken, l.r.Shift() } else if l.at(']', ']', '>') { l.text = l.r.Lexeme()[9:] l.r.Move(3) return TextToken, l.r.Shift() } l.r.Move(1) } } else { if l.atCaseInsensitive('d', 'o', 'c', 't', 'y', 'p', 'e') { l.r.Move(7) if l.r.Peek(0) == ' ' { l.r.Move(1) } for { if c := l.r.Peek(0); c == '>' || c == 0 && l.r.Err() != nil { l.text = l.r.Lexeme()[9:] if c == '>' { l.r.Move(1) } return DoctypeToken, l.r.Shift() } l.r.Move(1) } } } return CommentToken, l.shiftBogusComment() } func (l *Lexer) shiftBogusComment() []byte { for { c := l.r.Peek(0) if c == '>' { l.text = l.r.Lexeme()[2:] l.r.Move(1) return l.r.Shift() } else if c == 0 && l.r.Err() != nil { l.text = l.r.Lexeme()[2:] return l.r.Shift() } l.r.Move(1) } } func (l *Lexer) shiftStartTag() (TokenType, []byte) { for { if c := l.r.Peek(0); c == ' ' || c == '>' || c == '/' && l.r.Peek(1) == '>' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == 0 && l.r.Err() != nil { break } l.r.Move(1) } l.text = parse.ToLower(l.r.Lexeme()[1:]) if h := ToHash(l.text); h == Textarea || h == Title || h == Style || h == Xmp || h == Iframe || h == Script || h == Plaintext || h == Svg || h == Math { if h == Svg || h == Math { data := l.shiftXML(h) if l.err != nil { return ErrorToken, nil } l.inTag = false if h == Svg { return SvgToken, data } return MathToken, data } l.rawTag = h } return StartTagToken, l.r.Shift() } func (l *Lexer) shiftAttribute() []byte { nameStart := l.r.Pos() var c byte if 0 < len(l.tmplBegin) && l.at(l.tmplBegin...) { l.r.Move(len(l.tmplBegin)) l.moveTemplate() l.hasTmpl = true } for { // attribute name state if c = l.r.Peek(0); c == ' ' || c == '=' || c == '>' || c == '/' && l.r.Peek(1) == '>' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == 0 && l.r.Err() != nil { break } l.r.Move(1) } nameEnd := l.r.Pos() for { // after attribute name state if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' { l.r.Move(1) continue } break } nameHasTmpl := l.hasTmpl if c == '=' { l.r.Move(1) for { // before attribute value state if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' { l.r.Move(1) continue } break } attrPos := l.r.Pos() delim := c if delim == '"' || delim == '\'' { // attribute value single- and double-quoted state l.r.Move(1) for { c := l.r.Peek(0) if c == delim { l.r.Move(1) break } else if 0 < len(l.tmplBegin) && l.at(l.tmplBegin...) { l.r.Move(len(l.tmplBegin)) l.moveTemplate() l.hasTmpl = true } else if c == 0 && l.r.Err() != nil { break } else { l.r.Move(1) } } } else if 0 < len(l.tmplBegin) && l.at(l.tmplBegin...) { l.r.Move(len(l.tmplBegin)) l.moveTemplate() l.hasTmpl = true } else { // attribute value unquoted state for { if c := l.r.Peek(0); c == ' ' || c == '>' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == 0 && l.r.Err() != nil { break } l.r.Move(1) } } l.attrVal = l.r.Lexeme()[attrPos:] } else { l.r.Rewind(nameEnd) l.attrVal = nil } if 0 < len(l.tmplBegin) && l.at(l.tmplBegin...) { l.r.Move(len(l.tmplBegin)) l.moveTemplate() l.hasTmpl = true } l.text = l.r.Lexeme()[nameStart:nameEnd] if !nameHasTmpl { l.text = parse.ToLower(l.text) } return l.r.Shift() } func (l *Lexer) shiftEndTag() []byte { for { c := l.r.Peek(0) if c == '>' { l.text = l.r.Lexeme()[2:] l.r.Move(1) break } else if c == 0 && l.r.Err() != nil { l.text = l.r.Lexeme()[2:] break } l.r.Move(1) } end := len(l.text) for end > 0 { if c := l.text[end-1]; c == ' ' || c == '\t' || c == '\n' || c == '\r' { end-- continue } break } l.text = l.text[:end] return parse.ToLower(l.r.Shift()) } // shiftXML parses the content of a svg or math tag according to the XML 1.1 specifications, including the tag itself. // So far we have already parsed `' { l.r.Move(1) break } else if c == 0 { if l.r.Err() == nil { l.err = parse.NewErrorLexer(l.r, "unexpected NULL character") } return l.r.Shift() } l.r.Move(1) } return l.r.Shift() } func (l *Lexer) moveTemplate() { for { if c := l.r.Peek(0); c == 0 && l.r.Err() != nil { return } else if l.at(l.tmplEnd...) { l.r.Move(len(l.tmplEnd)) return } else if c == '"' || c == '\'' { l.r.Move(1) escape := false for { if c2 := l.r.Peek(0); c2 == 0 && l.r.Err() != nil { return } else if !escape && c2 == c { l.r.Move(1) break } else if c2 == '\\' { escape = !escape } else { escape = false } l.r.Move(1) } } else { l.r.Move(1) } } } //////////////////////////////////////////////////////////////// func (l *Lexer) at(b ...byte) bool { for i, c := range b { if l.r.Peek(i) != c { return false } } return true } func (l *Lexer) atCaseInsensitive(b ...byte) bool { for i, c := range b { if l.r.Peek(i) != c && (l.r.Peek(i)+('a'-'A')) != c { return false } } return true } parse-2.7.15/html/lex_test.go000066400000000000000000000257631463212301500160540ustar00rootroot00000000000000package html import ( "fmt" "io" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) type TTs []TokenType func TestTokens(t *testing.T) { var tests = []struct { html string expected []TokenType }{ {"", TTs{StartTagToken, StartTagCloseToken, EndTagToken}}, {"", TTs{StartTagToken, StartTagVoidToken}}, {"", TTs{CommentToken}}, {"", TTs{CommentToken}}, {"

text

", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken}}, {"", TTs{StartTagToken, AttributeToken, StartTagVoidToken}}, {"", TTs{StartTagToken, AttributeToken, AttributeToken, StartTagVoidToken}}, {"", TTs{StartTagToken, AttributeToken, AttributeToken, AttributeToken, AttributeToken, StartTagVoidToken}}, {"", TTs{DoctypeToken}}, {"", TTs{DoctypeToken}}, {"", TTs{CommentToken}}, {"", TTs{CommentToken}}, {"", TTs{CommentToken}}, {"< ", TTs{TextToken}}, {"

", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken}}, {"<p></p>", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken}}, {"</plaintext>", TTs{StartTagToken, StartTagCloseToken, TextToken}}, {"<script></script>", TTs{StartTagToken, StartTagCloseToken, EndTagToken}}, {"<script>var x='</script>';</script>", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken, TextToken, EndTagToken}}, {"<script><!--var x='</script>';--></script>", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken, TextToken, EndTagToken}}, {"<script><!--var x='<script></script>';--></script>", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken}}, {"<script><!--var x='<script>';--></script>", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken}}, {"<![CDATA[ test ]]>", TTs{TextToken}}, {"<svg>text</svg>", TTs{SvgToken}}, {"<math>text</math gibberish>", TTs{MathToken}}, {`<svg>text<x a="</svg>"></x></svg>`, TTs{SvgToken}}, {"<a><svg>text</svg></a>", TTs{StartTagToken, StartTagCloseToken, SvgToken, EndTagToken}}, // early endings {"<!-- comment", TTs{CommentToken}}, {"<? bogus comment", TTs{CommentToken}}, {"<foo", TTs{StartTagToken}}, {"</foo", TTs{EndTagToken}}, {"<foo x", TTs{StartTagToken, AttributeToken}}, {"<foo x=", TTs{StartTagToken, AttributeToken}}, {"<foo x='", TTs{StartTagToken, AttributeToken}}, {"<foo x=''", TTs{StartTagToken, AttributeToken}}, {"<!DOCTYPE note SYSTEM", TTs{DoctypeToken}}, {"<![CDATA[ test", TTs{TextToken}}, {"<script>", TTs{StartTagToken, StartTagCloseToken}}, {"<script><!--", TTs{StartTagToken, StartTagCloseToken, TextToken}}, {"<script><!--var x='<script></script>';-->", TTs{StartTagToken, StartTagCloseToken, TextToken}}, // NULL {"foo\x00bar", TTs{TextToken}}, {"<\x00foo>", TTs{TextToken}}, {"<foo\x00>", TTs{StartTagToken, StartTagCloseToken}}, {"</\x00bogus>", TTs{CommentToken}}, {"</foo\x00>", TTs{EndTagToken}}, {"<plaintext>\x00</plaintext>", TTs{StartTagToken, StartTagCloseToken, TextToken}}, {"<script>\x00</script>", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken}}, {"<!--\x00-->", TTs{CommentToken}}, {"<![CDATA[\x00]]>", TTs{TextToken}}, {"<!doctype\x00>", TTs{DoctypeToken}}, {"<?bogus\x00>", TTs{CommentToken}}, {"<?bogus\x00>", TTs{CommentToken}}, // go-fuzz {"</>", TTs{TextToken}}, } for _, tt := range tests { t.Run(tt.html, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.html)) i := 0 tokens := []TokenType{} for { token, _ := l.Next() if token == ErrorToken { test.T(t, l.Err(), io.EOF) break } tokens = append(tokens, token) i++ } test.T(t, tokens, tt.expected, "token types must match") }) } // coverage for i := 0; ; i++ { if TokenType(i).String() == fmt.Sprintf("Invalid(%d)", i) { break } } } func TestTags(t *testing.T) { var tests = []struct { html string expected string }{ {"<foo:bar.qux-norf/>", "foo:bar.qux-norf"}, {"<foo?bar/qux>", "foo?bar/qux"}, {"<!DOCTYPE note SYSTEM \"Note.dtd\">", " note SYSTEM \"Note.dtd\""}, {"</foo >", "foo"}, // early endings {"<foo ", "foo"}, } for _, tt := range tests { t.Run(tt.html, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.html)) for { token, _ := l.Next() if token == ErrorToken { test.T(t, l.Err(), io.EOF) test.Fail(t, "when error occurred we must be at the end") break } else if token == StartTagToken || token == EndTagToken || token == DoctypeToken { test.String(t, string(l.Text()), tt.expected) break } } }) } } func TestAttributes(t *testing.T) { var tests = []struct { attr string expected []string }{ {"<foo a=\"b\" />", []string{"a", "\"b\""}}, {"<foo \nchecked \r\n value\r=\t'=/>\"' />", []string{"checked", "", "value", "'=/>\"'"}}, {"<foo bar=\" a \n\t\r b \" />", []string{"bar", "\" a \n\t\r b \""}}, {"<foo a/>", []string{"a", ""}}, {"<foo /=/>", []string{"/", "/"}}, // early endings {"<foo x", []string{"x", ""}}, {"<foo x=", []string{"x", ""}}, {"<foo x='", []string{"x", "'"}}, // NULL {"<foo \x00>", []string{"\x00", ""}}, {"<foo \x00=\x00>", []string{"\x00", "\x00"}}, {"<foo \x00='\x00'>", []string{"\x00", "'\x00'"}}, } for _, tt := range tests { t.Run(tt.attr, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.attr)) i := 0 for { token, _ := l.Next() if token == ErrorToken { test.T(t, l.Err(), io.EOF) test.T(t, i, len(tt.expected), "when error occurred we must be at the end") break } else if token == AttributeToken { test.That(t, i+1 < len(tt.expected), "index", i+1, "must not exceed expected attributes size", len(tt.expected)) if i+1 < len(tt.expected) { test.String(t, string(l.AttrKey()), tt.expected[i], "attribute keys must match") test.String(t, string(l.AttrVal()), tt.expected[i+1], "attribute keys must match") i += 2 } } } }) } } func TestTemplates(t *testing.T) { var tests = []struct { html string tmpl []bool }{ {"<p>{{.}}</p>", []bool{true}}, {"<p> {{.}} </p>", []bool{true}}, {"<input type='{{.}}'/>", []bool{true}}, {"<input type={{.}} />", []bool{true}}, {"<input {{if eq .Type 0}}selected{{end}}>", []bool{true}}, {"<input {{if eq .Type 0}} selected {{end}}>", []bool{true, false, true}}, {"{{", []bool{true}}, {"{{'", []bool{true}}, } for _, tt := range tests { t.Run(tt.html, func(t *testing.T) { l := NewTemplateLexer(parse.NewInputString(tt.html), GoTemplate) tmpl := []bool{} for { token, _ := l.Next() if token == ErrorToken { test.T(t, l.Err(), io.EOF) break } else if token == TextToken || token == AttributeToken { tmpl = append(tmpl, l.HasTemplate()) } } test.T(t, tmpl, tt.tmpl, "HasTemplate must match") }) } } func TestErrors(t *testing.T) { var tests = []struct { html string col int }{ {"<svg>\x00</svg>", 6}, {"<svg></svg\x00>", 11}, } for _, tt := range tests { t.Run(tt.html, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.html)) for { token, _ := l.Next() if token == ErrorToken { if tt.col == 0 { test.T(t, l.Err(), io.EOF) } else if perr, ok := l.Err().(*parse.Error); ok { _, col, _ := perr.Position() test.T(t, col, tt.col) } else { test.Fail(t, "bad error:", l.Err()) } break } } }) } } func TestTextAndAttrVal(t *testing.T) { l := NewLexer(parse.NewInputString(`<div attr="val" >text<!--comment--><!DOCTYPE doctype><![CDATA[cdata]]><script>js</script><svg>image</svg>`)) _, data := l.Next() test.Bytes(t, data, []byte("<div")) test.Bytes(t, l.Text(), []byte("div")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte(` attr="val"`)) test.Bytes(t, l.AttrKey(), []byte("attr")) test.Bytes(t, l.AttrVal(), []byte(`"val"`)) _, data = l.Next() test.Bytes(t, data, []byte(">")) test.Bytes(t, l.Text(), nil) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("text")) test.Bytes(t, l.Text(), []byte("text")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("<!--comment-->")) test.Bytes(t, l.Text(), []byte("comment")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("<!DOCTYPE doctype>")) test.Bytes(t, l.Text(), []byte(" doctype")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("<![CDATA[cdata]]>")) test.Bytes(t, l.Text(), []byte("cdata")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("<script")) test.Bytes(t, l.Text(), []byte("script")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte(">")) test.Bytes(t, l.Text(), nil) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("js")) test.Bytes(t, l.Text(), []byte("js")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("</script>")) test.Bytes(t, l.Text(), []byte("script")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("<svg>image</svg>")) test.Bytes(t, l.Text(), []byte("svg")) test.Bytes(t, l.AttrVal(), nil) } func TestOffset(t *testing.T) { z := parse.NewInputString(`<div attr="val">text</div>`) l := NewLexer(z) test.T(t, z.Offset(), 0) _, _ = l.Next() test.T(t, z.Offset(), 4) // <div _, _ = l.Next() test.T(t, z.Offset(), 15) // attr="val" _, _ = l.Next() test.T(t, z.Offset(), 16) // > _, _ = l.Next() test.T(t, z.Offset(), 20) // text _, _ = l.Next() test.T(t, z.Offset(), 26) // </div> } //////////////////////////////////////////////////////////////// var J int var ss = [][]byte{ []byte(" style"), []byte("style"), []byte(" \r\n\tstyle"), []byte(" style"), []byte(" x"), []byte("x"), } func BenchmarkWhitespace1(b *testing.B) { for i := 0; i < b.N; i++ { for _, s := range ss { j := 0 for { if c := s[j]; c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' { j++ } else { break } } J += j } } } func BenchmarkWhitespace2(b *testing.B) { for i := 0; i < b.N; i++ { for _, s := range ss { j := 0 for { if c := s[j]; c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' { j++ continue } break } J += j } } } func BenchmarkWhitespace3(b *testing.B) { for i := 0; i < b.N; i++ { for _, s := range ss { j := 0 for { if c := s[j]; c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != '\f' { break } j++ } J += j } } } //////////////////////////////////////////////////////////////// func ExampleNewLexer() { l := NewLexer(parse.NewInputString("<span class='user'>John Doe</span>")) out := "" for { tt, data := l.Next() if tt == ErrorToken { break } out += string(data) } fmt.Println(out) // Output: <span class='user'>John Doe</span> } �������������parse-2.7.15/html/util.go���������������������������������������������������������������������������0000664�0000000�0000000�00000006733�14632123015�0015176�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package html var ( singleQuoteEntityBytes = []byte("&#39;") doubleQuoteEntityBytes = []byte("&#34;") ) // EscapeAttrVal returns the escaped attribute value bytes with quotes. Either single or double quotes are used, whichever is shorter. If there are no quotes present in the value and the value is in HTML (not XML), it will return the value without quotes. func EscapeAttrVal(buf *[]byte, b []byte, origQuote byte, mustQuote bool) []byte { singles := 0 doubles := 0 unquoted := true for _, c := range b { if charTable[c] { unquoted = false if c == '"' { doubles++ } else if c == '\'' { singles++ } } } if unquoted && (!mustQuote || origQuote == 0) { return b } else if singles == 0 && origQuote == '\'' || doubles == 0 && origQuote == '"' { if len(b)+2 > cap(*buf) { *buf = make([]byte, 0, len(b)+2) } t := (*buf)[:len(b)+2] t[0] = origQuote copy(t[1:], b) t[1+len(b)] = origQuote return t } n := len(b) + 2 var quote byte var escapedQuote []byte if singles > doubles || singles == doubles && origQuote != '\'' { n += doubles * 4 quote = '"' escapedQuote = doubleQuoteEntityBytes } else { n += singles * 4 quote = '\'' escapedQuote = singleQuoteEntityBytes } if n > cap(*buf) { *buf = make([]byte, 0, n) // maximum size, not actual size } t := (*buf)[:n] // maximum size, not actual size t[0] = quote j := 1 start := 0 for i, c := range b { if c == quote { j += copy(t[j:], b[start:i]) j += copy(t[j:], escapedQuote) start = i + 1 } } j += copy(t[j:], b[start:]) t[j] = quote return t[:j+1] } var charTable = [256]bool{ // ASCII false, false, false, false, false, false, false, false, false, true, true, false, true, true, false, false, // tab, line feed, form feed, carriage return false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, true, false, false, false, false, true, // space, "), ' false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, // <, =, > false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, // ` false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, // non-ASCII false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, } �������������������������������������parse-2.7.15/html/util_test.go����������������������������������������������������������������������0000664�0000000�0000000�00000002757�14632123015�0016237�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package html import ( "testing" "github.com/tdewolff/test" ) func TestEscapeAttrVal(t *testing.T) { var escapeAttrValTests = []struct { attrVal string expected string }{ {`xyz`, `xyz`}, {``, ``}, {`x/z`, `x/z`}, {`x'z`, `"x'z"`}, {`x"z`, `'x"z'`}, {`'x"z'`, `'x"z'`}, {`'x'"'z'`, `"x'&#34;'z"`}, {`"x"'"z"`, `'x"&#39;"z'`}, {`"x'z"`, `"x'z"`}, {`'x'z'`, `"x'z"`}, {`a'b=""`, `'a&#39;b=""'`}, {`x<z`, `"x<z"`}, {`'x"'"z'`, `'x"&#39;"z'`}, } var buf []byte for _, tt := range escapeAttrValTests { t.Run(tt.attrVal, func(t *testing.T) { b := []byte(tt.attrVal) var quote byte if 0 < len(b) && (b[0] == '\'' || b[0] == '"') { quote = b[0] } if len(b) > 1 && (b[0] == '"' || b[0] == '\'') && b[0] == b[len(b)-1] { b = b[1 : len(b)-1] } val := EscapeAttrVal(&buf, b, quote, false) test.String(t, string(val), tt.expected) }) } } func TestEscapeAttrValXML(t *testing.T) { var escapeAttrValTests = []struct { attrVal string expected string }{ {`"xyz"`, `"xyz"`}, {`'xyz'`, `'xyz'`}, {`xyz`, `xyz`}, {``, ``}, } var buf []byte for _, tt := range escapeAttrValTests { t.Run(tt.attrVal, func(t *testing.T) { b := []byte(tt.attrVal) var quote byte if 0 < len(b) && (b[0] == '\'' || b[0] == '"') { quote = b[0] } if len(b) > 1 && (b[0] == '"' || b[0] == '\'') && b[0] == b[len(b)-1] { b = b[1 : len(b)-1] } val := EscapeAttrVal(&buf, b, quote, true) test.String(t, string(val), tt.expected) }) } } �����������������parse-2.7.15/input.go�������������������������������������������������������������������������������0000664�0000000�0000000�00000010602�14632123015�0014402�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package parse import ( "io" "io/ioutil" ) var nullBuffer = []byte{0} // Input is a buffered reader that allows peeking forward and shifting, taking an io.Input. // It keeps data in-memory until Free, taking a byte length, is called to move beyond the data. type Input struct { buf []byte pos int // index in buf start int // index in buf err error restore func() } // NewInput returns a new Input for a given io.Input and uses ioutil.ReadAll to read it into a byte slice. // If the io.Input implements Bytes, that is used instead. It will append a NULL at the end of the buffer. func NewInput(r io.Reader) *Input { var b []byte if r != nil { if buffer, ok := r.(interface { Bytes() []byte }); ok { b = buffer.Bytes() } else { var err error b, err = ioutil.ReadAll(r) if err != nil { return &Input{ buf: nullBuffer, err: err, } } } } return NewInputBytes(b) } // NewInputString returns a new Input for a given string and appends NULL at the end. func NewInputString(s string) *Input { return NewInputBytes([]byte(s)) } // NewInputBytes returns a new Input for a given byte slice and appends NULL at the end. // To avoid reallocation, make sure the capacity has room for one more byte. func NewInputBytes(b []byte) *Input { z := &Input{ buf: b, } n := len(b) if n == 0 { z.buf = nullBuffer } else { // Append NULL to buffer, but try to avoid reallocation if cap(b) > n { // Overwrite next byte but restore when done b = b[:n+1] c := b[n] b[n] = 0 z.buf = b z.restore = func() { b[n] = c } } else { z.buf = append(b, 0) } } return z } // Restore restores the replaced byte past the end of the buffer by NULL. func (z *Input) Restore() { if z.restore != nil { z.restore() z.restore = nil } } // Err returns the error returned from io.Input or io.EOF when the end has been reached. func (z *Input) Err() error { return z.PeekErr(0) } // PeekErr returns the error at position pos. When pos is zero, this is the same as calling Err(). func (z *Input) PeekErr(pos int) error { if z.err != nil { return z.err } else if len(z.buf)-1 <= z.pos+pos { return io.EOF } return nil } // Peek returns the ith byte relative to the end position. // Peek returns 0 when an error has occurred, Err returns the erroz. func (z *Input) Peek(pos int) byte { pos += z.pos return z.buf[pos] } // PeekRune returns the rune and rune length of the ith byte relative to the end position. func (z *Input) PeekRune(pos int) (rune, int) { // from unicode/utf8 c := z.Peek(pos) if c < 0xC0 || len(z.buf)-1-z.pos < 2 { return rune(c), 1 } else if c < 0xE0 || len(z.buf)-1-z.pos < 3 { return rune(c&0x1F)<<6 | rune(z.Peek(pos+1)&0x3F), 2 } else if c < 0xF0 || len(z.buf)-1-z.pos < 4 { return rune(c&0x0F)<<12 | rune(z.Peek(pos+1)&0x3F)<<6 | rune(z.Peek(pos+2)&0x3F), 3 } return rune(c&0x07)<<18 | rune(z.Peek(pos+1)&0x3F)<<12 | rune(z.Peek(pos+2)&0x3F)<<6 | rune(z.Peek(pos+3)&0x3F), 4 } // Move advances the position. func (z *Input) Move(n int) { z.pos += n } // MoveRune advances the position by the length of the current rune. func (z *Input) MoveRune() { c := z.Peek(0) if c < 0xC0 || len(z.buf)-1-z.pos < 2 { z.pos++ } else if c < 0xE0 || len(z.buf)-1-z.pos < 3 { z.pos += 2 } else if c < 0xF0 || len(z.buf)-1-z.pos < 4 { z.pos += 3 } else { z.pos += 4 } } // Pos returns a mark to which can be rewinded. func (z *Input) Pos() int { return z.pos - z.start } // Rewind rewinds the position to the given position. func (z *Input) Rewind(pos int) { z.pos = z.start + pos } // Lexeme returns the bytes of the current selection. func (z *Input) Lexeme() []byte { return z.buf[z.start:z.pos:z.pos] } // Skip collapses the position to the end of the selection. func (z *Input) Skip() { z.start = z.pos } // Shift returns the bytes of the current selection and collapses the position to the end of the selection. func (z *Input) Shift() []byte { b := z.buf[z.start:z.pos:z.pos] z.start = z.pos return b } // Offset returns the character position in the buffez. func (z *Input) Offset() int { return z.pos } // Bytes returns the underlying buffez. func (z *Input) Bytes() []byte { return z.buf[: len(z.buf)-1 : len(z.buf)-1] } // Len returns the length of the underlying buffez. func (z *Input) Len() int { return len(z.buf) - 1 } // Reset resets position to the underlying buffez. func (z *Input) Reset() { z.start = 0 z.pos = 0 } ������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/input_test.go��������������������������������������������������������������������������0000664�0000000�0000000�00000010136�14632123015�0015443�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package parse import ( "bytes" "io" "testing" "github.com/tdewolff/test" ) func TestInput(t *testing.T) { s := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` z := NewInput(bytes.NewBufferString(s)) test.Bytes(t, z.Bytes(), []byte(s), "bytes match original buffer") test.T(t, z.err, nil, "buffer has no error") test.T(t, z.Err(), nil, "buffer is at EOF but must not return EOF until we reach that") test.That(t, z.Pos() == 0, "buffer must start at position 0") test.That(t, z.Peek(0) == 'L', "first character must be 'L'") test.That(t, z.Peek(1) == 'o', "second character must be 'o'") z.Move(1) test.That(t, z.Peek(0) == 'o', "must be 'o' at position 1") test.That(t, z.Peek(1) == 'r', "must be 'r' at position 2") z.Rewind(6) test.That(t, z.Peek(0) == 'i', "must be 'i' at position 6") test.That(t, z.Peek(1) == 'p', "must be 'p' at position 7") test.T(t, z.Offset(), 6, "offset") test.Bytes(t, z.Lexeme(), []byte("Lorem "), "buffered string must now read 'Lorem ' when at position 6") test.Bytes(t, z.Shift(), []byte("Lorem "), "shift must return the buffered string") test.That(t, z.Pos() == 0, "after shifting position must be 0") test.That(t, z.Peek(0) == 'i', "must be 'i' at position 0 after shifting") test.That(t, z.Peek(1) == 'p', "must be 'p' at position 1 after shifting") test.T(t, z.Err(), nil, "error must be nil at this point") z.Move(len(s) - len("Lorem ") - 1) test.T(t, z.Err(), nil, "error must be nil just before the end of the buffer") z.Skip() test.That(t, z.Pos() == 0, "after skipping position must be 0") z.Move(1) test.T(t, z.Err(), io.EOF, "error must be EOF when past the buffer") z.Move(-1) test.T(t, z.Err(), nil, "error must be nil just before the end of the buffer, even when it has been past the buffer") z.Reset() test.That(t, z.Peek(0) == 'L', "must be 'L' at position 0") test.That(t, z.Peek(1) == 'o', "must be 'o' at position 1") test.T(t, z.Len(), len(s)) } func TestInputRunes(t *testing.T) { z := NewInput(bytes.NewBufferString("aæ†\U00100000")) r, n := z.PeekRune(0) test.That(t, n == 1, "first character must be length 1") test.That(t, r == 'a', "first character must be rune 'a'") r, n = z.PeekRune(1) test.That(t, n == 2, "second character must be length 2") test.That(t, r == 'æ', "second character must be rune 'æ'") r, n = z.PeekRune(3) test.That(t, n == 3, "fourth character must be length 3") test.That(t, r == '†', "fourth character must be rune '†'") r, n = z.PeekRune(6) test.That(t, n == 4, "seventh character must be length 4") test.That(t, r == '\U00100000', "seventh character must be rune '\U00100000'") } func TestInputBadRune(t *testing.T) { z := NewInput(bytes.NewBufferString("\xF0")) // expect four byte rune r, n := z.PeekRune(0) test.T(t, n, 1, "length") test.T(t, r, rune(0xF0), "rune") } func TestInputZeroLen(t *testing.T) { z := NewInput(test.NewPlainReader(bytes.NewBufferString(""))) test.That(t, z.Peek(0) == 0, "first character must yield error") test.Bytes(t, z.Bytes(), []byte{}, "bytes match original buffer") } func TestInputEmptyInput(t *testing.T) { z := NewInput(test.NewEmptyReader()) test.That(t, z.Peek(0) == 0, "first character must yield error") test.T(t, z.Err(), io.EOF, "error must be EOF") test.That(t, z.Peek(0) == 0, "second peek must also yield error") } func TestInputErrorInput(t *testing.T) { z := NewInput(test.NewErrorReader(0)) test.That(t, z.Peek(0) == 0, "first character must yield error") test.T(t, z.Err(), test.ErrPlain, "error must be ErrPlain") test.That(t, z.Peek(0) == 0, "second peek must also yield error") } func TestInputBytes(t *testing.T) { b := []byte{'t', 'e', 's', 't'} z := NewInputBytes(b) test.That(t, z.Peek(4) == 0, "fifth character must yield NULL") } func TestInputRestore(t *testing.T) { b := []byte{'a', 'b', 'c', 'd'} z := NewInputBytes(b[:2]) test.T(t, len(z.buf), 3, "must have terminating NULL") test.T(t, z.buf[2], byte(0), "must have terminating NULL") test.Bytes(t, b, []byte{'a', 'b', 0, 'd'}, "terminating NULL overwrites underlying buffer") z.Restore() test.Bytes(t, b, []byte{'a', 'b', 'c', 'd'}, "terminating NULL has been restored") } ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/������������������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0013331�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/README.md���������������������������������������������������������������������������0000664�0000000�0000000�00000004230�14632123015�0014607�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# JS [![API reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/tdewolff/minify/v2/parse/js?tab=doc) This package is a JS lexer (ECMAScript 2020) written in [Go][1]. It follows the specification at [ECMAScript 2020 Language Specification](https://tc39.es/ecma262/). The lexer takes an io.Reader and converts it into tokens until the EOF. ## Installation Run the following command go get -u github.com/tdewolff/parse/v2/js or add the following import and run project with `go get` import "github.com/tdewolff/parse/v2/js" ## Lexer ### Usage The following initializes a new Lexer with io.Reader `r`: ``` go l := js.NewLexer(parse.NewInput(r)) ``` To tokenize until EOF an error, use: ``` go for { tt, text := l.Next() switch tt { case js.ErrorToken: // error or EOF set in l.Err() return // ... } } ``` ### Regular Expressions The ECMAScript specification for `PunctuatorToken` (of which the `/` and `/=` symbols) and `RegExpToken` depend on a parser state to differentiate between the two. The lexer will always parse the first token as `/` or `/=` operator, upon which the parser can rescan that token to scan a regular expression using `RegExp()`. ### Examples ``` go package main import ( "os" "github.com/tdewolff/parse/v2/js" ) // Tokenize JS from stdin. func main() { l := js.NewLexer(parse.NewInput(os.Stdin)) for { tt, text := l.Next() switch tt { case js.ErrorToken: if l.Err() != io.EOF { fmt.Println("Error on line", l.Line(), ":", l.Err()) } return case js.IdentifierToken: fmt.Println("Identifier", string(text)) case js.NumericToken: fmt.Println("Numeric", string(text)) // ... } } } ``` ## Parser ### Usage The following parses a file and returns an abstract syntax tree (AST). ``` go ast, err := js.NewParser(parse.NewInputString("if (state == 5) { console.log('In state five'); }")) ``` See [ast.go](https://github.com/tdewolff/parse/blob/master/js/ast.go) for all available data structures that can represent the abstact syntax tree. ## License Released under the [MIT license](https://github.com/tdewolff/parse/blob/master/LICENSE.md). [1]: http://golang.org/ "Go Language" ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/ast.go������������������������������������������������������������������������������0000664�0000000�0000000�00000147731�14632123015�0014464�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import ( "bytes" "fmt" "io" "strconv" "strings" "github.com/tdewolff/parse/v2" ) var ErrInvalidJSON = fmt.Errorf("invalid JSON") type JSONer interface { JSON(io.Writer) error } // AST is the full ECMAScript abstract syntax tree. type AST struct { BlockStmt // module } func (ast AST) String() string { s := "" for i, item := range ast.BlockStmt.List { if i != 0 { s += " " } s += item.String() } return s } // JS writes JavaScript to writer. func (ast AST) JS(w io.Writer) { for i, item := range ast.List { if i != 0 { w.Write([]byte("\n")) } item.JS(w) if _, ok := item.(*VarDecl); ok { w.Write([]byte(";")) } } } // JSONString returns a string of JavaScript. func (ast AST) JSString() string { sb := strings.Builder{} ast.JS(&sb) return sb.String() } // JSON writes JSON to writer. func (ast AST) JSON(w io.Writer) error { if 1 < len(ast.List) { return fmt.Errorf("%v: JS must be a single statement", ErrInvalidJSON) } else if len(ast.List) == 0 { return nil } exprStmt, ok := ast.List[0].(*ExprStmt) if !ok { return fmt.Errorf("%v: JS must be an expression statement", ErrInvalidJSON) } expr := exprStmt.Value if group, ok := expr.(*GroupExpr); ok { expr = group.X // allow parsing expr contained in group expr } if val, ok := expr.(JSONer); !ok { return fmt.Errorf("%v: JS must be a valid JSON expression", ErrInvalidJSON) } else { return val.JSON(w) } return nil } // JSONString returns a string of JSON if valid. func (ast AST) JSONString() (string, error) { sb := strings.Builder{} err := ast.JSON(&sb) return sb.String(), err } //////////////////////////////////////////////////////////////// // DeclType specifies the kind of declaration. type DeclType uint16 // DeclType values. const ( NoDecl DeclType = iota // undeclared variables VariableDecl // var FunctionDecl // function ArgumentDecl // function and method arguments LexicalDecl // let, const, class CatchDecl // catch statement argument ExprDecl // function expression name or class expression name ) func (decl DeclType) String() string { switch decl { case NoDecl: return "NoDecl" case VariableDecl: return "VariableDecl" case FunctionDecl: return "FunctionDecl" case ArgumentDecl: return "ArgumentDecl" case LexicalDecl: return "LexicalDecl" case CatchDecl: return "CatchDecl" case ExprDecl: return "ExprDecl" } return "Invalid(" + strconv.Itoa(int(decl)) + ")" } // Var is a variable, where Decl is the type of declaration and can be var|function for function scoped variables, let|const|class for block scoped variables. type Var struct { Data []byte Link *Var // is set when merging variable uses, as in: {a} {var a} where the first links to the second, only used for undeclared variables Uses uint16 Decl DeclType } // Name returns the variable name. func (v *Var) Name() []byte { for v.Link != nil { v = v.Link } return v.Data } func (v *Var) Info() string { s := fmt.Sprintf("%p type=%s name='%s' uses=%d", v, v.Decl, string(v.Data), v.Uses) links := 0 for v.Link != nil { v = v.Link links++ } if 0 < links { s += fmt.Sprintf(" links=%d => %p", links, v) } return s } func (v Var) String() string { return string(v.Name()) } // JS writes JavaScript to writer. func (v Var) JS(w io.Writer) { w.Write(v.Name()) } // VarsByUses is sortable by uses in descending order. type VarsByUses VarArray func (vs VarsByUses) Len() int { return len(vs) } func (vs VarsByUses) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] } func (vs VarsByUses) Less(i, j int) bool { return vs[i].Uses > vs[j].Uses } //////////////////////////////////////////////////////////////// // VarArray is a set of variables in scopes. type VarArray []*Var func (vs VarArray) String() string { s := "[" for i, v := range vs { if i != 0 { s += ", " } links := 0 for v.Link != nil { v = v.Link links++ } s += fmt.Sprintf("Var{%v %s %v %v}", v.Decl, string(v.Data), links, v.Uses) } return s + "]" } // Scope is a function or block scope with a list of variables declared and used. type Scope struct { Parent, Func *Scope // Parent is nil for global scope Declared VarArray // Link in Var are always nil Undeclared VarArray VarDecls []*VarDecl NumForDecls uint16 // offset into Declared to mark variables used in for statements NumFuncArgs uint16 // offset into Declared to mark variables used in function arguments NumArgUses uint16 // offset into Undeclared to mark variables used in arguments IsGlobalOrFunc bool HasWith bool } func (s Scope) String() string { return "Scope{Declared: " + s.Declared.String() + ", Undeclared: " + s.Undeclared.String() + "}" } // Declare declares a new variable. func (s *Scope) Declare(decl DeclType, name []byte) (*Var, bool) { // refer to new variable for previously undeclared symbols in the current and lower scopes // this happens in `{ a = 5; } var a` where both a's refer to the same variable curScope := s if decl == VariableDecl || decl == FunctionDecl { // find function scope for var and function declarations for s != s.Func { // make sure that `{let i;{var i}}` is an error if v := s.findDeclared(name, false); v != nil && v.Decl != decl && v.Decl != CatchDecl { return nil, false } s = s.Parent } } if v := s.findDeclared(name, true); v != nil { // variable already declared, might be an error or a duplicate declaration if (ArgumentDecl < v.Decl || FunctionDecl < decl) && v.Decl != ExprDecl { // only allow (v.Decl,decl) of: (var|function|argument,var|function), (expr,*), any other combination is a syntax error return nil, false } if v.Decl == ExprDecl { v.Decl = decl } v.Uses++ for s != curScope { curScope.AddUndeclared(v) // add variable declaration as used variable to the current scope curScope = curScope.Parent } return v, true } var v *Var // reuse variable if previously used, as in: a;var a if decl != ArgumentDecl { // in case of function f(a=b,b), where the first b is different from the second for i, uv := range s.Undeclared[s.NumArgUses:] { // no need to evaluate v.Link as v.Data stays the same and Link is nil in the active scope if 0 < uv.Uses && uv.Decl == NoDecl && bytes.Equal(name, uv.Data) { // must be NoDecl so that it can't be a var declaration that has been added v = uv s.Undeclared = append(s.Undeclared[:int(s.NumArgUses)+i], s.Undeclared[int(s.NumArgUses)+i+1:]...) break } } } if v == nil { // add variable to the context list and to the scope v = &Var{name, nil, 0, decl} } else { v.Decl = decl } v.Uses++ s.Declared = append(s.Declared, v) for s != curScope { curScope.AddUndeclared(v) // add variable declaration as used variable to the current scope curScope = curScope.Parent } return v, true } // Use increments the usage of a variable. func (s *Scope) Use(name []byte) *Var { // check if variable is declared in the current scope v := s.findDeclared(name, false) if v == nil { // check if variable is already used before in the current or lower scopes v = s.findUndeclared(name) if v == nil { // add variable to the context list and to the scope's undeclared v = &Var{name, nil, 0, NoDecl} s.Undeclared = append(s.Undeclared, v) } } v.Uses++ return v } // findDeclared finds a declared variable in the current scope. func (s *Scope) findDeclared(name []byte, skipForDeclared bool) *Var { start := 0 if skipForDeclared { // we skip the for initializer for declarations (only has effect for let/const) start = int(s.NumForDecls) } // reverse order to find the inner let first in `for(let a in []){let a; {a}}` for i := len(s.Declared) - 1; start <= i; i-- { v := s.Declared[i] // no need to evaluate v.Link as v.Data stays the same, and Link is always nil in Declared if bytes.Equal(name, v.Data) { return v } } return nil } // findUndeclared finds an undeclared variable in the current and contained scopes. func (s *Scope) findUndeclared(name []byte) *Var { for _, v := range s.Undeclared { // no need to evaluate v.Link as v.Data stays the same and Link is nil in the active scope if 0 < v.Uses && bytes.Equal(name, v.Data) { return v } } return nil } // add undeclared variable to scope, this is called for the block scope when declaring a var in it func (s *Scope) AddUndeclared(v *Var) { // don't add undeclared symbol if it's already there for _, vorig := range s.Undeclared { if v == vorig { return } } s.Undeclared = append(s.Undeclared, v) // add variable declaration as used variable to the current scope } // MarkForStmt marks the declared variables in current scope as for statement initializer to distinguish from declarations in body. func (s *Scope) MarkForStmt() { s.NumForDecls = uint16(len(s.Declared)) s.NumArgUses = uint16(len(s.Undeclared)) // ensures for different b's in for(var a in b){let b} } // MarkFuncArgs marks the declared/undeclared variables in the current scope as function arguments. func (s *Scope) MarkFuncArgs() { s.NumFuncArgs = uint16(len(s.Declared)) s.NumArgUses = uint16(len(s.Undeclared)) // ensures different b's in `function f(a=b){var b}`. } // HoistUndeclared copies all undeclared variables of the current scope to the parent scope. func (s *Scope) HoistUndeclared() { for i, vorig := range s.Undeclared { // no need to evaluate vorig.Link as vorig.Data stays the same if 0 < vorig.Uses && vorig.Decl == NoDecl { if v := s.Parent.findDeclared(vorig.Data, false); v != nil { // check if variable is declared in parent scope v.Uses += vorig.Uses vorig.Link = v s.Undeclared[i] = v // point reference to existing var (to avoid many Link chains) } else if v := s.Parent.findUndeclared(vorig.Data); v != nil { // check if variable is already used before in parent scope v.Uses += vorig.Uses vorig.Link = v s.Undeclared[i] = v // point reference to existing var (to avoid many Link chains) } else { // add variable to the context list and to the scope's undeclared s.Parent.Undeclared = append(s.Parent.Undeclared, vorig) } } } } // UndeclareScope undeclares all declared variables in the current scope and adds them to the parent scope. // Called when possible arrow func ends up being a parenthesized expression, scope is not further used. func (s *Scope) UndeclareScope() { // look if the variable already exists in the parent scope, if so replace the Var pointer in original use for _, vorig := range s.Declared { // no need to evaluate vorig.Link as vorig.Data stays the same, and Link is always nil in Declared // vorig.Uses will be atleast 1 if v := s.Parent.findDeclared(vorig.Data, false); v != nil { // check if variable has been declared in this scope v.Uses += vorig.Uses vorig.Link = v } else if v := s.Parent.findUndeclared(vorig.Data); v != nil { // check if variable is already used before in the current or lower scopes v.Uses += vorig.Uses vorig.Link = v } else { // add variable to the context list and to the scope's undeclared vorig.Decl = NoDecl s.Parent.Undeclared = append(s.Parent.Undeclared, vorig) } } s.Declared = s.Declared[:0] s.Undeclared = s.Undeclared[:0] } // Unscope moves all declared variables of the current scope to the parent scope. Undeclared variables are already in the parent scope. func (s *Scope) Unscope() { for _, vorig := range s.Declared { // no need to evaluate vorig.Link as vorig.Data stays the same, and Link is always nil in Declared // vorig.Uses will be atleast 1 s.Parent.Declared = append(s.Parent.Declared, vorig) } s.Declared = s.Declared[:0] s.Undeclared = s.Undeclared[:0] } //////////////////////////////////////////////////////////////// // INode is an interface for AST nodes type INode interface { String() string JS(io.Writer) } // IStmt is a dummy interface for statements. type IStmt interface { INode stmtNode() } // IBinding is a dummy interface for bindings. type IBinding interface { INode bindingNode() } // IExpr is a dummy interface for expressions. type IExpr interface { INode exprNode() } //////////////////////////////////////////////////////////////// // Comment block or line, usually a bang comment. type Comment struct { Value []byte } func (n Comment) String() string { return "Stmt(" + string(n.Value) + ")" } // JS writes JavaScript to writer. func (n Comment) JS(w io.Writer) { if wi, ok := w.(parse.Indenter); ok { wi.Writer.Write(n.Value) } else { w.Write(n.Value) } } // BlockStmt is a block statement. type BlockStmt struct { List []IStmt Scope } func (n BlockStmt) String() string { s := "Stmt({" for _, item := range n.List { s += " " + item.String() } return s + " })" } // JS writes JavaScript to writer. func (n BlockStmt) JS(w io.Writer) { if len(n.List) == 0 { w.Write([]byte("{}")) return } w.Write([]byte("{")) wi := parse.NewIndenter(w, 4) for _, item := range n.List { wi.Write([]byte("\n")) item.JS(wi) if _, ok := item.(*VarDecl); ok { w.Write([]byte(";")) } } w.Write([]byte("\n}")) } // EmptyStmt is an empty statement. type EmptyStmt struct{} func (n EmptyStmt) String() string { return "Stmt()" } // JS writes JavaScript to writer. func (n EmptyStmt) JS(w io.Writer) { w.Write([]byte(";")) } // ExprStmt is an expression statement. type ExprStmt struct { Value IExpr } func (n ExprStmt) String() string { val := n.Value.String() if val[0] == '(' && val[len(val)-1] == ')' { return "Stmt" + n.Value.String() } return "Stmt(" + n.Value.String() + ")" } // JS writes JavaScript to writer. func (n ExprStmt) JS(w io.Writer) { buf := &bytes.Buffer{} wb := io.Writer(buf) if wi, ok := w.(parse.Indenter); ok { // make sure that buf is indenter if w is so as well // this is to prevent newlines in literals from indenting wb = parse.NewIndenter(wb, wi.Indent()) w = wi.Writer } n.Value.JS(wb) expr := buf.Bytes() group := bytes.HasPrefix(expr, []byte("let ")) if group { w.Write([]byte("(")) } w.Write(expr) if group { w.Write([]byte(")")) } w.Write([]byte(";")) } // IfStmt is an if statement. type IfStmt struct { Cond IExpr Body IStmt Else IStmt // can be nil } func (n IfStmt) String() string { s := "Stmt(if " + n.Cond.String() + " " + n.Body.String() if n.Else != nil { s += " else " + n.Else.String() } return s + ")" } // JS writes JavaScript to writer. func (n IfStmt) JS(w io.Writer) { w.Write([]byte("if (")) n.Cond.JS(w) w.Write([]byte(")")) if _, ok := n.Body.(*EmptyStmt); !ok { w.Write([]byte(" ")) } n.Body.JS(w) if _, ok := n.Body.(*VarDecl); ok { w.Write([]byte(";")) } if n.Else != nil { w.Write([]byte(" else")) if _, ok := n.Else.(*EmptyStmt); !ok { w.Write([]byte(" ")) } n.Else.JS(w) if _, ok := n.Else.(*VarDecl); ok { w.Write([]byte(";")) } } } // DoWhileStmt is a do-while iteration statement. type DoWhileStmt struct { Cond IExpr Body IStmt } func (n DoWhileStmt) String() string { return "Stmt(do " + n.Body.String() + " while " + n.Cond.String() + ")" } // JS writes JavaScript to writer. func (n DoWhileStmt) JS(w io.Writer) { w.Write([]byte("do")) if _, ok := n.Body.(*EmptyStmt); !ok { w.Write([]byte(" ")) } n.Body.JS(w) if _, ok := n.Body.(*VarDecl); ok { w.Write([]byte("; ")) } else if _, ok := n.Body.(*Comment); !ok { w.Write([]byte(" ")) } w.Write([]byte("while (")) n.Cond.JS(w) w.Write([]byte(");")) } // WhileStmt is a while iteration statement. type WhileStmt struct { Cond IExpr Body IStmt } func (n WhileStmt) String() string { return "Stmt(while " + n.Cond.String() + " " + n.Body.String() + ")" } // JS writes JavaScript to writer. func (n WhileStmt) JS(w io.Writer) { w.Write([]byte("while (")) n.Cond.JS(w) w.Write([]byte(")")) if _, ok := n.Body.(*EmptyStmt); ok { w.Write([]byte(";")) return } w.Write([]byte(" ")) n.Body.JS(w) if _, ok := n.Body.(*VarDecl); ok { w.Write([]byte(";")) } } // ForStmt is a regular for iteration statement. type ForStmt struct { Init IExpr // can be nil Cond IExpr // can be nil Post IExpr // can be nil Body *BlockStmt } func (n ForStmt) String() string { s := "Stmt(for" if v, ok := n.Init.(*VarDecl); !ok && n.Init != nil || ok && len(v.List) != 0 { s += " " + n.Init.String() } s += " ;" if n.Cond != nil { s += " " + n.Cond.String() } s += " ;" if n.Post != nil { s += " " + n.Post.String() } return s + " " + n.Body.String() + ")" } // JS writes JavaScript to writer. func (n ForStmt) JS(w io.Writer) { w.Write([]byte("for (")) if v, ok := n.Init.(*VarDecl); !ok && n.Init != nil || ok && len(v.List) != 0 { n.Init.JS(w) } else { w.Write([]byte(" ")) } w.Write([]byte("; ")) if n.Cond != nil { n.Cond.JS(w) } w.Write([]byte("; ")) if n.Post != nil { n.Post.JS(w) } w.Write([]byte(") ")) n.Body.JS(w) } // ForInStmt is a for-in iteration statement. type ForInStmt struct { Init IExpr Value IExpr Body *BlockStmt } func (n ForInStmt) String() string { return "Stmt(for " + n.Init.String() + " in " + n.Value.String() + " " + n.Body.String() + ")" } // JS writes JavaScript to writer. func (n ForInStmt) JS(w io.Writer) { w.Write([]byte("for (")) n.Init.JS(w) w.Write([]byte(" in ")) n.Value.JS(w) w.Write([]byte(") ")) n.Body.JS(w) } // ForOfStmt is a for-of iteration statement. type ForOfStmt struct { Await bool Init IExpr Value IExpr Body *BlockStmt } func (n ForOfStmt) String() string { s := "Stmt(for" if n.Await { s += " await" } return s + " " + n.Init.String() + " of " + n.Value.String() + " " + n.Body.String() + ")" } // JS writes JavaScript to writer. func (n ForOfStmt) JS(w io.Writer) { w.Write([]byte("for")) if n.Await { w.Write([]byte(" await")) } w.Write([]byte(" (")) n.Init.JS(w) w.Write([]byte(" of ")) n.Value.JS(w) w.Write([]byte(") ")) n.Body.JS(w) } // CaseClause is a case clause or default clause for a switch statement. type CaseClause struct { TokenType Cond IExpr // can be nil List []IStmt } func (n CaseClause) String() string { s := " Clause(" + n.TokenType.String() if n.Cond != nil { s += " " + n.Cond.String() } for _, item := range n.List { s += " " + item.String() } return s + ")" } // JS writes JavaScript to writer. func (n CaseClause) JS(w io.Writer) { if n.Cond != nil { w.Write([]byte("case ")) n.Cond.JS(w) } else { w.Write([]byte("default")) } w.Write([]byte(":")) wi := parse.NewIndenter(w, 4) for _, item := range n.List { wi.Write([]byte("\n")) item.JS(wi) if _, ok := item.(*VarDecl); ok { w.Write([]byte(";")) } } } // SwitchStmt is a switch statement. type SwitchStmt struct { Init IExpr List []CaseClause Scope } func (n SwitchStmt) String() string { s := "Stmt(switch " + n.Init.String() for _, clause := range n.List { s += clause.String() } return s + ")" } // JS writes JavaScript to writer. func (n SwitchStmt) JS(w io.Writer) { w.Write([]byte("switch (")) n.Init.JS(w) if len(n.List) == 0 { w.Write([]byte(") {}")) return } w.Write([]byte(") {")) for _, clause := range n.List { w.Write([]byte("\n")) clause.JS(w) } w.Write([]byte("\n}")) } // BranchStmt is a continue or break statement. type BranchStmt struct { Type TokenType Label []byte // can be nil } func (n BranchStmt) String() string { s := "Stmt(" + n.Type.String() if n.Label != nil { s += " " + string(n.Label) } return s + ")" } // JS writes JavaScript to writer. func (n BranchStmt) JS(w io.Writer) { w.Write(n.Type.Bytes()) if n.Label != nil { w.Write([]byte(" ")) w.Write(n.Label) } w.Write([]byte(";")) } // ReturnStmt is a return statement. type ReturnStmt struct { Value IExpr // can be nil } func (n ReturnStmt) String() string { s := "Stmt(return" if n.Value != nil { s += " " + n.Value.String() } return s + ")" } // JS writes JavaScript to writer. func (n ReturnStmt) JS(w io.Writer) { w.Write([]byte("return")) if n.Value != nil { w.Write([]byte(" ")) n.Value.JS(w) } w.Write([]byte(";")) } // WithStmt is a with statement. type WithStmt struct { Cond IExpr Body IStmt } func (n WithStmt) String() string { return "Stmt(with " + n.Cond.String() + " " + n.Body.String() + ")" } // JS writes JavaScript to writer. func (n WithStmt) JS(w io.Writer) { w.Write([]byte("with (")) n.Cond.JS(w) w.Write([]byte(")")) if _, ok := n.Body.(*EmptyStmt); !ok { w.Write([]byte(" ")) } n.Body.JS(w) if _, ok := n.Body.(*VarDecl); ok { w.Write([]byte(";")) } } // LabelledStmt is a labelled statement. type LabelledStmt struct { Label []byte Value IStmt } func (n LabelledStmt) String() string { return "Stmt(" + string(n.Label) + " : " + n.Value.String() + ")" } // JS writes JavaScript to writer. func (n LabelledStmt) JS(w io.Writer) { w.Write(n.Label) w.Write([]byte(":")) if _, ok := n.Value.(*EmptyStmt); !ok { w.Write([]byte(" ")) } n.Value.JS(w) if _, ok := n.Value.(*VarDecl); ok { w.Write([]byte(";")) } } // ThrowStmt is a throw statement. type ThrowStmt struct { Value IExpr } func (n ThrowStmt) String() string { return "Stmt(throw " + n.Value.String() + ")" } // JS writes JavaScript to writer. func (n ThrowStmt) JS(w io.Writer) { w.Write([]byte("throw ")) n.Value.JS(w) w.Write([]byte(";")) } // TryStmt is a try statement. type TryStmt struct { Body *BlockStmt Binding IBinding // can be nil Catch *BlockStmt // can be nil Finally *BlockStmt // can be nil } func (n TryStmt) String() string { s := "Stmt(try " + n.Body.String() if n.Catch != nil { s += " catch" if n.Binding != nil { s += " Binding(" + n.Binding.String() + ")" } s += " " + n.Catch.String() } if n.Finally != nil { s += " finally " + n.Finally.String() } return s + ")" } // JS writes JavaScript to writer. func (n TryStmt) JS(w io.Writer) { w.Write([]byte("try ")) n.Body.JS(w) if n.Catch != nil { w.Write([]byte(" catch")) if n.Binding != nil { w.Write([]byte("(")) n.Binding.JS(w) w.Write([]byte(")")) } w.Write([]byte(" ")) n.Catch.JS(w) } if n.Finally != nil { w.Write([]byte(" finally ")) n.Finally.JS(w) } } // DebuggerStmt is a debugger statement. type DebuggerStmt struct{} func (n DebuggerStmt) String() string { return "Stmt(debugger)" } // JS writes JavaScript to writer. func (n DebuggerStmt) JS(w io.Writer) { w.Write([]byte("debugger;")) } // Alias is a name space import or import/export specifier for import/export statements. type Alias struct { Name []byte // can be nil Binding []byte // can be nil } func (alias Alias) String() string { s := "" if alias.Name != nil { s += string(alias.Name) + " as " } return s + string(alias.Binding) } // JS writes JavaScript to writer. func (alias Alias) JS(w io.Writer) { if alias.Name != nil { w.Write(alias.Name) w.Write([]byte(" as ")) } w.Write(alias.Binding) } // ImportStmt is an import statement. type ImportStmt struct { List []Alias Default []byte // can be nil Module []byte } func (n ImportStmt) String() string { s := "Stmt(import" if n.Default != nil { s += " " + string(n.Default) if n.List != nil { s += " ," } } if len(n.List) == 1 && len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' { s += " " + n.List[0].String() } else if n.List != nil { s += " {" for i, item := range n.List { if i != 0 { s += " ," } if item.Binding != nil { s += " " + item.String() } } s += " }" } if n.Default != nil || n.List != nil { s += " from" } return s + " " + string(n.Module) + ")" } // JS writes JavaScript to writer. func (n ImportStmt) JS(w io.Writer) { if wi, ok := w.(parse.Indenter); ok { w = wi.Writer } w.Write([]byte("import")) if n.Default != nil { w.Write([]byte(" ")) w.Write(n.Default) if n.List != nil { w.Write([]byte(",")) } } if len(n.List) == 1 && len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' { w.Write([]byte(" ")) n.List[0].JS(w) } else if n.List != nil { if len(n.List) == 0 { w.Write([]byte(" {}")) } else { w.Write([]byte(" {")) for j, item := range n.List { if j != 0 { w.Write([]byte(",")) } if item.Binding != nil { w.Write([]byte(" ")) item.JS(w) } } w.Write([]byte(" }")) } } if n.Default != nil || n.List != nil { w.Write([]byte(" from")) } w.Write([]byte(" ")) w.Write(n.Module) w.Write([]byte(";")) } // ExportStmt is an export statement. type ExportStmt struct { List []Alias Module []byte // can be nil Default bool Decl IExpr } func (n ExportStmt) String() string { s := "Stmt(export" if n.Decl != nil { if n.Default { s += " default" } return s + " " + n.Decl.String() + ")" } else if len(n.List) == 1 && (len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' || n.List[0].Name == nil && len(n.List[0].Binding) == 1 && n.List[0].Binding[0] == '*') { s += " " + n.List[0].String() } else if 0 < len(n.List) { s += " {" for i, item := range n.List { if i != 0 { s += " ," } if item.Binding != nil { s += " " + item.String() } } s += " }" } if n.Module != nil { s += " from " + string(n.Module) } return s + ")" } // JS writes JavaScript to writer. func (n ExportStmt) JS(w io.Writer) { if wi, ok := w.(parse.Indenter); ok { w = wi.Writer } w.Write([]byte("export")) if n.Decl != nil { if n.Default { w.Write([]byte(" default")) } w.Write([]byte(" ")) n.Decl.JS(w) w.Write([]byte(";")) return } else if len(n.List) == 1 && (len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' || n.List[0].Name == nil && len(n.List[0].Binding) == 1 && n.List[0].Binding[0] == '*') { w.Write([]byte(" ")) n.List[0].JS(w) } else if len(n.List) == 0 { w.Write([]byte(" {}")) } else { w.Write([]byte(" {")) for j, item := range n.List { if j != 0 { w.Write([]byte(",")) } if item.Binding != nil { w.Write([]byte(" ")) item.JS(w) } } w.Write([]byte(" }")) } if n.Module != nil { w.Write([]byte(" from ")) w.Write(n.Module) } w.Write([]byte(";")) } // DirectivePrologueStmt is a string literal at the beginning of a function or module (usually "use strict"). type DirectivePrologueStmt struct { Value []byte } func (n DirectivePrologueStmt) String() string { return "Stmt(" + string(n.Value) + ")" } // JS writes JavaScript to writer. func (n DirectivePrologueStmt) JS(w io.Writer) { if wi, ok := w.(parse.Indenter); ok { w = wi.Writer } w.Write(n.Value) w.Write([]byte(";")) } func (n Comment) stmtNode() {} func (n BlockStmt) stmtNode() {} func (n EmptyStmt) stmtNode() {} func (n ExprStmt) stmtNode() {} func (n IfStmt) stmtNode() {} func (n DoWhileStmt) stmtNode() {} func (n WhileStmt) stmtNode() {} func (n ForStmt) stmtNode() {} func (n ForInStmt) stmtNode() {} func (n ForOfStmt) stmtNode() {} func (n SwitchStmt) stmtNode() {} func (n BranchStmt) stmtNode() {} func (n ReturnStmt) stmtNode() {} func (n WithStmt) stmtNode() {} func (n LabelledStmt) stmtNode() {} func (n ThrowStmt) stmtNode() {} func (n TryStmt) stmtNode() {} func (n DebuggerStmt) stmtNode() {} func (n ImportStmt) stmtNode() {} func (n ExportStmt) stmtNode() {} func (n DirectivePrologueStmt) stmtNode() {} //////////////////////////////////////////////////////////////// // PropertyName is a property name for binding properties, method names, and in object literals. type PropertyName struct { Literal LiteralExpr Computed IExpr // can be nil } // IsSet returns true is PropertyName is not nil. func (n PropertyName) IsSet() bool { return n.IsComputed() || n.Literal.TokenType != ErrorToken } // IsComputed returns true if PropertyName is computed. func (n PropertyName) IsComputed() bool { return n.Computed != nil } // IsIdent returns true if PropertyName equals the given identifier name. func (n PropertyName) IsIdent(data []byte) bool { return !n.IsComputed() && n.Literal.TokenType == IdentifierToken && bytes.Equal(data, n.Literal.Data) } func (n PropertyName) String() string { if n.Computed != nil { val := n.Computed.String() if val[0] == '(' { return "[" + val[1:len(val)-1] + "]" } return "[" + val + "]" } return string(n.Literal.Data) } // JS writes JavaScript to writer. func (n PropertyName) JS(w io.Writer) { if n.Computed != nil { w.Write([]byte("[")) n.Computed.JS(w) w.Write([]byte("]")) return } if wi, ok := w.(parse.Indenter); ok { w = wi.Writer } w.Write(n.Literal.Data) } // BindingArray is an array binding pattern. type BindingArray struct { List []BindingElement Rest IBinding // can be nil } func (n BindingArray) String() string { s := "[" for i, item := range n.List { if i != 0 { s += "," } s += " " + item.String() } if n.Rest != nil { if len(n.List) != 0 { s += "," } s += " ...Binding(" + n.Rest.String() + ")" } else if 0 < len(n.List) && n.List[len(n.List)-1].Binding == nil { s += "," } return s + " ]" } // JS writes JavaScript to writer. func (n BindingArray) JS(w io.Writer) { w.Write([]byte("[")) for j, item := range n.List { if j != 0 { w.Write([]byte(",")) } if item.Binding != nil { if j != 0 { w.Write([]byte(" ")) } item.JS(w) } } if n.Rest != nil { if len(n.List) != 0 { w.Write([]byte(", ")) } w.Write([]byte("...")) n.Rest.JS(w) } else if 0 < len(n.List) && n.List[len(n.List)-1].Binding == nil { w.Write([]byte(",")) } w.Write([]byte("]")) } // BindingObjectItem is a binding property. type BindingObjectItem struct { Key *PropertyName // can be nil Value BindingElement } func (n BindingObjectItem) String() string { s := "" if n.Key != nil { if v, ok := n.Value.Binding.(*Var); !ok || !n.Key.IsIdent(v.Data) { s += " " + n.Key.String() + ":" } } return s + " " + n.Value.String() } // JS writes JavaScript to writer. func (n BindingObjectItem) JS(w io.Writer) { if n.Key != nil { if v, ok := n.Value.Binding.(*Var); !ok || !n.Key.IsIdent(v.Data) { n.Key.JS(w) w.Write([]byte(": ")) } } n.Value.JS(w) } // BindingObject is an object binding pattern. type BindingObject struct { List []BindingObjectItem Rest *Var // can be nil } func (n BindingObject) String() string { s := "{" for i, item := range n.List { if i != 0 { s += "," } s += item.String() } if n.Rest != nil { if len(n.List) != 0 { s += "," } s += " ...Binding(" + string(n.Rest.Data) + ")" } return s + " }" } // JS writes JavaScript to writer. func (n BindingObject) JS(w io.Writer) { w.Write([]byte("{")) for j, item := range n.List { if j != 0 { w.Write([]byte(", ")) } item.JS(w) } if n.Rest != nil { if len(n.List) != 0 { w.Write([]byte(", ")) } w.Write([]byte("...")) w.Write(n.Rest.Data) } w.Write([]byte("}")) } // BindingElement is a binding element. type BindingElement struct { Binding IBinding // can be nil (in case of ellision) Default IExpr // can be nil } func (n BindingElement) String() string { if n.Binding == nil { return "Binding()" } s := "Binding(" + n.Binding.String() if n.Default != nil { s += " = " + n.Default.String() } return s + ")" } // JS writes JavaScript to writer. func (n BindingElement) JS(w io.Writer) { if n.Binding == nil { return } n.Binding.JS(w) if n.Default != nil { w.Write([]byte(" = ")) n.Default.JS(w) } } func (v *Var) bindingNode() {} func (n BindingArray) bindingNode() {} func (n BindingObject) bindingNode() {} //////////////////////////////////////////////////////////////// // VarDecl is a variable statement or lexical declaration. type VarDecl struct { TokenType List []BindingElement Scope *Scope InFor, InForInOf bool } func (n VarDecl) String() string { s := "Decl(" + n.TokenType.String() for _, item := range n.List { s += " " + item.String() } return s + ")" } // JS writes JavaScript to writer. func (n VarDecl) JS(w io.Writer) { w.Write(n.TokenType.Bytes()) for j, item := range n.List { if j != 0 { w.Write([]byte(",")) } w.Write([]byte(" ")) item.JS(w) } } // Params is a list of parameters for functions, methods, and arrow function. type Params struct { List []BindingElement Rest IBinding // can be nil } func (n Params) String() string { s := "Params(" for i, item := range n.List { if i != 0 { s += ", " } s += item.String() } if n.Rest != nil { if len(n.List) != 0 { s += ", " } s += "...Binding(" + n.Rest.String() + ")" } return s + ")" } // JS writes JavaScript to writer. func (n Params) JS(w io.Writer) { w.Write([]byte("(")) for j, item := range n.List { if j != 0 { w.Write([]byte(", ")) } item.JS(w) } if n.Rest != nil { if len(n.List) != 0 { w.Write([]byte(", ")) } w.Write([]byte("...")) n.Rest.JS(w) } w.Write([]byte(")")) } // FuncDecl is an (async) (generator) function declaration or expression. type FuncDecl struct { Async bool Generator bool Name *Var // can be nil Params Params Body BlockStmt } func (n FuncDecl) String() string { s := "Decl(" if n.Async { s += "async function" } else { s += "function" } if n.Generator { s += "*" } if n.Name != nil { s += " " + string(n.Name.Data) } return s + " " + n.Params.String() + " " + n.Body.String() + ")" } // JS writes JavaScript to writer. func (n FuncDecl) JS(w io.Writer) { if n.Async { w.Write([]byte("async function")) } else { w.Write([]byte("function")) } if n.Generator { w.Write([]byte("*")) } if n.Name != nil { w.Write([]byte(" ")) w.Write(n.Name.Data) } n.Params.JS(w) w.Write([]byte(" ")) n.Body.JS(w) } // MethodDecl is a method definition in a class declaration. type MethodDecl struct { Static bool Async bool Generator bool Get bool Set bool Name PropertyName Params Params Body BlockStmt } func (n MethodDecl) String() string { s := "" if n.Static { s += " static" } if n.Async { s += " async" } if n.Generator { s += " *" } if n.Get { s += " get" } if n.Set { s += " set" } s += " " + n.Name.String() + " " + n.Params.String() + " " + n.Body.String() return "Method(" + s[1:] + ")" } // JS writes JavaScript to writer. func (n MethodDecl) JS(w io.Writer) { writen := false if n.Static { w.Write([]byte("static")) writen = true } if n.Async { if writen { w.Write([]byte(" ")) } w.Write([]byte("async")) writen = true } if n.Generator { if writen { w.Write([]byte(" ")) } w.Write([]byte("*")) writen = true } if n.Get { if writen { w.Write([]byte(" ")) } w.Write([]byte("get")) writen = true } if n.Set { if writen { w.Write([]byte(" ")) } w.Write([]byte("set")) writen = true } if writen { w.Write([]byte(" ")) } n.Name.JS(w) w.Write([]byte(" ")) n.Params.JS(w) w.Write([]byte(" ")) n.Body.JS(w) } // Field is a field definition in a class declaration. type Field struct { Static bool Name PropertyName Init IExpr } func (n Field) String() string { s := "Field(" if n.Static { s += "static " } s += n.Name.String() if n.Init != nil { s += " = " + n.Init.String() } return s + ")" } // JS writes JavaScript to writer. func (n Field) JS(w io.Writer) { if n.Static { w.Write([]byte("static ")) } n.Name.JS(w) if n.Init != nil { w.Write([]byte(" = ")) n.Init.JS(w) } } // ClassElement is a class element that is either a static block, a field definition, or a class method type ClassElement struct { StaticBlock *BlockStmt // can be nil Method *MethodDecl // can be nil Field } func (n ClassElement) String() string { if n.StaticBlock != nil { return "Static(" + n.StaticBlock.String() + ")" } else if n.Method != nil { return n.Method.String() } return n.Field.String() } // JS writes JavaScript to writer. func (n ClassElement) JS(w io.Writer) { if n.StaticBlock != nil { w.Write([]byte("static ")) n.StaticBlock.JS(w) return } else if n.Method != nil { n.Method.JS(w) return } n.Field.JS(w) w.Write([]byte(";")) } // ClassDecl is a class declaration. type ClassDecl struct { Name *Var // can be nil Extends IExpr // can be nil List []ClassElement } func (n ClassDecl) String() string { s := "Decl(class" if n.Name != nil { s += " " + string(n.Name.Data) } if n.Extends != nil { s += " extends " + n.Extends.String() } for _, item := range n.List { s += " " + item.String() } return s + ")" } // JS writes JavaScript to writer. func (n ClassDecl) JS(w io.Writer) { w.Write([]byte("class")) if n.Name != nil { w.Write([]byte(" ")) w.Write(n.Name.Data) } if n.Extends != nil { w.Write([]byte(" extends ")) n.Extends.JS(w) } if len(n.List) == 0 { w.Write([]byte(" {}")) return } w.Write([]byte(" {")) wi := parse.NewIndenter(w, 4) for _, item := range n.List { wi.Write([]byte("\n")) item.JS(wi) } w.Write([]byte("\n}")) } func (n VarDecl) stmtNode() {} func (n FuncDecl) stmtNode() {} func (n ClassDecl) stmtNode() {} func (n VarDecl) exprNode() {} // not a real IExpr, used for ForInit and ExportDecl func (n FuncDecl) exprNode() {} func (n ClassDecl) exprNode() {} func (n MethodDecl) exprNode() {} // not a real IExpr, used for ObjectExpression PropertyName //////////////////////////////////////////////////////////////// // LiteralExpr can be this, null, boolean, numeric, string, or regular expression literals. type LiteralExpr struct { TokenType Data []byte } func (n LiteralExpr) String() string { return string(n.Data) } // JS writes JavaScript to writer. func (n LiteralExpr) JS(w io.Writer) { if wi, ok := w.(parse.Indenter); ok { w = wi.Writer } w.Write(n.Data) } // JSON writes JSON to writer. func (n LiteralExpr) JSON(w io.Writer) error { if wi, ok := w.(parse.Indenter); ok { w = wi.Writer } if n.TokenType == TrueToken || n.TokenType == FalseToken || n.TokenType == NullToken || n.TokenType == DecimalToken || n.TokenType == IntegerToken { w.Write(n.Data) return nil } else if n.TokenType == StringToken { data := n.Data if n.Data[0] == '\'' { data = parse.Copy(data) data = bytes.ReplaceAll(data, []byte(`\'`), []byte(`'`)) data = bytes.ReplaceAll(data, []byte(`"`), []byte(`\"`)) data[0] = '"' data[len(data)-1] = '"' } w.Write(data) return nil } js := &strings.Builder{} n.JS(js) return fmt.Errorf("%v: literal expression is not valid JSON: %v", ErrInvalidJSON, js.String()) } // Element is an array literal element. type Element struct { Value IExpr // can be nil Spread bool } func (n Element) String() string { s := "" if n.Value != nil { if n.Spread { s += "..." } s += n.Value.String() } return s } // JS writes JavaScript to writer. func (n Element) JS(w io.Writer) { if n.Value != nil { if n.Spread { w.Write([]byte("...")) } n.Value.JS(w) } } // ArrayExpr is an array literal. type ArrayExpr struct { List []Element } func (n ArrayExpr) String() string { s := "[" for i, item := range n.List { if i != 0 { s += ", " } if item.Value != nil { if item.Spread { s += "..." } s += item.Value.String() } } if 0 < len(n.List) && n.List[len(n.List)-1].Value == nil { s += "," } return s + "]" } // JS writes JavaScript to writer. func (n ArrayExpr) JS(w io.Writer) { w.Write([]byte("[")) for j, item := range n.List { if j != 0 { w.Write([]byte(", ")) } if item.Value != nil { if item.Spread { w.Write([]byte("...")) } item.Value.JS(w) } } if 0 < len(n.List) && n.List[len(n.List)-1].Value == nil { w.Write([]byte(",")) } w.Write([]byte("]")) } // JSON writes JSON to writer. func (n ArrayExpr) JSON(w io.Writer) error { w.Write([]byte("[")) for i, item := range n.List { if i != 0 { w.Write([]byte(", ")) } if item.Value == nil || item.Spread { js := &strings.Builder{} n.JS(js) return fmt.Errorf("%v: array literal is not valid JSON: %v", ErrInvalidJSON, js.String()) } if val, ok := item.Value.(JSONer); !ok { js := &strings.Builder{} item.Value.JS(js) return fmt.Errorf("%v: value is not valid JSON: %v", ErrInvalidJSON, js.String()) } else if err := val.JSON(w); err != nil { return err } } w.Write([]byte("]")) return nil } // Property is a property definition in an object literal. type Property struct { // either Name or Spread are set. When Spread is set then Value is AssignmentExpression // if Init is set then Value is IdentifierReference, otherwise it can also be MethodDefinition Name *PropertyName // can be nil Spread bool Value IExpr Init IExpr // can be nil } func (n Property) String() string { s := "" if n.Name != nil { if v, ok := n.Value.(*Var); !ok || !n.Name.IsIdent(v.Data) { s += n.Name.String() + ": " } } else if n.Spread { s += "..." } s += n.Value.String() if n.Init != nil { s += " = " + n.Init.String() } return s } // JS writes JavaScript to writer. func (n Property) JS(w io.Writer) { if n.Name != nil { if v, ok := n.Value.(*Var); !ok || !n.Name.IsIdent(v.Data) { n.Name.JS(w) w.Write([]byte(": ")) } } else if n.Spread { w.Write([]byte("...")) } n.Value.JS(w) if n.Init != nil { w.Write([]byte(" = ")) n.Init.JS(w) } } // JSON writes JSON to writer. func (n Property) JSON(w io.Writer) error { if n.Name == nil || n.Spread || n.Init != nil { js := &strings.Builder{} n.JS(js) return fmt.Errorf("%v: property is not valid JSON: %v", ErrInvalidJSON, js.String()) } else if n.Name.Literal.TokenType == StringToken { _ = n.Name.Literal.JSON(w) } else if n.Name.Literal.TokenType == IdentifierToken || n.Name.Literal.TokenType == IntegerToken || n.Name.Literal.TokenType == DecimalToken { w.Write([]byte(`"`)) w.Write(n.Name.Literal.Data) w.Write([]byte(`"`)) } else { js := &strings.Builder{} n.JS(js) return fmt.Errorf("%v: property is not valid JSON: %v", ErrInvalidJSON, js.String()) } w.Write([]byte(": ")) if val, ok := n.Value.(JSONer); !ok { js := &strings.Builder{} n.Value.JS(js) return fmt.Errorf("%v: value is not valid JSON: %v", ErrInvalidJSON, js.String()) } else if err := val.JSON(w); err != nil { return err } return nil } // ObjectExpr is an object literal. type ObjectExpr struct { List []Property } func (n ObjectExpr) String() string { s := "{" for i, item := range n.List { if i != 0 { s += ", " } s += item.String() } return s + "}" } // JS writes JavaScript to writer. func (n ObjectExpr) JS(w io.Writer) { w.Write([]byte("{")) for j, item := range n.List { if j != 0 { w.Write([]byte(", ")) } item.JS(w) } w.Write([]byte("}")) } // JSON writes JSON to writer. func (n ObjectExpr) JSON(w io.Writer) error { w.Write([]byte("{")) for i, item := range n.List { if i != 0 { w.Write([]byte(", ")) } if err := item.JSON(w); err != nil { return err } } w.Write([]byte("}")) return nil } // TemplatePart is a template head or middle. type TemplatePart struct { Value []byte Expr IExpr } func (n TemplatePart) String() string { return string(n.Value) + n.Expr.String() } // JS writes JavaScript to writer. func (n TemplatePart) JS(w io.Writer) { w.Write(n.Value) n.Expr.JS(w) } // TemplateExpr is a template literal or member/call expression, super property, or optional chain with template literal. type TemplateExpr struct { Tag IExpr // can be nil List []TemplatePart Tail []byte Prec OpPrec Optional bool } func (n TemplateExpr) String() string { s := "" if n.Tag != nil { s += n.Tag.String() if n.Optional { s += "?." } } for _, item := range n.List { s += item.String() } return s + string(n.Tail) } // JS writes JavaScript to writer. func (n TemplateExpr) JS(w io.Writer) { if wi, ok := w.(parse.Indenter); ok { w = wi.Writer } if n.Tag != nil { n.Tag.JS(w) if n.Optional { w.Write([]byte("?.")) } } for _, item := range n.List { item.JS(w) } w.Write(n.Tail) } // JSON writes JSON to writer. func (n TemplateExpr) JSON(w io.Writer) error { if wi, ok := w.(parse.Indenter); ok { w = wi.Writer } if n.Tag != nil || len(n.List) != 0 { js := &strings.Builder{} n.JS(js) return fmt.Errorf("%v: value is not valid JSON: %v", ErrInvalidJSON, js.String()) } // allow template literal string to be converted to normal string (to allow for minified JS) data := parse.Copy(n.Tail) data = bytes.ReplaceAll(data, []byte("\n"), []byte("\\n")) data = bytes.ReplaceAll(data, []byte("\r"), []byte("\\r")) data = bytes.ReplaceAll(data, []byte("\\`"), []byte("`")) data = bytes.ReplaceAll(data, []byte("\\$"), []byte("$")) data = bytes.ReplaceAll(data, []byte(`"`), []byte(`\"`)) data[0] = '"' data[len(data)-1] = '"' w.Write(data) return nil } // GroupExpr is a parenthesized expression. type GroupExpr struct { X IExpr } func (n GroupExpr) String() string { return "(" + n.X.String() + ")" } // JS writes JavaScript to writer. func (n GroupExpr) JS(w io.Writer) { w.Write([]byte("(")) n.X.JS(w) w.Write([]byte(")")) } // IndexExpr is a member/call expression, super property, or optional chain with an index expression. type IndexExpr struct { X IExpr Y IExpr Prec OpPrec Optional bool } func (n IndexExpr) String() string { if n.Optional { return "(" + n.X.String() + "?.[" + n.Y.String() + "])" } return "(" + n.X.String() + "[" + n.Y.String() + "])" } // JS writes JavaScript to writer. func (n IndexExpr) JS(w io.Writer) { n.X.JS(w) if n.Optional { w.Write([]byte("?.[")) } else { w.Write([]byte("[")) } n.Y.JS(w) w.Write([]byte("]")) } // DotExpr is a member/call expression, super property, or optional chain with a dot expression. type DotExpr struct { X IExpr Y LiteralExpr Prec OpPrec Optional bool } func (n DotExpr) String() string { if n.Optional { return "(" + n.X.String() + "?." + n.Y.String() + ")" } return "(" + n.X.String() + "." + n.Y.String() + ")" } // JS writes JavaScript to writer. func (n DotExpr) JS(w io.Writer) { lit, ok := n.X.(*LiteralExpr) group := ok && !n.Optional && (lit.TokenType == DecimalToken || lit.TokenType == IntegerToken) if group { w.Write([]byte("(")) } n.X.JS(w) if n.Optional { w.Write([]byte("?.")) } else { if group { w.Write([]byte(")")) } w.Write([]byte(".")) } n.Y.JS(w) } // NewTargetExpr is a new target meta property. type NewTargetExpr struct{} func (n NewTargetExpr) String() string { return "(new.target)" } // JS writes JavaScript to writer. func (n NewTargetExpr) JS(w io.Writer) { w.Write([]byte("new.target")) } // ImportMetaExpr is a import meta meta property. type ImportMetaExpr struct{} func (n ImportMetaExpr) String() string { return "(import.meta)" } // JS writes JavaScript to writer. func (n ImportMetaExpr) JS(w io.Writer) { w.Write([]byte("import.meta")) } type Arg struct { Value IExpr Rest bool } func (n Arg) String() string { s := "" if n.Rest { s += "..." } return s + n.Value.String() } // JS writes JavaScript to writer. func (n Arg) JS(w io.Writer) { if n.Rest { w.Write([]byte("...")) } n.Value.JS(w) } // Args is a list of arguments as used by new and call expressions. type Args struct { List []Arg } func (n Args) String() string { s := "(" for i, item := range n.List { if i != 0 { s += ", " } s += item.String() } return s + ")" } // JS writes JavaScript to writer. func (n Args) JS(w io.Writer) { for j, item := range n.List { if j != 0 { w.Write([]byte(", ")) } item.JS(w) } } // NewExpr is a new expression or new member expression. type NewExpr struct { X IExpr Args *Args // can be nil } func (n NewExpr) String() string { if n.Args != nil { return "(new " + n.X.String() + n.Args.String() + ")" } return "(new " + n.X.String() + ")" } // JS writes JavaScript to writer. func (n NewExpr) JS(w io.Writer) { w.Write([]byte("new ")) n.X.JS(w) if n.Args != nil { w.Write([]byte("(")) n.Args.JS(w) w.Write([]byte(")")) } else { w.Write([]byte("()")) } } // CallExpr is a call expression. type CallExpr struct { X IExpr Args Args Optional bool } func (n CallExpr) String() string { if n.Optional { return "(" + n.X.String() + "?." + n.Args.String() + ")" } return "(" + n.X.String() + n.Args.String() + ")" } // JS writes JavaScript to writer. func (n CallExpr) JS(w io.Writer) { n.X.JS(w) if n.Optional { w.Write([]byte("?.(")) } else { w.Write([]byte("(")) } n.Args.JS(w) w.Write([]byte(")")) } // UnaryExpr is an update or unary expression. type UnaryExpr struct { Op TokenType X IExpr } func (n UnaryExpr) String() string { if n.Op == PostIncrToken || n.Op == PostDecrToken { return "(" + n.X.String() + n.Op.String() + ")" } else if IsIdentifierName(n.Op) { return "(" + n.Op.String() + " " + n.X.String() + ")" } return "(" + n.Op.String() + n.X.String() + ")" } // JS writes JavaScript to writer. func (n UnaryExpr) JS(w io.Writer) { if n.Op == PostIncrToken || n.Op == PostDecrToken { n.X.JS(w) w.Write(n.Op.Bytes()) return } else if unary, ok := n.X.(*UnaryExpr); ok && (n.Op == PosToken && (unary.Op == PreIncrToken || unary.Op == PosToken) || n.Op == NegToken && (unary.Op == PreDecrToken || unary.Op == NegToken)) || IsIdentifierName(n.Op) { w.Write(n.Op.Bytes()) w.Write([]byte(" ")) n.X.JS(w) return } w.Write(n.Op.Bytes()) n.X.JS(w) } // JSON writes JSON to writer. func (n UnaryExpr) JSON(w io.Writer) error { if lit, ok := n.X.(*LiteralExpr); ok && n.Op == NegToken && (lit.TokenType == DecimalToken || lit.TokenType == IntegerToken) { w.Write([]byte("-")) w.Write(lit.Data) return nil } else if n.Op == NotToken && lit.TokenType == IntegerToken && (lit.Data[0] == '0' || lit.Data[0] == '1') { if lit.Data[0] == '0' { w.Write([]byte("true")) } else { w.Write([]byte("false")) } return nil } js := &strings.Builder{} n.JS(js) return fmt.Errorf("%v: unary expression is not valid JSON: %v", ErrInvalidJSON, js.String()) } // BinaryExpr is a binary expression. type BinaryExpr struct { Op TokenType X, Y IExpr } func (n BinaryExpr) String() string { if IsIdentifierName(n.Op) { return "(" + n.X.String() + " " + n.Op.String() + " " + n.Y.String() + ")" } return "(" + n.X.String() + n.Op.String() + n.Y.String() + ")" } // JS writes JavaScript to writer. func (n BinaryExpr) JS(w io.Writer) { n.X.JS(w) w.Write([]byte(" ")) w.Write(n.Op.Bytes()) w.Write([]byte(" ")) n.Y.JS(w) } // CondExpr is a conditional expression. type CondExpr struct { Cond, X, Y IExpr } func (n CondExpr) String() string { return "(" + n.Cond.String() + " ? " + n.X.String() + " : " + n.Y.String() + ")" } // JS writes JavaScript to writer. func (n CondExpr) JS(w io.Writer) { n.Cond.JS(w) w.Write([]byte(" ? ")) n.X.JS(w) w.Write([]byte(" : ")) n.Y.JS(w) } // YieldExpr is a yield expression. type YieldExpr struct { Generator bool X IExpr // can be nil } func (n YieldExpr) String() string { if n.X == nil { return "(yield)" } s := "(yield" if n.Generator { s += "*" } return s + " " + n.X.String() + ")" } // JS writes JavaScript to writer. func (n YieldExpr) JS(w io.Writer) { w.Write([]byte("yield")) if n.X == nil { return } if n.Generator { w.Write([]byte("*")) } w.Write([]byte(" ")) n.X.JS(w) } // ArrowFunc is an (async) arrow function. type ArrowFunc struct { Async bool Params Params Body BlockStmt } func (n ArrowFunc) String() string { s := "(" if n.Async { s += "async " } return s + n.Params.String() + " => " + n.Body.String() + ")" } // JS writes JavaScript to writer. func (n ArrowFunc) JS(w io.Writer) { if n.Async { w.Write([]byte("async ")) } n.Params.JS(w) w.Write([]byte(" => ")) n.Body.JS(w) } // CommaExpr is a series of comma expressions. type CommaExpr struct { List []IExpr } func (n CommaExpr) String() string { s := "(" for i, item := range n.List { if i != 0 { s += "," } s += item.String() } return s + ")" } // JS writes JavaScript to writer. func (n CommaExpr) JS(w io.Writer) { for j, item := range n.List { if j != 0 { w.Write([]byte(",")) } item.JS(w) } } func (v *Var) exprNode() {} func (n LiteralExpr) exprNode() {} func (n ArrayExpr) exprNode() {} func (n ObjectExpr) exprNode() {} func (n TemplateExpr) exprNode() {} func (n GroupExpr) exprNode() {} func (n DotExpr) exprNode() {} func (n IndexExpr) exprNode() {} func (n NewTargetExpr) exprNode() {} func (n ImportMetaExpr) exprNode() {} func (n NewExpr) exprNode() {} func (n CallExpr) exprNode() {} func (n UnaryExpr) exprNode() {} func (n BinaryExpr) exprNode() {} func (n CondExpr) exprNode() {} func (n YieldExpr) exprNode() {} func (n ArrowFunc) exprNode() {} func (n CommaExpr) exprNode() {} ���������������������������������������parse-2.7.15/js/ast_test.go�������������������������������������������������������������������������0000664�0000000�0000000�00000016731�14632123015�0015516�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import ( "io" "regexp" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) func TestJS(t *testing.T) { var tests = []struct { js string expected string }{ {"if (true) { x(1, 2, 3); }", "if (true) { x(1, 2, 3); }"}, {"if (true) { true; }", "if (true) { true; }"}, {"if (true) { true; } else { false; }", "if (true) { true; } else { false; }"}, {"if (true) { true; } else { if(true) { true; } else { false; } }", "if (true) { true; } else { if (true) { true; } else { false; } }"}, {"do { continue; } while (true);", "do { continue; } while (true);"}, {"do { x = 1; } while (true);", "do { x = 1; } while (true);"}, {"while (true) { true; }", "while (true) { true; }"}, {"while (true) { x = 1; }", "while (true) { x = 1; }"}, {"for ( ; ; ) { true; }", "for ( ; ; ) { true; }"}, {"for (x = 1; ; ) { true; }", "for (x = 1; ; ) { true; }"}, {"for (x = 1; x < 2; ) { true; }", "for (x = 1; x < 2; ) { true; }"}, {"for (x = 1; x < 2; x++) { true; }", "for (x = 1; x < 2; x++) { true; }"}, {"for (x = 1; x < 2; x++) { x = 1; }", "for (x = 1; x < 2; x++) { x = 1; }"}, {"for (var x in [1, 2]) { true; }", "for (var x in [1, 2]) { true; }"}, {"for (var x in [1, 2]) { x = 1; }", "for (var x in [1, 2]) { x = 1; }"}, {"for (const element of [1, 2]) { true; }", "for (const element of [1, 2]) { true; }"}, {"for (const element of [1, 2]) { x = 1; }", "for (const element of [1, 2]) { x = 1; }"}, {"switch (true) { case true: break; case false: false; }", "switch (true) { case true: break; case false: false; }"}, {"switch (true) { case true: x(); break; case false: x(); false; }", "switch (true) { case true: x(); break; case false: x(); false; }"}, {"switch (true) { default: false; }", "switch (true) { default: false; }"}, {"for (i = 0; i < 3; i++) { continue; }", "for (i = 0; i < 3; i++) { continue; }"}, {"for (i = 0; i < 3; i++) { x = 1; }", "for (i = 0; i < 3; i++) { x = 1; }"}, {"function f(){return;}", "function f() { return; }"}, {"function f(){return 1;}", "function f() { return 1; }"}, {"with (true) { true; }", "with (true) { true; }"}, {"with (true) { x = 1; }", "with (true) { x = 1; }"}, {"loop: for (x = 0; x < 1; x++) { true; }", "loop: for (x = 0; x < 1; x++) { true; }"}, {"throw x;", "throw x;"}, {"try { true; } catch(e) { }", "try { true; } catch(e) {}"}, {"try { true; } catch(e) { true; }", "try { true; } catch(e) { true; }"}, {"try { true; } catch(e) { x = 1; }", "try { true; } catch(e) { x = 1; }"}, {"debugger;", "debugger;"}, {"import * as name from 'module-name';", "import * as name from 'module-name';"}, {"import defaultExport from 'module-name';", "import defaultExport from 'module-name';"}, {"import * as name from 'module-name';", "import * as name from 'module-name';"}, {"import { export1 } from 'module-name';", "import { export1 } from 'module-name';"}, {"import { export1 as alias1 } from 'module-name';", "import { export1 as alias1 } from 'module-name';"}, {"import { export1 , export2 } from 'module-name';", "import { export1, export2 } from 'module-name';"}, {"import { foo , bar } from 'module-name/path/to/specific/un-exported/file';", "import { foo, bar } from 'module-name/path/to/specific/un-exported/file';"}, {"import defaultExport, * as name from 'module-name';", "import defaultExport, * as name from 'module-name';"}, {"import 'module-name';", "import 'module-name';"}, {"var promise = import('module-name');", "var promise = import('module-name');"}, {"export { myFunction as default }", "export { myFunction as default };"}, {"export default k = 12;", "export default k = 12;"}, {"'use strict';", "'use strict';"}, {"let [name1, name2 = 6] = z;", "let [name1, name2 = 6] = z;"}, {"let {name1, key2: name2} = z;", "let {name1, key2: name2} = z;"}, {"let [{name: key, ...rest}, ...[c,d=9]] = z;", "let [{name: key, ...rest}, ...[c, d = 9]] = z;"}, {"var x;", "var x;"}, {"var x = 1;", "var x = 1;"}, {"var x, y = [];", "var x, y = [];"}, {"let x;", "let x;"}, {"let x = 1;", "let x = 1;"}, {"const x = 1;", "const x = 1;"}, {"function xyz (a, b) { }", "function xyz(a, b) {}"}, {"function xyz (a, b, ...c) { }", "function xyz(a, b, ...c) {}"}, {"function xyz (a, b) { }", "function xyz(a, b) {}"}, {"class A { field; static get method () { } }", "class A { field; static get method () {} }"}, {"class A { field; }", "class A { field; }"}, {"class A { field = 5; }", "class A { field = 5; }"}, {"class A { field; static get method () { } }", "class A { field; static get method () {} }"}, {"class B extends A { field; static get method () { } }", "class B extends A { field; static get method () {} }"}, {"x = 1;", "x = 1;"}, {"'test';", "'test';"}, {"[1, 2, 3];", "[1, 2, 3];"}, {`x = {x: "value"};`, `x = {x: "value"};`}, {`x = {"x": "value"};`, `x = {x: "value"};`}, {`x = {"1a": 2};`, `x = {"1a": 2};`}, {`x = {x: "value", y: "value"};`, `x = {x: "value", y: "value"};`}, {"x = `value`;", "x = `value`;"}, {"x = `value${'hi'}`;", "x = `value${'hi'}`;"}, {"x = (1 + 1) / 1;", "x = (1 + 1) / 1;"}, {"x = y[1];", "x = y[1];"}, {"x = y.z;", "x = y.z;"}, {"x = new.target;", "x = new.target;"}, {"x = import.meta;", "x = import.meta;"}, {"x(1, 2);", "x(1, 2);"}, {"new x;", "new x();"}, {"new x(1);", "new x(1);"}, {"new Date().getTime();", "new Date().getTime();"}, {"x();", "x();"}, {"x = y?.z;", "x = y?.z;"}, {"x = -a;", "x = -a;"}, {"x = - --a;", "x = - --a;"}, {"a << b;", "a << b;"}, {"a && b;", "a && b;"}, {"a || b;", "a || b;"}, {"x = function* foo (x) { while (x < 2) { yield x; x++; } };", "x = function* foo(x) { while (x < 2) { yield x; x++; } };"}, {"(x) => { y(); };", "(x) => { y(); };"}, {"(x, y) => { z(); };", "(x, y) => { z(); };"}, {"async (x, y) => { z(); };", "async (x, y) => { z(); };"}, {"await x;", "await x;"}, {"export default await x;", "export default await x;"}, {"export let a = await x;", "export let a = await x;"}, {"if(k00)while((0))", "if (k00) while ((0));"}, {"export{};from", "export {}; from;"}, {"import{} from 'a'", "import {} from 'a';"}, {"import o,{} from''", "import o, {} from '';"}, {"if(0)var s;else", "if (0) var s; else;"}, {"async\n()", "async();"}, {"{};;", "{} ;"}, {"{}\n;", "{} ;"}, {"- - --3", "- - --3;"}, {"([,,])=>P", "([,,]) => { return P; };"}, {"(t)=>{//!\n}", "(t) => { //! };"}, // space after //! is newline {"import();", "import();"}, {"0\n.k", "(0).k;"}, {"do//!\n; while(1)", "//! do; while (1);"}, // space after //! is newline {"//!\nn=>{ return n }", "//! (n) => { return n; };"}, // space after //! is newline {"//!\n{//!\n}", "//! { //! }"}, // space after //! is newline {`for(;;)let = 5`, `for ( ; ; ) { (let = 5); }`}, {"{`\n`}", "{ ` `; }"}, // space in template literal is newline } re := regexp.MustCompile("\n *") for _, tt := range tests { t.Run(tt.js, func(t *testing.T) { ast, err := Parse(parse.NewInputString(tt.js), Options{}) if err != io.EOF { test.Error(t, err) } src := ast.JSString() src = re.ReplaceAllString(src, " ") test.String(t, src, tt.expected) }) } } func TestJSON(t *testing.T) { input := `[{"key": [2.5, '\r'], '"': -2E+9}, null, false, true, 5.0e-6, "string", 'stri"ng']` ast, err := Parse(parse.NewInputString(input), Options{}) if err != nil { t.Fatal(err) } json, err := ast.JSONString() if err != nil { t.Fatal(err) } test.String(t, json, `[{"key": [2.5, "\r"], "\"": -2E+9}, null, false, true, 5.0e-6, "string", "stri\"ng"]`) } ���������������������������������������parse-2.7.15/js/benchmark_test.go�������������������������������������������������������������������0000664�0000000�0000000�00000017250�14632123015�0016656�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import ( "bytes" "fmt" "math/rand" "testing" ) var z = 0 var n = []int{4, 12, 20, 30, 40, 50, 60, 150} var randStrings [][]byte var mapStrings []map[string]bool var mapStructStrings []map[string]struct{} var mapInts []map[int]bool var arrayStrings [][]string var arrayBytes [][][]byte var arrayInts [][]int var identifiers [][]byte func helperRandString() string { cs := []byte("abcdefghijklmnopqrstuvwxyz") b := make([]byte, rand.Intn(10)) for i := range b { b[i] = cs[rand.Intn(len(cs))] } return string(b) } func helperCaseBytes() []byte { cs := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") b := make([]byte, 100) for i := range b { b[i] = cs[rand.Intn(len(cs))] } return b } func init() { for j := 0; j < len(n); j++ { ms := map[string]bool{} mss := map[string]struct{}{} mi := map[int]bool{} as := []string{} ab := [][]byte{} ai := []int{} for i := 0; i < n[j]; i++ { s := helperRandString() ms[s] = true mss[s] = struct{}{} mi[i] = true as = append(as, s) ab = append(ab, []byte(s)) ai = append(ai, i) } mapStrings = append(mapStrings, ms) mapStructStrings = append(mapStructStrings, mss) mapInts = append(mapInts, mi) arrayStrings = append(arrayStrings, as) arrayBytes = append(arrayBytes, ab) arrayInts = append(arrayInts, ai) } keywords := []string{"as", "for", "while", "extends", "do", "else", "static"} for j := 0; j < 1000; j++ { randStrings = append(randStrings, []byte(helperRandString())) if rand.Float64() < 0.5 { identifiers = append(identifiers, []byte(keywords[rand.Intn(len(keywords))])) } else { identifiers = append(identifiers, []byte(helperRandString())) } } } func BenchmarkAddMapStrings(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { m := map[string]bool{} for i := 0; i < n[j]; i++ { m[arrayStrings[j][i]] = true } } }) } } func BenchmarkAddMapInts(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { m := map[int]bool{} for i := 0; i < n[j]; i++ { m[arrayInts[j][i]] = true } } }) } } func BenchmarkAddArrayStrings(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { a := []string{} for i := 0; i < n[j]; i++ { a = append(a, arrayStrings[j][i]) } } }) } } func BenchmarkAddArrayBytes(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { a := [][]byte{} for i := 0; i < n[j]; i++ { a = append(a, arrayBytes[j][i]) } } }) } } func BenchmarkAddArrayInts(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { a := []int{} for i := 0; i < n[j]; i++ { a = append(a, arrayInts[j][i]) } } }) } } func BenchmarkLookupMapStrings(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { for i := 0; i < n[j]; i++ { if mapStrings[j][arrayStrings[j][i]] == true { z++ } } } }) } } func BenchmarkLookupMapStructStrings(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { for i := 0; i < n[j]; i++ { if _, ok := mapStructStrings[j][string(arrayBytes[j][i])]; ok { z++ } } } }) } } func BenchmarkLookupMapBytes(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { for i := 0; i < n[j]; i++ { if mapStrings[j][string(arrayBytes[j][i])] == true { z++ } } } }) } } func BenchmarkLookupMapInts(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { for i := 0; i < n[j]; i++ { if mapInts[j][arrayInts[j][i]] == true { z++ } } } }) } } func BenchmarkLookupArrayStrings(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { for i := 0; i < n[j]; i++ { s := arrayStrings[j][i] for _, ss := range arrayStrings[j] { if s == ss { z++ break } } } } }) } } func BenchmarkLookupArrayBytes(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { for i := 0; i < n[j]; i++ { s := arrayBytes[j][i] for _, ss := range arrayBytes[j] { if bytes.Equal(s, ss) { z++ break } } } } }) } } func BenchmarkLookupArrayInts(b *testing.B) { for j := 0; j < len(n); j++ { b.Run(fmt.Sprintf("%v", n[j]), func(b *testing.B) { for k := 0; k < b.N; k++ { for i := 0; i < n[j]; i++ { q := arrayInts[j][i] for _, qq := range arrayInts[j] { if q == qq { z++ break } } } } }) } } type benchRef uint type benchVar struct { ptr *benchVar data []byte } var listAST []interface{} var listPtr []*benchVar var listVar []benchVar func BenchmarkASTPtr(b *testing.B) { for j := 0; j < 3; j++ { b.Run(fmt.Sprintf("%v", j), func(b *testing.B) { for k := 0; k < b.N; k++ { listAST = listAST[:0:0] listPtr = listPtr[:0:0] for _, b := range randStrings { v := &benchVar{nil, b} listAST = append(listAST, v) listPtr = append(listPtr, v) } } }) } } func BenchmarkASTIdx(b *testing.B) { for j := 0; j < 3; j++ { b.Run(fmt.Sprintf("%v", j), func(b *testing.B) { for k := 0; k < b.N; k++ { listAST = listAST[:0:0] listPtr = listPtr[:0:0] listVar = listVar[:0:0] for _, b := range randStrings { listVar = append(listVar, benchVar{nil, b}) v := &listVar[len(listVar)-1] ref := benchRef(len(listVar) - 1) listAST = append(listAST, ref) listPtr = append(listPtr, v) } } }) } } var listInterface []interface{} var listVars []*Var func BenchmarkInterfaceAddPtr(b *testing.B) { listInterface = listInterface[:0:0] for k := 0; k < b.N; k++ { v := &Var{nil, nil, 0, 0} listInterface = append(listInterface, v) } } //func BenchmarkInterfaceAddVal32(b *testing.B) { // listInterface = listInterface[:0:0] // for k := 0; k < b.N; k++ { // v := &Var{0, nil, nil, 0} // listInterface = append(listInterface, v.Ref) // } //} // //func BenchmarkInterfaceAddVal64(b *testing.B) { // listInterface = listInterface[:0:0] // for k := 0; k < b.N; k++ { // v := &Var{0, nil, nil, 0} // listInterface = append(listInterface, uint64(v.Ref)) // } //} func BenchmarkInterfaceCheckPtr(b *testing.B) { v := &Var{nil, nil, 0, 0} i := interface{}(v) for k := 0; k < b.N; k++ { if r, ok := i.(*Var); ok { _ = r z++ } } } //func BenchmarkInterfaceCheckVal(b *testing.B) { // ref := VarRef(300) // i := interface{}(ref) // for k := 0; k < b.N; k++ { // if r, ok := i.(VarRef); ok { // _ = r // z++ // } // } //} //////////////////////////////////////////////////////////////// func BenchmarkMap(b *testing.B) { for k := 0; k < b.N; k++ { for _, ident := range identifiers { _ = Keywords[string(ident)] } } } func BenchmarkCompareCase1(b *testing.B) { v := helperCaseBytes() b.ResetTimer() for k := 0; k < b.N; k++ { for _, c := range v { if c == 'x' || c == 'X' { z++ } } } } func BenchmarkCompareCase2(b *testing.B) { v := helperCaseBytes() b.ResetTimer() for k := 0; k < b.N; k++ { for _, c := range v { if c|0x20 == 'x' { z++ } } } } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/lex.go������������������������������������������������������������������������������0000664�0000000�0000000�00000051611�14632123015�0014454�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// Package js is an ECMAScript5.1 lexer following the specifications at http://www.ecma-international.org/ecma-262/5.1/. package js import ( "unicode" "unicode/utf8" "github.com/tdewolff/parse/v2" ) var identifierStart = []*unicode.RangeTable{unicode.Lu, unicode.Ll, unicode.Lt, unicode.Lm, unicode.Lo, unicode.Nl, unicode.Other_ID_Start} var identifierContinue = []*unicode.RangeTable{unicode.Lu, unicode.Ll, unicode.Lt, unicode.Lm, unicode.Lo, unicode.Nl, unicode.Mn, unicode.Mc, unicode.Nd, unicode.Pc, unicode.Other_ID_Continue} // IsIdentifierStart returns true if the byte-slice start is the start of an identifier func IsIdentifierStart(b []byte) bool { r, _ := utf8.DecodeRune(b) return r == '$' || r == '\\' || r == '_' || unicode.IsOneOf(identifierStart, r) } // IsIdentifierContinue returns true if the byte-slice start is a continuation of an identifier func IsIdentifierContinue(b []byte) bool { r, _ := utf8.DecodeRune(b) return r == '$' || r == '\\' || r == '\u200C' || r == '\u200D' || unicode.IsOneOf(identifierContinue, r) } // IsIdentifierEnd returns true if the byte-slice end is a start or continuation of an identifier func IsIdentifierEnd(b []byte) bool { r, _ := utf8.DecodeLastRune(b) return r == '$' || r == '\\' || r == '\u200C' || r == '\u200D' || unicode.IsOneOf(identifierContinue, r) } //////////////////////////////////////////////////////////////// // Lexer is the state for the lexer. type Lexer struct { r *parse.Input err error prevLineTerminator bool prevNumericLiteral bool level int templateLevels []int } // NewLexer returns a new Lexer for a given io.Reader. func NewLexer(r *parse.Input) *Lexer { return &Lexer{ r: r, prevLineTerminator: true, level: 0, templateLevels: []int{}, } } // Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned. func (l *Lexer) Err() error { if l.err != nil { return l.err } return l.r.Err() } // RegExp reparses the input stream for a regular expression. It is assumed that we just received DivToken or DivEqToken with Next(). This function will go back and read that as a regular expression. func (l *Lexer) RegExp() (TokenType, []byte) { if 0 < l.r.Offset() && l.r.Peek(-1) == '/' { l.r.Move(-1) } else if 1 < l.r.Offset() && l.r.Peek(-1) == '=' && l.r.Peek(-2) == '/' { l.r.Move(-2) } else { l.err = parse.NewErrorLexer(l.r, "expected / or /=") return ErrorToken, nil } l.r.Skip() // trick to set start = pos if l.consumeRegExpToken() { return RegExpToken, l.r.Shift() } l.err = parse.NewErrorLexer(l.r, "unexpected EOF or newline") return ErrorToken, nil } // Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message. func (l *Lexer) Next() (TokenType, []byte) { l.err = nil // clear error from previous ErrorToken prevLineTerminator := l.prevLineTerminator l.prevLineTerminator = false prevNumericLiteral := l.prevNumericLiteral l.prevNumericLiteral = false // study on 50x jQuery shows: // spaces: 20k // alpha: 16k // newlines: 14.4k // operators: 4k // numbers and dot: 3.6k // (): 3.4k // {}: 1.8k // []: 0.9k // "': 1k // semicolon: 2.4k // colon: 0.8k // comma: 2.4k // slash: 1.4k // `~: almost 0 c := l.r.Peek(0) switch c { case ' ', '\t', '\v', '\f': l.r.Move(1) for l.consumeWhitespace() { } l.prevLineTerminator = prevLineTerminator return WhitespaceToken, l.r.Shift() case '\n', '\r': l.r.Move(1) for l.consumeLineTerminator() { } l.prevLineTerminator = true return LineTerminatorToken, l.r.Shift() case '>', '=', '!', '+', '*', '%', '&', '|', '^', '~', '?': if tt := l.consumeOperatorToken(); tt != ErrorToken { return tt, l.r.Shift() } case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.': if tt := l.consumeNumericToken(); tt != ErrorToken || l.r.Pos() != 0 { l.prevNumericLiteral = true return tt, l.r.Shift() } else if c == '.' { l.r.Move(1) if l.r.Peek(0) == '.' && l.r.Peek(1) == '.' { l.r.Move(2) return EllipsisToken, l.r.Shift() } return DotToken, l.r.Shift() } case ',': l.r.Move(1) return CommaToken, l.r.Shift() case ';': l.r.Move(1) return SemicolonToken, l.r.Shift() case '(': l.level++ l.r.Move(1) return OpenParenToken, l.r.Shift() case ')': l.level-- l.r.Move(1) return CloseParenToken, l.r.Shift() case '/': if tt := l.consumeCommentToken(); tt != ErrorToken || l.err != nil { if l.err != nil { return ErrorToken, nil } return tt, l.r.Shift() } else if tt := l.consumeOperatorToken(); tt != ErrorToken { return tt, l.r.Shift() } case '{': l.level++ l.r.Move(1) return OpenBraceToken, l.r.Shift() case '}': l.level-- if len(l.templateLevels) != 0 && l.level == l.templateLevels[len(l.templateLevels)-1] { return l.consumeTemplateToken(), l.r.Shift() } l.r.Move(1) return CloseBraceToken, l.r.Shift() case ':': l.r.Move(1) return ColonToken, l.r.Shift() case '\'', '"': return l.consumeStringToken(), l.r.Shift() case ']': l.r.Move(1) return CloseBracketToken, l.r.Shift() case '[': l.r.Move(1) return OpenBracketToken, l.r.Shift() case '<', '-': if l.consumeHTMLLikeCommentToken(prevLineTerminator) { return CommentToken, l.r.Shift() } else if tt := l.consumeOperatorToken(); tt != ErrorToken { return tt, l.r.Shift() } case '`': l.templateLevels = append(l.templateLevels, l.level) return l.consumeTemplateToken(), l.r.Shift() case '#': l.r.Move(1) if l.consumeIdentifierToken() { return PrivateIdentifierToken, l.r.Shift() } default: if l.consumeIdentifierToken() { if prevNumericLiteral { l.err = parse.NewErrorLexer(l.r, "unexpected identifier after number") return ErrorToken, nil } else if keyword, ok := Keywords[string(l.r.Lexeme())]; ok { return keyword, l.r.Shift() } return IdentifierToken, l.r.Shift() } if 0xC0 <= c { if l.consumeWhitespace() { for l.consumeWhitespace() { } l.prevLineTerminator = prevLineTerminator return WhitespaceToken, l.r.Shift() } else if l.consumeLineTerminator() { for l.consumeLineTerminator() { } l.prevLineTerminator = true return LineTerminatorToken, l.r.Shift() } } else if c == 0 && l.r.Err() != nil { return ErrorToken, nil } } r, _ := l.r.PeekRune(0) l.err = parse.NewErrorLexer(l.r, "unexpected %s", parse.Printable(r)) l.r.MoveRune() // allow to continue after error return ErrorToken, l.r.Shift() } //////////////////////////////////////////////////////////////// /* The following functions follow the specifications at http://www.ecma-international.org/ecma-262/5.1/ */ func (l *Lexer) consumeWhitespace() bool { c := l.r.Peek(0) if c == ' ' || c == '\t' || c == '\v' || c == '\f' { l.r.Move(1) return true } else if 0xC0 <= c { if r, n := l.r.PeekRune(0); r == '\u00A0' || r == '\uFEFF' || unicode.Is(unicode.Zs, r) { l.r.Move(n) return true } } return false } func (l *Lexer) isLineTerminator() bool { c := l.r.Peek(0) if c == '\n' || c == '\r' { return true } else if c == 0xE2 && l.r.Peek(1) == 0x80 && (l.r.Peek(2) == 0xA8 || l.r.Peek(2) == 0xA9) { return true } return false } func (l *Lexer) consumeLineTerminator() bool { c := l.r.Peek(0) if c == '\n' { l.r.Move(1) return true } else if c == '\r' { if l.r.Peek(1) == '\n' { l.r.Move(2) } else { l.r.Move(1) } return true } else if c == 0xE2 && l.r.Peek(1) == 0x80 && (l.r.Peek(2) == 0xA8 || l.r.Peek(2) == 0xA9) { l.r.Move(3) return true } return false } func (l *Lexer) consumeDigit() bool { if c := l.r.Peek(0); c >= '0' && c <= '9' { l.r.Move(1) return true } return false } func (l *Lexer) consumeHexDigit() bool { if c := l.r.Peek(0); (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') { l.r.Move(1) return true } return false } func (l *Lexer) consumeBinaryDigit() bool { if c := l.r.Peek(0); c == '0' || c == '1' { l.r.Move(1) return true } return false } func (l *Lexer) consumeOctalDigit() bool { if c := l.r.Peek(0); c >= '0' && c <= '7' { l.r.Move(1) return true } return false } func (l *Lexer) consumeUnicodeEscape() bool { if l.r.Peek(0) != '\\' || l.r.Peek(1) != 'u' { return false } mark := l.r.Pos() l.r.Move(2) if c := l.r.Peek(0); c == '{' { l.r.Move(1) if l.consumeHexDigit() { for l.consumeHexDigit() { } if c := l.r.Peek(0); c == '}' { l.r.Move(1) return true } } l.r.Rewind(mark) return false } else if !l.consumeHexDigit() || !l.consumeHexDigit() || !l.consumeHexDigit() || !l.consumeHexDigit() { l.r.Rewind(mark) return false } return true } func (l *Lexer) consumeSingleLineComment() { for { c := l.r.Peek(0) if c == '\r' || c == '\n' || c == 0 && l.r.Err() != nil { break } else if 0xC0 <= c { if r, _ := l.r.PeekRune(0); r == '\u2028' || r == '\u2029' { break } } l.r.Move(1) } } //////////////////////////////////////////////////////////////// func (l *Lexer) consumeHTMLLikeCommentToken(prevLineTerminator bool) bool { c := l.r.Peek(0) if c == '<' && l.r.Peek(1) == '!' && l.r.Peek(2) == '-' && l.r.Peek(3) == '-' { // opening HTML-style single line comment l.r.Move(4) l.consumeSingleLineComment() return true } else if prevLineTerminator && c == '-' && l.r.Peek(1) == '-' && l.r.Peek(2) == '>' { // closing HTML-style single line comment // (only if current line didn't contain any meaningful tokens) l.r.Move(3) l.consumeSingleLineComment() return true } return false } func (l *Lexer) consumeCommentToken() TokenType { c := l.r.Peek(1) if c == '/' { // single line comment l.r.Move(2) l.consumeSingleLineComment() return CommentToken } else if c == '*' { l.r.Move(2) tt := CommentToken for { c := l.r.Peek(0) if c == '*' && l.r.Peek(1) == '/' { l.r.Move(2) break } else if c == 0 && l.r.Err() != nil { l.err = parse.NewErrorLexer(l.r, "unexpected EOF in comment") return ErrorToken } else if l.consumeLineTerminator() { l.prevLineTerminator = true tt = CommentLineTerminatorToken } else { l.r.Move(1) } } return tt } return ErrorToken } var opTokens = map[byte]TokenType{ '=': EqToken, '!': NotToken, '<': LtToken, '>': GtToken, '+': AddToken, '-': SubToken, '*': MulToken, '/': DivToken, '%': ModToken, '&': BitAndToken, '|': BitOrToken, '^': BitXorToken, '~': BitNotToken, '?': QuestionToken, } var opEqTokens = map[byte]TokenType{ '=': EqEqToken, '!': NotEqToken, '<': LtEqToken, '>': GtEqToken, '+': AddEqToken, '-': SubEqToken, '*': MulEqToken, '/': DivEqToken, '%': ModEqToken, '&': BitAndEqToken, '|': BitOrEqToken, '^': BitXorEqToken, } var opOpTokens = map[byte]TokenType{ '<': LtLtToken, '+': IncrToken, '-': DecrToken, '*': ExpToken, '&': AndToken, '|': OrToken, '?': NullishToken, } var opOpEqTokens = map[byte]TokenType{ '<': LtLtEqToken, '*': ExpEqToken, '&': AndEqToken, '|': OrEqToken, '?': NullishEqToken, } func (l *Lexer) consumeOperatorToken() TokenType { c := l.r.Peek(0) l.r.Move(1) if l.r.Peek(0) == '=' { l.r.Move(1) if l.r.Peek(0) == '=' && (c == '!' || c == '=') { l.r.Move(1) if c == '!' { return NotEqEqToken } return EqEqEqToken } return opEqTokens[c] } else if l.r.Peek(0) == c && (c == '+' || c == '-' || c == '*' || c == '&' || c == '|' || c == '?' || c == '<') { l.r.Move(1) if l.r.Peek(0) == '=' && c != '+' && c != '-' { l.r.Move(1) return opOpEqTokens[c] } return opOpTokens[c] } else if c == '?' && l.r.Peek(0) == '.' && (l.r.Peek(1) < '0' || l.r.Peek(1) > '9') { l.r.Move(1) return OptChainToken } else if c == '=' && l.r.Peek(0) == '>' { l.r.Move(1) return ArrowToken } else if c == '>' && l.r.Peek(0) == '>' { l.r.Move(1) if l.r.Peek(0) == '>' { l.r.Move(1) if l.r.Peek(0) == '=' { l.r.Move(1) return GtGtGtEqToken } return GtGtGtToken } else if l.r.Peek(0) == '=' { l.r.Move(1) return GtGtEqToken } return GtGtToken } return opTokens[c] } func (l *Lexer) consumeIdentifierToken() bool { c := l.r.Peek(0) if identifierStartTable[c] { l.r.Move(1) } else if 0xC0 <= c { if r, n := l.r.PeekRune(0); unicode.IsOneOf(identifierStart, r) { l.r.Move(n) } else { return false } } else if !l.consumeUnicodeEscape() { return false } for { c := l.r.Peek(0) if identifierTable[c] { l.r.Move(1) } else if 0xC0 <= c { if r, n := l.r.PeekRune(0); r == '\u200C' || r == '\u200D' || unicode.IsOneOf(identifierContinue, r) { l.r.Move(n) } else { break } } else if !l.consumeUnicodeEscape() { break } } return true } func (l *Lexer) consumeNumericSeparator(f func() bool) bool { if l.r.Peek(0) != '_' { return false } l.r.Move(1) if !f() { l.r.Move(-1) return false } return true } func (l *Lexer) consumeNumericToken() TokenType { // assume to be on 0 1 2 3 4 5 6 7 8 9 . first := l.r.Peek(0) if first == '0' { l.r.Move(1) if l.r.Peek(0) == 'x' || l.r.Peek(0) == 'X' { l.r.Move(1) if l.consumeHexDigit() { for l.consumeHexDigit() || l.consumeNumericSeparator(l.consumeHexDigit) { } if l.r.Peek(0) == 'n' { l.r.Move(1) } return HexadecimalToken } l.r.Move(-1) return IntegerToken } else if l.r.Peek(0) == 'b' || l.r.Peek(0) == 'B' { l.r.Move(1) if l.consumeBinaryDigit() { for l.consumeBinaryDigit() || l.consumeNumericSeparator(l.consumeBinaryDigit) { } if l.r.Peek(0) == 'n' { l.r.Move(1) } return BinaryToken } l.r.Move(-1) return IntegerToken } else if l.r.Peek(0) == 'o' || l.r.Peek(0) == 'O' { l.r.Move(1) if l.consumeOctalDigit() { for l.consumeOctalDigit() || l.consumeNumericSeparator(l.consumeOctalDigit) { } if l.r.Peek(0) == 'n' { l.r.Move(1) } return OctalToken } l.r.Move(-1) return IntegerToken } else if l.r.Peek(0) == 'n' { l.r.Move(1) return IntegerToken } else if '0' <= l.r.Peek(0) && l.r.Peek(0) <= '9' { l.err = parse.NewErrorLexer(l.r, "legacy octal numbers are not supported") return ErrorToken } } else if first != '.' { for l.consumeDigit() || l.consumeNumericSeparator(l.consumeDigit) { } } // we have parsed a 0 or an integer number c := l.r.Peek(0) if c == '.' { l.r.Move(1) if l.consumeDigit() { for l.consumeDigit() || l.consumeNumericSeparator(l.consumeDigit) { } c = l.r.Peek(0) } else if first == '.' { // number starts with a dot and must be followed by digits l.r.Move(-1) return ErrorToken // may be dot or ellipsis } else { c = l.r.Peek(0) } } else if c == 'n' { l.r.Move(1) return IntegerToken } else if c != 'e' && c != 'E' { return IntegerToken } if c == 'e' || c == 'E' { l.r.Move(1) c = l.r.Peek(0) if c == '+' || c == '-' { l.r.Move(1) } if !l.consumeDigit() { l.err = parse.NewErrorLexer(l.r, "invalid number") return ErrorToken } for l.consumeDigit() || l.consumeNumericSeparator(l.consumeDigit) { } } return DecimalToken } func (l *Lexer) consumeStringToken() TokenType { // assume to be on ' or " delim := l.r.Peek(0) l.r.Move(1) for { c := l.r.Peek(0) if c == delim { l.r.Move(1) break } else if c == '\\' { l.r.Move(1) if !l.consumeLineTerminator() { if c := l.r.Peek(0); c == delim || c == '\\' { l.r.Move(1) } } continue } else if c == '\n' || c == '\r' || c == 0 && l.r.Err() != nil { l.err = parse.NewErrorLexer(l.r, "unterminated string literal") return ErrorToken } l.r.Move(1) } return StringToken } func (l *Lexer) consumeRegExpToken() bool { // assume to be on / l.r.Move(1) inClass := false for { c := l.r.Peek(0) if !inClass && c == '/' { l.r.Move(1) break } else if c == '[' { inClass = true } else if c == ']' { inClass = false } else if c == '\\' { l.r.Move(1) if l.isLineTerminator() || l.r.Peek(0) == 0 && l.r.Err() != nil { return false } } else if l.isLineTerminator() || c == 0 && l.r.Err() != nil { return false } l.r.Move(1) } // flags for { c := l.r.Peek(0) if identifierTable[c] { l.r.Move(1) } else if 0xC0 <= c { if r, n := l.r.PeekRune(0); r == '\u200C' || r == '\u200D' || unicode.IsOneOf(identifierContinue, r) { l.r.Move(n) } else { break } } else { break } } return true } func (l *Lexer) consumeTemplateToken() TokenType { // assume to be on ` or } when already within template continuation := l.r.Peek(0) == '}' l.r.Move(1) for { c := l.r.Peek(0) if c == '`' { l.templateLevels = l.templateLevels[:len(l.templateLevels)-1] l.r.Move(1) if continuation { return TemplateEndToken } return TemplateToken } else if c == '$' && l.r.Peek(1) == '{' { l.level++ l.r.Move(2) if continuation { return TemplateMiddleToken } return TemplateStartToken } else if c == '\\' { l.r.Move(1) if c := l.r.Peek(0); c != 0 { l.r.Move(1) } continue } else if c == 0 && l.r.Err() != nil { l.err = parse.NewErrorLexer(l.r, "unterminated template literal") return ErrorToken } l.r.Move(1) } } var identifierStartTable = [256]bool{ // ASCII false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, // $ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, // A, B, C, D, E, F, G true, true, true, true, true, true, true, true, // H, I, J, K, L, M, N, O true, true, true, true, true, true, true, true, // P, Q, R, S, T, U, V, W true, true, true, false, false, false, false, true, // X, Y, Z, _ false, true, true, true, true, true, true, true, // a, b, c, d, e, f, g true, true, true, true, true, true, true, true, // h, i, j, k, l, m, n, o true, true, true, true, true, true, true, true, // p, q, r, s, t, u, v, w true, true, true, false, false, false, false, false, // x, y, z // non-ASCII false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, } var identifierTable = [256]bool{ // ASCII false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, // $ false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, // 0, 1, 2, 3, 4, 5, 6, 7 true, true, false, false, false, false, false, false, // 8, 9 false, true, true, true, true, true, true, true, // A, B, C, D, E, F, G true, true, true, true, true, true, true, true, // H, I, J, K, L, M, N, O true, true, true, true, true, true, true, true, // P, Q, R, S, T, U, V, W true, true, true, false, false, false, false, true, // X, Y, Z, _ false, true, true, true, true, true, true, true, // a, b, c, d, e, f, g true, true, true, true, true, true, true, true, // h, i, j, k, l, m, n, o true, true, true, true, true, true, true, true, // p, q, r, s, t, u, v, w true, true, true, false, false, false, false, false, // x, y, z // non-ASCII false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, } �����������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/lex_test.go�������������������������������������������������������������������������0000664�0000000�0000000�00000030214�14632123015�0015507�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import ( "fmt" "io" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) type TTs []TokenType func TestTokens(t *testing.T) { var tokenTests = []struct { js string expected []TokenType }{ {" \t\v\f\u00A0\uFEFF\u2000", TTs{}}, // WhitespaceToken {"\n\r\r\n\u2028\u2029", TTs{LineTerminatorToken}}, {"5.2 .04 1. 2.e3 0x0F 5e99", TTs{DecimalToken, DecimalToken, DecimalToken, DecimalToken, HexadecimalToken, DecimalToken}}, {"2_3 5_4.1_2 1_1n 0o2_3 0b1_1 0xF_F", TTs{IntegerToken, DecimalToken, IntegerToken, OctalToken, BinaryToken, HexadecimalToken}}, {"0o22 0b11", TTs{OctalToken, BinaryToken}}, {"0n 2345n 0o5n 0b1n 0x5n 435.333n", TTs{IntegerToken, IntegerToken, OctalToken, BinaryToken, HexadecimalToken, DecimalToken, ErrorToken}}, {"a = 'string'", TTs{IdentifierToken, EqToken, StringToken}}, {"/*comment*/ //comment", TTs{CommentToken, CommentToken}}, {"{ } ( ) [ ]", TTs{OpenBraceToken, CloseBraceToken, OpenParenToken, CloseParenToken, OpenBracketToken, CloseBracketToken}}, {". ; , < > <= ...", TTs{DotToken, SemicolonToken, CommaToken, LtToken, GtToken, LtEqToken, EllipsisToken}}, {">= == != === !==", TTs{GtEqToken, EqEqToken, NotEqToken, EqEqEqToken, NotEqEqToken}}, {"+ - * / % ** ++ --", TTs{AddToken, SubToken, MulToken, DivToken, ModToken, ExpToken, IncrToken, DecrToken}}, {"<< >> >>> & | ^", TTs{LtLtToken, GtGtToken, GtGtGtToken, BitAndToken, BitOrToken, BitXorToken}}, {"! ~ && || ? : ?? ?.", TTs{NotToken, BitNotToken, AndToken, OrToken, QuestionToken, ColonToken, NullishToken, OptChainToken}}, {"= += -= *= **= /= %= <<=", TTs{EqToken, AddEqToken, SubEqToken, MulEqToken, ExpEqToken, DivEqToken, ModEqToken, LtLtEqToken}}, {">>= >>>= &= |= ^= =>", TTs{GtGtEqToken, GtGtGtEqToken, BitAndEqToken, BitOrEqToken, BitXorEqToken, ArrowToken}}, {"&&= ||= ??=", TTs{AndEqToken, OrEqToken, NullishEqToken}}, {"?.5", TTs{QuestionToken, DecimalToken}}, {"?.a", TTs{OptChainToken, IdentifierToken}}, {"await break case catch class const continue", TTs{AwaitToken, BreakToken, CaseToken, CatchToken, ClassToken, ConstToken, ContinueToken}}, {"debugger default delete do else enum export extends", TTs{DebuggerToken, DefaultToken, DeleteToken, DoToken, ElseToken, EnumToken, ExportToken, ExtendsToken}}, {"false finally for function if import in instanceof", TTs{FalseToken, FinallyToken, ForToken, FunctionToken, IfToken, ImportToken, InToken, InstanceofToken}}, {"new null return super switch this throw true", TTs{NewToken, NullToken, ReturnToken, SuperToken, SwitchToken, ThisToken, ThrowToken, TrueToken}}, {"try typeof var void while with yield", TTs{TryToken, TypeofToken, VarToken, VoidToken, WhileToken, WithToken, YieldToken}}, {"implements interface let package private protected public static", TTs{ImplementsToken, InterfaceToken, LetToken, PackageToken, PrivateToken, ProtectedToken, PublicToken, StaticToken}}, {"as async from get meta of set target", TTs{AsToken, AsyncToken, FromToken, GetToken, MetaToken, OfToken, SetToken, TargetToken}}, {"#ident", TTs{PrivateIdentifierToken}}, {"/*co\nm\u2028m/*ent*/ //co//mment\u2029//comment", TTs{CommentLineTerminatorToken, CommentToken, LineTerminatorToken, CommentToken}}, {"<!-", TTs{LtToken, NotToken, SubToken}}, {"1<!--2\n", TTs{IntegerToken, CommentToken, LineTerminatorToken}}, {"x=y-->10\n", TTs{IdentifierToken, EqToken, IdentifierToken, DecrToken, GtToken, IntegerToken, LineTerminatorToken}}, {" /*comment*/ -->nothing\n", TTs{CommentToken, DecrToken, GtToken, IdentifierToken, LineTerminatorToken}}, {"1 /*comment\nmultiline*/ -->nothing\n", TTs{IntegerToken, CommentLineTerminatorToken, CommentToken, LineTerminatorToken}}, {"$ _\u200C \\u2000 _\\u200C \u200C", TTs{IdentifierToken, IdentifierToken, IdentifierToken, IdentifierToken, ErrorToken}}, {">>>=>>>>=", TTs{GtGtGtEqToken, GtGtGtToken, GtEqToken}}, {"1/", TTs{IntegerToken, DivToken}}, {"1/=", TTs{IntegerToken, DivEqToken}}, {"'str\\i\\'ng'", TTs{StringToken}}, {"'str\\\\'abc", TTs{StringToken, IdentifierToken}}, {"'str\\\ni\\\\u00A0ng'", TTs{StringToken}}, {"'str\u2028\u2029ing'", TTs{StringToken}}, {"0b0101 0o0707 0b17", TTs{BinaryToken, OctalToken, BinaryToken, IntegerToken}}, {"`template`", TTs{TemplateToken}}, {"`a${x+y}b`", TTs{TemplateStartToken, IdentifierToken, AddToken, IdentifierToken, TemplateEndToken}}, {"`tmpl${x}tmpl${x}`", TTs{TemplateStartToken, IdentifierToken, TemplateMiddleToken, IdentifierToken, TemplateEndToken}}, {"`temp\nlate`", TTs{TemplateToken}}, {"`outer${{x: 10}}bar${ raw`nested${2}endnest` }end`", TTs{TemplateStartToken, OpenBraceToken, IdentifierToken, ColonToken, IntegerToken, CloseBraceToken, TemplateMiddleToken, IdentifierToken, TemplateStartToken, IntegerToken, TemplateEndToken, TemplateEndToken}}, {"`tmpl ${ a ? '' : `tmpl2 ${b ? 'b' : 'c'}` }`", TTs{TemplateStartToken, IdentifierToken, QuestionToken, StringToken, ColonToken, TemplateStartToken, IdentifierToken, QuestionToken, StringToken, ColonToken, StringToken, TemplateEndToken, TemplateEndToken}}, // early endings {"'string", TTs{ErrorToken}}, {"'\n", TTs{ErrorToken}}, {"'\u2028", TTs{ErrorToken}}, {"'str\\\U00100000ing\\0'", TTs{StringToken}}, {"'strin\\00g'", TTs{StringToken}}, {"/*comment", TTs{ErrorToken}}, {"a=/regexp", TTs{IdentifierToken, EqToken, DivToken, IdentifierToken}}, {"\\u002", TTs{ErrorToken}}, {"`template", TTs{ErrorToken}}, {"`template${x}template", TTs{TemplateStartToken, IdentifierToken, ErrorToken}}, {"a++=1", TTs{IdentifierToken, IncrToken, EqToken, IntegerToken}}, {"a++==1", TTs{IdentifierToken, IncrToken, EqEqToken, IntegerToken}}, {"a++===1", TTs{IdentifierToken, IncrToken, EqEqEqToken, IntegerToken}}, // null characters {"'string\x00'return", TTs{StringToken, ReturnToken}}, {"//comment\x00comment\nreturn", TTs{CommentToken, LineTerminatorToken, ReturnToken}}, {"/*comment\x00*/return", TTs{CommentToken, ReturnToken}}, {"`template\x00`return", TTs{TemplateToken, ReturnToken}}, {"`template\\\x00`return", TTs{TemplateToken, ReturnToken}}, // numbers {"0xg", TTs{IntegerToken, ErrorToken}}, {"0bg", TTs{IntegerToken, ErrorToken}}, {"0og", TTs{IntegerToken, ErrorToken}}, {"010", TTs{ErrorToken}}, // Decimal(0) Decimal(10) Identifier(xF) {"50e+-0", TTs{ErrorToken}}, {"5.a", TTs{DecimalToken, ErrorToken}}, {"5..a", TTs{DecimalToken, DotToken, IdentifierToken}}, // coverage {"Ø a〉", TTs{IdentifierToken, IdentifierToken, ErrorToken}}, {"\u00A0\uFEFF\u2000", TTs{}}, {"\u2028\u2029", TTs{LineTerminatorToken}}, {"\\u0029ident", TTs{IdentifierToken}}, {"\\u{0029FEF}ident", TTs{IdentifierToken}}, {"\\u{}", TTs{ErrorToken}}, {"\\ugident", TTs{ErrorToken}}, {"'str\ring'", TTs{ErrorToken}}, {"a=/\\\n", TTs{IdentifierToken, EqToken, DivToken, ErrorToken}}, {"a=/x\n", TTs{IdentifierToken, EqToken, DivToken, IdentifierToken, LineTerminatorToken}}, {"`\\``", TTs{TemplateToken}}, {"`\\${ 1 }`", TTs{TemplateToken}}, {"`\\\r\n`", TTs{TemplateToken}}, // go fuzz {"`", TTs{ErrorToken}}, // issues {"_\u00bare_unicode_escape_identifier", TTs{IdentifierToken}}, // tdewolff/minify#449 } for _, tt := range tokenTests { t.Run(tt.js, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.js)) i := 0 tokens := []TokenType{} for { token, _ := l.Next() if token == ErrorToken { if l.Err() != io.EOF { tokens = append(tokens, token) } break } else if token == WhitespaceToken { continue } tokens = append(tokens, token) i++ } test.T(t, tokens, tt.expected, "token types must match") }) } // coverage for _, start := range []int{0, 0x0100, 0x0200, 0x0600, 0x0800} { for i := start; ; i++ { if TokenType(i).String() == fmt.Sprintf("Invalid(%d)", i) { break } } } test.That(t, IsPunctuator(CommaToken)) test.That(t, IsPunctuator(GtGtEqToken)) test.That(t, !IsPunctuator(WhileToken)) test.That(t, !IsOperator(CommaToken)) test.That(t, IsOperator(GtGtEqToken)) test.That(t, !IsOperator(WhileToken)) test.That(t, !IsIdentifier(CommaToken)) test.That(t, !IsIdentifier(GtGtEqToken)) test.That(t, IsReservedWord(WhileToken)) test.That(t, IsIdentifier(AsyncToken)) test.That(t, IsIdentifierName(WhileToken)) test.That(t, IsIdentifierName(AsToken)) test.That(t, IsIdentifierStart([]byte("a"))) test.That(t, !IsIdentifierStart([]byte("6"))) test.That(t, !IsIdentifierStart([]byte("["))) test.That(t, IsIdentifierContinue([]byte("a"))) test.That(t, IsIdentifierContinue([]byte("6"))) test.That(t, !IsIdentifierContinue([]byte("["))) test.That(t, IsIdentifierEnd([]byte(".a"))) test.That(t, IsIdentifierEnd([]byte(".6"))) test.That(t, !IsIdentifierEnd([]byte(".["))) } func TestRegExp(t *testing.T) { var tokenTests = []struct { js string expected []TokenType }{ {"a = /[a-z/]/g", TTs{IdentifierToken, EqToken, RegExpToken}}, {"a=/=/g1", TTs{IdentifierToken, EqToken, RegExpToken}}, {"a = /'\\\\/\n", TTs{IdentifierToken, EqToken, RegExpToken, LineTerminatorToken}}, {"a=/\\//g1", TTs{IdentifierToken, EqToken, RegExpToken}}, {"new RegExp(a + /\\d{1,2}/.source)", TTs{NewToken, IdentifierToken, OpenParenToken, IdentifierToken, AddToken, RegExpToken, DotToken, IdentifierToken, CloseParenToken}}, {"a=/regexp\x00/;return", TTs{IdentifierToken, EqToken, RegExpToken, SemicolonToken, ReturnToken}}, {"a=/regexp\\\x00/;return", TTs{IdentifierToken, EqToken, RegExpToken, SemicolonToken, ReturnToken}}, {"a=/x/\u200C\u3009", TTs{IdentifierToken, EqToken, RegExpToken, ErrorToken}}, {"a=/end", TTs{IdentifierToken, EqToken, ErrorToken}}, {"a=/\\\nend", TTs{IdentifierToken, EqToken, ErrorToken}}, {"a=/\\\u2028", TTs{IdentifierToken, EqToken, ErrorToken}}, {"a=/regexp/Ø", TTs{IdentifierToken, EqToken, RegExpToken}}, } for _, tt := range tokenTests { t.Run(tt.js, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.js)) i := 0 tokens := []TokenType{} for { token, _ := l.Next() if token == DivToken || token == DivEqToken { token, _ = l.RegExp() } if token == ErrorToken { if l.Err() != io.EOF { tokens = append(tokens, token) } break } else if token == WhitespaceToken { continue } tokens = append(tokens, token) i++ } test.T(t, tokens, tt.expected, "token types must match") }) } token, _ := NewLexer(parse.NewInputString("")).RegExp() test.T(t, token, ErrorToken) } func TestOffset(t *testing.T) { z := parse.NewInputString(`var i=5;`) l := NewLexer(z) test.T(t, z.Offset(), 0) _, _ = l.Next() test.T(t, z.Offset(), 3) // var _, _ = l.Next() test.T(t, z.Offset(), 4) // ws _, _ = l.Next() test.T(t, z.Offset(), 5) // i _, _ = l.Next() test.T(t, z.Offset(), 6) // = _, _ = l.Next() test.T(t, z.Offset(), 7) // 5 _, _ = l.Next() test.T(t, z.Offset(), 8) // ; } func TestLexerErrors(t *testing.T) { var tests = []struct { js string err string }{ {"@", "unexpected @"}, {"\x00", "unexpected 0x00"}, {"\x7f", "unexpected 0x7F"}, {"\u200F", "unexpected U+200F"}, {"\u2010", "unexpected \u2010"}, {".0E", "invalid number"}, {`"a`, "unterminated string literal"}, {"'a\nb'", "unterminated string literal"}, {"`", "unterminated template literal"}, } for _, tt := range tests { t.Run(tt.js, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.js)) for l.Err() == nil { l.Next() } test.T(t, l.Err().(*parse.Error).Message, tt.err) }) } l := NewLexer(parse.NewInputString("")) l.RegExp() test.T(t, l.Err().(*parse.Error).Message, "expected / or /=") l = NewLexer(parse.NewInputString("/")) l.Next() l.RegExp() test.T(t, l.Err().(*parse.Error).Message, "unexpected EOF or newline") // see #118 l = NewLexer(parse.NewInputString("\uFFFDa")) tt, data := l.Next() test.T(t, tt, ErrorToken) test.T(t, string(data), "�") test.T(t, l.Err().(*parse.Error).Message, "unexpected �") tt, data = l.Next() test.T(t, tt, IdentifierToken) test.T(t, string(data), "a") } //////////////////////////////////////////////////////////////// func ExampleNewLexer() { l := NewLexer(parse.NewInputString("var x = 'lorem ipsum';")) out := "" for { tt, data := l.Next() if tt == ErrorToken { break } out += string(data) } fmt.Println(out) // Output: var x = 'lorem ipsum'; } ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/parse.go����������������������������������������������������������������������������0000664�0000000�0000000�00000172650�14632123015�0015005�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import ( "bytes" "errors" "fmt" "io" "github.com/tdewolff/parse/v2" "github.com/tdewolff/parse/v2/buffer" ) type Options struct { WhileToFor bool Inline bool } // Parser is the state for the parser. type Parser struct { l *Lexer o Options err error data []byte tt TokenType prevLT bool in, await, yield, deflt, retrn bool assumeArrowFunc bool allowDirectivePrologue bool comments []IStmt stmtLevel int exprLevel int scope *Scope } // Parse returns a JS AST tree of. func Parse(r *parse.Input, o Options) (*AST, error) { ast := &AST{} p := &Parser{ l: NewLexer(r), o: o, tt: WhitespaceToken, // trick so that next() works in: true, await: true, } if o.Inline { p.next() p.retrn = true p.allowDirectivePrologue = true p.enterScope(&ast.BlockStmt.Scope, true) for { if p.tt == ErrorToken { break } ast.BlockStmt.List = append(ast.BlockStmt.List, p.parseStmt(true)) } } else { // catch shebang in first line var shebang []byte if r.Peek(0) == '#' && r.Peek(1) == '!' { r.Move(2) p.l.consumeSingleLineComment() // consume till end-of-line shebang = r.Shift() } // parse JS module p.next() ast.BlockStmt = p.parseModule() if 0 < len(shebang) { ast.BlockStmt.List = append([]IStmt{&Comment{shebang}}, ast.BlockStmt.List...) } } if p.err != nil { offset := p.l.r.Offset() - len(p.data) return nil, parse.NewError(buffer.NewReader(p.l.r.Bytes()), offset, p.err.Error()) } else if p.l.Err() != nil && p.l.Err() != io.EOF { return nil, p.l.Err() } return ast, nil } //////////////////////////////////////////////////////////////// func (p *Parser) next() { p.prevLT = false p.tt, p.data = p.l.Next() Loop: for { switch p.tt { case WhitespaceToken: // no-op case LineTerminatorToken: p.prevLT = true case CommentToken, CommentLineTerminatorToken: if 2 < len(p.data) && p.data[2] == '!' { p.comments = append(p.comments, &Comment{p.data}) } if p.tt == CommentLineTerminatorToken { p.prevLT = true } default: break Loop } p.tt, p.data = p.l.Next() } } func (p *Parser) failMessage(msg string, args ...interface{}) { if p.err == nil { p.err = fmt.Errorf(msg, args...) p.tt = ErrorToken } } func (p *Parser) fail(in string, expected ...TokenType) { if p.err == nil { msg := "unexpected" if 0 < len(expected) { msg = "expected" for i, tt := range expected[:len(expected)-1] { if 0 < i { msg += "," } msg += " " + tt.String() + "" } if 2 < len(expected) { msg += ", or" } else if 1 < len(expected) { msg += " or" } msg += " " + expected[len(expected)-1].String() + " instead of" } if p.tt == ErrorToken { if p.l.Err() == io.EOF { msg += " EOF" } else if lexerErr, ok := p.l.Err().(*parse.Error); ok { msg = lexerErr.Message } else { // does not happen } } else { msg += " " + string(p.data) + "" } if in != "" { msg += " in " + in } p.err = errors.New(msg) p.tt = ErrorToken } } func (p *Parser) consume(in string, tt TokenType) bool { if p.tt != tt { p.fail(in, tt) return false } p.next() return true } func (p *Parser) enterScope(scope *Scope, isFunc bool) *Scope { // create a new scope object and add it to the parent parent := p.scope p.scope = scope *scope = Scope{ Parent: parent, } if isFunc { scope.Func = scope } else if parent != nil { scope.Func = parent.Func } return parent } func (p *Parser) exitScope(parent *Scope) { p.scope.HoistUndeclared() p.scope = parent } func (p *Parser) parseModule() (module BlockStmt) { p.enterScope(&module.Scope, true) p.allowDirectivePrologue = true for { switch p.tt { case ErrorToken: if 0 < len(p.comments) { module.List = append(p.comments, module.List...) p.comments = p.comments[:0] } return case ImportToken: p.next() if p.tt == OpenParenToken { // could be an import call expression left := &LiteralExpr{ImportToken, []byte("import")} p.exprLevel++ suffix := p.parseExpressionSuffix(left, OpExpr, OpCall) p.exprLevel-- module.List = append(module.List, &ExprStmt{suffix}) if !p.prevLT && p.tt == SemicolonToken { p.next() } } else { importStmt := p.parseImportStmt() module.List = append(module.List, &importStmt) } case ExportToken: exportStmt := p.parseExportStmt() module.List = append(module.List, &exportStmt) default: module.List = append(module.List, p.parseStmt(true)) } } } func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) { p.stmtLevel++ if 1000 < p.stmtLevel { p.failMessage("too many nested statements") return nil } allowDirectivePrologue := p.allowDirectivePrologue p.allowDirectivePrologue = false switch tt := p.tt; tt { case OpenBraceToken: stmt = p.parseBlockStmt("block statement") case ConstToken, VarToken: if !allowDeclaration && tt == ConstToken { p.fail("statement") return } p.next() varDecl := p.parseVarDecl(tt, true) stmt = varDecl if !p.prevLT && p.tt != SemicolonToken && p.tt != CloseBraceToken && p.tt != ErrorToken { if tt == ConstToken { p.fail("const declaration") } else { p.fail("var statement") } return } case LetToken: let := p.data p.next() if allowDeclaration && (IsIdentifier(p.tt) || p.tt == YieldToken || p.tt == AwaitToken || p.tt == OpenBracketToken || p.tt == OpenBraceToken) { stmt = p.parseVarDecl(tt, false) if !p.prevLT && p.tt != SemicolonToken && p.tt != CloseBraceToken && p.tt != ErrorToken { p.fail("let declaration") return } } else if p.tt == OpenBracketToken { p.failMessage("unexpected let [ in single-statement context") return } else { // expression stmt = &ExprStmt{p.parseIdentifierExpression(OpExpr, let)} if !p.prevLT && p.tt != SemicolonToken && p.tt != CloseBraceToken && p.tt != ErrorToken { p.fail("expression") return } } case IfToken: p.next() if !p.consume("if statement", OpenParenToken) { return } cond := p.parseExpression(OpExpr) if !p.consume("if statement", CloseParenToken) { return } body := p.parseStmt(false) var elseBody IStmt if p.tt == ElseToken { p.next() elseBody = p.parseStmt(false) } stmt = &IfStmt{cond, body, elseBody} case ContinueToken, BreakToken: tt := p.tt p.next() var label []byte if !p.prevLT && p.isIdentifierReference(p.tt) { label = p.data p.next() } stmt = &BranchStmt{tt, label} case WithToken: p.next() if !p.consume("with statement", OpenParenToken) { return } cond := p.parseExpression(OpExpr) if !p.consume("with statement", CloseParenToken) { return } p.scope.Func.HasWith = true stmt = &WithStmt{cond, p.parseStmt(false)} case DoToken: stmt = &DoWhileStmt{} p.next() body := p.parseStmt(false) if !p.consume("do-while statement", WhileToken) { return } if !p.consume("do-while statement", OpenParenToken) { return } stmt = &DoWhileStmt{p.parseExpression(OpExpr), body} if !p.consume("do-while statement", CloseParenToken) { return } case WhileToken: p.next() if !p.consume("while statement", OpenParenToken) { return } cond := p.parseExpression(OpExpr) if !p.consume("while statement", CloseParenToken) { return } body := p.parseStmt(false) if p.o.WhileToFor { varDecl := &VarDecl{TokenType: VarToken, Scope: p.scope, InFor: true} p.scope.Func.VarDecls = append(p.scope.Func.VarDecls, varDecl) block, ok := body.(*BlockStmt) if !ok { block = &BlockStmt{List: []IStmt{body}} } stmt = &ForStmt{varDecl, cond, nil, block} } else { stmt = &WhileStmt{cond, body} } case ForToken: p.next() await := p.await && p.tt == AwaitToken if await { p.next() } if !p.consume("for statement", OpenParenToken) { return } body := &BlockStmt{} parent := p.enterScope(&body.Scope, false) var init IExpr p.in = false if p.tt == VarToken || p.tt == LetToken || p.tt == ConstToken { tt := p.tt p.next() varDecl := p.parseVarDecl(tt, true) if p.err != nil { return } else if p.tt != SemicolonToken && (1 < len(varDecl.List) || varDecl.List[0].Default != nil) { p.fail("for statement") return } else if p.tt == SemicolonToken && varDecl.List[0].Default == nil { // all but the first item were already verified if _, ok := varDecl.List[0].Binding.(*Var); !ok { p.fail("for statement") return } } init = varDecl } else if await { init = p.parseExpression(OpLHS) } else if p.tt != SemicolonToken { init = p.parseExpression(OpExpr) } p.in = true isLHSExpr := isLHSExpr(init) if isLHSExpr && p.tt == InToken { if await { p.fail("for statement", OfToken) return } p.next() value := p.parseExpression(OpExpr) if !p.consume("for statement", CloseParenToken) { return } p.scope.MarkForStmt() if p.tt == OpenBraceToken { body.List = p.parseStmtList("") } else if p.tt != SemicolonToken { body.List = []IStmt{p.parseStmt(false)} } else { p.next() } if varDecl, ok := init.(*VarDecl); ok { varDecl.InForInOf = true } stmt = &ForInStmt{init, value, body} } else if isLHSExpr && p.tt == OfToken { p.next() value := p.parseExpression(OpAssign) if !p.consume("for statement", CloseParenToken) { return } p.scope.MarkForStmt() if p.tt == OpenBraceToken { body.List = p.parseStmtList("") } else if p.tt != SemicolonToken { body.List = []IStmt{p.parseStmt(false)} } else { p.next() } if varDecl, ok := init.(*VarDecl); ok { varDecl.InForInOf = true } stmt = &ForOfStmt{await, init, value, body} } else if p.tt == SemicolonToken { var cond, post IExpr if await { p.fail("for statement", OfToken) return } p.next() if p.tt != SemicolonToken { cond = p.parseExpression(OpExpr) } if !p.consume("for statement", SemicolonToken) { return } if p.tt != CloseParenToken { post = p.parseExpression(OpExpr) } if !p.consume("for statement", CloseParenToken) { return } p.scope.MarkForStmt() if p.tt == OpenBraceToken { body.List = p.parseStmtList("") } else if p.tt != SemicolonToken { body.List = []IStmt{p.parseStmt(false)} } else { p.next() } if init == nil { varDecl := &VarDecl{TokenType: VarToken, Scope: p.scope, InFor: true} p.scope.Func.VarDecls = append(p.scope.Func.VarDecls, varDecl) init = varDecl } else if varDecl, ok := init.(*VarDecl); ok { varDecl.InFor = true } stmt = &ForStmt{init, cond, post, body} } else if isLHSExpr { p.fail("for statement", InToken, OfToken, SemicolonToken) return } else { p.fail("for statement", SemicolonToken) return } p.exitScope(parent) case SwitchToken: p.next() if !p.consume("switch statement", OpenParenToken) { return } init := p.parseExpression(OpExpr) if !p.consume("switch statement", CloseParenToken) { return } // case block if !p.consume("switch statement", OpenBraceToken) { return } switchStmt := &SwitchStmt{Init: init} parent := p.enterScope(&switchStmt.Scope, false) for { if p.tt == ErrorToken { p.fail("switch statement") return } else if p.tt == CloseBraceToken { p.next() break } clause := p.tt var list IExpr if p.tt == CaseToken { p.next() list = p.parseExpression(OpExpr) } else if p.tt == DefaultToken { p.next() } else { p.fail("switch statement", CaseToken, DefaultToken) return } if !p.consume("switch statement", ColonToken) { return } var stmts []IStmt for p.tt != CaseToken && p.tt != DefaultToken && p.tt != CloseBraceToken && p.tt != ErrorToken { stmts = append(stmts, p.parseStmt(true)) } switchStmt.List = append(switchStmt.List, CaseClause{clause, list, stmts}) } p.exitScope(parent) stmt = switchStmt case FunctionToken: if !allowDeclaration { p.fail("statement") return } stmt = p.parseFuncDecl() case AsyncToken: // async function async := p.data p.next() if p.tt == FunctionToken && !p.prevLT { if !allowDeclaration { p.fail("statement") return } stmt = p.parseAsyncFuncDecl() } else { // expression stmt = &ExprStmt{p.parseAsyncExpression(OpExpr, async)} if !p.prevLT && p.tt != SemicolonToken && p.tt != CloseBraceToken && p.tt != ErrorToken { p.fail("expression") return } } case ClassToken: if !allowDeclaration { p.fail("statement") return } stmt = p.parseClassDecl() case ThrowToken: p.next() if p.prevLT { p.failMessage("unexpected newline in throw statement") return } stmt = &ThrowStmt{p.parseExpression(OpExpr)} case TryToken: p.next() body := p.parseBlockStmt("try statement") var binding IBinding var catch, finally *BlockStmt if p.tt == CatchToken { p.next() catch = &BlockStmt{} parent := p.enterScope(&catch.Scope, false) if p.tt == OpenParenToken { p.next() binding = p.parseBinding(CatchDecl) // local to block scope of catch if !p.consume("try-catch statement", CloseParenToken) { return } } catch.List = p.parseStmtList("try-catch statement") p.exitScope(parent) } else if p.tt != FinallyToken { p.fail("try statement", CatchToken, FinallyToken) return } if p.tt == FinallyToken { p.next() finally = p.parseBlockStmt("try-finally statement") } stmt = &TryStmt{body, binding, catch, finally} case DebuggerToken: stmt = &DebuggerStmt{} p.next() case SemicolonToken: stmt = &EmptyStmt{} p.next() case ErrorToken: stmt = &EmptyStmt{} return default: if p.retrn && p.tt == ReturnToken { p.next() var value IExpr if !p.prevLT && p.tt != SemicolonToken && p.tt != CloseBraceToken && p.tt != ErrorToken { value = p.parseExpression(OpExpr) } stmt = &ReturnStmt{value} } else if p.isIdentifierReference(p.tt) { // LabelledStatement, Expression label := p.data p.next() if p.tt == ColonToken { p.next() prevDeflt := p.deflt if p.tt == FunctionToken { p.deflt = false } stmt = &LabelledStmt{label, p.parseStmt(true)} // allows illegal async function, generator function, let, const, or class declarations p.deflt = prevDeflt } else { // expression stmt = &ExprStmt{p.parseIdentifierExpression(OpExpr, label)} if !p.prevLT && p.tt != SemicolonToken && p.tt != CloseBraceToken && p.tt != ErrorToken { p.fail("expression") return } } } else { // expression stmt = &ExprStmt{p.parseExpression(OpExpr)} if !p.prevLT && p.tt != SemicolonToken && p.tt != CloseBraceToken && p.tt != ErrorToken { p.fail("expression") return } else if lit, ok := stmt.(*ExprStmt).Value.(*LiteralExpr); ok && allowDirectivePrologue && lit.TokenType == StringToken && len(lit.Data) == 12 && bytes.Equal(lit.Data[1:11], []byte("use strict")) { stmt = &DirectivePrologueStmt{lit.Data} p.allowDirectivePrologue = true } } } if !p.prevLT && p.tt == SemicolonToken { p.next() } p.stmtLevel-- return } func (p *Parser) parseStmtList(in string) (list []IStmt) { comments := len(p.comments) if !p.consume(in, OpenBraceToken) { return } for { if p.tt == ErrorToken { p.fail("") return } else if p.tt == CloseBraceToken { p.next() break } list = append(list, p.parseStmt(true)) } if comments < len(p.comments) { list2 := make([]IStmt, 0, len(p.comments)-comments+len(list)) list2 = append(list2, p.comments[comments:]...) list2 = append(list2, list...) list = list2 p.comments = p.comments[:comments] } return } func (p *Parser) parseBlockStmt(in string) (blockStmt *BlockStmt) { blockStmt = &BlockStmt{} parent := p.enterScope(&blockStmt.Scope, false) blockStmt.List = p.parseStmtList(in) p.exitScope(parent) return } func (p *Parser) parseImportStmt() (importStmt ImportStmt) { // assume we're passed import if p.tt == StringToken { importStmt.Module = p.data p.next() } else { expectClause := true if IsIdentifier(p.tt) || p.tt == YieldToken { importStmt.Default = p.data p.next() expectClause = p.tt == CommaToken if expectClause { p.next() } } if expectClause && p.tt == MulToken { star := p.data p.next() if !p.consume("import statement", AsToken) { return } if !IsIdentifier(p.tt) && p.tt != YieldToken { p.fail("import statement", IdentifierToken) return } importStmt.List = []Alias{Alias{star, p.data}} p.next() } else if expectClause && p.tt == OpenBraceToken { p.next() importStmt.List = []Alias{} for IsIdentifierName(p.tt) || p.tt == StringToken { tt := p.tt var name, binding []byte = nil, p.data p.next() if p.tt == AsToken { p.next() if !IsIdentifier(p.tt) && p.tt != YieldToken { p.fail("import statement", IdentifierToken) return } name = binding binding = p.data p.next() } else if !IsIdentifier(tt) && tt != YieldToken || tt == StringToken { p.fail("import statement", IdentifierToken, StringToken) return } importStmt.List = append(importStmt.List, Alias{name, binding}) if p.tt == CommaToken { p.next() if p.tt == CloseBraceToken { importStmt.List = append(importStmt.List, Alias{}) break } } } if !p.consume("import statement", CloseBraceToken) { return } } else if expectClause && importStmt.Default != nil { p.fail("import statement", MulToken, OpenBraceToken) return } else if importStmt.Default == nil { p.fail("import statement", StringToken, IdentifierToken, MulToken, OpenBraceToken) return } if !p.consume("import statement", FromToken) { return } if p.tt != StringToken { p.fail("import statement", StringToken) return } importStmt.Module = p.data p.next() } if p.tt == SemicolonToken { p.next() } return } func (p *Parser) parseExportStmt() (exportStmt ExportStmt) { // assume we're at export p.next() prevYield, prevAwait, prevDeflt := p.yield, p.await, p.deflt p.yield, p.await, p.deflt = false, true, true if p.tt == MulToken || p.tt == OpenBraceToken { if p.tt == MulToken { star := p.data p.next() if p.tt == AsToken { p.next() if !IsIdentifierName(p.tt) && p.tt != StringToken { p.fail("export statement", IdentifierToken, StringToken) return } exportStmt.List = []Alias{Alias{star, p.data}} p.next() } else { exportStmt.List = []Alias{Alias{nil, star}} } if p.tt != FromToken { p.fail("export statement", FromToken) return } } else { p.next() for IsIdentifierName(p.tt) || p.tt == StringToken { var name, binding []byte = nil, p.data p.next() if p.tt == AsToken { p.next() if !IsIdentifierName(p.tt) && p.tt != StringToken { p.fail("export statement", IdentifierToken, StringToken) return } name = binding binding = p.data p.next() } exportStmt.List = append(exportStmt.List, Alias{name, binding}) if p.tt == CommaToken { p.next() if p.tt == CloseBraceToken { exportStmt.List = append(exportStmt.List, Alias{}) break } } } if !p.consume("export statement", CloseBraceToken) { return } } if p.tt == FromToken { p.next() if p.tt != StringToken { p.fail("export statement", StringToken) return } exportStmt.Module = p.data p.next() } } else if p.tt == VarToken || p.tt == ConstToken || p.tt == LetToken { tt := p.tt p.next() exportStmt.Decl = p.parseVarDecl(tt, false) } else if p.tt == FunctionToken { exportStmt.Decl = p.parseFuncDecl() } else if p.tt == AsyncToken { // async function p.next() if p.tt != FunctionToken || p.prevLT { p.fail("export statement", FunctionToken) return } exportStmt.Decl = p.parseAsyncFuncDecl() } else if p.tt == ClassToken { exportStmt.Decl = p.parseClassDecl() } else if p.tt == DefaultToken { exportStmt.Default = true p.next() if p.tt == FunctionToken { // hoistable declaration exportStmt.Decl = p.parseFuncDecl() } else if p.tt == AsyncToken { // async function or async arrow function async := p.data p.next() if p.tt == FunctionToken && !p.prevLT { // hoistable declaration exportStmt.Decl = p.parseAsyncFuncDecl() } else { // expression exportStmt.Decl = p.parseAsyncExpression(OpAssign, async) } } else if p.tt == ClassToken { exportStmt.Decl = p.parseClassDecl() } else { exportStmt.Decl = p.parseExpression(OpAssign) } } else { p.fail("export statement", MulToken, OpenBraceToken, VarToken, LetToken, ConstToken, FunctionToken, AsyncToken, ClassToken, DefaultToken) return } if p.tt == SemicolonToken { p.next() } p.yield, p.await, p.deflt = prevYield, prevAwait, prevDeflt return } func (p *Parser) parseVarDecl(tt TokenType, canBeHoisted bool) (varDecl *VarDecl) { // assume we're past var, let or const varDecl = &VarDecl{ TokenType: tt, Scope: p.scope, } declType := LexicalDecl if tt == VarToken { declType = VariableDecl if canBeHoisted { p.scope.Func.VarDecls = append(p.scope.Func.VarDecls, varDecl) } } for { // binding element, var declaration in for-in or for-of can never have a default var bindingElement BindingElement bindingElement.Binding = p.parseBinding(declType) if p.tt == EqToken { p.next() bindingElement.Default = p.parseExpression(OpAssign) } else if _, ok := bindingElement.Binding.(*Var); !ok && (p.in || 0 < len(varDecl.List)) { p.fail("var statement", EqToken) return } else if tt == ConstToken && (p.in || !p.in && p.tt != OfToken && p.tt != InToken) { p.fail("const statement", EqToken) } varDecl.List = append(varDecl.List, bindingElement) if p.tt == CommaToken { p.next() } else { break } } return } func (p *Parser) parseFuncParams(in string) (params Params) { // FormalParameters if !p.consume(in, OpenParenToken) { return } for p.tt != CloseParenToken && p.tt != ErrorToken { if p.tt == EllipsisToken { // binding rest element p.next() params.Rest = p.parseBinding(ArgumentDecl) p.consume(in, CloseParenToken) return } params.List = append(params.List, p.parseBindingElement(ArgumentDecl)) if p.tt != CommaToken { break } p.next() } if p.tt != CloseParenToken { p.fail(in) return } p.next() // mark undeclared vars as arguments in `function f(a=b){var b}` where the b's are different vars p.scope.MarkFuncArgs() return } func (p *Parser) parseFuncDecl() (funcDecl *FuncDecl) { return p.parseFunc(false, false) } func (p *Parser) parseAsyncFuncDecl() (funcDecl *FuncDecl) { return p.parseFunc(true, false) } func (p *Parser) parseFuncExpr() (funcDecl *FuncDecl) { return p.parseFunc(false, true) } func (p *Parser) parseAsyncFuncExpr() (funcDecl *FuncDecl) { return p.parseFunc(true, true) } func (p *Parser) parseFunc(async, expr bool) (funcDecl *FuncDecl) { // assume we're at function p.next() funcDecl = &FuncDecl{} funcDecl.Async = async funcDecl.Generator = p.tt == MulToken if funcDecl.Generator { p.next() } var ok bool var name []byte if expr && (IsIdentifier(p.tt) || p.tt == YieldToken || p.tt == AwaitToken) || !expr && p.isIdentifierReference(p.tt) { name = p.data if !expr { funcDecl.Name, ok = p.scope.Declare(FunctionDecl, p.data) if !ok { p.failMessage("identifier %s has already been declared", string(p.data)) return } } p.next() } else if !expr && !p.deflt { p.fail("function declaration", IdentifierToken) return } else if p.tt != OpenParenToken { p.fail("function declaration", IdentifierToken, OpenParenToken) return } parent := p.enterScope(&funcDecl.Body.Scope, true) prevAwait, prevYield, prevRetrn := p.await, p.yield, p.retrn p.await, p.yield, p.retrn = funcDecl.Async, funcDecl.Generator, true if expr && name != nil { funcDecl.Name, _ = p.scope.Declare(ExprDecl, name) // cannot fail } funcDecl.Params = p.parseFuncParams("function declaration") prevAllowDirectivePrologue, prevExprLevel := p.allowDirectivePrologue, p.exprLevel p.allowDirectivePrologue, p.exprLevel = true, 0 funcDecl.Body.List = p.parseStmtList("function declaration") p.allowDirectivePrologue, p.exprLevel = prevAllowDirectivePrologue, prevExprLevel p.await, p.yield, p.retrn = prevAwait, prevYield, prevRetrn p.exitScope(parent) return } func (p *Parser) parseClassDecl() (classDecl *ClassDecl) { return p.parseAnyClass(false) } func (p *Parser) parseClassExpr() (classDecl *ClassDecl) { return p.parseAnyClass(true) } func (p *Parser) parseAnyClass(expr bool) (classDecl *ClassDecl) { // assume we're at class p.next() classDecl = &ClassDecl{} if IsIdentifier(p.tt) || p.tt == YieldToken || p.tt == AwaitToken { if !expr { var ok bool classDecl.Name, ok = p.scope.Declare(LexicalDecl, p.data) if !ok { p.failMessage("identifier %s has already been declared", string(p.data)) return } } else { //classDecl.Name, ok = p.scope.Declare(ExprDecl, p.data) // classes do not register vars classDecl.Name = &Var{p.data, nil, 1, ExprDecl} } p.next() } else if !expr && !p.deflt { p.fail("class declaration", IdentifierToken) return } if p.tt == ExtendsToken { p.next() classDecl.Extends = p.parseExpression(OpLHS) } if !p.consume("class declaration", OpenBraceToken) { return } for { if p.tt == ErrorToken { p.fail("class declaration") return } else if p.tt == SemicolonToken { p.next() continue } else if p.tt == CloseBraceToken { p.next() break } classDecl.List = append(classDecl.List, p.parseClassElement()) } return } func (p *Parser) parseClassElement() ClassElement { method := &MethodDecl{} var data []byte // either static, async, get, or set if p.tt == StaticToken { method.Static = true data = p.data p.next() if p.tt == OpenBraceToken { prevYield, prevAwait, prevRetrn := p.yield, p.await, p.retrn p.yield, p.await, p.retrn = false, true, false elem := ClassElement{StaticBlock: p.parseBlockStmt("class static block")} p.yield, p.await, p.retrn = prevYield, prevAwait, prevRetrn return elem } } if p.tt == MulToken { method.Generator = true p.next() } else if p.tt == AsyncToken { data = p.data p.next() if !p.prevLT { method.Async = true if p.tt == MulToken { method.Generator = true data = nil p.next() } } } else if p.tt == GetToken { method.Get = true data = p.data p.next() } else if p.tt == SetToken { method.Set = true data = p.data p.next() } isField := false if data != nil && p.tt == OpenParenToken { // (static) method name is: static, async, get, or set method.Name.Literal = LiteralExpr{IdentifierToken, data} if method.Async || method.Get || method.Set { method.Async = false method.Get = false method.Set = false } else { method.Static = false } } else if data != nil && (p.tt == EqToken || p.tt == SemicolonToken || p.tt == CloseBraceToken) { // (static) field name is: static, async, get, or set method.Name.Literal = LiteralExpr{IdentifierToken, data} if !method.Async && !method.Get && !method.Set { method.Static = false } isField = true } else { if p.tt == PrivateIdentifierToken { method.Name.Literal = LiteralExpr{p.tt, p.data} p.next() } else { method.Name = p.parsePropertyName("method or field definition") } if (data == nil || method.Static) && p.tt != OpenParenToken { isField = true } } if isField { var init IExpr if p.tt == EqToken { p.next() init = p.parseExpression(OpAssign) } return ClassElement{Field: Field{Static: method.Static, Name: method.Name, Init: init}} } parent := p.enterScope(&method.Body.Scope, true) prevAwait, prevYield, prevRetrn := p.await, p.yield, p.retrn p.await, p.yield, p.retrn = method.Async, method.Generator, true method.Params = p.parseFuncParams("method definition") prevAllowDirectivePrologue, prevExprLevel := p.allowDirectivePrologue, p.exprLevel p.allowDirectivePrologue, p.exprLevel = true, 0 method.Body.List = p.parseStmtList("method function") p.allowDirectivePrologue, p.exprLevel = prevAllowDirectivePrologue, prevExprLevel p.await, p.yield, p.retrn = prevAwait, prevYield, prevRetrn p.exitScope(parent) return ClassElement{Method: method} } func (p *Parser) parsePropertyName(in string) (propertyName PropertyName) { if IsIdentifierName(p.tt) { propertyName.Literal = LiteralExpr{IdentifierToken, p.data} p.next() } else if p.tt == StringToken { // reinterpret string as identifier or number if we can, except for empty strings if isIdent := AsIdentifierName(p.data[1 : len(p.data)-1]); isIdent { propertyName.Literal = LiteralExpr{IdentifierToken, p.data[1 : len(p.data)-1]} } else if isNum := AsDecimalLiteral(p.data[1 : len(p.data)-1]); isNum { propertyName.Literal = LiteralExpr{DecimalToken, p.data[1 : len(p.data)-1]} } else { propertyName.Literal = LiteralExpr{p.tt, p.data} } p.next() } else if IsNumeric(p.tt) { propertyName.Literal = LiteralExpr{p.tt, p.data} p.next() } else if p.tt == OpenBracketToken { p.next() propertyName.Computed = p.parseExpression(OpAssign) if !p.consume(in, CloseBracketToken) { return } } else { p.fail(in, IdentifierToken, StringToken, NumericToken, OpenBracketToken) return } return } func (p *Parser) parseBindingElement(decl DeclType) (bindingElement BindingElement) { // BindingElement bindingElement.Binding = p.parseBinding(decl) if p.tt == EqToken { p.next() bindingElement.Default = p.parseExpression(OpAssign) } return } func (p *Parser) parseBinding(decl DeclType) (binding IBinding) { // BindingIdentifier, BindingPattern if p.isIdentifierReference(p.tt) { var ok bool binding, ok = p.scope.Declare(decl, p.data) if !ok { p.failMessage("identifier %s has already been declared", string(p.data)) return } p.next() } else if p.tt == OpenBracketToken { p.next() array := BindingArray{} if p.tt == CommaToken { array.List = append(array.List, BindingElement{}) } last := 0 for p.tt != CloseBracketToken { // elision for p.tt == CommaToken { p.next() if p.tt == CommaToken { array.List = append(array.List, BindingElement{}) } } // binding rest element if p.tt == EllipsisToken { p.next() array.Rest = p.parseBinding(decl) if p.tt != CloseBracketToken { p.fail("array binding pattern", CloseBracketToken) return } break } else if p.tt == CloseBracketToken { array.List = array.List[:last] break } array.List = append(array.List, p.parseBindingElement(decl)) last = len(array.List) if p.tt != CommaToken && p.tt != CloseBracketToken { p.fail("array binding pattern", CommaToken, CloseBracketToken) return } } p.next() // always CloseBracketToken binding = &array } else if p.tt == OpenBraceToken { p.next() object := BindingObject{} for p.tt != CloseBraceToken { // binding rest property if p.tt == EllipsisToken { p.next() if !p.isIdentifierReference(p.tt) { p.fail("object binding pattern", IdentifierToken) return } var ok bool object.Rest, ok = p.scope.Declare(decl, p.data) if !ok { p.failMessage("identifier %s has already been declared", string(p.data)) return } p.next() if p.tt != CloseBraceToken { p.fail("object binding pattern", CloseBraceToken) return } break } item := BindingObjectItem{} if p.isIdentifierReference(p.tt) { name := p.data item.Key = &PropertyName{LiteralExpr{IdentifierToken, p.data}, nil} p.next() if p.tt == ColonToken { // property name + : + binding element p.next() item.Value = p.parseBindingElement(decl) } else { // single name binding var ok bool item.Key.Literal.Data = parse.Copy(item.Key.Literal.Data) // copy so that renaming doesn't rename the key item.Value.Binding, ok = p.scope.Declare(decl, name) if !ok { p.failMessage("identifier %s has already been declared", string(name)) return } if p.tt == EqToken { p.next() item.Value.Default = p.parseExpression(OpAssign) } } } else { propertyName := p.parsePropertyName("object binding pattern") item.Key = &propertyName if !p.consume("object binding pattern", ColonToken) { return } item.Value = p.parseBindingElement(decl) } object.List = append(object.List, item) if p.tt == CommaToken { p.next() } else if p.tt != CloseBraceToken { p.fail("object binding pattern", CommaToken, CloseBraceToken) return } } p.next() // always CloseBracketToken binding = &object } else { p.fail("binding") return } return } func (p *Parser) parseArrayLiteral() (array ArrayExpr) { // assume we're on [ p.next() prevComma := true for { if p.tt == ErrorToken { p.fail("expression") return } else if p.tt == CloseBracketToken { p.next() break } else if p.tt == CommaToken { if prevComma { array.List = append(array.List, Element{}) } prevComma = true p.next() } else { spread := p.tt == EllipsisToken if spread { p.next() } array.List = append(array.List, Element{p.parseAssignExprOrParam(), spread}) prevComma = false if spread && p.tt != CloseBracketToken { p.assumeArrowFunc = false } } } return } func (p *Parser) parseObjectLiteral() (object ObjectExpr) { // assume we're on { p.next() for { if p.tt == ErrorToken { p.fail("object literal", CloseBraceToken) return } else if p.tt == CloseBraceToken { p.next() break } property := Property{} if p.tt == EllipsisToken { p.next() property.Spread = true property.Value = p.parseAssignExprOrParam() if _, isIdent := property.Value.(*Var); !isIdent || p.tt != CloseBraceToken { p.assumeArrowFunc = false } } else { // try to parse as MethodDefinition, otherwise fall back to PropertyName:AssignExpr or IdentifierReference var data []byte method := MethodDecl{} if p.tt == MulToken { p.next() method.Generator = true } else if p.tt == AsyncToken { data = p.data p.next() if !p.prevLT { method.Async = true if p.tt == MulToken { p.next() method.Generator = true data = nil } } else { method.Name.Literal = LiteralExpr{IdentifierToken, data} data = nil } } else if p.tt == GetToken { data = p.data p.next() method.Get = true } else if p.tt == SetToken { data = p.data p.next() method.Set = true } // PropertyName if data != nil && !method.Generator && (p.tt == EqToken || p.tt == CommaToken || p.tt == CloseBraceToken || p.tt == ColonToken || p.tt == OpenParenToken) { method.Name.Literal = LiteralExpr{IdentifierToken, data} method.Async = false method.Get = false method.Set = false } else if !method.Name.IsSet() { // did not parse async [LT] method.Name = p.parsePropertyName("object literal") if !method.Name.IsSet() { return } } if p.tt == OpenParenToken { // MethodDefinition parent := p.enterScope(&method.Body.Scope, true) prevAwait, prevYield, prevRetrn := p.await, p.yield, p.retrn p.await, p.yield, p.retrn = method.Async, method.Generator, true method.Params = p.parseFuncParams("method definition") method.Body.List = p.parseStmtList("method definition") p.await, p.yield, p.retrn = prevAwait, prevYield, prevRetrn p.exitScope(parent) property.Value = &method p.assumeArrowFunc = false } else if p.tt == ColonToken { // PropertyName : AssignmentExpression p.next() property.Name = &method.Name property.Value = p.parseAssignExprOrParam() } else if method.Name.IsComputed() || !p.isIdentifierReference(method.Name.Literal.TokenType) { p.fail("object literal", ColonToken, OpenParenToken) return } else { // IdentifierReference (= AssignmentExpression)? name := method.Name.Literal.Data method.Name.Literal.Data = parse.Copy(method.Name.Literal.Data) // copy so that renaming doesn't rename the key property.Name = &method.Name // set key explicitly so after renaming the original is still known if p.assumeArrowFunc { var ok bool property.Value, ok = p.scope.Declare(ArgumentDecl, name) if !ok { property.Value = p.scope.Use(name) p.assumeArrowFunc = false } } else { property.Value = p.scope.Use(name) } if p.tt == EqToken { p.next() prevAssumeArrowFunc := p.assumeArrowFunc p.assumeArrowFunc = false property.Init = p.parseExpression(OpAssign) p.assumeArrowFunc = prevAssumeArrowFunc } } } object.List = append(object.List, property) if p.tt == CommaToken { p.next() } else if p.tt != CloseBraceToken { p.fail("object literal") return } } return } func (p *Parser) parseTemplateLiteral(precLeft OpPrec) (template TemplateExpr) { // assume we're on 'Template' or 'TemplateStart' template.Prec = OpMember if precLeft < OpMember { template.Prec = OpCall } for p.tt == TemplateStartToken || p.tt == TemplateMiddleToken { tpl := p.data p.next() template.List = append(template.List, TemplatePart{tpl, p.parseExpression(OpExpr)}) } if p.tt != TemplateToken && p.tt != TemplateEndToken { p.fail("template literal", TemplateToken) return } template.Tail = p.data p.next() // TemplateEndToken return } func (p *Parser) parseArguments() (args Args) { // assume we're on ( p.next() args.List = make([]Arg, 0, 4) for p.tt != CloseParenToken && p.tt != ErrorToken { rest := p.tt == EllipsisToken if rest { p.next() } args.List = append(args.List, Arg{ Value: p.parseExpression(OpAssign), Rest: rest, }) if p.tt != CloseParenToken { if p.tt != CommaToken { p.fail("arguments", CommaToken, CloseParenToken) return } else { p.next() // CommaToken } } } p.consume("arguments", CloseParenToken) return } func (p *Parser) parseAsyncArrowFunc() (arrowFunc *ArrowFunc) { // expect we're at Identifier or Yield or ( arrowFunc = &ArrowFunc{} parent := p.enterScope(&arrowFunc.Body.Scope, true) prevAwait, prevYield := p.await, p.yield p.await, p.yield = true, false if IsIdentifier(p.tt) || !prevYield && p.tt == YieldToken { ref, _ := p.scope.Declare(ArgumentDecl, p.data) // cannot fail p.next() arrowFunc.Params.List = []BindingElement{{Binding: ref}} } else { arrowFunc.Params = p.parseFuncParams("arrow function") // CallExpression of 'async(params)' already handled } arrowFunc.Async = true arrowFunc.Body.List = p.parseArrowFuncBody() p.await, p.yield = prevAwait, prevYield p.exitScope(parent) return } func (p *Parser) parseIdentifierArrowFunc(v *Var) (arrowFunc *ArrowFunc) { // expect we're at => arrowFunc = &ArrowFunc{} parent := p.enterScope(&arrowFunc.Body.Scope, true) prevAwait, prevYield := p.await, p.yield p.await, p.yield = false, false if 1 < v.Uses { v.Uses-- v, _ = p.scope.Declare(ArgumentDecl, parse.Copy(v.Data)) // cannot fail } else { // if v.Uses==1 it must be undeclared and be the last added p.scope.Parent.Undeclared = p.scope.Parent.Undeclared[:len(p.scope.Parent.Undeclared)-1] v.Decl = ArgumentDecl p.scope.Declared = append(p.scope.Declared, v) } arrowFunc.Params.List = []BindingElement{{v, nil}} arrowFunc.Body.List = p.parseArrowFuncBody() p.await, p.yield = prevAwait, prevYield p.exitScope(parent) return } func (p *Parser) parseArrowFuncBody() (list []IStmt) { // expect we're at arrow if p.tt != ArrowToken { p.fail("arrow function", ArrowToken) return } else if p.prevLT { p.fail("expression") return } p.next() // mark undeclared vars as arguments in `function f(a=b){var b}` where the b's are different vars p.scope.MarkFuncArgs() if p.tt == OpenBraceToken { prevIn, prevRetrn := p.in, p.retrn p.in, p.retrn = true, true prevAllowDirectivePrologue, prevExprLevel := p.allowDirectivePrologue, p.exprLevel p.allowDirectivePrologue, p.exprLevel = true, 0 list = p.parseStmtList("arrow function") p.allowDirectivePrologue, p.exprLevel = prevAllowDirectivePrologue, prevExprLevel p.in, p.retrn = prevIn, prevRetrn } else { list = []IStmt{&ReturnStmt{p.parseExpression(OpAssign)}} } return } func (p *Parser) parseIdentifierExpression(prec OpPrec, ident []byte) IExpr { var left IExpr left = p.scope.Use(ident) return p.parseExpressionSuffix(left, prec, OpPrimary) } func (p *Parser) parseAsyncExpression(prec OpPrec, async []byte) IExpr { // IdentifierReference, AsyncFunctionExpression, AsyncGeneratorExpression // CoverCallExpressionAndAsyncArrowHead, AsyncArrowFunction // assume we're at a token after async var left IExpr precLeft := OpPrimary if !p.prevLT && p.tt == FunctionToken { // primary expression left = p.parseAsyncFuncExpr() } else if !p.prevLT && prec <= OpAssign && (p.tt == OpenParenToken || IsIdentifier(p.tt) || p.tt == YieldToken || p.tt == AwaitToken) { // async arrow function expression or call expression if p.tt == AwaitToken || p.yield && p.tt == YieldToken { p.fail("arrow function") return nil } else if p.tt == OpenParenToken { return p.parseParenthesizedExpression(prec, async) } left = p.parseAsyncArrowFunc() precLeft = OpAssign } else { left = p.scope.Use(async) } // can be async(args), async => ..., or e.g. async + ... return p.parseExpressionSuffix(left, prec, precLeft) } // parseExpression parses an expression that has a precedence of prec or higher. func (p *Parser) parseExpression(prec OpPrec) IExpr { p.exprLevel++ if 1000 < p.exprLevel { p.failMessage("too many nested expressions") return nil } // reparse input if we have / or /= as the beginning of a new expression, this should be a regular expression! if p.tt == DivToken || p.tt == DivEqToken { p.tt, p.data = p.l.RegExp() if p.tt == ErrorToken { p.fail("regular expression") return nil } } var left IExpr precLeft := OpPrimary if IsIdentifier(p.tt) && p.tt != AsyncToken { left = p.scope.Use(p.data) p.next() suffix := p.parseExpressionSuffix(left, prec, precLeft) p.exprLevel-- return suffix } else if IsNumeric(p.tt) { left = &LiteralExpr{p.tt, p.data} p.next() suffix := p.parseExpressionSuffix(left, prec, precLeft) p.exprLevel-- return suffix } switch tt := p.tt; tt { case StringToken, ThisToken, NullToken, TrueToken, FalseToken, RegExpToken: left = &LiteralExpr{p.tt, p.data} p.next() case OpenBracketToken: prevIn := p.in p.in = true array := p.parseArrayLiteral() p.in = prevIn left = &array case OpenBraceToken: prevIn := p.in p.in = true object := p.parseObjectLiteral() p.in = prevIn left = &object case OpenParenToken: // parenthesized expression or arrow parameter list if OpAssign < prec { // must be a parenthesized expression p.next() prevIn := p.in p.in = true left = &GroupExpr{p.parseExpression(OpExpr)} p.in = prevIn if !p.consume("expression", CloseParenToken) { return nil } break } suffix := p.parseParenthesizedExpression(prec, nil) p.exprLevel-- return suffix case NotToken, BitNotToken, TypeofToken, VoidToken, DeleteToken: if OpUnary < prec { p.fail("expression") return nil } p.next() left = &UnaryExpr{tt, p.parseExpression(OpUnary)} precLeft = OpUnary case AddToken: if OpUnary < prec { p.fail("expression") return nil } p.next() left = &UnaryExpr{PosToken, p.parseExpression(OpUnary)} precLeft = OpUnary case SubToken: if OpUnary < prec { p.fail("expression") return nil } p.next() left = &UnaryExpr{NegToken, p.parseExpression(OpUnary)} precLeft = OpUnary case IncrToken: if OpUpdate < prec { p.fail("expression") return nil } p.next() left = &UnaryExpr{PreIncrToken, p.parseExpression(OpUnary)} precLeft = OpUnary case DecrToken: if OpUpdate < prec { p.fail("expression") return nil } p.next() left = &UnaryExpr{PreDecrToken, p.parseExpression(OpUnary)} precLeft = OpUnary case AwaitToken: // either accepted as IdentifierReference or as AwaitExpression if p.await && prec <= OpUnary { p.next() left = &UnaryExpr{tt, p.parseExpression(OpUnary)} precLeft = OpUnary } else if p.await { p.fail("expression") return nil } else { left = p.scope.Use(p.data) p.next() } case NewToken: p.next() if p.tt == DotToken { p.next() if !p.consume("new.target expression", TargetToken) { return nil } left = &NewTargetExpr{} precLeft = OpMember } else { newExpr := &NewExpr{p.parseExpression(OpNew), nil} if p.tt == OpenParenToken { args := p.parseArguments() if len(args.List) != 0 { newExpr.Args = &args } precLeft = OpMember } else { precLeft = OpNew } left = newExpr } case ImportToken: // OpMember < prec does never happen left = &LiteralExpr{p.tt, p.data} p.next() if p.tt == DotToken { p.next() if !p.consume("import.meta expression", MetaToken) { return nil } left = &ImportMetaExpr{} precLeft = OpMember } else if p.tt != OpenParenToken { p.fail("import expression", OpenParenToken) return nil } else if OpCall < prec { p.fail("expression") return nil } else { precLeft = OpCall } case SuperToken: // OpMember < prec does never happen left = &LiteralExpr{p.tt, p.data} p.next() if OpCall < prec && p.tt != DotToken && p.tt != OpenBracketToken { p.fail("super expression", OpenBracketToken, DotToken) return nil } else if p.tt != DotToken && p.tt != OpenBracketToken && p.tt != OpenParenToken { p.fail("super expression", OpenBracketToken, OpenParenToken, DotToken) return nil } if OpCall < prec { precLeft = OpMember } else { precLeft = OpCall } case YieldToken: // either accepted as IdentifierReference or as YieldExpression if p.yield && prec <= OpAssign { // YieldExpression p.next() yieldExpr := YieldExpr{} if !p.prevLT { yieldExpr.Generator = p.tt == MulToken if yieldExpr.Generator { p.next() yieldExpr.X = p.parseExpression(OpAssign) } else if p.tt != CloseBraceToken && p.tt != CloseBracketToken && p.tt != CloseParenToken && p.tt != ColonToken && p.tt != CommaToken && p.tt != SemicolonToken { yieldExpr.X = p.parseExpression(OpAssign) } } left = &yieldExpr precLeft = OpAssign } else if p.yield { p.fail("expression") return nil } else { left = p.scope.Use(p.data) p.next() } case AsyncToken: async := p.data p.next() prevIn := p.in p.in = true left = p.parseAsyncExpression(prec, async) p.in = prevIn case ClassToken: prevIn := p.in p.in = true left = p.parseClassExpr() p.in = prevIn case FunctionToken: prevIn := p.in p.in = true left = p.parseFuncExpr() p.in = prevIn case TemplateToken, TemplateStartToken: prevIn := p.in p.in = true template := p.parseTemplateLiteral(precLeft) left = &template p.in = prevIn case PrivateIdentifierToken: if OpCompare < prec || !p.in { p.fail("expression") return nil } left = &LiteralExpr{p.tt, p.data} p.next() if p.tt != InToken { p.fail("relational expression", InToken) return nil } default: p.fail("expression") return nil } suffix := p.parseExpressionSuffix(left, prec, precLeft) p.exprLevel-- return suffix } func (p *Parser) parseExpressionSuffix(left IExpr, prec, precLeft OpPrec) IExpr { for i := 0; ; i++ { if 1000 < p.exprLevel+i { p.failMessage("too many nested expressions") return nil } switch tt := p.tt; tt { case EqToken, MulEqToken, DivEqToken, ModEqToken, ExpEqToken, AddEqToken, SubEqToken, LtLtEqToken, GtGtEqToken, GtGtGtEqToken, BitAndEqToken, BitXorEqToken, BitOrEqToken, AndEqToken, OrEqToken, NullishEqToken: if OpAssign < prec { return left } else if precLeft < OpLHS { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpAssign)} precLeft = OpAssign case LtToken, LtEqToken, GtToken, GtEqToken, InToken, InstanceofToken: if OpCompare < prec || !p.in && tt == InToken { return left } else if precLeft < OpCompare { // can only fail after a yield or arrow function expression p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpShift)} precLeft = OpCompare case EqEqToken, NotEqToken, EqEqEqToken, NotEqEqToken: if OpEquals < prec { return left } else if precLeft < OpEquals { // can only fail after a yield or arrow function expression p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpCompare)} precLeft = OpEquals case AndToken: if OpAnd < prec { return left } else if precLeft < OpAnd { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpBitOr)} precLeft = OpAnd case OrToken: if OpOr < prec { return left } else if precLeft < OpOr { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpAnd)} precLeft = OpOr case NullishToken: if OpCoalesce < prec { return left } else if precLeft < OpBitOr && precLeft != OpCoalesce { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpBitOr)} precLeft = OpCoalesce case DotToken: // OpMember < prec does never happen if precLeft < OpCall { p.fail("expression") return nil } p.next() if !IsIdentifierName(p.tt) && p.tt != PrivateIdentifierToken { p.fail("dot expression", IdentifierToken) return nil } exprPrec := OpMember if precLeft < OpMember { exprPrec = OpCall } if p.tt != PrivateIdentifierToken { p.tt = IdentifierToken } left = &DotExpr{left, LiteralExpr{p.tt, p.data}, exprPrec, false} p.next() if precLeft < OpMember { precLeft = OpCall } else { precLeft = OpMember } case OpenBracketToken: // OpMember < prec does never happen if precLeft < OpCall { p.fail("expression") return nil } p.next() exprPrec := OpMember if precLeft < OpMember { exprPrec = OpCall } prevIn := p.in p.in = true left = &IndexExpr{left, p.parseExpression(OpExpr), exprPrec, false} p.in = prevIn if !p.consume("index expression", CloseBracketToken) { return nil } if precLeft < OpMember { precLeft = OpCall } else { precLeft = OpMember } case OpenParenToken: if OpCall < prec { return left } else if precLeft < OpCall { p.fail("expression") return nil } prevIn := p.in p.in = true left = &CallExpr{left, p.parseArguments(), false} precLeft = OpCall p.in = prevIn case TemplateToken, TemplateStartToken: // OpMember < prec does never happen if precLeft < OpCall { p.fail("expression") return nil } prevIn := p.in p.in = true template := p.parseTemplateLiteral(precLeft) template.Tag = left left = &template if precLeft < OpMember { precLeft = OpCall } else { precLeft = OpMember } p.in = prevIn case OptChainToken: if OpCall < prec { return left } else if precLeft < OpCall { p.fail("expression") return nil } p.next() if p.tt == OpenParenToken { left = &CallExpr{left, p.parseArguments(), true} } else if p.tt == OpenBracketToken { p.next() left = &IndexExpr{left, p.parseExpression(OpExpr), OpCall, true} if !p.consume("optional chaining expression", CloseBracketToken) { return nil } } else if p.tt == TemplateToken || p.tt == TemplateStartToken { template := p.parseTemplateLiteral(precLeft) template.Prec = OpCall template.Tag = left template.Optional = true left = &template } else if IsIdentifierName(p.tt) { left = &DotExpr{left, LiteralExpr{IdentifierToken, p.data}, OpCall, true} p.next() } else if p.tt == PrivateIdentifierToken { left = &DotExpr{left, LiteralExpr{p.tt, p.data}, OpCall, true} p.next() } else { p.fail("optional chaining expression", IdentifierToken, OpenParenToken, OpenBracketToken, TemplateToken) return nil } precLeft = OpCall case IncrToken: if p.prevLT || OpUpdate < prec { return left } else if precLeft < OpLHS { p.fail("expression") return nil } p.next() left = &UnaryExpr{PostIncrToken, left} precLeft = OpUpdate case DecrToken: if p.prevLT || OpUpdate < prec { return left } else if precLeft < OpLHS { p.fail("expression") return nil } p.next() left = &UnaryExpr{PostDecrToken, left} precLeft = OpUpdate case ExpToken: if OpExp < prec { return left } else if precLeft < OpUpdate { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpExp)} precLeft = OpExp case MulToken, DivToken, ModToken: if OpMul < prec { return left } else if precLeft < OpMul { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpExp)} precLeft = OpMul case AddToken, SubToken: if OpAdd < prec { return left } else if precLeft < OpAdd { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpMul)} precLeft = OpAdd case LtLtToken, GtGtToken, GtGtGtToken: if OpShift < prec { return left } else if precLeft < OpShift { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpAdd)} precLeft = OpShift case BitAndToken: if OpBitAnd < prec { return left } else if precLeft < OpBitAnd { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpEquals)} precLeft = OpBitAnd case BitXorToken: if OpBitXor < prec { return left } else if precLeft < OpBitXor { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpBitAnd)} precLeft = OpBitXor case BitOrToken: if OpBitOr < prec { return left } else if precLeft < OpBitOr { p.fail("expression") return nil } p.next() left = &BinaryExpr{tt, left, p.parseExpression(OpBitXor)} precLeft = OpBitOr case QuestionToken: if OpAssign < prec { return left } else if precLeft < OpCoalesce { p.fail("expression") return nil } p.next() prevIn := p.in p.in = true ifExpr := p.parseExpression(OpAssign) p.in = prevIn if !p.consume("conditional expression", ColonToken) { return nil } elseExpr := p.parseExpression(OpAssign) left = &CondExpr{left, ifExpr, elseExpr} precLeft = OpAssign case CommaToken: if OpExpr < prec { return left } p.next() if commaExpr, ok := left.(*CommaExpr); ok { commaExpr.List = append(commaExpr.List, p.parseExpression(OpAssign)) i-- // adjust expression nesting limit } else { left = &CommaExpr{[]IExpr{left, p.parseExpression(OpAssign)}} } precLeft = OpExpr case ArrowToken: // handle identifier => ..., where identifier could also be yield or await if OpAssign < prec { return left } else if precLeft < OpPrimary || p.prevLT { p.fail("expression") return nil } v, ok := left.(*Var) if !ok { p.fail("expression") return nil } left = p.parseIdentifierArrowFunc(v) precLeft = OpAssign default: return left } } } func (p *Parser) parseAssignExprOrParam() IExpr { // this could be a BindingElement or an AssignmentExpression. Here we handle BindingIdentifier with a possible Initializer, BindingPattern will be handled by parseArrayLiteral or parseObjectLiteral if p.assumeArrowFunc && p.isIdentifierReference(p.tt) { tt := p.tt data := p.data p.next() if p.tt == EqToken || p.tt == CommaToken || p.tt == CloseParenToken || p.tt == CloseBraceToken || p.tt == CloseBracketToken { var ok bool var left IExpr left, ok = p.scope.Declare(ArgumentDecl, data) if ok { p.assumeArrowFunc = false left = p.parseExpressionSuffix(left, OpAssign, OpPrimary) p.assumeArrowFunc = true return left } } p.assumeArrowFunc = false if tt == AsyncToken { return p.parseAsyncExpression(OpAssign, data) } return p.parseIdentifierExpression(OpAssign, data) } else if p.tt != OpenBracketToken && p.tt != OpenBraceToken { p.assumeArrowFunc = false } return p.parseExpression(OpAssign) } func (p *Parser) parseParenthesizedExpression(prec OpPrec, async []byte) IExpr { // parse ArrowFunc, AsyncArrowFunc, AsyncCallExpr, ParenthesizedExpr var left IExpr precLeft := OpPrimary // expect to be at ( p.next() isAsync := async != nil // prevLT is false before open parenthesis arrowFunc := &ArrowFunc{} parent := p.enterScope(&arrowFunc.Body.Scope, true) prevAssumeArrowFunc, prevIn := p.assumeArrowFunc, p.in p.assumeArrowFunc, p.in = true, true // parse an Arguments expression but assume we might be parsing an (async) arrow function or ParenthesisedExpression. If this is really an arrow function, parsing as an Arguments expression cannot fail as AssignmentExpression, ArrayLiteral, and ObjectLiteral are supersets of SingleNameBinding, ArrayBindingPattern, and ObjectBindingPattern respectively. Any identifier that would be a BindingIdentifier in case of an arrow function, will be added as such to the scope. If finally this is not an arrow function, we will demote those variables as undeclared and merge them with the parent scope. rests := 0 var args Args for p.tt != CloseParenToken && p.tt != ErrorToken { if 0 < len(args.List) && args.List[len(args.List)-1].Rest { // only last parameter can have ellipsis p.assumeArrowFunc = false if !isAsync { p.fail("arrow function", CloseParenToken) } } rest := p.tt == EllipsisToken if rest { p.next() rests++ } args.List = append(args.List, Arg{p.parseAssignExprOrParam(), rest}) if p.tt != CommaToken { break } p.next() } if p.tt != CloseParenToken { p.fail("expression") return nil } p.next() isArrowFunc := !p.prevLT && p.tt == ArrowToken && p.assumeArrowFunc hasLastRest := 0 < rests && p.assumeArrowFunc p.assumeArrowFunc, p.in = prevAssumeArrowFunc, prevIn if isArrowFunc { prevAwait, prevYield := p.await, p.yield p.await, p.yield = isAsync, false // arrow function arrowFunc.Async = isAsync arrowFunc.Params = Params{List: make([]BindingElement, 0, len(args.List)-rests)} for _, arg := range args.List { if arg.Rest { arrowFunc.Params.Rest = p.exprToBinding(arg.Value) } else { arrowFunc.Params.List = append(arrowFunc.Params.List, p.exprToBindingElement(arg.Value)) // can not fail when assumArrowFunc is set } } arrowFunc.Body.List = p.parseArrowFuncBody() p.await, p.yield = prevAwait, prevYield p.exitScope(parent) left = arrowFunc precLeft = OpAssign } else if !isAsync && (len(args.List) == 0 || hasLastRest) { p.fail("arrow function", ArrowToken) return nil } else if isAsync && OpCall < prec || !isAsync && 0 < rests { p.fail("expression") return nil } else { // for any nested FuncExpr/ArrowFunc scope, Parent will point to the temporary scope created in case this was an arrow function instead of a parenthesized expression. This is not a problem as Parent is only used for defining new variables, and we already parsed all the nested scopes so that Parent (not Func) are not relevant anymore. Anyways, the Parent will just point to an empty scope, whose Parent/Func will point to valid scopes. This should not be a big deal. // Here we move all declared ArgumentDecls (in case of an arrow function) to its parent scope as undeclared variables (identifiers used in a parenthesized expression). p.exitScope(parent) arrowFunc.Body.Scope.UndeclareScope() if isAsync { // call expression left = p.scope.Use(async) left = &CallExpr{left, args, false} precLeft = OpCall } else { // parenthesized expression if 1 < len(args.List) { commaExpr := &CommaExpr{} for _, arg := range args.List { commaExpr.List = append(commaExpr.List, arg.Value) } left = &GroupExpr{commaExpr} } else { left = &GroupExpr{args.List[0].Value} } } } return p.parseExpressionSuffix(left, prec, precLeft) } // exprToBindingElement and exprToBinding convert a CoverParenthesizedExpressionAndArrowParameterList into FormalParameters. // Any unbound variables of the parameters (Initializer, ComputedPropertyName) are kept in the parent scope func (p *Parser) exprToBindingElement(expr IExpr) (bindingElement BindingElement) { if assign, ok := expr.(*BinaryExpr); ok && assign.Op == EqToken { bindingElement.Binding = p.exprToBinding(assign.X) bindingElement.Default = assign.Y } else { bindingElement.Binding = p.exprToBinding(expr) } return } func (p *Parser) exprToBinding(expr IExpr) (binding IBinding) { if expr == nil { // no-op } else if v, ok := expr.(*Var); ok { binding = v } else if array, ok := expr.(*ArrayExpr); ok { bindingArray := BindingArray{} for _, item := range array.List { if item.Spread { // can only BindingIdentifier or BindingPattern bindingArray.Rest = p.exprToBinding(item.Value) break } var bindingElement BindingElement bindingElement = p.exprToBindingElement(item.Value) bindingArray.List = append(bindingArray.List, bindingElement) } binding = &bindingArray } else if object, ok := expr.(*ObjectExpr); ok { bindingObject := BindingObject{} for _, item := range object.List { if item.Spread { // can only be BindingIdentifier bindingObject.Rest = item.Value.(*Var) break } bindingElement := p.exprToBindingElement(item.Value) if v, ok := item.Value.(*Var); item.Name == nil || (ok && item.Name.IsIdent(v.Data)) { // IdentifierReference : Initializer bindingElement.Default = item.Init } bindingObject.List = append(bindingObject.List, BindingObjectItem{Key: item.Name, Value: bindingElement}) } binding = &bindingObject } else { p.failMessage("invalid parameters in arrow function") } return } func (p *Parser) isIdentifierReference(tt TokenType) bool { return IsIdentifier(tt) || !p.yield && tt == YieldToken || !p.await && tt == AwaitToken } ����������������������������������������������������������������������������������������parse-2.7.15/js/parse_test.go�����������������������������������������������������������������������0000664�0000000�0000000�00000156534�14632123015�0016047�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import ( "fmt" "io" "sort" "strings" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) //////////////////////////////////////////////////////////////// func TestParse(t *testing.T) { var tests = []struct { js string expected string }{ // grammar {"", ""}, {"\n", ""}, {"#!/usr/bin/env node", "Stmt(#!/usr/bin/env node)"}, {"/* comment */", ""}, {"/*! comment */", "Stmt(/*! comment */)"}, {"5 + /*! comment */ 6", "Stmt(/*! comment */) Stmt(5+6)"}, {"var a = /*! comment */ 6", "Stmt(/*! comment */) Decl(var Binding(a = 6))"}, {"{}", "Stmt({ })"}, {`"use strict"`, `Stmt("use strict")`}, {"var a = b;", "Decl(var Binding(a = b))"}, {"const a = b;", "Decl(const Binding(a = b))"}, {"let a = b;", "Decl(let Binding(a = b))"}, {"let [a,b] = [1, 2];", "Decl(let Binding([ Binding(a), Binding(b) ] = [1, 2]))"}, {"let [a,[b,c]] = [1, [2, 3]];", "Decl(let Binding([ Binding(a), Binding([ Binding(b), Binding(c) ]) ] = [1, [2, 3]]))"}, {"let [,,c] = [1, 2, 3];", "Decl(let Binding([ Binding(), Binding(), Binding(c) ] = [1, 2, 3]))"}, {"let [a, ...b] = [1, 2, 3];", "Decl(let Binding([ Binding(a), ...Binding(b) ] = [1, 2, 3]))"}, {"let {a, b} = {a: 3, b: 4};", "Decl(let Binding({ Binding(a), Binding(b) } = {a: 3, b: 4}))"}, {"let {a: [b, {c}]} = {a: [5, {c: 3}]};", "Decl(let Binding({ a: Binding([ Binding(b), Binding({ Binding(c) }) ]) } = {a: [5, {c: 3}]}))"}, {"let [a = 2] = [];", "Decl(let Binding([ Binding(a = 2) ] = []))"}, {"let {a: b = 2} = {};", "Decl(let Binding({ a: Binding(b = 2) } = {}))"}, {"var a = 5 * 4 / 3 ** 2 + ( 5 - 3 );", "Decl(var Binding(a = (((5*4)/(3**2))+((5-3)))))"}, {"var a, b = c;", "Decl(var Binding(a) Binding(b = c))"}, {"var a,\nb = c;", "Decl(var Binding(a) Binding(b = c))"}, {";", "Stmt()"}, {"{; var a = 3;}", "Stmt({ Stmt() Decl(var Binding(a = 3)) })"}, {"{a=5}", "Stmt({ Stmt(a=5) })"}, {"break", "Stmt(break)"}, {"break LABEL", "Stmt(break LABEL)"}, {"continue", "Stmt(continue)"}, {"continue LABEL", "Stmt(continue LABEL)"}, {"if (a == 5) continue LABEL", "Stmt(if (a==5) Stmt(continue LABEL))"}, {"if (a == 5) continue LABEL else continue LABEL", "Stmt(if (a==5) Stmt(continue LABEL) else Stmt(continue LABEL))"}, {"if (a) b; else if (c) d;", "Stmt(if a Stmt(b) else Stmt(if c Stmt(d)))"}, {"if (a) 1; else if (b) 2; else 3", "Stmt(if a Stmt(1) else Stmt(if b Stmt(2) else Stmt(3)))"}, {"with (a = 5) continue LABEL", "Stmt(with (a=5) Stmt(continue LABEL))"}, {"do a++; while (a < 4)", "Stmt(do Stmt(a++) while (a<4))"}, {"do {a++} while (a < 4)", "Stmt(do Stmt({ Stmt(a++) }) while (a<4))"}, {"while (a < 4) a++", "Stmt(while (a<4) Stmt(a++))"}, {"for (var a = 0; a < 4; a++) b = a", "Stmt(for Decl(var Binding(a = 0)) ; (a<4) ; (a++) Stmt({ Stmt(b=a) }))"}, {"for (5; a < 4; a++) {}", "Stmt(for 5 ; (a<4) ; (a++) Stmt({ }))"}, {"for (;;) {}", "Stmt(for ; ; Stmt({ }))"}, {"for (a,b=5;;) {}", "Stmt(for (a,(b=5)) ; ; Stmt({ }))"}, {"for (let a;;) {}", "Stmt(for Decl(let Binding(a)) ; ; Stmt({ }))"}, {"for (var a in b) {}", "Stmt(for Decl(var Binding(a)) in b Stmt({ }))"}, {"for (var a in b) c", "Stmt(for Decl(var Binding(a)) in b Stmt({ Stmt(c) }))"}, {"for (var a of b) {}", "Stmt(for Decl(var Binding(a)) of b Stmt({ }))"}, {"for (var a of b) c", "Stmt(for Decl(var Binding(a)) of b Stmt({ Stmt(c) }))"}, {"for ((b in c) in d) {}", "Stmt(for ((b in c)) in d Stmt({ }))"}, {"for (c && (a in b);;) {}", "Stmt(for (c&&((a in b))) ; ; Stmt({ }))"}, {"for (a in b) {}", "Stmt(for a in b Stmt({ }))"}, {"for (a = b;;) {}", "Stmt(for (a=b) ; ; Stmt({ }))"}, {"for (!a;;) {}", "Stmt(for (!a) ; ; Stmt({ }))"}, {"for (var [a] in b) {}", "Stmt(for Decl(var Binding([ Binding(a) ])) in b Stmt({ }))"}, {"for await (async of b);", "Stmt(for await async of b Stmt({ }))"}, {"for (async of => 5;;);", "Stmt(for (async Params(Binding(of)) => Stmt({ Stmt(return 5) })) ; ; Stmt({ }))"}, {"throw 5", "Stmt(throw 5)"}, {"try {} catch {b}", "Stmt(try Stmt({ }) catch Stmt({ Stmt(b) }))"}, {"try {} finally {c}", "Stmt(try Stmt({ }) finally Stmt({ Stmt(c) }))"}, {"try {} catch {b} finally {c}", "Stmt(try Stmt({ }) catch Stmt({ Stmt(b) }) finally Stmt({ Stmt(c) }))"}, {"try {} catch (e) {b}", "Stmt(try Stmt({ }) catch Binding(e) Stmt({ Stmt(b) }))"}, {"debugger", "Stmt(debugger)"}, {"label: var a", "Stmt(label : Decl(var Binding(a)))"}, {"yield: var a", "Stmt(yield : Decl(var Binding(a)))"}, {"function a(){ await: var a }", "Decl(function a Params() Stmt({ Stmt(await : Decl(var Binding(a))) }))"}, {"switch (5) {}", "Stmt(switch 5)"}, {"switch (5) { case 3: {} default: {}}", "Stmt(switch 5 Clause(case 3 Stmt({ })) Clause(default Stmt({ })))"}, {"function a(b) {}", "Decl(function a Params(Binding(b)) Stmt({ }))"}, {"async function a(b) {}", "Decl(async function a Params(Binding(b)) Stmt({ }))"}, {"function* a(b) {}", "Decl(function* a Params(Binding(b)) Stmt({ }))"}, {"function a(b,) {}", "Decl(function a Params(Binding(b)) Stmt({ }))"}, {"function a(b, c) {}", "Decl(function a Params(Binding(b), Binding(c)) Stmt({ }))"}, {"function a(...b) {}", "Decl(function a Params(...Binding(b)) Stmt({ }))"}, {"function a(b, ...c) {}", "Decl(function a Params(Binding(b), ...Binding(c)) Stmt({ }))"}, {"function a(b) {return}", "Decl(function a Params(Binding(b)) Stmt({ Stmt(return) }))"}, {"function a(){return 5*3}", "Decl(function a Params() Stmt({ Stmt(return (5*3)) }))"}, {"class A {}", "Decl(class A)"}, {"class A {;}", "Decl(class A)"}, {"!class{}", "Stmt(!Decl(class))"}, {"class A extends B { }", "Decl(class A extends B)"}, {"class A { a(b) {} }", "Decl(class A Method(a Params(Binding(b)) Stmt({ })))"}, {"class A { 5(b) {} }", "Decl(class A Method(5 Params(Binding(b)) Stmt({ })))"}, {"class A { 'a'(b) {} }", "Decl(class A Method(a Params(Binding(b)) Stmt({ })))"}, {"class A { '5'(b) {} }", "Decl(class A Method(5 Params(Binding(b)) Stmt({ })))"}, {"class A { '%'(b) {} }", "Decl(class A Method('%' Params(Binding(b)) Stmt({ })))"}, {"class A { get() {} }", "Decl(class A Method(get Params() Stmt({ })))"}, {"class A { get a() {} }", "Decl(class A Method(get a Params() Stmt({ })))"}, {"class A { set a(b) {} }", "Decl(class A Method(set a Params(Binding(b)) Stmt({ })))"}, {"class A { * a(b) {} }", "Decl(class A Method(* a Params(Binding(b)) Stmt({ })))"}, {"class A { async a(b) {} }", "Decl(class A Method(async a Params(Binding(b)) Stmt({ })))"}, {"class A { async * a(b) {} }", "Decl(class A Method(async * a Params(Binding(b)) Stmt({ })))"}, {"class A { static() {} }", "Decl(class A Method(static Params() Stmt({ })))"}, {"class A { static a(b) {} }", "Decl(class A Method(static a Params(Binding(b)) Stmt({ })))"}, {"class A { [5](b) {} }", "Decl(class A Method([5] Params(Binding(b)) Stmt({ })))"}, {"class A { field }", "Decl(class A Field(field))"}, {"class A { #field }", "Decl(class A Field(#field))"}, {"class A { static }", "Decl(class A Field(static))"}, {"class A { static field }", "Decl(class A Field(static field))"}, {"class A { field=5 }", "Decl(class A Field(field = 5))"}, {"class A { #field=5 }", "Decl(class A Field(#field = 5))"}, {"class A { static #field=5 }", "Decl(class A Field(static #field = 5))"}, {"class A { #method(){} }", "Decl(class A Method(#method Params() Stmt({ })))"}, {"class A { get }", "Decl(class A Field(get))"}, {"class A { field static get method(){ return 5 } }", "Decl(class A Field(field) Method(static get method Params() Stmt({ Stmt(return 5) })))"}, {"class A { static { this.field = 5 } }", "Decl(class A Static(Stmt({ Stmt((this.field)=5) })))"}, //{"class A { get get get(){} }", "Decl(class A Definition(get) Method(get get Params() Stmt({ })))"}, // doesn't look like this should be supported {"`tmpl`", "Stmt(`tmpl`)"}, {"`tmpl${x}`", "Stmt(`tmpl${x}`)"}, {"`tmpl${x}tmpl${x}`", "Stmt(`tmpl${x}tmpl${x}`)"}, {`import "pkg";`, `Stmt(import "pkg")`}, {`import yield from "pkg"`, `Stmt(import yield from "pkg")`}, {`import * as yield from "pkg"`, `Stmt(import * as yield from "pkg")`}, {`import {yield, for as yield,} from "pkg"`, `Stmt(import { yield , for as yield , } from "pkg")`}, {`import yield, * as yield from "pkg"`, `Stmt(import yield , * as yield from "pkg")`}, {`import yield, {yield} from "pkg"`, `Stmt(import yield , { yield } from "pkg")`}, {`import {yield,} from "pkg"`, `Stmt(import { yield , } from "pkg")`}, {`import {"abc'def" as a} from "pkg"`, `Stmt(import { "abc'def" as a } from "pkg")`}, {`export * from "pkg";`, `Stmt(export * from "pkg")`}, {`export * as for from "pkg"`, `Stmt(export * as for from "pkg")`}, {`export * as "abc'def" from "pkg"`, `Stmt(export * as "abc'def" from "pkg")`}, {`export {if, for as switch} from "pkg"`, `Stmt(export { if , for as switch } from "pkg")`}, {"export {if, for as switch,}", "Stmt(export { if , for as switch , })"}, {`export {"abc'def", "a" as 'b'}`, `Stmt(export { "abc'def" , "a" as 'b' })`}, {"export var a", "Stmt(export Decl(var Binding(a)))"}, {"export function a(b){}", "Stmt(export Decl(function a Params(Binding(b)) Stmt({ })))"}, {"export async function a(b){}", "Stmt(export Decl(async function a Params(Binding(b)) Stmt({ })))"}, {"export class A{}", "Stmt(export Decl(class A))"}, {"export default function(b){}", "Stmt(export default Decl(function Params(Binding(b)) Stmt({ })))"}, {"export default async function(b){}", "Stmt(export default Decl(async function Params(Binding(b)) Stmt({ })))"}, {"export default class{}", "Stmt(export default Decl(class))"}, {"export default a", "Stmt(export default a)"}, {"export default async", "Stmt(export default async)"}, // yield, await, async {"yield\na = 5", "Stmt(yield) Stmt(a=5)"}, {"yield * yield * a", "Stmt((yield*yield)*a)"}, {"(yield) => 5", "Stmt(Params(Binding(yield)) => Stmt({ Stmt(return 5) }))"}, {"function a(){ (await) => 5 }", "Decl(function a Params() Stmt({ Stmt(Params(Binding(await)) => Stmt({ Stmt(return 5) })) }))"}, {"async", "Stmt(async)"}, {"async = a", "Stmt(async=a)"}, {"async\n= a", "Stmt(async=a)"}, {"async a => b", "Stmt(async Params(Binding(a)) => Stmt({ Stmt(return b) }))"}, {"async (a) => b", "Stmt(async Params(Binding(a)) => Stmt({ Stmt(return b) }))"}, {"async(a)", "Stmt(async(a))"}, {"async();", "Stmt(async())"}, {"async(a=6, ...b)", "Stmt(async((a=6), ...b))"}, {"async(function(){})", "Stmt(async(Decl(function Params() Stmt({ }))))"}, {"async.value", "Stmt(async.value)"}, {"if(a)async.value", "Stmt(if a Stmt(async.value))"}, {"function a(){ async\nawait => b }", "Decl(function a Params() Stmt({ Stmt(async) Stmt(Params(Binding(await)) => Stmt({ Stmt(return b) })) }))"}, {"a + async\nb", "Stmt(a+async) Stmt(b)"}, {"a + async\nfunction f(){}", "Stmt(a+async) Decl(function f Params() Stmt({ }))"}, {"class a extends async {}", "Decl(class a extends async)"}, {"function*a(){ yield a = 5 }", "Decl(function* a Params() Stmt({ Stmt(yield (a=5)) }))"}, {"function*a(){ yield * a = 5 }", "Decl(function* a Params() Stmt({ Stmt(yield* (a=5)) }))"}, {"function*a(){ yield\na = 5 }", "Decl(function* a Params() Stmt({ Stmt(yield) Stmt(a=5) }))"}, {"function*a(){ yield yield a }", "Decl(function* a Params() Stmt({ Stmt(yield (yield a)) }))"}, {"function*a(){ yield * yield * a }", "Decl(function* a Params() Stmt({ Stmt(yield* (yield* a)) }))"}, {"function*a(b = yield c){}", "Decl(function* a Params(Binding(b = (yield c))) Stmt({ }))"}, {"function*a(){ x = function yield(){} }", "Decl(function* a Params() Stmt({ Stmt(x=Decl(function yield Params() Stmt({ }))) }))"}, {"function*a(){ x = function b(){ x = yield } }", "Decl(function* a Params() Stmt({ Stmt(x=Decl(function b Params() Stmt({ Stmt(x=yield) }))) }))"}, {"function*a(){ (yield) }", "Decl(function* a Params() Stmt({ Stmt((yield)) }))"}, {"function*a(){ (yield a) }", "Decl(function* a Params() Stmt({ Stmt((yield a)) }))"}, {"function a(){ let\nawait }", "Decl(function a Params() Stmt({ Decl(let Binding(await)) }))"}, {"function*a(){ b => yield%5 }", "Decl(function* a Params() Stmt({ Stmt(Params(Binding(b)) => Stmt({ Stmt(return (yield%5)) })) }))"}, {"x = {await}", "Stmt(x={await})"}, {"async function a(){ x = {await: 5} }", "Decl(async function a Params() Stmt({ Stmt(x={await: 5}) }))"}, {"async function a(){ x = await a }", "Decl(async function a Params() Stmt({ Stmt(x=(await a)) }))"}, {"async function a(){ x = await a+y }", "Decl(async function a Params() Stmt({ Stmt(x=((await a)+y)) }))"}, {"async function a(b = await c){}", "Decl(async function a Params(Binding(b = (await c))) Stmt({ }))"}, {"async function a(){ x = function await(){} }", "Decl(async function a Params() Stmt({ Stmt(x=Decl(function await Params() Stmt({ }))) }))"}, {"async function a(){ x = function b(){ x = await } }", "Decl(async function a Params() Stmt({ Stmt(x=Decl(function b Params() Stmt({ Stmt(x=await) }))) }))"}, {"async function a(){ for await (var a of b) {} }", "Decl(async function a Params() Stmt({ Stmt(for await Decl(var Binding(a)) of b Stmt({ })) }))"}, {"async function a(){ (await a) }", "Decl(async function a Params() Stmt({ Stmt((await a)) }))"}, {"x = {async a(b){}}", "Stmt(x={Method(async a Params(Binding(b)) Stmt({ }))})"}, {"async(...a)", "Stmt(async(...a))"}, {"async(...a,...b)", "Stmt(async(...a, ...b))"}, // bindings {"let [] = z", "Decl(let Binding([ ] = z))"}, {"let [,] = z", "Decl(let Binding([ ] = z))"}, {"let [,a] = z", "Decl(let Binding([ Binding(), Binding(a) ] = z))"}, {"let [name = 5] = z", "Decl(let Binding([ Binding(name = 5) ] = z))"}, {"let [name = 5,] = z", "Decl(let Binding([ Binding(name = 5) ] = z))"}, {"let [name = 5,,] = z", "Decl(let Binding([ Binding(name = 5) ] = z))"}, {"let [name = 5,, ...yield] = z", "Decl(let Binding([ Binding(name = 5), Binding(), ...Binding(yield) ] = z))"}, {"let [...yield] = z", "Decl(let Binding([ ...Binding(yield) ] = z))"}, {"let [,,...yield] = z", "Decl(let Binding([ Binding(), Binding(), ...Binding(yield) ] = z))"}, {"let [name = 5,, ...[yield]] = z", "Decl(let Binding([ Binding(name = 5), Binding(), ...Binding([ Binding(yield) ]) ] = z))"}, {"let [name = 5,, ...{yield}] = z", "Decl(let Binding([ Binding(name = 5), Binding(), ...Binding({ Binding(yield) }) ] = z))"}, {"let {} = z", "Decl(let Binding({ } = z))"}, {"let {name = 5} = z", "Decl(let Binding({ Binding(name = 5) } = z))"}, {"function a(){ let {await = 5} = z }", "Decl(function a Params() Stmt({ Decl(let Binding({ Binding(await = 5) } = z)) }))"}, {"let {await: a} = z", "Decl(let Binding({ await: Binding(a) } = z))"}, {"let {if: name} = z", "Decl(let Binding({ if: Binding(name) } = z))"}, {"let {\"string\": name} = z", "Decl(let Binding({ string: Binding(name) } = z))"}, {"let {[a = 5]: name} = z", "Decl(let Binding({ [a=5]: Binding(name) } = z))"}, {"let {if: name = 5} = z", "Decl(let Binding({ if: Binding(name = 5) } = z))"}, {"let {if: yield = 5} = z", "Decl(let Binding({ if: Binding(yield = 5) } = z))"}, {"let {if: [name] = 5} = z", "Decl(let Binding({ if: Binding([ Binding(name) ] = 5) } = z))"}, {"let {if: {name} = 5} = z", "Decl(let Binding({ if: Binding({ Binding(name) } = 5) } = z))"}, {"let {...yield} = z", "Decl(let Binding({ ...Binding(yield) } = z))"}, {"let {if: name, ...yield} = z", "Decl(let Binding({ if: Binding(name), ...Binding(yield) } = z))"}, {"let i;for(let i;;);", "Decl(let Binding(i)) Stmt(for Decl(let Binding(i)) ; ; Stmt({ }))"}, {"let i;for(let i in x);", "Decl(let Binding(i)) Stmt(for Decl(let Binding(i)) in x Stmt({ }))"}, {"let i;for(let i of x);", "Decl(let Binding(i)) Stmt(for Decl(let Binding(i)) of x Stmt({ }))"}, {"for(let a in [0,1,2]){let a=5}", "Stmt(for Decl(let Binding(a)) in [0, 1, 2] Stmt({ Decl(let Binding(a = 5)) }))"}, {"for(var a in [0,1,2]){let a=5}", "Stmt(for Decl(var Binding(a)) in [0, 1, 2] Stmt({ Decl(let Binding(a = 5)) }))"}, {"for(var a in [0,1,2]){var a=5}", "Stmt(for Decl(var Binding(a)) in [0, 1, 2] Stmt({ Decl(var Binding(a = 5)) }))"}, {"for(let a=0; a<10; a++){let a=5}", "Stmt(for Decl(let Binding(a = 0)) ; (a<10) ; (a++) Stmt({ Decl(let Binding(a = 5)) }))"}, {"for(var a=0; a<10; a++){let a=5}", "Stmt(for Decl(var Binding(a = 0)) ; (a<10) ; (a++) Stmt({ Decl(let Binding(a = 5)) }))"}, {"for(var a=0; a<10; a++){var a=5}", "Stmt(for Decl(var Binding(a = 0)) ; (a<10) ; (a++) Stmt({ Decl(var Binding(a = 5)) }))"}, // expressions {"x = [a, ...b]", "Stmt(x=[a, ...b])"}, {"x = [...b]", "Stmt(x=[...b])"}, {"x = [...a, ...b]", "Stmt(x=[...a, ...b])"}, {"x = [,]", "Stmt(x=[,])"}, {"x = [,,]", "Stmt(x=[, ,])"}, {"x = [a,]", "Stmt(x=[a])"}, {"x = [a,,]", "Stmt(x=[a, ,])"}, {"x = [,a]", "Stmt(x=[, a])"}, {"x = {a}", "Stmt(x={a})"}, {"x = {...a}", "Stmt(x={...a})"}, {"x = {a, ...b}", "Stmt(x={a, ...b})"}, {"x = {...a, ...b}", "Stmt(x={...a, ...b})"}, {"x = {a=5}", "Stmt(x={a = 5})"}, {"x = {yield=5}", "Stmt(x={yield = 5})"}, {"x = {a:5}", "Stmt(x={a: 5})"}, {"x = {yield:5}", "Stmt(x={yield: 5})"}, {"x = {async:5}", "Stmt(x={async: 5})"}, {"x = {if:5}", "Stmt(x={if: 5})"}, {"x = {\"string\":5}", "Stmt(x={string: 5})"}, {"x = {3:5}", "Stmt(x={3: 5})"}, {"x = {[3]:5}", "Stmt(x={[3]: 5})"}, {"x = {a, if: b, do(){}, ...d}", "Stmt(x={a, if: b, Method(do Params() Stmt({ })), ...d})"}, {"x = {*a(){}}", "Stmt(x={Method(* a Params() Stmt({ }))})"}, {"x = {async*a(){}}", "Stmt(x={Method(async * a Params() Stmt({ }))})"}, {"x = {get a(){}}", "Stmt(x={Method(get a Params() Stmt({ }))})"}, {"x = {set a(){}}", "Stmt(x={Method(set a Params() Stmt({ }))})"}, {"x = {get(){ return 5 }}", "Stmt(x={Method(get Params() Stmt({ Stmt(return 5) }))})"}, {"x = {set(){}}", "Stmt(x={Method(set Params() Stmt({ }))})"}, {"x = (a, b)", "Stmt(x=((a,b)))"}, {"x = function() {}", "Stmt(x=Decl(function Params() Stmt({ })))"}, {"x = async function() {}", "Stmt(x=Decl(async function Params() Stmt({ })))"}, {"x = class {}", "Stmt(x=Decl(class))"}, {"x = class {a(){}}", "Stmt(x=Decl(class Method(a Params() Stmt({ }))))"}, {"x = class A { [(0())]; };", "Stmt(x=Decl(class A Field([(0())])))"}, {"x = a => a++", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(return (a++)) })))"}, {"x = a => {a++}", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(a++) })))"}, {"x = a => {return}", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(return) })))"}, {"x = a => {return a}", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(return a) })))"}, {"x = yield => a++", "Stmt(x=(Params(Binding(yield)) => Stmt({ Stmt(return (a++)) })))"}, {"x = yield => {a++}", "Stmt(x=(Params(Binding(yield)) => Stmt({ Stmt(a++) })))"}, {"x = async a => a++", "Stmt(x=(async Params(Binding(a)) => Stmt({ Stmt(return (a++)) })))"}, {"x = async a => {a++}", "Stmt(x=(async Params(Binding(a)) => Stmt({ Stmt(a++) })))"}, {"x = async a => await b", "Stmt(x=(async Params(Binding(a)) => Stmt({ Stmt(return (await b)) })))"}, {"function a(){ x = await => a++ }", "Decl(function a Params() Stmt({ Stmt(x=(Params(Binding(await)) => Stmt({ Stmt(return (a++)) }))) }))"}, {"x = a??b", "Stmt(x=(a??b))"}, {"x = a[b]", "Stmt(x=(a[b]))"}, {"x = a`tpl`", "Stmt(x=a`tpl`)"}, {"x = a.#b", "Stmt(x=(a.#b))"}, {"x = a?.b?.c.d", "Stmt(x=(((a?.b)?.c).d))"}, {"x = a?.[b]?.`tpl`", "Stmt(x=(a?.[b])?.`tpl`)"}, {"x = a?.(b)", "Stmt(x=(a?.(b)))"}, {"x = a?.#b", "Stmt(x=(a?.#b))"}, {"x = super(a)", "Stmt(x=(super(a)))"}, {"x = a(a,b,...c,)", "Stmt(x=(a(a, b, ...c)))"}, {"x = a(...a,...b)", "Stmt(x=(a(...a, ...b)))"}, {"x = new a", "Stmt(x=(new a))"}, {"x = new a()", "Stmt(x=(new a))"}, {"x = new a(b)", "Stmt(x=(new a(b)))"}, {"x = new a().b(c)", "Stmt(x=(((new a).b)(c)))"}, {"x = new a().#b", "Stmt(x=((new a).#b))"}, {"x = new new.target", "Stmt(x=(new (new.target)))"}, {"x = new import.meta", "Stmt(x=(new (import.meta)))"}, {"x = import(a)", "Stmt(x=(import(a)))"}, {"import('module')", "Stmt(import('module'))"}, {"x = +a", "Stmt(x=(+a))"}, {"x = ++a", "Stmt(x=(++a))"}, {"x = -a", "Stmt(x=(-a))"}, {"x = --a", "Stmt(x=(--a))"}, {"x = a--", "Stmt(x=(a--))"}, {"x = a<<b", "Stmt(x=(a<<b))"}, {"x = a|b", "Stmt(x=(a|b))"}, {"x = a&b", "Stmt(x=(a&b))"}, {"x = a^b", "Stmt(x=(a^b))"}, {"x = a||b", "Stmt(x=(a||b))"}, {"x = a&&b", "Stmt(x=(a&&b))"}, {"x = !a", "Stmt(x=(!a))"}, {"x = delete a", "Stmt(x=(delete a))"}, {"x = a in b", "Stmt(x=(a in b))"}, {"x = a.replace(b, c)", "Stmt(x=((a.replace)(b, c)))"}, {"x &&= a", "Stmt(x&&=a)"}, {"x ||= a", "Stmt(x||=a)"}, {"x ??= a", "Stmt(x??=a)"}, {"class a extends async function(){}{}", "Decl(class a extends Decl(async function Params() Stmt({ })))"}, {"x = a?b:c=d", "Stmt(x=(a ? b : (c=d)))"}, {"implements = 0", "Stmt(implements=0)"}, {"interface = 0", "Stmt(interface=0)"}, {"let = 0", "Stmt(let=0)"}, {"(let [a] = 0)", "Stmt(((let[a])=0))"}, {"package = 0", "Stmt(package=0)"}, {"private = 0", "Stmt(private=0)"}, {"protected = 0", "Stmt(protected=0)"}, {"public = 0", "Stmt(public=0)"}, {"static = 0", "Stmt(static=0)"}, {"#x in a", "Stmt(#x in a)"}, // expression to arrow function parameters {"x = (a,b,c) => {a++}", "Stmt(x=(Params(Binding(a), Binding(b), Binding(c)) => Stmt({ Stmt(a++) })))"}, {"x = (a,b,...c) => {a++}", "Stmt(x=(Params(Binding(a), Binding(b), ...Binding(c)) => Stmt({ Stmt(a++) })))"}, {"x = ([a, ...b]) => {a++}", "Stmt(x=(Params(Binding([ Binding(a), ...Binding(b) ])) => Stmt({ Stmt(a++) })))"}, {"x = ([,a,]) => {a++}", "Stmt(x=(Params(Binding([ Binding(), Binding(a) ])) => Stmt({ Stmt(a++) })))"}, {"x = ({a}) => {a++}", "Stmt(x=(Params(Binding({ Binding(a) })) => Stmt({ Stmt(a++) })))"}, {"x = ({a:b, c:d}) => {a++}", "Stmt(x=(Params(Binding({ a: Binding(b), c: Binding(d) })) => Stmt({ Stmt(a++) })))"}, {"x = ({a:[b]}) => {a++}", "Stmt(x=(Params(Binding({ a: Binding([ Binding(b) ]) })) => Stmt({ Stmt(a++) })))"}, {"x = ({a=5}) => {a++}", "Stmt(x=(Params(Binding({ Binding(a = 5) })) => Stmt({ Stmt(a++) })))"}, {"x = ({...a}) => {a++}", "Stmt(x=(Params(Binding({ ...Binding(a) })) => Stmt({ Stmt(a++) })))"}, {"x = ([{...a}]) => {a++}", "Stmt(x=(Params(Binding([ Binding({ ...Binding(a) }) ])) => Stmt({ Stmt(a++) })))"}, {"x = ([{a: b}]) => {a++}", "Stmt(x=(Params(Binding([ Binding({ a: Binding(b) }) ])) => Stmt({ Stmt(a++) })))"}, {"x = (a = 5) => {a++}", "Stmt(x=(Params(Binding(a = 5)) => Stmt({ Stmt(a++) })))"}, {"x = ({a = 5}) => {a++}", "Stmt(x=(Params(Binding({ Binding(a = 5) })) => Stmt({ Stmt(a++) })))"}, {`({a:b=1}={})=>b`, `Stmt(Params(Binding({ a: Binding(b = 1) } = {})) => Stmt({ Stmt(return b) }))`}, // expression precedence {"!!a", "Stmt(!(!a))"}, {"x = a.b.c", "Stmt(x=((a.b).c))"}, {"x = a+b+c", "Stmt(x=((a+b)+c))"}, {"x = a**b**c", "Stmt(x=(a**(b**c)))"}, {"a++ < b", "Stmt((a++)<b)"}, {"a&&b&&c", "Stmt((a&&b)&&c)"}, {"a||b||c", "Stmt((a||b)||c)"}, {"new new a(b)", "Stmt(new (new a(b)))"}, {"new super.a(b)", "Stmt(new (super.a)(b))"}, {"new new.target(a)", "Stmt(new (new.target)(a))"}, {"new import.meta(a)", "Stmt(new (import.meta)(a))"}, {"a(b)[c]", "Stmt((a(b))[c])"}, {"a[b]`tmpl`", "Stmt((a[b])`tmpl`)"}, {"a||b?c:d", "Stmt((a||b) ? c : d)"}, {"a??b?c:d", "Stmt((a??b) ? c : d)"}, {"a==b==c", "Stmt((a==b)==c)"}, {"new a?.b", "Stmt((new a)?.b)"}, {"new a++", "Stmt((new a)++)"}, {"new a--", "Stmt((new a)--)"}, {"a<<b<<c", "Stmt((a<<b)<<c)"}, {"a&b&c", "Stmt((a&b)&c)"}, {"a|b|c", "Stmt((a|b)|c)"}, {"a^b^c", "Stmt((a^b)^c)"}, {"a,b,c", "Stmt(a,b,c)"}, // regular expressions {"/abc/", "Stmt(/abc/)"}, {"function f(){return /abc/;}", "Decl(function f Params() Stmt({ Stmt(return /abc/) }))"}, {"a/b/g", "Stmt((a/b)/g)"}, {"{}/1/g", "Stmt({ }) Stmt(/1/g)"}, {"i(0)/1/g", "Stmt(((i(0))/1)/g)"}, {"if(0)/1/g", "Stmt(if 0 Stmt(/1/g))"}, {"a.if(0)/1/g", "Stmt((((a.if)(0))/1)/g)"}, {"this/1/g", "Stmt((this/1)/g)"}, {"switch(a){case /1/g:}", "Stmt(switch a Clause(case /1/g))"}, {"(a+b)/1/g", "Stmt((((a+b))/1)/g)"}, {"f(); function foo() {} /42/i", "Stmt(f()) Decl(function foo Params() Stmt({ })) Stmt(/42/i)"}, {"x = function() {} /42/i", "Stmt(x=((Decl(function Params() Stmt({ }))/42)/i))"}, {"x = function foo() {} /42/i", "Stmt(x=((Decl(function foo Params() Stmt({ }))/42)/i))"}, {"x = /foo/", "Stmt(x=/foo/)"}, {"x = (/foo/)", "Stmt(x=(/foo/))"}, {"x = {a: /foo/}", "Stmt(x={a: /foo/})"}, {"x = (a) / foo", "Stmt(x=((a)/foo))"}, {"do { /foo/ } while (a)", "Stmt(do Stmt({ Stmt(/foo/) }) while a)"}, {"if (true) /foo/", "Stmt(if true Stmt(/foo/))"}, {"/abc/ ? /def/ : /geh/", "Stmt(/abc/ ? /def/ : /geh/)"}, {"yield * /abc/", "Stmt(yield*/abc/)"}, // variable reuse {"var a; var a", "Decl(var Binding(a)) Decl(var Binding(a))"}, {"var a; {let a}", "Decl(var Binding(a)) Stmt({ Decl(let Binding(a)) })"}, {"{let a} var a", "Stmt({ Decl(let Binding(a)) }) Decl(var Binding(a))"}, {"function a(b){var b}", "Decl(function a Params(Binding(b)) Stmt({ Decl(var Binding(b)) }))"}, {"a=function(b){var b}", "Stmt(a=Decl(function Params(Binding(b)) Stmt({ Decl(var Binding(b)) })))"}, {"a=function b(){var b}", "Stmt(a=Decl(function b Params() Stmt({ Decl(var Binding(b)) })))"}, {"a=function b(){let b}", "Stmt(a=Decl(function b Params() Stmt({ Decl(let Binding(b)) })))"}, {"a=>{var a}", "Stmt(Params(Binding(a)) => Stmt({ Decl(var Binding(a)) }))"}, {"try{}catch(a){var a}", "Stmt(try Stmt({ }) catch Binding(a) Stmt({ Decl(var Binding(a)) }))"}, // ASI {"function f(){return a}", "Decl(function f Params() Stmt({ Stmt(return a) }))"}, {"function f(){return; a}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"}, {"function f(){return\na}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"}, {"function f(){return /*comment*/ a}", "Decl(function f Params() Stmt({ Stmt(return a) }))"}, {"function f(){return /*com\nment*/ a}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"}, {"function f(){return //comment\n a}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"}, {"if(a)while(true)\n;else;", "Stmt(if a Stmt(while true Stmt()) else Stmt())"}, {"if(a)for(;;)\n;else;", "Stmt(if a Stmt(for ; ; Stmt({ })) else Stmt())"}, {"a?.b\n`c`", "Stmt((a?.b)`c`)"}, {"() => { const v=6; x={v} }", "Stmt(Params() => Stmt({ Decl(const Binding(v = 6)) Stmt(x={v}) }))"}, {`([]=l=>{let{e}={e}})`, `Stmt(([]=(Params(Binding(l)) => Stmt({ Decl(let Binding({ Binding(e) } = {e})) }))))`}, // go-fuzz } for _, tt := range tests { t.Run(tt.js, func(t *testing.T) { ast, err := Parse(parse.NewInputString(tt.js), Options{}) if err != io.EOF { test.Error(t, err) } test.String(t, ast.String(), tt.expected) }) } // coverage for i := 0; ; i++ { if OpPrec(i).String() == fmt.Sprintf("Invalid(%d)", i) { break } } for i := 0; ; i++ { if DeclType(i).String() == fmt.Sprintf("Invalid(%d)", i) { break } } } func TestParseError(t *testing.T) { var tests = []struct { js string err string }{ {"5a", "unexpected identifier after number"}, {"{a", "unexpected EOF"}, {"if", "expected ( instead of EOF in if statement"}, {"if(a", "expected ) instead of EOF in if statement"}, {"if(a)let b", "unexpected b in expression"}, {"if(a)const b", "unexpected const in statement"}, {"with", "expected ( instead of EOF in with statement"}, {"with(a", "expected ) instead of EOF in with statement"}, {"do a++", "expected while instead of EOF in do-while statement"}, {"do a++ while", "unexpected while in expression"}, {"do a++; while", "expected ( instead of EOF in do-while statement"}, {"do a++; while(a", "expected ) instead of EOF in do-while statement"}, {"while", "expected ( instead of EOF in while statement"}, {"while(a", "expected ) instead of EOF in while statement"}, {"for", "expected ( instead of EOF in for statement"}, {"for(a", "expected in, of, or ; instead of EOF in for statement"}, {"for(a;a", "expected ; instead of EOF in for statement"}, {"for(a;a;a", "expected ) instead of EOF in for statement"}, {"for(var [a],b;", "unexpected ; in for statement"}, {"for(var [a]=5,{b};", "expected = instead of ; in var statement"}, {`for(let{[(`, `unexpected EOF in expression`}, {"for(async of b);", "expected => instead of b in arrow function"}, {"for await", "expected ( instead of EOF in for statement"}, {"function a(){for await", "expected ( instead of await in for statement"}, {"async function a(){ for await(a;", "expected of instead of ; in for statement"}, {"async function a(){ for await(a in", "expected of instead of in in for statement"}, {"for(var a of b", "expected ) instead of EOF in for statement"}, {"switch", "expected ( instead of EOF in switch statement"}, {"switch(a", "expected ) instead of EOF in switch statement"}, {"switch(a)", "expected { instead of EOF in switch statement"}, {"switch(a){bad:5}", "expected case or default instead of bad in switch statement"}, {"switch(a){case", "unexpected EOF in expression"}, {"switch(a){case a", "expected : instead of EOF in switch statement"}, {"switch(a){case a:", "unexpected EOF in switch statement"}, {"try", "expected { instead of EOF in try statement"}, {"try{", "unexpected EOF"}, {"try{}", "expected catch or finally instead of EOF in try statement"}, {"try{}catch(a", "expected ) instead of EOF in try-catch statement"}, {"try{}catch(a,", "expected ) instead of , in try-catch statement"}, {"try{}catch", "expected { instead of EOF in try-catch statement"}, {"try{}finally", "expected { instead of EOF in try-finally statement"}, {"function", "expected Identifier instead of EOF in function declaration"}, {"function(", "expected Identifier instead of ( in function declaration"}, {"!function", "expected Identifier or ( instead of EOF in function declaration"}, {"async function", "expected Identifier instead of EOF in function declaration"}, {"function a", "expected ( instead of EOF in function declaration"}, {"function a(b", "unexpected EOF in function declaration"}, {"function a(b,", "unexpected EOF in function declaration"}, {"function a(...b", "expected ) instead of EOF in function declaration"}, {"function a()", "expected { instead of EOF in function declaration"}, {"class", "expected Identifier instead of EOF in class declaration"}, {"class{", "expected Identifier instead of { in class declaration"}, {"!class", "expected { instead of EOF in class declaration"}, {"class A", "expected { instead of EOF in class declaration"}, {"class A{", "unexpected EOF in class declaration"}, {"class A extends a b {}", "expected { instead of b in class declaration"}, {"class A{+", "expected Identifier, String, Numeric, or [ instead of + in method or field definition"}, {"class A{[a", "expected ] instead of EOF in method or field definition"}, {"var [...a", "expected ] instead of EOF in array binding pattern"}, {"var [a", "expected , or ] instead of EOF in array binding pattern"}, {"var [a]", "expected = instead of EOF in var statement"}, {"var {[a", "expected ] instead of EOF in object binding pattern"}, {"var {+", "expected Identifier, String, Numeric, or [ instead of + in object binding pattern"}, {"var {a", "expected , or } instead of EOF in object binding pattern"}, {"var {...a", "expected } instead of EOF in object binding pattern"}, {"var {a}", "expected = instead of EOF in var statement"}, {"var 0", "unexpected 0 in binding"}, {"const a", "expected = instead of EOF in const statement"}, {"x={", "expected } instead of EOF in object literal"}, {"x={[a", "expected ] instead of EOF in object literal"}, {"x={[a]", "expected : or ( instead of EOF in object literal"}, {"x={+", "expected Identifier, String, Numeric, or [ instead of + in object literal"}, {"x={async\na", "unexpected a in object literal"}, {"class a extends ||", "unexpected || in expression"}, {"class a extends =", "unexpected = in expression"}, {"class a extends ?", "unexpected ? in expression"}, {"class a extends =>", "unexpected => in expression"}, {"x=a?b", "expected : instead of EOF in conditional expression"}, {"x=(a", "unexpected EOF in expression"}, {"x+(a", "expected ) instead of EOF in expression"}, {"x={a", "unexpected EOF in object literal"}, {"x=a[b", "expected ] instead of EOF in index expression"}, {"x=async a", "expected => instead of EOF in arrow function"}, {"x=async (a", "unexpected EOF in expression"}, {"x=async (a,", "unexpected EOF in expression"}, {"x=async function", "expected Identifier or ( instead of EOF in function declaration"}, {"x=async function *", "expected Identifier or ( instead of EOF in function declaration"}, {"x=async function a", "expected ( instead of EOF in function declaration"}, {"x=?.?.b", "unexpected ?. in expression"}, {"x=a?.?.b", "expected Identifier, (, [, or Template instead of ?. in optional chaining expression"}, {"x=a?..b", "expected Identifier, (, [, or Template instead of . in optional chaining expression"}, {"x=a?.[b", "expected ] instead of EOF in optional chaining expression"}, {"`tmp${", "unexpected EOF in expression"}, {"`tmp${x", "expected Template instead of EOF in template literal"}, {"`tmpl` x `tmpl`", "unexpected x in expression"}, {"x=5=>", "unexpected => in expression"}, {"x=new.bad", "expected target instead of bad in new.target expression"}, {"x=import.bad", "expected meta instead of bad in import.meta expression"}, {"x=super", "expected [, (, or . instead of EOF in super expression"}, {"x=super(a", "expected , or ) instead of EOF in arguments"}, {"x=super[a", "expected ] instead of EOF in index expression"}, {"x=super.", "expected Identifier instead of EOF in dot expression"}, {"x=new super(b)", "expected [ or . instead of ( in super expression"}, {"x=import", "expected ( instead of EOF in import expression"}, {"x=import(5", "expected , or ) instead of EOF in arguments"}, {"x=new import(b)", "unexpected ( in expression"}, {"import", "expected String, Identifier, *, or { instead of EOF in import statement"}, {"import *", "expected as instead of EOF in import statement"}, {"import * as", "expected Identifier instead of EOF in import statement"}, {"import {", "expected } instead of EOF in import statement"}, {"import {yield", "expected } instead of EOF in import statement"}, {"import {yield as", "expected Identifier instead of EOF in import statement"}, {"import {yield,", "expected } instead of EOF in import statement"}, {"import yield", "expected from instead of EOF in import statement"}, {"import yield,", "expected * or { instead of EOF in import statement"}, {"import yield from", "expected String instead of EOF in import statement"}, {"export", "expected *, {, var, let, const, function, async, class, or default instead of EOF in export statement"}, {"export *", "expected from instead of EOF in export statement"}, {"export * as", "expected Identifier or String instead of EOF in export statement"}, {"export * as if", "expected from instead of EOF in export statement"}, {"export {", "expected } instead of EOF in export statement"}, {"export {yield", "expected } instead of EOF in export statement"}, {"export {yield,", "expected } instead of EOF in export statement"}, {"export {yield as", "expected Identifier or String instead of EOF in export statement"}, {"export {} from", "expected String instead of EOF in export statement"}, {"export {} from", "expected String instead of EOF in export statement"}, {"export async", "expected function instead of EOF in export statement"}, {"export default async=>a,b", "unexpected , in expression"}, {"throw", "unexpected EOF in expression"}, {"throw\n", "unexpected newline in throw statement"}, {"#private", "expected in instead of EOF in relational expression"}, {"new #private in z", "unexpected #private in expression"}, {"new\n#private in z", "unexpected #private in expression"}, // no declarations {"if(a) function f(){}", "unexpected function in statement"}, {"if(a) async function f(){}", "unexpected function in statement"}, {"if(a) class c{}", "unexpected class in statement"}, // yield, async, await {"yield a = 5", "unexpected a in expression"}, {"function*a() { yield: var a", "unexpected : in expression"}, {"function*a() { x = b + yield c", "unexpected yield in expression"}, {"function a(b = yield c){}", "unexpected c in function declaration"}, {"function*a(){ (yield) => yield }", "unexpected => in expression"}, {"function*a(){ (yield=5) => yield }", "unexpected = in expression"}, {"function*a(){ (...yield) => yield }", "unexpected => in expression"}, {"function*a(){ yield yield\n?.() }", "unexpected ?. in expression"}, {"x = await\n=> a++", "unexpected => in expression"}, {"x=async (await,", "unexpected , in expression"}, {"async function a() { class a extends await", "unexpected await in expression"}, {"async function a() { await: var a", "unexpected : in expression"}, {"async function a() { let await", "unexpected await in binding"}, {"async function a() { let\nawait", "unexpected await in binding"}, {"async function a() { x = new await c", "unexpected await in expression"}, {"async function a() { x = await =>", "unexpected => in expression"}, {"async function a(){ (await) => await }", "unexpected ) in expression"}, {"async function a(){ (await=5) => await }", "unexpected = in expression"}, {"async function a(){ (...await) => await }", "unexpected ) in expression"}, {"async+a b", "unexpected b in expression"}, {"(async\nfunction(){})", "unexpected function in expression"}, {"a + async b", "unexpected b in expression"}, {"async await => 5", "unexpected await in arrow function"}, {"await: var a", "unexpected : in expression"}, {"(await) => 5", "unexpected ) in expression"}, {"async\nawait => b", "unexpected => in expression"}, {"let\nawait", "unexpected await in binding"}, {"let {await = 5} = z", "expected : instead of = in object binding"}, {"x = await => a++", "unexpected => in expression"}, {"function*a(){ b => yield 0 }", "unexpected 0 in expression"}, // specific cases {"{a, if: b, do(){}, ...d}", "unexpected if in expression"}, // block stmt {"let {if = 5}", "expected : instead of = in object binding pattern"}, {"let {...}", "expected Identifier instead of } in object binding pattern"}, {"let {...[]}", "expected Identifier instead of [ in object binding pattern"}, {"let {...{}}", "expected Identifier instead of { in object binding pattern"}, {"for", "expected ( instead of EOF in for statement"}, {"for b", "expected ( instead of b in for statement"}, {"for (a b)", "expected in, of, or ; instead of b in for statement"}, {"for (var a in b;) {}", "expected ) instead of ; in for statement"}, {"for (var a,b in c) {}", "unexpected in in for statement"}, {"for (var a,b of c) {}", "unexpected of in for statement"}, {"if (a) 1 else 3", "unexpected else in expression"}, {"x = [...]", "unexpected ] in expression"}, {"x = {...}", "unexpected } in expression"}, {"let\nawait 0", "unexpected await in binding"}, {"const\nawait 0", "unexpected await in binding"}, {"var\nawait 0", "unexpected await in binding"}, {"function a(){let\nawait 0", "unexpected 0 in let declaration"}, // expression to arrow function parameters {"x = ()", "expected => instead of EOF in arrow function"}, {"x = [x] => a", "unexpected => in expression"}, {"x = ((x)) => a", "unexpected => in expression"}, {"x = ([...x, y]) => a", "unexpected => in expression"}, {"x = ({...x, y}) => a", "unexpected => in expression"}, {"x = ({b(){}}) => a", "unexpected => in expression"}, {"x = (a, b, ...c)", "expected => instead of EOF in arrow function"}, {"x = (a+b) =>", "unexpected => in expression"}, {"x = ([...a, b]) =>", "unexpected => in expression"}, {"x = ([...5]) =>", "unexpected => in expression"}, {"x = ([5]) =>", "unexpected => in expression"}, {"x = ({...a, b}) =>", "unexpected => in expression"}, {"x = ({...5}) =>", "unexpected => in expression"}, {"x = ({5: 5}) =>", "unexpected => in expression"}, {"x = ({[4+5]: 5}) =>", "unexpected => in expression"}, {"x = (a, a) =>", "unexpected => in expression"}, {"x = (a, a, ...a) =>", "unexpected => in expression"}, {"x = (a, ...a) =>", "unexpected => in expression"}, {"(A,{}%0%{})=>0", "invalid parameters in arrow function"}, {"({}``=1)=>0", "invalid parameters in arrow function"}, // expression precedence {"x = a + yield b", "unexpected b in expression"}, {"a??b||c", "unexpected || in expression"}, {"a??b&&c", "unexpected && in expression"}, {"a||b??c", "unexpected ?? in expression"}, {"a&&b??c", "unexpected ?? in expression"}, {"x = a++--", "unexpected -- in expression"}, {"x = a\n++", "unexpected EOF in expression"}, {"x = a++?", "unexpected EOF in expression"}, {"a+b =", "unexpected = in expression"}, {"!a**b", "unexpected ** in expression"}, {"new !a", "unexpected ! in expression"}, {"new +a", "unexpected + in expression"}, {"new -a", "unexpected - in expression"}, {"new ++a", "unexpected ++ in expression"}, {"new --a", "unexpected -- in expression"}, {"a=>{return a} < b", "unexpected < in expression"}, {"a=>{return a} == b", "unexpected == in expression"}, {"a=>{return a} . b", "unexpected . in expression"}, {"a=>{return a} (", "unexpected ( in expression"}, {"a=>{return a} [", "unexpected [ in expression"}, {"a=>{return a} '", "unterminated string literal"}, {"a=>{return a} `", "unterminated template literal"}, {"a=>{return a} ++", "unexpected ++ in expression"}, {"a=>{return a} --", "unexpected -- in expression"}, {"a=>{return a} * b", "unexpected * in expression"}, {"a=>{return a} + b", "unexpected + in expression"}, {"a=>{return a} << b", "unexpected << in expression"}, {"a=>{return a} & b", "unexpected & in expression"}, {"a=>{return a} | b", "unexpected | in expression"}, {"a=>{return a} ^ b", "unexpected ^ in expression"}, {"a=>{return a} ? b", "unexpected ? in expression"}, {"a=>{return a} => b=>b", "unexpected => in expression"}, {"class a extends b=>b", "expected { instead of => in class declaration"}, // regular expressions {"x = x / foo /", "unexpected EOF in expression"}, {"bar (true) /foo/", "unexpected EOF in expression"}, {"yield /abc/", "unexpected EOF in expression"}, // variable reuse {"let a; var a", "identifier a has already been declared"}, {"let a; {var a}", "identifier a has already been declared"}, {"{let a; {var a}}", "identifier a has already been declared"}, {"var a; let a", "identifier a has already been declared"}, {"{var a} let a", "identifier a has already been declared"}, {"var a; const a", "identifier a has already been declared"}, {"var a; class a{}", "identifier a has already been declared"}, {"function a(b){let b}", "identifier b has already been declared"}, {"a=function(b){let b}", "identifier b has already been declared"}, {"a=>{let a}", "identifier a has already been declared"}, {"let a;function a(){}", "identifier a has already been declared"}, {"try{}catch(a){let a}", "identifier a has already been declared"}, {"let {a, a}", "identifier a has already been declared"}, {"let {a, ...a}", "identifier a has already been declared"}, {"for(let a in [0,1,2]){var a = 5}", "identifier a has already been declared"}, {"for(let a=0; a<10; a++){var a = 5}", "identifier a has already been declared"}, //{"function a(){}; var a", "identifier a has already been declared"}, //{"export function a(){}; var a", "identifier a has already been declared"}, //{"export default function a(){}; var a", "identifier a has already been declared"}, // other {"\x00", "unexpected 0x00"}, {"@", "unexpected @"}, {"\u200F", "unexpected U+200F"}, {"\u2010", "unexpected \u2010"}, {"a=\u2010", "unexpected \u2010 in expression"}, {"/", "unexpected EOF or newline in regular expression"}, {"({...[]})=>a", "unexpected => in expression"}, // go-fuzz {`for(;;)let[a]`, `unexpected let [ in single-statement context`}, // go-fuzz } for _, tt := range tests { t.Run(tt.js, func(t *testing.T) { _, err := Parse(parse.NewInputString(tt.js), Options{}) test.That(t, err != io.EOF && err != nil) e := err.Error() if len(tt.err) < len(err.Error()) { e = e[:len(tt.err)] } test.String(t, e, tt.err) }) } } type ScopeVars struct { bound, uses string scopes int refs map[*Var]int } func NewScopeVars() *ScopeVars { return &ScopeVars{ refs: map[*Var]int{}, } } func (sv *ScopeVars) String() string { return "bound:" + sv.bound + " uses:" + sv.uses } func (sv *ScopeVars) Ref(v *Var) int { if ref, ok := sv.refs[v]; ok { return ref } sv.refs[v] = len(sv.refs) + 1 return len(sv.refs) } func (sv *ScopeVars) AddScope(scope Scope) { if sv.scopes != 0 { sv.bound += "/" sv.uses += "/" } sv.scopes++ bounds := []string{} for _, v := range scope.Declared { bounds = append(bounds, fmt.Sprintf("%s=%d", string(v.Data), sv.Ref(v))) } sv.bound += strings.Join(bounds, ",") uses := []string{} for _, v := range scope.Undeclared { links := "" for v.Link != nil { v = v.Link links += "*" } uses = append(uses, fmt.Sprintf("%s=%d%s", string(v.Data), sv.Ref(v), links)) } sv.uses += strings.Join(uses, ",") } func (sv *ScopeVars) AddExpr(iexpr IExpr) { switch expr := iexpr.(type) { case *FuncDecl: sv.AddScope(expr.Body.Scope) for _, item := range expr.Params.List { if item.Binding != nil { sv.AddBinding(item.Binding) } if item.Default != nil { sv.AddExpr(item.Default) } } if expr.Params.Rest != nil { sv.AddBinding(expr.Params.Rest) } for _, item := range expr.Body.List { sv.AddStmt(item) } case *ClassDecl: for _, item := range expr.List { if item.Method != nil { sv.AddScope(item.Method.Body.Scope) } } case *ArrowFunc: sv.AddScope(expr.Body.Scope) for _, item := range expr.Params.List { if item.Binding != nil { sv.AddBinding(item.Binding) } if item.Default != nil { sv.AddExpr(item.Default) } } if expr.Params.Rest != nil { sv.AddBinding(expr.Params.Rest) } for _, item := range expr.Body.List { sv.AddStmt(item) } case *CondExpr: sv.AddExpr(expr.Cond) sv.AddExpr(expr.X) sv.AddExpr(expr.Y) case *UnaryExpr: sv.AddExpr(expr.X) case *BinaryExpr: sv.AddExpr(expr.X) sv.AddExpr(expr.Y) case *GroupExpr: sv.AddExpr(expr.X) case *CommaExpr: for _, item := range expr.List { sv.AddExpr(item) } } } func (sv *ScopeVars) AddBinding(ibinding IBinding) { switch binding := ibinding.(type) { case *BindingArray: for _, item := range binding.List { if item.Binding != nil { sv.AddBinding(item.Binding) } if item.Default != nil { sv.AddExpr(item.Default) } } if binding.Rest != nil { sv.AddBinding(binding.Rest) } case *BindingObject: for _, item := range binding.List { if item.Key.IsComputed() { sv.AddExpr(item.Key.Computed) } if item.Value.Binding != nil { sv.AddBinding(item.Value.Binding) } if item.Value.Default != nil { sv.AddExpr(item.Value.Default) } } } } func (sv *ScopeVars) AddStmt(istmt IStmt) { switch stmt := istmt.(type) { case *BlockStmt: sv.AddScope(stmt.Scope) for _, item := range stmt.List { sv.AddStmt(item) } case *FuncDecl: sv.AddScope(stmt.Body.Scope) for _, item := range stmt.Params.List { if item.Binding != nil { sv.AddBinding(item.Binding) } if item.Default != nil { sv.AddExpr(item.Default) } } if stmt.Params.Rest != nil { sv.AddBinding(stmt.Params.Rest) } for _, item := range stmt.Body.List { sv.AddStmt(item) } case *ClassDecl: for _, item := range stmt.List { if item.Method != nil { sv.AddScope(item.Method.Body.Scope) } } case *ReturnStmt: sv.AddExpr(stmt.Value) case *ThrowStmt: sv.AddExpr(stmt.Value) case *ForStmt: sv.AddStmt(stmt.Body) case *ForInStmt: sv.AddStmt(stmt.Body) case *ForOfStmt: sv.AddStmt(stmt.Body) case *IfStmt: sv.AddStmt(stmt.Body) if stmt.Else != nil { sv.AddStmt(stmt.Else) } case *TryStmt: if 0 < len(stmt.Body.List) { sv.AddStmt(stmt.Body) } if stmt.Catch != nil { sv.AddStmt(stmt.Catch) } if stmt.Finally != nil { sv.AddStmt(stmt.Finally) } case *VarDecl: for _, item := range stmt.List { if item.Default != nil { sv.AddExpr(item.Default) } } case *ExprStmt: sv.AddExpr(stmt.Value) } } func TestParseScope(t *testing.T) { // vars registers all bound and unbound variables per scope. Unbound variables are not defined in that particular scope and are defined in another scope (parent, global, child of a parent, ...). Bound variables are variables that are defined in this scope. Each scope is separated by /, and the variables are separated by commas. Each variable is assigned a unique ID (sort by first bounded than unbounded per scope) in order to make sure which identifiers refer to the same variable. // var and function declarations are function-scoped // const, let, and class declarations are block-scoped var tests = []struct { js string bound, uses string }{ {"a; a", "", "a=1"}, {"a;{a;{a}}", "//", "a=1/a=1/a=1*"}, {"var a; b", "a=1", "b=2"}, {"var {a:b, c=d, ...e} = z;", "b=1,c=2,e=3", "d=4,z=5"}, {"var [a, b=c, ...d] = z;", "a=1,b=2,d=3", "c=4,z=5"}, {"x={a:b, c=d, ...e};", "", "x=1,b=2,c=3,d=4,e=5"}, {"x=[a, b=c, ...d];", "", "x=1,a=2,b=3,c=4,d=5"}, {"yield = 5", "", "yield=1"}, {"function a(){ await = 5 }", "a=1/", "await=2/await=2"}, {"function a(b,c){var d; e = 5; a}", "a=1/b=3,c=4,d=5", "e=2/e=2,a=1"}, {"function a(b,c=b){}", "a=1/b=2,c=3", "/"}, {"function a(b=c,c){}", "a=1/b=3,c=4", "c=2/c=2"}, {"function a(b=c){var c}", "a=1/b=3,c=4", "c=2/c=2"}, {"function a(b){var b}", "a=1/b=2", "/"}, {"!function a(b,c){var d; e = 5; a}", "/a=2,b=3,c=4,d=5", "e=1/e=1"}, {"a=function(b,c=b){}", "/b=2,c=3", "a=1/"}, {"a=function(b=c,c){}", "/b=3,c=4", "a=1,c=2/c=2"}, {"a=function(b=c){var c}", "/b=3,c=4", "a=1,c=2/c=2"}, {"a=function(b){var b}", "/b=2", "a=1/"}, {"export function a(){}", "a=1", ""}, {"export default function a(){}", "a=1", ""}, {"class a{b(){}}", "a=1/", "/"}, // classes are not tracked {"!class a{b(){}}", "/", "/"}, {"a => a%5", "/a=1", "/"}, {"a => a%b", "/a=2", "b=1/b=1"}, {"var a;a => a%5", "a=1/a=2", "/"}, {"(a) + (a)", "", "a=1"}, {"(a,b)", "", "a=1,b=2"}, {"(a,b) + (a,b)", "", "a=1,b=2"}, {"(a) + (a => a%5)", "/a=2", "a=1/"}, {"(a=b) => {var c; d = 5}", "/a=3,c=4", "b=1,d=2/b=1,d=2"}, {"(a,b=a) => {}", "/a=1,b=2", "/"}, {"(a=b,b)=>{}", "/a=2,b=3", "b=1/b=1"}, {"a=>{var a}", "/a=1", "/"}, {"(a=b) => {var b}", "/a=2,b=3", "b=1/b=1"}, {"({[a+b]:c}) => {}", "/c=3", "a=1,b=2/a=1,b=2"}, {"({a:b, c=d, ...e}=f) => 5", "/b=3,c=4,e=5", "d=1,f=2/d=1,f=2"}, {"([a, b=c, ...d]=e) => 5", "/a=3,b=4,d=5", "c=1,e=2/c=1,e=2"}, {"(a) + ((b,c) => {var d; e = 5; return e})", "/b=3,c=4,d=5", "a=1,e=2/e=2"}, {"(a) + ((a,b) => {var c; d = 5; return d})", "/a=3,b=4,c=5", "a=1,d=2/d=2"}, {"{(a) + ((a,b) => {var c; d = 5; return d})}", "//a=3,b=4,c=5", "a=1,d=2/a=1,d=2/d=2"}, {"(a=(b=>b/a)) => a", "/a=1/b=2", "//a=1*"}, {"(a=(b=>b/c)) => a", "/a=2/b=3", "c=1/c=1/c=1"}, {"(a=(function b(){})) => a", "/a=1/b=2", "//"}, {"label: a", "", "a=1"}, {"yield => yield%5", "/yield=1", "/"}, {"function a(){ await => await%5 }", "a=1//await=2", "//"}, {"function*a(){ b => { yield%5 } }", "a=1//b=3", "yield=2/yield=2/yield=2"}, {"async function a(){ b => await%5 }", "a=1//b=3", "await=2/await=2/await=2"}, {"let a; {let b = a}", "a=1/b=2", "/a=1"}, {"let a; {var b}", "a=1,b=2/", "/b=2"}, // may remove b from uses {"let a; {var b = a}", "a=1,b=2/", "/b=2,a=1"}, {"let a; {class b{}}", "a=1/b=2", "/"}, {"a = 5; var a;", "a=1", ""}, {"a = 5; let a;", "a=1", ""}, {"a = 5; {var a}", "a=1/", "/a=1"}, {"a = 5; {let a}", "/a=2", "a=1/"}, {"{a = 5} var a", "a=1/", "/a=1"}, {"{a = 5} let a", "a=1/", "/a=1"}, {"var a; {a = 5}", "a=1/", "/a=1"}, {"var a; {var a}", "a=1/", "/a=1"}, {"var a; {let a}", "a=1/a=2", "/"}, {"let a; {a = 5}", "a=1/", "/a=1"}, {"{var a} a = 5", "a=1/", "/a=1"}, {"{let a} a = 5", "/a=2", "a=1/"}, {"!function(){throw new Error()}", "/", "Error=1/Error=1"}, {"!function(){return a}", "/", "a=1/a=1"}, {"!function(){return a};var a;", "a=1/", "/a=1"}, {"!function(){return a};if(5){var a}", "a=1//", "/a=1/a=1"}, {"try{}catch(a){var a}", "a=1/a=2", "/a=1"}, {"try{}catch(a){let b; c}", "/a=2,b=3", "c=1/c=1"}, {"try{}catch(a){var b; c}", "b=1/a=3", "c=2/b=1,c=2"}, {"var a;try{}catch(a){a}", "a=1/a=2", "/"}, {"var a;try{}catch(a){var a}", "a=1/a=2", "/a=1"}, {"var a;try{}catch(b){var a}", "a=1/b=2", "/a=1"}, {"function r(o){function l(t){if(!z[t]){if(!o[t]);}}}", "r=1/o=3,l=4/t=5/", "z=2/z=2/z=2,o=3/o=3*,t=5"}, {"function a(){var name;{var name}}", "a=1/name=2/", "//name=2"}, // may remove name from uses {"function a(){var name;{var name=7}}", "a=1/name=2/", "//name=2"}, {"!function(){a};!function(){a};var a", "a=1//", "/a=1/a=1"}, {"!function(){var a;!function(){a;var a}}", "/a=1/a=2", "//"}, {"!function(){var a;!function(){!function(){a}}}", "/a=1//", "//a=1/a=1*"}, {"!function(){var a;!function(){a;!function(){a}}}", "/a=1//", "//a=1/a=1*"}, {"!function(){var b;{(T=x),T}{var T}}", "/b=2,T=3//", "x=1/x=1/x=1,T=3/T=3"}, {"var T;!function(){var b;{(T=x),T}{var T}}", "T=1/b=3,T=4//", "x=2/x=2/x=2,T=4/T=4"}, {"!function(){let a=b,b=c,c=d,d=e,e=f,f=g,g=h,h=a,j;for(let i=0;;)j=4;}", "/a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,j=9/i=10", "//j=9"}, {"{a} {a} var a", "a=1//", "/a=1/a=1"}, // second block must add a new var in case the block contains a var decl {"(a),(a)", "", "a=1"}, // second parens could have been arrow function, so must have added new var {"var a,b,c;(a = b[c])", "a=1,b=2,c=3", ""}, // parens could have been arrow function, so must have added new var {"!function(a){var b,c;return b?(c=function(){return[a];a.dispatch()},c):t}", "/a=2,b=3,c=4/", "t=1/t=1/a=2*"}, {"(...{a=function(){return [b]}}) => 5", "/a=2/", "b=1/b=1/b=1"}, {"(...[a=function(){return [b]}]) => 5", "/a=2/", "b=1/b=1/b=1"}, {`a=>{for(let b of c){b,a;{var d}}}`, "/a=2,d=3/b=4/", "c=1/c=1/c=1,a=2,d=3/d=3"}, {`var a;{let b;{var a}}`, "a=1/b=2/", "/a=1/a=1"}, {`for(let b of c){let b;{b}}`, "/b=2,b=3/", "c=1/c=1/b=3"}, {`for(var b of c){let b;{b}}`, "b=1/b=3/", "c=2/b=1,c=2/b=3"}, {`for(var b of c){var b;{b}}`, "b=1//", "c=2/b=1,c=2/b=1"}, {`function a(b){for(let c of b){let b;}}`, "a=1/b=2/c=3,b=4", "//b=2"}, } for _, tt := range tests { t.Run(tt.js, func(t *testing.T) { ast, err := Parse(parse.NewInputString(tt.js), Options{}) if err != io.EOF { test.Error(t, err) } vars := NewScopeVars() vars.AddScope(ast.Scope) for _, istmt := range ast.List { vars.AddStmt(istmt) } test.String(t, vars.String(), "bound:"+tt.bound+" uses:"+tt.uses) }) } } func TestScope(t *testing.T) { js := "let a,b; b = 5; var c; {d}{{d}}" ast, err := Parse(parse.NewInputString(js), Options{}) if err != io.EOF { test.Error(t, err) } scope := ast.Scope // test output test.T(t, scope.String(), "Scope{Declared: [Var{LexicalDecl a 0 1}, Var{LexicalDecl b 0 2}, Var{VariableDecl c 0 1}], Undeclared: [Var{NoDecl d 0 2}]}") // test sort sort.Sort(VarsByUses(scope.Declared)) test.T(t, scope.String(), "Scope{Declared: [Var{LexicalDecl b 0 2}, Var{LexicalDecl a 0 1}, Var{VariableDecl c 0 1}], Undeclared: [Var{NoDecl d 0 2}]}") // test variable link test.T(t, ast.List[3].(*BlockStmt).Scope.String(), "Scope{Declared: [], Undeclared: [Var{NoDecl d 0 2}]}") test.T(t, ast.List[4].(*BlockStmt).Scope.String(), "Scope{Declared: [], Undeclared: [Var{NoDecl d 0 2}]}") test.T(t, ast.List[4].(*BlockStmt).List[0].(*BlockStmt).Scope.String(), "Scope{Declared: [], Undeclared: [Var{NoDecl d 1 2}]}") } func TestParseInputError(t *testing.T) { _, err := Parse(parse.NewInput(test.NewErrorReader(0)), Options{}) test.T(t, err, test.ErrPlain) _, err = Parse(parse.NewInput(test.NewErrorReader(1)), Options{}) test.T(t, err, test.ErrPlain) } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/table.go����������������������������������������������������������������������������0000664�0000000�0000000�00000006750�14632123015�0014757�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import "strconv" // OpPrec is the operator precedence type OpPrec int // OpPrec values. const ( OpExpr OpPrec = iota // a,b OpAssign // a?b:c, yield x, ()=>x, async ()=>x, a=b, a+=b, ... OpCoalesce // a??b OpOr // a||b OpAnd // a&&b OpBitOr // a|b OpBitXor // a^b OpBitAnd // a&b OpEquals // a==b, a!=b, a===b, a!==b OpCompare // a<b, a>b, a<=b, a>=b, a instanceof b, a in b OpShift // a<<b, a>>b, a>>>b OpAdd // a+b, a-b OpMul // a*b, a/b, a%b OpExp // a**b OpUnary // ++x, --x, delete x, void x, typeof x, +x, -x, ~x, !x, await x OpUpdate // x++, x-- OpLHS // CallExpr/OptChainExpr or NewExpr OpCall // a?.b, a(b), super(a), import(a) OpNew // new a OpMember // a[b], a.b, a`b`, super[x], super.x, new.target, import.meta, new a(b) OpPrimary // literal, function, class, parenthesized ) func (prec OpPrec) String() string { switch prec { case OpExpr: return "OpExpr" case OpAssign: return "OpAssign" case OpCoalesce: return "OpCoalesce" case OpOr: return "OpOr" case OpAnd: return "OpAnd" case OpBitOr: return "OpBitOr" case OpBitXor: return "OpBitXor" case OpBitAnd: return "OpBitAnd" case OpEquals: return "OpEquals" case OpCompare: return "OpCompare" case OpShift: return "OpShift" case OpAdd: return "OAdd" case OpMul: return "OpMul" case OpExp: return "OpExp" case OpUnary: return "OpUnary" case OpUpdate: return "OpUpdate" case OpLHS: return "OpLHS" case OpCall: return "OpCall" case OpNew: return "OpNew" case OpMember: return "OpMember" case OpPrimary: return "OpPrimary" } return "Invalid(" + strconv.Itoa(int(prec)) + ")" } // Keywords is a map of reserved, strict, and other keywords var Keywords = map[string]TokenType{ // reserved "await": AwaitToken, "break": BreakToken, "case": CaseToken, "catch": CatchToken, "class": ClassToken, "const": ConstToken, "continue": ContinueToken, "debugger": DebuggerToken, "default": DefaultToken, "delete": DeleteToken, "do": DoToken, "else": ElseToken, "enum": EnumToken, "export": ExportToken, "extends": ExtendsToken, "false": FalseToken, "finally": FinallyToken, "for": ForToken, "function": FunctionToken, "if": IfToken, "import": ImportToken, "in": InToken, "instanceof": InstanceofToken, "new": NewToken, "null": NullToken, "return": ReturnToken, "super": SuperToken, "switch": SwitchToken, "this": ThisToken, "throw": ThrowToken, "true": TrueToken, "try": TryToken, "typeof": TypeofToken, "var": VarToken, "void": VoidToken, "while": WhileToken, "with": WithToken, "yield": YieldToken, // strict mode "let": LetToken, "static": StaticToken, "implements": ImplementsToken, "interface": InterfaceToken, "package": PackageToken, "private": PrivateToken, "protected": ProtectedToken, "public": PublicToken, // extra "as": AsToken, "async": AsyncToken, "from": FromToken, "get": GetToken, "meta": MetaToken, "of": OfToken, "set": SetToken, "target": TargetToken, } ������������������������parse-2.7.15/js/tokentype.go������������������������������������������������������������������������0000664�0000000�0000000�00000020536�14632123015�0015710�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import "strconv" // TokenType determines the type of token, eg. a number or a semicolon. type TokenType uint16 // from LSB to MSB: 8 bits for tokens per category, 1 bit for numeric, 1 bit for punctuator, 1 bit for operator, 1 bit for identifier, 4 bits unused // TokenType values. const ( ErrorToken TokenType = iota // extra token when errors occur WhitespaceToken LineTerminatorToken // \r \n \r\n CommentToken CommentLineTerminatorToken StringToken TemplateToken TemplateStartToken TemplateMiddleToken TemplateEndToken RegExpToken PrivateIdentifierToken ) // Numeric token values. const ( NumericToken TokenType = 0x0100 + iota DecimalToken BinaryToken OctalToken HexadecimalToken IntegerToken ) // Punctuator token values. const ( PunctuatorToken TokenType = 0x0200 + iota OpenBraceToken // { CloseBraceToken // } OpenParenToken // ( CloseParenToken // ) OpenBracketToken // [ CloseBracketToken // ] DotToken // . SemicolonToken // ; CommaToken // , QuestionToken // ? ColonToken // : ArrowToken // => EllipsisToken // ... ) // Operator token values. const ( OperatorToken TokenType = 0x0600 + iota EqToken // = EqEqToken // == EqEqEqToken // === NotToken // ! NotEqToken // != NotEqEqToken // !== LtToken // < LtEqToken // <= LtLtToken // << LtLtEqToken // <<= GtToken // > GtEqToken // >= GtGtToken // >> GtGtEqToken // >>= GtGtGtToken // >>> GtGtGtEqToken // >>>= AddToken // + AddEqToken // += IncrToken // ++ SubToken // - SubEqToken // -= DecrToken // -- MulToken // * MulEqToken // *= ExpToken // ** ExpEqToken // **= DivToken // / DivEqToken // /= ModToken // % ModEqToken // %= BitAndToken // & BitOrToken // | BitXorToken // ^ BitNotToken // ~ BitAndEqToken // &= BitOrEqToken // |= BitXorEqToken // ^= AndToken // && OrToken // || NullishToken // ?? AndEqToken // &&= OrEqToken // ||= NullishEqToken // ??= OptChainToken // ?. // unused in lexer PosToken // +a NegToken // -a PreIncrToken // ++a PreDecrToken // --a PostIncrToken // a++ PostDecrToken // a-- ) // Reserved token values. const ( ReservedToken TokenType = 0x0800 + iota AwaitToken BreakToken CaseToken CatchToken ClassToken ConstToken ContinueToken DebuggerToken DefaultToken DeleteToken DoToken ElseToken EnumToken ExportToken ExtendsToken FalseToken FinallyToken ForToken FunctionToken IfToken ImportToken InToken InstanceofToken NewToken NullToken ReturnToken SuperToken SwitchToken ThisToken ThrowToken TrueToken TryToken TypeofToken YieldToken VarToken VoidToken WhileToken WithToken ) // Identifier token values. const ( IdentifierToken TokenType = 0x1000 + iota AsToken AsyncToken FromToken GetToken ImplementsToken InterfaceToken LetToken MetaToken OfToken PackageToken PrivateToken ProtectedToken PublicToken SetToken StaticToken TargetToken ) // IsNumeric return true if token is numeric. func IsNumeric(tt TokenType) bool { return tt&0x0100 != 0 } // IsPunctuator return true if token is a punctuator. func IsPunctuator(tt TokenType) bool { return tt&0x0200 != 0 } // IsOperator return true if token is an operator. func IsOperator(tt TokenType) bool { return tt&0x0400 != 0 } // IsIdentifierName matches IdentifierName, i.e. any identifier func IsIdentifierName(tt TokenType) bool { return tt&0x1800 != 0 } // IsReservedWord matches ReservedWord func IsReservedWord(tt TokenType) bool { return tt&0x0800 != 0 } // IsIdentifier matches Identifier, i.e. IdentifierName but not ReservedWord. Does not match yield or await. func IsIdentifier(tt TokenType) bool { return tt&0x1000 != 0 } func (tt TokenType) String() string { s := tt.Bytes() if s == nil { return "Invalid(" + strconv.Itoa(int(tt)) + ")" } return string(s) } var operatorBytes = [][]byte{ []byte("Operator"), []byte("="), []byte("=="), []byte("==="), []byte("!"), []byte("!="), []byte("!=="), []byte("<"), []byte("<="), []byte("<<"), []byte("<<="), []byte(">"), []byte(">="), []byte(">>"), []byte(">>="), []byte(">>>"), []byte(">>>="), []byte("+"), []byte("+="), []byte("++"), []byte("-"), []byte("-="), []byte("--"), []byte("*"), []byte("*="), []byte("**"), []byte("**="), []byte("/"), []byte("/="), []byte("%"), []byte("%="), []byte("&"), []byte("|"), []byte("^"), []byte("~"), []byte("&="), []byte("|="), []byte("^="), []byte("&&"), []byte("||"), []byte("??"), []byte("&&="), []byte("||="), []byte("??="), []byte("?."), []byte("+"), []byte("-"), []byte("++"), []byte("--"), []byte("++"), []byte("--"), } var reservedWordBytes = [][]byte{ []byte("Reserved"), []byte("await"), []byte("break"), []byte("case"), []byte("catch"), []byte("class"), []byte("const"), []byte("continue"), []byte("debugger"), []byte("default"), []byte("delete"), []byte("do"), []byte("else"), []byte("enum"), []byte("export"), []byte("extends"), []byte("false"), []byte("finally"), []byte("for"), []byte("function"), []byte("if"), []byte("import"), []byte("in"), []byte("instanceof"), []byte("new"), []byte("null"), []byte("return"), []byte("super"), []byte("switch"), []byte("this"), []byte("throw"), []byte("true"), []byte("try"), []byte("typeof"), []byte("yield"), []byte("var"), []byte("void"), []byte("while"), []byte("with"), } var identifierBytes = [][]byte{ []byte("Identifier"), []byte("as"), []byte("async"), []byte("from"), []byte("get"), []byte("implements"), []byte("interface"), []byte("let"), []byte("meta"), []byte("of"), []byte("package"), []byte("private"), []byte("protected"), []byte("public"), []byte("set"), []byte("static"), []byte("target"), } // Bytes returns the string representation of a TokenType. func (tt TokenType) Bytes() []byte { if IsOperator(tt) && int(tt-OperatorToken) < len(operatorBytes) { return operatorBytes[tt-OperatorToken] } else if IsReservedWord(tt) && int(tt-ReservedToken) < len(reservedWordBytes) { return reservedWordBytes[tt-ReservedToken] } else if IsIdentifier(tt) && int(tt-IdentifierToken) < len(identifierBytes) { return identifierBytes[tt-IdentifierToken] } switch tt { case ErrorToken: return []byte("Error") case WhitespaceToken: return []byte("Whitespace") case LineTerminatorToken: return []byte("LineTerminator") case CommentToken: return []byte("Comment") case CommentLineTerminatorToken: return []byte("CommentLineTerminator") case StringToken: return []byte("String") case TemplateToken: return []byte("Template") case TemplateStartToken: return []byte("TemplateStart") case TemplateMiddleToken: return []byte("TemplateMiddle") case TemplateEndToken: return []byte("TemplateEnd") case RegExpToken: return []byte("RegExp") case PrivateIdentifierToken: return []byte("PrivateIdentifier") case NumericToken: return []byte("Numeric") case DecimalToken: return []byte("Decimal") case BinaryToken: return []byte("Binary") case OctalToken: return []byte("Octal") case HexadecimalToken: return []byte("Hexadecimal") case IntegerToken: return []byte("Integer") case PunctuatorToken: return []byte("Punctuator") case OpenBraceToken: return []byte("{") case CloseBraceToken: return []byte("}") case OpenParenToken: return []byte("(") case CloseParenToken: return []byte(")") case OpenBracketToken: return []byte("[") case CloseBracketToken: return []byte("]") case DotToken: return []byte(".") case SemicolonToken: return []byte(";") case CommaToken: return []byte(",") case QuestionToken: return []byte("?") case ColonToken: return []byte(":") case ArrowToken: return []byte("=>") case EllipsisToken: return []byte("...") } return nil } ������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/util.go�����������������������������������������������������������������������������0000664�0000000�0000000�00000001627�14632123015�0014643�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js func isLHSExpr(i IExpr) bool { switch i.(type) { case *CommaExpr, *CondExpr, *YieldExpr, *ArrowFunc, *BinaryExpr, *UnaryExpr: return false } return true } // AsIdentifierName returns true if a valid identifier name is given. func AsIdentifierName(b []byte) bool { if len(b) == 0 || !identifierStartTable[b[0]] { return false } i := 1 for i < len(b) { if identifierTable[b[i]] { i++ } else { return false } } return true } // AsDecimalLiteral returns true if a valid decimal literal is given. func AsDecimalLiteral(b []byte) bool { if len(b) == 0 || (b[0] < '0' || '9' < b[0]) && (b[0] != '.' || len(b) == 1) { return false } else if b[0] == '0' { return len(b) == 1 } i := 1 for i < len(b) && '0' <= b[i] && b[i] <= '9' { i++ } if i < len(b) && b[i] == '.' && b[0] != '.' { i++ for i < len(b) && '0' <= b[i] && b[i] <= '9' { i++ } } return i == len(b) } ���������������������������������������������������������������������������������������������������������parse-2.7.15/js/util_test.go������������������������������������������������������������������������0000664�0000000�0000000�00000001365�14632123015�0015701�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import ( "testing" "github.com/tdewolff/test" ) func TestAsIdentifierName(t *testing.T) { test.That(t, !AsIdentifierName([]byte(""))) test.That(t, !AsIdentifierName([]byte("5"))) test.That(t, AsIdentifierName([]byte("ab"))) test.That(t, !AsIdentifierName([]byte("a="))) } func TestAsDecimalLiteral(t *testing.T) { test.That(t, !AsDecimalLiteral([]byte(""))) test.That(t, !AsDecimalLiteral([]byte("a"))) test.That(t, AsDecimalLiteral([]byte("12"))) test.That(t, AsDecimalLiteral([]byte("12.56"))) test.That(t, AsDecimalLiteral([]byte(".56"))) test.That(t, !AsDecimalLiteral([]byte(".56a"))) test.That(t, !AsDecimalLiteral([]byte("."))) test.That(t, AsDecimalLiteral([]byte("0"))) test.That(t, !AsDecimalLiteral([]byte("00"))) } ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/walk.go�����������������������������������������������������������������������������0000664�0000000�0000000�00000011241�14632123015�0014615�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js // IVisitor represents the AST Visitor // Each INode encountered by `Walk` is passed to `Enter`, children nodes will be ignored if the returned IVisitor is nil // `Exit` is called upon the exit of a node type IVisitor interface { Enter(n INode) IVisitor Exit(n INode) } // Walk traverses an AST in depth-first order func Walk(v IVisitor, n INode) { if n == nil { return } if v = v.Enter(n); v == nil { return } defer v.Exit(n) switch n := n.(type) { case *AST: Walk(v, &n.BlockStmt) case *Var: return case *BlockStmt: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, n.List[i]) } } case *EmptyStmt: return case *ExprStmt: Walk(v, n.Value) case *IfStmt: Walk(v, n.Body) Walk(v, n.Else) Walk(v, n.Cond) case *DoWhileStmt: Walk(v, n.Body) Walk(v, n.Cond) case *WhileStmt: Walk(v, n.Body) Walk(v, n.Cond) case *ForStmt: if n.Body != nil { Walk(v, n.Body) } Walk(v, n.Init) Walk(v, n.Cond) Walk(v, n.Post) case *ForInStmt: if n.Body != nil { Walk(v, n.Body) } Walk(v, n.Init) Walk(v, n.Value) case *ForOfStmt: if n.Body != nil { Walk(v, n.Body) } Walk(v, n.Init) Walk(v, n.Value) case *CaseClause: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, n.List[i]) } } Walk(v, n.Cond) case *SwitchStmt: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } Walk(v, n.Init) case *BranchStmt: return case *ReturnStmt: Walk(v, n.Value) case *WithStmt: Walk(v, n.Body) Walk(v, n.Cond) case *LabelledStmt: Walk(v, n.Value) case *ThrowStmt: Walk(v, n.Value) case *TryStmt: if n.Body != nil { Walk(v, n.Body) } if n.Catch != nil { Walk(v, n.Catch) } if n.Finally != nil { Walk(v, n.Finally) } Walk(v, n.Binding) case *DebuggerStmt: return case *Alias: return case *ImportStmt: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } case *ExportStmt: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } Walk(v, n.Decl) case *DirectivePrologueStmt: return case *PropertyName: Walk(v, &n.Literal) Walk(v, n.Computed) case *BindingArray: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } Walk(v, n.Rest) case *BindingObjectItem: if n.Key != nil { Walk(v, n.Key) } Walk(v, &n.Value) case *BindingObject: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } if n.Rest != nil { Walk(v, n.Rest) } case *BindingElement: Walk(v, n.Binding) Walk(v, n.Default) case *VarDecl: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } case *Params: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } Walk(v, n.Rest) case *FuncDecl: Walk(v, &n.Body) Walk(v, &n.Params) if n.Name != nil { Walk(v, n.Name) } case *MethodDecl: Walk(v, &n.Body) Walk(v, &n.Params) Walk(v, &n.Name) case *Field: Walk(v, &n.Name) Walk(v, n.Init) case *ClassDecl: if n.Name != nil { Walk(v, n.Name) } Walk(v, n.Extends) for _, item := range n.List { if item.StaticBlock != nil { Walk(v, item.StaticBlock) } else if item.Method != nil { Walk(v, item.Method) } else { Walk(v, &item.Field) } } case *LiteralExpr: return case *Element: Walk(v, n.Value) case *ArrayExpr: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } case *Property: if n.Name != nil { Walk(v, n.Name) } Walk(v, n.Value) Walk(v, n.Init) case *ObjectExpr: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } case *TemplatePart: Walk(v, n.Expr) case *TemplateExpr: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } Walk(v, n.Tag) case *GroupExpr: Walk(v, n.X) case *IndexExpr: Walk(v, n.X) Walk(v, n.Y) case *DotExpr: Walk(v, n.X) Walk(v, &n.Y) case *NewTargetExpr: return case *ImportMetaExpr: return case *Arg: Walk(v, n.Value) case *Args: if n.List != nil { for i := 0; i < len(n.List); i++ { Walk(v, &n.List[i]) } } case *NewExpr: if n.Args != nil { Walk(v, n.Args) } Walk(v, n.X) case *CallExpr: Walk(v, &n.Args) Walk(v, n.X) case *UnaryExpr: Walk(v, n.X) case *BinaryExpr: Walk(v, n.X) Walk(v, n.Y) case *CondExpr: Walk(v, n.Cond) Walk(v, n.X) Walk(v, n.Y) case *YieldExpr: Walk(v, n.X) case *ArrowFunc: Walk(v, &n.Body) Walk(v, &n.Params) case *CommaExpr: for _, item := range n.List { Walk(v, item) } default: return } } ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/js/walk_test.go������������������������������������������������������������������������0000664�0000000�0000000�00000003516�14632123015�0015662�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package js import ( "bytes" "regexp" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) type walker struct{} func (w *walker) Enter(n INode) IVisitor { switch n := n.(type) { case *Var: if bytes.Equal(n.Data, []byte("x")) { n.Data = []byte("obj") } } return w } func (w *walker) Exit(n INode) {} func TestWalk(t *testing.T) { js := ` if (true) { for (i = 0; i < 1; i++) { x.y = i } }` ast, err := Parse(parse.NewInputString(js), Options{}) if err != nil { t.Fatal(err) } Walk(&walker{}, ast) re := regexp.MustCompile("\n *") t.Run("TestWalk", func(t *testing.T) { src := ast.JSString() src = re.ReplaceAllString(src, " ") test.String(t, src, "if (true) { for (i = 0; i < 1; i++) { obj.y = i; } }") }) } func TestWalkNilNode(t *testing.T) { nodes := []INode{ &AST{}, &Var{}, &BlockStmt{}, &EmptyStmt{}, &ExprStmt{}, &IfStmt{}, &DoWhileStmt{}, &WhileStmt{}, &ForStmt{}, &ForInStmt{}, &ForOfStmt{}, &CaseClause{}, &SwitchStmt{}, &BranchStmt{}, &ReturnStmt{}, &WithStmt{}, &LabelledStmt{}, &ThrowStmt{}, &TryStmt{}, &DebuggerStmt{}, &Alias{}, &ImportStmt{}, &ExportStmt{}, &DirectivePrologueStmt{}, &PropertyName{}, &BindingArray{}, &BindingObjectItem{}, &BindingObject{}, &BindingElement{}, &VarDecl{}, &Params{}, &FuncDecl{}, &MethodDecl{}, &Field{}, &ClassElement{}, &ClassDecl{}, &LiteralExpr{}, &Element{}, &ArrayExpr{}, &Property{}, &ObjectExpr{}, &TemplatePart{}, &TemplateExpr{}, &GroupExpr{}, &IndexExpr{}, &DotExpr{}, &NewTargetExpr{}, &ImportMetaExpr{}, &Arg{}, &Args{}, &NewExpr{}, &CallExpr{}, &UnaryExpr{}, &BinaryExpr{}, &CondExpr{}, &YieldExpr{}, &ArrowFunc{}, } t.Run("TestWalkNilNode", func(t *testing.T) { for _, n := range nodes { Walk(&walker{}, n) } }) } ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/json/����������������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0013666�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/json/README.md�������������������������������������������������������������������������0000664�0000000�0000000�00000003421�14632123015�0015145�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# JSON [![API reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/tdewolff/parse/v2/json?tab=doc) This package is a JSON lexer (ECMA-404) written in [Go][1]. It follows the specification at [JSON](http://json.org/). The lexer takes an io.Reader and converts it into tokens until the EOF. ## Installation Run the following command go get -u github.com/tdewolff/parse/v2/json or add the following import and run project with `go get` import "github.com/tdewolff/parse/v2/json" ## Parser ### Usage The following initializes a new Parser with io.Reader `r`: ``` go p := json.NewParser(parse.NewInput(r)) ``` To tokenize until EOF an error, use: ``` go for { gt, text := p.Next() switch gt { case json.ErrorGrammar: // error or EOF set in p.Err() return // ... } } ``` All grammars: ``` go ErrorGrammar GrammarType = iota // extra grammar when errors occur WhitespaceGrammar // space \t \r \n LiteralGrammar // null true false NumberGrammar StringGrammar StartObjectGrammar // { EndObjectGrammar // } StartArrayGrammar // [ EndArrayGrammar // ] ``` ### Examples ``` go package main import ( "os" "github.com/tdewolff/parse/v2/json" ) // Tokenize JSON from stdin. func main() { p := json.NewParser(parse.NewInput(os.Stdin)) for { gt, text := p.Next() switch gt { case json.ErrorGrammar: if p.Err() != io.EOF { fmt.Println("Error on line", p.Line(), ":", p.Err()) } return case json.LiteralGrammar: fmt.Println("Literal", string(text)) case json.NumberGrammar: fmt.Println("Number", string(text)) // ... } } } ``` ## License Released under the [MIT license](https://github.com/tdewolff/parse/blob/master/LICENSE.md). [1]: http://golang.org/ "Go Language" �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/json/parse.go��������������������������������������������������������������������������0000664�0000000�0000000�00000016100�14632123015�0015325�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// Package json is a JSON parser following the specifications at http://json.org/. package json import ( "strconv" "github.com/tdewolff/parse/v2" ) // GrammarType determines the type of grammar type GrammarType uint32 // GrammarType values. const ( ErrorGrammar GrammarType = iota // extra grammar when errors occur WhitespaceGrammar LiteralGrammar NumberGrammar StringGrammar StartObjectGrammar // { EndObjectGrammar // } StartArrayGrammar // [ EndArrayGrammar // ] ) // String returns the string representation of a GrammarType. func (gt GrammarType) String() string { switch gt { case ErrorGrammar: return "Error" case WhitespaceGrammar: return "Whitespace" case LiteralGrammar: return "Literal" case NumberGrammar: return "Number" case StringGrammar: return "String" case StartObjectGrammar: return "StartObject" case EndObjectGrammar: return "EndObject" case StartArrayGrammar: return "StartArray" case EndArrayGrammar: return "EndArray" } return "Invalid(" + strconv.Itoa(int(gt)) + ")" } //////////////////////////////////////////////////////////////// // State determines the current state the parser is in. type State uint32 // State values. const ( ValueState State = iota // extra token when errors occur ObjectKeyState ObjectValueState ArrayState ) // String returns the string representation of a State. func (state State) String() string { switch state { case ValueState: return "Value" case ObjectKeyState: return "ObjectKey" case ObjectValueState: return "ObjectValue" case ArrayState: return "Array" } return "Invalid(" + strconv.Itoa(int(state)) + ")" } //////////////////////////////////////////////////////////////// // Parser is the state for the lexer. type Parser struct { r *parse.Input state []State err error needComma bool } // NewParser returns a new Parser for a given io.Reader. func NewParser(r *parse.Input) *Parser { return &Parser{ r: r, state: []State{ValueState}, } } // Err returns the error encountered during tokenization, this is often io.EOF but also other errors can be returned. func (p *Parser) Err() error { if p.err != nil { return p.err } return p.r.Err() } // State returns the state the parser is currently in (ie. which token is expected). func (p *Parser) State() State { return p.state[len(p.state)-1] } // Next returns the next Grammar. It returns ErrorGrammar when an error was encountered. Using Err() one can retrieve the error message. func (p *Parser) Next() (GrammarType, []byte) { p.moveWhitespace() c := p.r.Peek(0) state := p.state[len(p.state)-1] if c == ',' { if state != ArrayState && state != ObjectKeyState { p.err = parse.NewErrorLexer(p.r, "unexpected comma character") return ErrorGrammar, nil } p.r.Move(1) p.moveWhitespace() p.needComma = false c = p.r.Peek(0) } p.r.Skip() if p.needComma && c != '}' && c != ']' && c != 0 { p.err = parse.NewErrorLexer(p.r, "expected comma character or an array or object ending") return ErrorGrammar, nil } else if c == '{' { p.state = append(p.state, ObjectKeyState) p.r.Move(1) return StartObjectGrammar, p.r.Shift() } else if c == '}' { if state != ObjectKeyState { p.err = parse.NewErrorLexer(p.r, "unexpected right brace character") return ErrorGrammar, nil } p.needComma = true p.state = p.state[:len(p.state)-1] if p.state[len(p.state)-1] == ObjectValueState { p.state[len(p.state)-1] = ObjectKeyState } p.r.Move(1) return EndObjectGrammar, p.r.Shift() } else if c == '[' { p.state = append(p.state, ArrayState) p.r.Move(1) return StartArrayGrammar, p.r.Shift() } else if c == ']' { p.needComma = true if state != ArrayState { p.err = parse.NewErrorLexer(p.r, "unexpected right bracket character") return ErrorGrammar, nil } p.state = p.state[:len(p.state)-1] if p.state[len(p.state)-1] == ObjectValueState { p.state[len(p.state)-1] = ObjectKeyState } p.r.Move(1) return EndArrayGrammar, p.r.Shift() } else if state == ObjectKeyState { if c != '"' || !p.consumeStringToken() { p.err = parse.NewErrorLexer(p.r, "expected object key to be a quoted string") return ErrorGrammar, nil } n := p.r.Pos() p.moveWhitespace() if c := p.r.Peek(0); c != ':' { p.err = parse.NewErrorLexer(p.r, "expected colon character after object key") return ErrorGrammar, nil } p.r.Move(1) p.state[len(p.state)-1] = ObjectValueState return StringGrammar, p.r.Shift()[:n] } else { p.needComma = true if state == ObjectValueState { p.state[len(p.state)-1] = ObjectKeyState } if c == '"' && p.consumeStringToken() { return StringGrammar, p.r.Shift() } else if p.consumeNumberToken() { return NumberGrammar, p.r.Shift() } else if p.consumeLiteralToken() { return LiteralGrammar, p.r.Shift() } c := p.r.Peek(0) // pick up movement from consumeStringToken to detect NULL or EOF if c == 0 && p.r.Err() == nil { p.err = parse.NewErrorLexer(p.r, "unexpected NULL character") return ErrorGrammar, nil } else if c == 0 { // EOF return ErrorGrammar, nil } } p.err = parse.NewErrorLexer(p.r, "unexpected character '%c'", c) return ErrorGrammar, nil } //////////////////////////////////////////////////////////////// /* The following functions follow the specifications at http://json.org/ */ func (p *Parser) moveWhitespace() { for { if c := p.r.Peek(0); c != ' ' && c != '\n' && c != '\r' && c != '\t' { break } p.r.Move(1) } } func (p *Parser) consumeLiteralToken() bool { c := p.r.Peek(0) if c == 't' && p.r.Peek(1) == 'r' && p.r.Peek(2) == 'u' && p.r.Peek(3) == 'e' { p.r.Move(4) return true } else if c == 'f' && p.r.Peek(1) == 'a' && p.r.Peek(2) == 'l' && p.r.Peek(3) == 's' && p.r.Peek(4) == 'e' { p.r.Move(5) return true } else if c == 'n' && p.r.Peek(1) == 'u' && p.r.Peek(2) == 'l' && p.r.Peek(3) == 'l' { p.r.Move(4) return true } return false } func (p *Parser) consumeNumberToken() bool { mark := p.r.Pos() if p.r.Peek(0) == '-' { p.r.Move(1) } c := p.r.Peek(0) if c >= '1' && c <= '9' { p.r.Move(1) for { if c := p.r.Peek(0); c < '0' || c > '9' { break } p.r.Move(1) } } else if c != '0' { p.r.Rewind(mark) return false } else { p.r.Move(1) // 0 } if c := p.r.Peek(0); c == '.' { p.r.Move(1) if c := p.r.Peek(0); c < '0' || c > '9' { p.r.Move(-1) return true } for { if c := p.r.Peek(0); c < '0' || c > '9' { break } p.r.Move(1) } } mark = p.r.Pos() if c := p.r.Peek(0); c == 'e' || c == 'E' { p.r.Move(1) if c := p.r.Peek(0); c == '+' || c == '-' { p.r.Move(1) } if c := p.r.Peek(0); c < '0' || c > '9' { p.r.Rewind(mark) return true } for { if c := p.r.Peek(0); c < '0' || c > '9' { break } p.r.Move(1) } } return true } func (p *Parser) consumeStringToken() bool { // assume to be on " p.r.Move(1) for { c := p.r.Peek(0) if c == '"' { escaped := false for i := p.r.Pos() - 1; i >= 0; i-- { if p.r.Lexeme()[i] == '\\' { escaped = !escaped } else { break } } if !escaped { p.r.Move(1) break } } else if c == 0 { return false } p.r.Move(1) } return true } ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/json/parse_test.go���������������������������������������������������������������������0000664�0000000�0000000�00000012137�14632123015�0016372�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package json import ( "fmt" "io" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) type GTs []GrammarType func TestGrammars(t *testing.T) { var grammarTests = []struct { json string expected []GrammarType }{ {" \t\n\r", GTs{}}, // WhitespaceGrammar {"null", GTs{LiteralGrammar}}, {"[]", GTs{StartArrayGrammar, EndArrayGrammar}}, {"15.2", GTs{NumberGrammar}}, {"0.4", GTs{NumberGrammar}}, {"5e9", GTs{NumberGrammar}}, {"-4E-3", GTs{NumberGrammar}}, {"true", GTs{LiteralGrammar}}, {"false", GTs{LiteralGrammar}}, {"null", GTs{LiteralGrammar}}, {`""`, GTs{StringGrammar}}, {`"abc"`, GTs{StringGrammar}}, {`"\""`, GTs{StringGrammar}}, {`"\\"`, GTs{StringGrammar}}, {"{}", GTs{StartObjectGrammar, EndObjectGrammar}}, {`{"a": "b", "c": "d"}`, GTs{StartObjectGrammar, StringGrammar, StringGrammar, StringGrammar, StringGrammar, EndObjectGrammar}}, {`{"a": [1, 2], "b": {"c": 3}}`, GTs{StartObjectGrammar, StringGrammar, StartArrayGrammar, NumberGrammar, NumberGrammar, EndArrayGrammar, StringGrammar, StartObjectGrammar, StringGrammar, NumberGrammar, EndObjectGrammar, EndObjectGrammar}}, {"[null,]", GTs{StartArrayGrammar, LiteralGrammar, EndArrayGrammar}}, } for _, tt := range grammarTests { t.Run(tt.json, func(t *testing.T) { p := NewParser(parse.NewInputString(tt.json)) i := 0 for { grammar, _ := p.Next() if grammar == ErrorGrammar { test.T(t, p.Err(), io.EOF) test.T(t, i, len(tt.expected), "when error occurred we must be at the end") break } else if grammar == WhitespaceGrammar { continue } test.That(t, i < len(tt.expected), "index", i, "must not exceed expected grammar types size", len(tt.expected)) if i < len(tt.expected) { test.T(t, grammar, tt.expected[i], "grammar types must match") } i++ } }) } // coverage for i := 0; ; i++ { if GrammarType(i).String() == fmt.Sprintf("Invalid(%d)", i) { break } } for i := 0; ; i++ { if State(i).String() == fmt.Sprintf("Invalid(%d)", i) { break } } } func TestGrammarsErrorEOF(t *testing.T) { var grammarErrorTests = []struct { json string }{ {`{"":"`}, {"\"a\\"}, } for _, tt := range grammarErrorTests { t.Run(tt.json, func(t *testing.T) { p := NewParser(parse.NewInputString(tt.json)) for { grammar, _ := p.Next() if grammar == ErrorGrammar { test.T(t, p.Err(), io.EOF) break } } }) } } func TestGrammarsError(t *testing.T) { var grammarErrorTests = []struct { json string col int }{ {"true, false", 5}, {"[true false]", 7}, {"]", 1}, {"}", 1}, {"{0: 1}", 2}, {"{\"a\" 1}", 6}, {"1.", 2}, {"1e+", 2}, {"[true, \x00]", 8}, {"\"string\x00\"", 8}, {"{\"id\": noquote}", 8}, {"{\"id\"\x00: 5}", 6}, {"{\"id: \x00}", 7}, {"{\"id: 5\x00", 8}, } for _, tt := range grammarErrorTests { t.Run(tt.json, func(t *testing.T) { p := NewParser(parse.NewInputString(tt.json)) for { grammar, _ := p.Next() if grammar == ErrorGrammar { if perr, ok := p.Err().(*parse.Error); ok { _, col, _ := perr.Position() test.T(t, col, tt.col) } else { test.Fail(t, "not a parse error:", p.Err()) } break } } }) } } func TestStates(t *testing.T) { var stateTests = []struct { json string expected []State }{ {"null", []State{ValueState}}, {"[null]", []State{ArrayState, ArrayState, ValueState}}, {"{\"\":null}", []State{ObjectKeyState, ObjectValueState, ObjectKeyState, ValueState}}, } for _, tt := range stateTests { t.Run(tt.json, func(t *testing.T) { p := NewParser(parse.NewInputString(tt.json)) i := 0 for { grammar, _ := p.Next() state := p.State() if grammar == ErrorGrammar { test.T(t, p.Err(), io.EOF) test.T(t, i, len(tt.expected), "when error occurred we must be at the end") break } else if grammar == WhitespaceGrammar { continue } test.That(t, i < len(tt.expected), "index", i, "must not exceed expected states size", len(tt.expected)) if i < len(tt.expected) { test.T(t, state, tt.expected[i], "states must match") } i++ } }) } } func TestOffset(t *testing.T) { z := parse.NewInputString(`{"key": [5, "string", null, true]}`) p := NewParser(z) test.T(t, z.Offset(), 0) _, _ = p.Next() test.T(t, z.Offset(), 1) // { _, _ = p.Next() test.T(t, z.Offset(), 7) // "key": _, _ = p.Next() test.T(t, z.Offset(), 9) // [ _, _ = p.Next() test.T(t, z.Offset(), 10) // 5 _, _ = p.Next() test.T(t, z.Offset(), 20) // , "string" _, _ = p.Next() test.T(t, z.Offset(), 26) // , null _, _ = p.Next() test.T(t, z.Offset(), 32) // , true _, _ = p.Next() test.T(t, z.Offset(), 33) // ] _, _ = p.Next() test.T(t, z.Offset(), 34) // } } //////////////////////////////////////////////////////////////// func ExampleNewParser() { p := NewParser(parse.NewInputString(`{"key": 5}`)) out := "" for { state := p.State() gt, data := p.Next() if gt == ErrorGrammar { break } out += string(data) if state == ObjectKeyState && gt != EndObjectGrammar { out += ":" } // not handling comma insertion } fmt.Println(out) // Output: {"key":5} } ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/position.go����������������������������������������������������������������������������0000664�0000000�0000000�00000004025�14632123015�0015111�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package parse import ( "fmt" "io" "strings" "unicode" ) // Position returns the line and column number for a certain position in a file. It is useful for recovering the position in a file that caused an error. // It only treates \n, \r, and \r\n as newlines, which might be different from some languages also recognizing \f, \u2028, and \u2029 to be newlines. func Position(r io.Reader, offset int) (line, col int, context string) { l := NewInput(r) line = 1 for l.Pos() < offset { c := l.Peek(0) n := 1 newline := false if c == '\n' { newline = true } else if c == '\r' { if l.Peek(1) == '\n' { newline = true n = 2 } else { newline = true } } else if c >= 0xC0 { var r rune if r, n = l.PeekRune(0); r == '\u2028' || r == '\u2029' { newline = true } } else if c == 0 && l.Err() != nil { break } if 1 < n && offset < l.Pos()+n { break } l.Move(n) if newline { line++ offset -= l.Pos() l.Skip() } } col = len([]rune(string(l.Lexeme()))) + 1 context = positionContext(l, line, col) return } func positionContext(l *Input, line, col int) (context string) { for { c := l.Peek(0) if c == 0 && l.Err() != nil || c == '\n' || c == '\r' { break } l.Move(1) } rs := []rune(string(l.Lexeme())) // cut off front or rear of context to stay between 60 characters limit := 60 offset := 20 ellipsisFront := "" ellipsisRear := "" if limit < len(rs) { if col <= limit-offset { ellipsisRear = "..." rs = rs[:limit-3] } else if col >= len(rs)-offset-3 { ellipsisFront = "..." col -= len(rs) - offset - offset - 7 rs = rs[len(rs)-offset-offset-4:] } else { ellipsisFront = "..." ellipsisRear = "..." rs = rs[col-offset-1 : col+offset] col = offset + 4 } } // replace unprintable characters by a space for i, r := range rs { if !unicode.IsGraphic(r) { rs[i] = '·' } } context += fmt.Sprintf("%5d: %s%s%s\n", line, ellipsisFront, string(rs), ellipsisRear) context += fmt.Sprintf("%s^", strings.Repeat(" ", 6+col)) return } �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/position_test.go�����������������������������������������������������������������������0000664�0000000�0000000�00000005415�14632123015�0016154�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package parse import ( "bytes" "fmt" "strings" "testing" "github.com/tdewolff/test" ) func TestPosition(t *testing.T) { var newlineTests = []struct { offset int buf string line int col int }{ // \u2028, \u2029, and \u2318 are three bytes long {0, "x", 1, 1}, {1, "xx", 1, 2}, {2, "x\nx", 2, 1}, {2, "\n\nx", 3, 1}, {3, "\nxxx", 2, 3}, {2, "\r\nx", 2, 1}, {1, "\rx", 2, 1}, {3, "\u2028x", 2, 1}, {3, "\u2029x", 2, 1}, // edge cases {0, "", 1, 1}, {2, "x", 1, 2}, {0, "\nx", 1, 1}, {1, "\r\ny", 1, 1}, {-1, "x", 1, 1}, {0, "\x00a", 1, 1}, {2, "a\x00\n", 1, 3}, // unicode {1, "x\u2028x", 1, 2}, {2, "x\u2028x", 1, 2}, {3, "x\u2028x", 1, 2}, {0, "x\u2318x", 1, 1}, {1, "x\u2318x", 1, 2}, {2, "x\u2318x", 1, 2}, {3, "x\u2318x", 1, 2}, {4, "x\u2318x", 1, 3}, } for _, tt := range newlineTests { t.Run(fmt.Sprint(tt.buf, " ", tt.offset), func(t *testing.T) { r := bytes.NewBufferString(tt.buf) line, col, _ := Position(r, tt.offset) test.T(t, line, tt.line, "line") test.T(t, col, tt.col, "column") }) } } func TestPositionContext(t *testing.T) { var newlineTests = []struct { offset int buf string context string }{ {10, "0123456789@123456789012345678901234567890123456789012345678901234567890123456789", "0123456789@1234567890123456789012345678901234567890123456..."}, // 80 characters -> 60 characters {40, "0123456789012345678901234567890123456789@123456789012345678901234567890123456789", "...01234567890123456789@12345678901234567890..."}, {60, "012345678901234567890123456789012345678901234567890123456789@12345678901234567890", "...78901234567890123456789@12345678901234567890"}, {60, "012345678901234567890123456789012345678901234567890123456789@12345678901234567890123", "...01234567890123456789@12345678901234567890123"}, {60, "012345678901234567890123456789012345678901234567890123456789@123456789012345678901234", "...01234567890123456789@12345678901234567890..."}, {60, "0123456789012345678901234567890123456789ÎÎÎÎÎÎÎÎÎÎ@123456789012345678901234567890", "...0123456789ÎÎÎÎÎÎÎÎÎÎ@12345678901234567890..."}, {60, "012345678901234567890123456789012345678912 456780123456789@12345678901234567890", "...789·12·45678·0123456789@12345678901234567890"}, } for _, tt := range newlineTests { t.Run(fmt.Sprint(tt.buf, " ", tt.offset), func(t *testing.T) { r := bytes.NewBufferString(tt.buf) _, _, context := Position(r, tt.offset) i := strings.IndexByte(context, '\n') pointer := context[i+1:] context = context[:i] test.T(t, context[7:], tt.context) // check if @ and ^ are at the same position j := strings.IndexByte(context, '@') k := strings.IndexByte(pointer, '^') test.T(t, len([]rune(pointer[:k])), len([]rune(context[:j]))) }) } } ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/�������������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0014413�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/decimal.go���������������������������������������������������������������������0000664�0000000�0000000�00000002513�14632123015�0016341�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package strconv import ( "math" ) // ParseDecimal parses number of the format 1.2 func ParseDecimal(b []byte) (float64, int) { // float64 has up to 17 significant decimal digits and an exponent in [-1022,1023] i := 0 //sign := 1.0 //if 0 < len(b) && b[0] == '-' { // sign = -1.0 // i++ //} start := -1 dot := -1 n := uint64(0) for ; i < len(b); i++ { // parse up to 18 significant digits (with dot will be 17) ignoring zeros before/after c := b[i] if '0' <= c && c <= '9' { if start == -1 { if '1' <= c && c <= '9' { n = uint64(c - '0') start = i } } else if i-start < 18 { n *= 10 n += uint64(c - '0') } } else if c == '.' { if dot != -1 { break } dot = i } else { break } } if i == 1 && dot == 0 { return 0.0, 0 // only dot } else if start == -1 { return 0.0, i // only zeros and dot } else if dot == -1 { dot = i } exp := (dot - start) - LenUint(n) if dot < start { exp++ } if 1023 < exp { return math.Inf(1), i //if sign == 1.0 { // return math.Inf(1), i //} else { // return math.Inf(-1), i //} } else if exp < -1022 { return 0.0, i } f := float64(n) // sign * float64(n) if 0 <= exp && exp < 23 { return f * float64pow10[exp], i } else if 23 < exp && exp < 0 { return f / float64pow10[exp], i } return f * math.Pow10(exp), i } �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/decimal_test.go����������������������������������������������������������������0000664�0000000�0000000�00000002243�14632123015�0017400�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package strconv import ( "fmt" "testing" "github.com/tdewolff/test" ) func TestParseDecimal(t *testing.T) { tests := []struct { f string expected float64 }{ {"5", 5}, {"5.1", 5.1}, {"0.0000000000000000000000000005", 5e-28}, {"18446744073709551620", 18446744073709551620.0}, {"1000000000000000000000000.0000", 1e24}, // TODO {"1000000000000000000000000000000000000000000", 1e42}, // TODO } for _, tt := range tests { t.Run(fmt.Sprint(tt.f), func(t *testing.T) { f, n := ParseDecimal([]byte(tt.f)) test.T(t, n, len(tt.f)) test.Float(t, f, tt.expected) }) } } func TestParseDecimalError(t *testing.T) { tests := []struct { f string n int expected float64 }{ {"+1", 0, 0}, {"-1", 0, 0}, {".", 0, 0}, {"1e1", 1, 1}, } for _, tt := range tests { t.Run(fmt.Sprint(tt.f), func(t *testing.T) { f, n := ParseDecimal([]byte(tt.f)) test.T(t, n, tt.n) test.T(t, f, tt.expected) }) } } func FuzzParseDecimal(f *testing.F) { f.Add("5") f.Add("5.1") f.Add("18446744073709551620") f.Add("0.0000000000000000000000000005") f.Fuzz(func(t *testing.T, s string) { ParseDecimal([]byte(s)) }) } �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/float.go�����������������������������������������������������������������������0000664�0000000�0000000�00000013101�14632123015�0016043�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package strconv import ( "math" ) var float64pow10 = []float64{ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22, } // ParseFloat parses a byte-slice and returns the float it represents. // If an invalid character is encountered, it will stop there. func ParseFloat(b []byte) (float64, int) { i := 0 neg := false if i < len(b) && (b[i] == '+' || b[i] == '-') { neg = b[i] == '-' i++ } start := i dot := -1 trunk := -1 n := uint64(0) for ; i < len(b); i++ { c := b[i] if '0' <= c && c <= '9' { if trunk == -1 { if math.MaxUint64/10 < n { trunk = i } else { n *= 10 n += uint64(c - '0') } } } else if dot == -1 && c == '.' { dot = i } else { break } } if i == start || i == start+1 && dot == start { return 0.0, 0 } f := float64(n) if neg { f = -f } mantExp := int64(0) if dot != -1 { if trunk == -1 { trunk = i } mantExp = int64(trunk - dot - 1) } else if trunk != -1 { mantExp = int64(trunk - i) } expExp := int64(0) if i < len(b) && (b[i] == 'e' || b[i] == 'E') { startExp := i i++ if e, expLen := ParseInt(b[i:]); 0 < expLen { expExp = e i += expLen } else { i = startExp } } exp := expExp - mantExp // copied from strconv/atof.go if exp == 0 { return f, i } else if 0 < exp && exp <= 15+22 { // int * 10^k // If exponent is big but number of digits is not, // can move a few zeros into the integer part. if 22 < exp { f *= float64pow10[exp-22] exp = 22 } if -1e15 <= f && f <= 1e15 { return f * float64pow10[exp], i } } else if -22 <= exp && exp < 0 { // int / 10^k return f / float64pow10[-exp], i } f *= math.Pow10(int(-mantExp)) return f * math.Pow10(int(expExp)), i } const log2 = 0.3010299956639812 func float64exp(f float64) int { exp2 := 0 if f != 0.0 { x := math.Float64bits(f) exp2 = int(x>>(64-11-1))&0x7FF - 1023 + 1 } exp10 := float64(exp2) * log2 if exp10 < 0 { exp10 -= 1.0 } return int(exp10) } // AppendFloat appends a float to `b` with precision `prec`. It returns the new slice and whether successful or not. Precision is the number of decimals to display, thus prec + 1 == number of significant digits. func AppendFloat(b []byte, f float64, prec int) ([]byte, bool) { if math.IsNaN(f) || math.IsInf(f, 0) { return b, false } neg := false if f < 0.0 { f = -f neg = true } if prec < 0 || 17 < prec { prec = 17 // maximum number of significant digits in double } prec -= float64exp(f) // number of digits in front of the dot f *= math.Pow10(prec) // calculate mantissa and exponent mant := int64(f) mantLen := LenInt(mant) mantExp := mantLen - prec - 1 if mant == 0 { return append(b, '0'), true } // expLen is zero for positive exponents, because positive exponents are determined later on in the big conversion loop exp := 0 expLen := 0 if 0 < mantExp { // positive exponent is determined in the loop below // but if we initially decreased the exponent to fit in an integer, we can't set the new exponent in the loop alone, // since the number of zeros at the end determines the positive exponent in the loop, and we just artificially lost zeros if prec < 0 { exp = mantExp } expLen = 1 + LenInt(int64(exp)) // e + digits } else if mantExp < -3 { exp = mantExp expLen = 2 + LenInt(int64(exp)) // e + minus + digits } else if mantExp < -1 { mantLen += -mantExp - 1 // extra zero between dot and first digit } // reserve space in b i := len(b) maxLen := 1 + mantLen + expLen // dot + mantissa digits + exponent if neg { maxLen++ } if cap(b) < i+maxLen { b = append(b, make([]byte, maxLen)...) } else { b = b[:i+maxLen] } // write to string representation if neg { b[i] = '-' i++ } // big conversion loop, start at the end and move to the front // initially print trailing zeros and remove them later on // for example if the first non-zero digit is three positions in front of the dot, it will overwrite the zeros with a positive exponent zero := true last := i + mantLen // right-most position of digit that is non-zero + dot dot := last - prec - exp // position of dot j := last for 0 < mant { if j == dot { b[j] = '.' j-- } newMant := mant / 10 digit := mant - 10*newMant if zero && 0 < digit { // first non-zero digit, if we are still behind the dot we can trim the end to this position // otherwise trim to the dot (including the dot) if dot < j { i = j + 1 // decrease negative exponent further to get rid of dot if exp < 0 { newExp := exp - (j - dot) // getting rid of the dot shouldn't lower the exponent to more digits (e.g. -9 -> -10) if LenInt(int64(newExp)) == LenInt(int64(exp)) { exp = newExp dot = j j-- i-- } } } else { i = dot } last = j zero = false } b[j] = '0' + byte(digit) j-- mant = newMant } if dot < j { // extra zeros behind the dot for dot < j { b[j] = '0' j-- } b[j] = '.' } else if last+3 < dot { // add positive exponent because we have 3 or more zeros in front of the dot i = last + 1 exp = dot - last - 1 } else if j == dot { // handle 0.1 b[j] = '.' } // exponent if exp != 0 { if exp == 1 { b[i] = '0' i++ } else if exp == 2 { b[i] = '0' b[i+1] = '0' i += 2 } else { b[i] = 'e' i++ if exp < 0 { b[i] = '-' i++ exp = -exp } i += LenInt(int64(exp)) j := i for 0 < exp { newExp := exp / 10 digit := exp - 10*newExp j-- b[j] = '0' + byte(digit) exp = newExp } } } return b[:i], true } ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/float_test.go������������������������������������������������������������������0000664�0000000�0000000�00000012544�14632123015�0017114�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package strconv import ( "fmt" "math" "math/rand" "strconv" "testing" "github.com/tdewolff/test" ) func TestParseFloat(t *testing.T) { floatTests := []struct { f string expected float64 }{ {"5", 5}, {"5.1", 5.1}, {"-5.1", -5.1}, {"5.1e-2", 5.1e-2}, {"5.1e+2", 5.1e+2}, {"0.0e1", 0.0e1}, {"18446744073709551620", 18446744073709551620.0}, {"1e23", 1e23}, // TODO: hard to test due to float imprecision // {"1.7976931348623e+308", 1.7976931348623e+308) // {"4.9406564584124e-308", 4.9406564584124e-308) } for _, tt := range floatTests { t.Run(fmt.Sprint(tt.f), func(t *testing.T) { f, n := ParseFloat([]byte(tt.f)) test.T(t, n, len(tt.f)) test.T(t, f, tt.expected) }) } } func TestParseFloatError(t *testing.T) { floatTests := []struct { f string n int expected float64 }{ {"e1", 0, 0}, {".", 0, 0}, {"1e", 1, 1}, {"1e+", 1, 1}, {"1e+1", 4, 10}, } for _, tt := range floatTests { t.Run(fmt.Sprint(tt.f), func(t *testing.T) { f, n := ParseFloat([]byte(tt.f)) test.T(t, n, tt.n) test.T(t, f, tt.expected) }) } } func TestAppendFloat(t *testing.T) { floatTests := []struct { f float64 prec int expected string }{ {0, 6, "0"}, {1, 6, "1"}, {9, 6, "9"}, {9.99999, 6, "9.99999"}, {123, 6, "123"}, {0.123456, 6, ".123456"}, {0.066, 6, ".066"}, {0.0066, 6, ".0066"}, {12e2, 6, "1200"}, {12e3, 6, "12e3"}, {0.1, 6, ".1"}, {0.001, 6, ".001"}, {0.0001, 6, "1e-4"}, {-1, 6, "-1"}, {-123, 6, "-123"}, {-123.456, 6, "-123.456"}, {-12e3, 6, "-12e3"}, {-0.1, 6, "-.1"}, {-0.0001, 6, "-1e-4"}, {0.000100009, 10, "100009e-9"}, {0.0001000009, 10, "1.000009e-4"}, {1e18, 0, "1e18"}, //{1e19, 0, "1e19"}, //{1e19, 18, "1e19"}, {1e1, 0, "10"}, {1e2, 1, "100"}, {1e3, 2, "1e3"}, {1e10, -1, "1e10"}, {1e15, -1, "1e15"}, {1e-5, 6, "1e-5"}, {math.NaN(), 0, ""}, {math.Inf(1), 0, ""}, {math.Inf(-1), 0, ""}, {0, 19, "0"}, {0.000923361977200859392, -1, "9.23361977200859392e-4"}, {1234, 2, "1.23e3"}, {12345, 2, "1.23e4"}, {12.345, 2, "12.3"}, {12.345, 3, "12.34"}, } for _, tt := range floatTests { t.Run(fmt.Sprint(tt.f), func(t *testing.T) { f, _ := AppendFloat([]byte{}, tt.f, tt.prec) test.String(t, string(f), tt.expected) }) } b := make([]byte, 0, 22) AppendFloat(b, 12.34, -1) test.String(t, string(b[:5]), "12.34", "in buffer") } func FuzzParseFloat(f *testing.F) { f.Add("5") f.Add("99") f.Add("18446744073709551615") f.Add("5") f.Add("5.1") f.Add("-5.1") f.Add("5.1e-2") f.Add("5.1e+2") f.Add("0.0e1") f.Add("18446744073709551620") f.Add("1e23") f.Fuzz(func(t *testing.T, s string) { ParseFloat([]byte(s)) }) } func FuzzAppendFloat(f *testing.F) { f.Add(0.0, 6) f.Add(1.0, 6) f.Add(9.0, 6) f.Add(9.99999, 6) f.Add(123.0, 6) f.Add(0.123456, 6) f.Add(0.066, 6) f.Add(0.0066, 6) f.Add(12e2, 6) f.Add(12e3, 6) f.Add(0.1, 6) f.Add(0.001, 6) f.Add(0.0001, 6) f.Add(-1.0, 6) f.Add(-123.0, 6) f.Add(-123.456, 6) f.Add(-12e3, 6) f.Add(-0.1, 6) f.Add(-0.0001, 6) f.Add(0.000100009, 10) f.Add(0.0001000009, 10) f.Add(1e18, 0) f.Add(1e1, 0) f.Add(1e2, 1) f.Add(1e3, 2) f.Add(1e10, -1) f.Add(1e15, -1) f.Add(1e-5, 6) f.Add(math.NaN(), 0) f.Add(math.Inf(1), 0) f.Add(math.Inf(-1), 0) f.Add(0.0, 19) f.Add(0.000923361977200859392, -1) f.Add(1234.0, 2) f.Add(12345.0, 2) f.Add(12.345, 2) f.Add(12.345, 3) f.Fuzz(func(t *testing.T, f float64, prec int) { AppendFloat([]byte{}, f, prec) }) } //////////////////////////////////////////////////////////////// func TestAppendFloatRandom(t *testing.T) { N := int(1e6) if testing.Short() { N = 0 } r := rand.New(rand.NewSource(99)) //prec := 10 for i := 0; i < N; i++ { f := r.ExpFloat64() //f = math.Floor(f*float64(prec)) / float64(prec) b, _ := AppendFloat([]byte{}, f, -1) f2, _ := strconv.ParseFloat(string(b), 64) if math.Abs(f-f2) > 1e-6 { fmt.Println("Bad:", f, "!=", f2, "in", string(b)) } } } func BenchmarkFloatToBytes1(b *testing.B) { r := []byte{} //make([]byte, 10) f := 123.456 for i := 0; i < b.N; i++ { r = strconv.AppendFloat(r[:0], f, 'g', 6, 64) } } func BenchmarkFloatToBytes2(b *testing.B) { r := make([]byte, 10) f := 123.456 for i := 0; i < b.N; i++ { r, _ = AppendFloat(r[:0], f, 6) } } func BenchmarkModf1(b *testing.B) { f := 123.456 x := 0.0 for i := 0; i < b.N; i++ { a, b := math.Modf(f) x += a + b } } func BenchmarkModf2(b *testing.B) { f := 123.456 x := 0.0 for i := 0; i < b.N; i++ { a := float64(int64(f)) b := f - a x += a + b } } func BenchmarkPrintInt1(b *testing.B) { X := int64(123456789) n := LenInt(X) r := make([]byte, n) for i := 0; i < b.N; i++ { x := X j := n for x > 0 { j-- r[j] = '0' + byte(x%10) x /= 10 } } } func BenchmarkPrintInt2(b *testing.B) { X := int64(123456789) n := LenInt(X) r := make([]byte, n) for i := 0; i < b.N; i++ { x := X j := n for x > 0 { j-- newX := x / 10 r[j] = '0' + byte(x-10*newX) x = newX } } } var int64pow10 = []int64{ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, } func BenchmarkPrintInt3(b *testing.B) { X := int64(123456789) n := LenInt(X) r := make([]byte, n) for i := 0; i < b.N; i++ { x := X j := 0 for j < n { pow := int64pow10[n-j-1] tmp := x / pow r[j] = '0' + byte(tmp) j++ x -= tmp * pow } } } ������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/int.go�������������������������������������������������������������������������0000664�0000000�0000000�00000004001�14632123015�0015527�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package strconv import ( "math" ) // ParseInt parses a byte-slice and returns the integer it represents. // If an invalid character is encountered, it will stop there. func ParseInt(b []byte) (int64, int) { i := 0 neg := false if len(b) > 0 && (b[0] == '+' || b[0] == '-') { neg = b[0] == '-' i++ } start := i n := uint64(0) for i < len(b) { c := b[i] if '0' <= c && c <= '9' { if uint64(-math.MinInt64)/10 < n || uint64(-math.MinInt64)-uint64(c-'0') < n*10 { return 0, 0 } n *= 10 n += uint64(c - '0') } else { break } i++ } if i == start { return 0, 0 } if !neg && uint64(math.MaxInt64) < n { return 0, 0 } else if neg { return -int64(n), i } return int64(n), i } // ParseUint parses a byte-slice and returns the integer it represents. // If an invalid character is encountered, it will stop there. func ParseUint(b []byte) (uint64, int) { i := 0 n := uint64(0) for i < len(b) { c := b[i] if '0' <= c && c <= '9' { if math.MaxUint64/10 < n || math.MaxUint64-uint64(c-'0') < n*10 { return 0, 0 } n *= 10 n += uint64(c - '0') } else { break } i++ } return n, i } // LenInt returns the written length of an integer. func LenInt(i int64) int { if i < 0 { if i == -9223372036854775808 { return 19 } i = -i } return LenUint(uint64(i)) } func LenUint(i uint64) int { switch { case i < 10: return 1 case i < 100: return 2 case i < 1000: return 3 case i < 10000: return 4 case i < 100000: return 5 case i < 1000000: return 6 case i < 10000000: return 7 case i < 100000000: return 8 case i < 1000000000: return 9 case i < 10000000000: return 10 case i < 100000000000: return 11 case i < 1000000000000: return 12 case i < 10000000000000: return 13 case i < 100000000000000: return 14 case i < 1000000000000000: return 15 case i < 10000000000000000: return 16 case i < 100000000000000000: return 17 case i < 1000000000000000000: return 18 case i < 10000000000000000000: return 19 } return 20 } �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/int_test.go��������������������������������������������������������������������0000664�0000000�0000000�00000006342�14632123015�0016600�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package strconv import ( "fmt" "math" "math/rand" "testing" "github.com/tdewolff/test" ) func TestParseInt(t *testing.T) { intTests := []struct { i string expected int64 }{ {"5", 5}, {"99", 99}, {"999", 999}, {"-5", -5}, {"+5", 5}, {"9223372036854775807", 9223372036854775807}, {"-9223372036854775807", -9223372036854775807}, {"-9223372036854775808", -9223372036854775808}, } for _, tt := range intTests { t.Run(fmt.Sprint(tt.i), func(t *testing.T) { i, n := ParseInt([]byte(tt.i)) test.T(t, n, len(tt.i)) test.T(t, i, tt.expected) }) } } func TestParseIntError(t *testing.T) { intTests := []struct { i string n int expected int64 }{ {"a", 0, 0}, {"+", 0, 0}, {"9223372036854775808", 0, 0}, {"-9223372036854775809", 0, 0}, {"18446744073709551620", 0, 0}, } for _, tt := range intTests { t.Run(fmt.Sprint(tt.i), func(t *testing.T) { i, n := ParseInt([]byte(tt.i)) test.T(t, n, tt.n) test.T(t, i, tt.expected) }) } } func FuzzParseInt(f *testing.F) { f.Add("5") f.Add("-99") f.Add("9223372036854775807") f.Add("-9223372036854775808") f.Fuzz(func(t *testing.T, s string) { ParseInt([]byte(s)) }) } func TestParseUint(t *testing.T) { intTests := []struct { i string expected uint64 }{ {"5", 5}, {"99", 99}, {"999", 999}, {"18446744073709551615", 18446744073709551615}, } for _, tt := range intTests { t.Run(fmt.Sprint(tt.i), func(t *testing.T) { i, n := ParseUint([]byte(tt.i)) test.T(t, n, len(tt.i)) test.T(t, i, tt.expected) }) } } func TestParseUintError(t *testing.T) { intTests := []struct { i string n int expected uint64 }{ {"a", 0, 0}, {"18446744073709551616", 0, 0}, {"-1", 0, 0}, } for _, tt := range intTests { t.Run(fmt.Sprint(tt.i), func(t *testing.T) { i, n := ParseUint([]byte(tt.i)) test.T(t, n, tt.n) test.T(t, i, tt.expected) }) } } func FuzzParseUint(f *testing.F) { f.Add("5") f.Add("99") f.Add("18446744073709551615") f.Fuzz(func(t *testing.T, s string) { ParseUint([]byte(s)) }) } func TestLenInt(t *testing.T) { lenIntTests := []struct { i int64 expected int }{ {0, 1}, {1, 1}, {10, 2}, {99, 2}, {9223372036854775807, 19}, {-9223372036854775808, 19}, // coverage {100, 3}, {1000, 4}, {10000, 5}, {100000, 6}, {1000000, 7}, {10000000, 8}, {100000000, 9}, {1000000000, 10}, {10000000000, 11}, {100000000000, 12}, {1000000000000, 13}, {10000000000000, 14}, {100000000000000, 15}, {1000000000000000, 16}, {10000000000000000, 17}, {100000000000000000, 18}, {1000000000000000000, 19}, } for _, tt := range lenIntTests { t.Run(fmt.Sprint(tt.i), func(t *testing.T) { test.T(t, LenInt(tt.i), tt.expected) }) } } //////////////////////////////////////////////////////////////// var num []int64 func TestMain(t *testing.T) { for j := 0; j < 1000; j++ { num = append(num, rand.Int63n(1000)) } } func BenchmarkLenIntLog(b *testing.B) { n := 0 for i := 0; i < b.N; i++ { for j := 0; j < 1000; j++ { n += int(math.Log10(math.Abs(float64(num[j])))) + 1 } } } func BenchmarkLenIntSwitch(b *testing.B) { n := 0 for i := 0; i < b.N; i++ { for j := 0; j < 1000; j++ { n += LenInt(num[j]) } } } ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/number.go����������������������������������������������������������������������0000664�0000000�0000000�00000004512�14632123015�0016234�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package strconv import ( "math" "unicode/utf8" ) // ParseNumber parses a byte-slice and returns the number it represents and the amount of decimals. // If an invalid character is encountered, it will stop there. func ParseNumber(b []byte, groupSym rune, decSym rune) (int64, int, int) { n, dec := 0, 0 sign := int64(1) price := int64(0) hasDecimals := false if 0 < len(b) && b[0] == '-' { sign = -1 n++ } for n < len(b) { if '0' <= b[n] && b[n] <= '9' { digit := sign * int64(b[n]-'0') if sign == 1 && (math.MaxInt64/10 < price || math.MaxInt64-digit < price*10) { break } else if sign == -1 && (price < math.MinInt64/10 || price*10 < math.MinInt64-digit) { break } price *= 10 price += digit if hasDecimals { dec++ } n++ } else if r, size := utf8.DecodeRune(b[n:]); !hasDecimals && (r == groupSym || r == decSym) { if r == decSym { hasDecimals = true } n += size } else { break } } return price, dec, n } // AppendNumber will append an int64 formatted as a number with the given number of decimal digits. func AppendNumber(b []byte, price int64, dec int, groupSize int, groupSym rune, decSym rune) []byte { if dec < 0 { dec = 0 } if utf8.RuneLen(groupSym) == -1 { groupSym = '.' } if utf8.RuneLen(decSym) == -1 { decSym = ',' } sign := int64(1) if price < 0 { sign = -1 } // calculate size n := LenInt(price) if dec < n && 0 < groupSize && groupSym != 0 { n += utf8.RuneLen(groupSym) * (n - dec - 1) / groupSize } if 0 < dec { if n <= dec { n = 1 + dec // zero and decimals } n += utf8.RuneLen(decSym) } if sign == -1 { n++ } // resize byte slice i := len(b) if cap(b) < i+n { b = append(b, make([]byte, n)...) } else { b = b[:i+n] } // print fractional-part i += n - 1 if 0 < dec { for 0 < dec { c := byte(sign*(price%10)) + '0' price /= 10 b[i] = c dec-- i-- } i -= utf8.RuneLen(decSym) utf8.EncodeRune(b[i+1:], decSym) } // print integer-part if price == 0 { b[i] = '0' if sign == -1 { b[i-1] = '-' } return b } j := 0 for price != 0 { if 0 < groupSize && groupSym != 0 && 0 < j && j%groupSize == 0 { i -= utf8.RuneLen(groupSym) utf8.EncodeRune(b[i+1:], groupSym) } c := byte(sign*(price%10)) + '0' price /= 10 b[i] = c i-- j++ } if sign == -1 { b[i] = '-' } return b } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/strconv/number_test.go�����������������������������������������������������������������0000664�0000000�0000000�00000004366�14632123015�0017302�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package strconv import ( "testing" "github.com/tdewolff/test" ) func TestParseNumber(t *testing.T) { tests := []struct { s string num int64 dec int n int }{ {"5", 5, 0, 1}, {"-5", -5, 0, 2}, {"5,0", 50, 1, 3}, {"5,0a", 50, 1, 3}, {"-1000,00", -100000, 2, 8}, {"9223372036854775807", 9223372036854775807, 0, 19}, {"-9223372036854775807", -9223372036854775807, 0, 20}, {"-9223372036854775808", -9223372036854775808, 0, 20}, {"92233720368547758070", 9223372036854775807, 0, 19}, {"-92233720368547758080", -9223372036854775808, 0, 20}, } for _, tt := range tests { t.Run(tt.s, func(t *testing.T) { num, dec, n := ParseNumber([]byte(tt.s), '.', ',') test.T(t, []interface{}{num, dec, n}, []interface{}{tt.num, tt.dec, tt.n}) }) } } func TestAppendNumber(t *testing.T) { tests := []struct { num int64 dec int s string }{ {0, 0, "0"}, {0, -1, "0"}, {0, 2, "0,00"}, {1, 2, "0,01"}, {-1, 2, "-0,01"}, {100, 2, "1,00"}, {-100, 2, "-1,00"}, {1000, 0, "1.000"}, {100000, 2, "1.000,00"}, {123456789012, 2, "1.234.567.890,12"}, {9223372036854775807, 2, "92.233.720.368.547.758,07"}, {-9223372036854775808, 2, "-92.233.720.368.547.758,08"}, } for _, tt := range tests { t.Run(tt.s, func(t *testing.T) { num := AppendNumber(make([]byte, 0, 4), tt.num, tt.dec, 3, '.', ',') test.String(t, string(num), tt.s) }) } // coverage test.String(t, string(AppendNumber(make([]byte, 0, 7), 12345, 1, 3, -1, -1)), "1.234,5") } func FuzzParseNumber(f *testing.F) { f.Add("5") f.Add("-5") f.Add("5,0") f.Add("5,0a") f.Add("-1000,00") f.Add("9223372036854775807") f.Add("-9223372036854775807") f.Add("-9223372036854775808") f.Add("92233720368547758070") f.Add("-92233720368547758080") f.Fuzz(func(t *testing.T, s string) { ParseNumber([]byte(s), '.', ',') }) } func FuzzAppendNumber(f *testing.F) { f.Add(int64(0), 0) f.Add(int64(0), -1) f.Add(int64(0), 2) f.Add(int64(1), 2) f.Add(int64(-1), 2) f.Add(int64(100), 2) f.Add(int64(-100), 2) f.Add(int64(1000), 0) f.Add(int64(100000), 2) f.Add(int64(123456789012), 2) f.Add(int64(9223372036854775807), 2) f.Add(int64(-9223372036854775808), 2) f.Fuzz(func(t *testing.T, num int64, dec int) { AppendNumber([]byte{}, num, dec, 3, '.', ',') }) } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/���������������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0014057�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/README.md������������������������������������������������������������������������0000664�0000000�0000000�00000000777�14632123015�0015351�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# Fuzzing parse Fuzz testing for `parse` using [go-fuzz](https://github.com/dvyukov/go-fuzz). Pull requests to add more corpora or testers are appreciated. To run the tests, install `go-fuzz`: ``` go get -u github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-build cd $GOPATH/github.com/tdewolff/parse/tests/number go-fuzz-build go-fuzz -bin fuzz-fuzz.zip ``` If restarts is not close to `1/10000`, something is probably wrong. If not finding new corpus for a while, restart the fuzzer. �parse-2.7.15/tests/css-token/�����������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0015765�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/css-token/corpus/����������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0017300�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/css-token/corpus/1.txt�����������������������������������������������������������0000664�0000000�0000000�00000000017�14632123015�0020177�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������-test128\@\ '" �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/css-token/main.go����������������������������������������������������������������0000664�0000000�0000000�00000000250�14632123015�0017235�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// +build gofuzz package fuzz import ( "github.com/tdewolff/parse/v2/css" ) // Fuzz is a fuzz test. func Fuzz(data []byte) int { _ = css.IsIdent(data) return 1 } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/data-uri/������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0015565�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/data-uri/corpus/�����������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0017100�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/data-uri/corpus/1.corpus���������������������������������������������������������0000664�0000000�0000000�00000000062�14632123015�0020473�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/data-uri/corpus/2.corpus���������������������������������������������������������0000664�0000000�0000000�00000000216�14632123015�0020475�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:image/png;base64,iVBORw0KGgoAAA ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4 //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU 5ErkJggg== ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/data-uri/corpus/3.corpus���������������������������������������������������������0000664�0000000�0000000�00000000073�14632123015�0020477�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678 ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/data-uri/corpus/4.corpus���������������������������������������������������������0000664�0000000�0000000�00000001304�14632123015�0020476�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDADIiJSwlHzIsKSw4NTI7S31RS0VFS5ltc1p9tZ++u7Kfr6zI4f/zyNT/16yv+v/9////////wfD/////////////2wBDATU4OEtCS5NRUZP/zq/O////////////////////////////////////////////////////////////////////wAARCAAYAEADAREAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAQMAAgQF/8QAJRABAAIBBAEEAgMAAAAAAAAAAQIRAAMSITEEEyJBgTORUWFx/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AOgM52xQDrjvAV5Xv0vfKUALlTQfeBm0HThMNHXkL0Lw/swN5qgA8yT4MCS1OEOJV8mBz9Z05yfW8iSx7p4j+jA1aD6Wj7ZMzstsfvAas4UyRHvjrAkC9KhpLMClQntlqFc2X1gUj4viwVObKrddH9YDoHvuujAEuNV+bLwFS8XxdSr+Cq3Vf+4F5RgQl6ZR2p1eAzU/HX80YBYyJLCuexwJCO2O1bwCRidAfWBSctswbI12GAJT3yiwFR7+MBjGK2g/WAJR3FdF84E2rK5VR0YH/9k= ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/data-uri/main.go�����������������������������������������������������������������0000664�0000000�0000000�00000000300�14632123015�0017031�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// +build gofuzz package fuzz import "github.com/tdewolff/parse/v2" // Fuzz is a fuzz test. func Fuzz(data []byte) int { data = parse.Copy(data) _, _, _ = parse.DataURI(data) return 1 } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/dimension/�����������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0016044�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/dimension/corpus/����������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0017357�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/dimension/corpus/1.corpus��������������������������������������������������������0000664�0000000�0000000�00000000062�14632123015�0020752�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/dimension/corpus/2.corpus��������������������������������������������������������0000664�0000000�0000000�00000000216�14632123015�0020754�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:image/png;base64,iVBORw0KGgoAAA ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4 //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU 5ErkJggg== ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/dimension/corpus/3.corpus��������������������������������������������������������0000664�0000000�0000000�00000000073�14632123015�0020756�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678 ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/dimension/corpus/4.corpus��������������������������������������������������������0000664�0000000�0000000�00000001304�14632123015�0020755�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDADIiJSwlHzIsKSw4NTI7S31RS0VFS5ltc1p9tZ++u7Kfr6zI4f/zyNT/16yv+v/9////////wfD/////////////2wBDATU4OEtCS5NRUZP/zq/O////////////////////////////////////////////////////////////////////wAARCAAYAEADAREAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAQMAAgQF/8QAJRABAAIBBAEEAgMAAAAAAAAAAQIRAAMSITEEEyJBgTORUWFx/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AOgM52xQDrjvAV5Xv0vfKUALlTQfeBm0HThMNHXkL0Lw/swN5qgA8yT4MCS1OEOJV8mBz9Z05yfW8iSx7p4j+jA1aD6Wj7ZMzstsfvAas4UyRHvjrAkC9KhpLMClQntlqFc2X1gUj4viwVObKrddH9YDoHvuujAEuNV+bLwFS8XxdSr+Cq3Vf+4F5RgQl6ZR2p1eAzU/HX80YBYyJLCuexwJCO2O1bwCRidAfWBSctswbI12GAJT3yiwFR7+MBjGK2g/WAJR3FdF84E2rK5VR0YH/9k= ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/dimension/main.go����������������������������������������������������������������0000664�0000000�0000000�00000000246�14632123015�0017321�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// +build gofuzz package fuzz import "github.com/tdewolff/parse/v2" // Fuzz is a fuzz test. func Fuzz(data []byte) int { _, _ = parse.Dimension(data) return 1 } ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/js-string/�����������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0015777�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/js-string/main.go����������������������������������������������������������������0000664�0000000�0000000�00000001274�14632123015�0017256�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// +build gofuzz package fuzz import ( "fmt" "strings" "unicode/utf8" "github.com/tdewolff/parse/v2" "github.com/tdewolff/parse/v2/js" ) // Fuzz is a fuzz test. func Fuzz(data []byte) int { if !utf8.Valid(data) { return 0 } o := js.Options{} input := parse.NewInputBytes(data) if ast, err := js.Parse(input, o); err == nil { src := ast.JSString() input2 := parse.NewInputString(src) if ast2, err := js.Parse(input2, o); err != nil { if !strings.HasPrefix(err.Error(), "too many nested") { panic(err) } } else if src2 := ast2.JSString(); src != src2 { fmt.Println("JS1:", src) fmt.Println("JS2:", src2) panic("ASTs not equal") } return 1 } return 0 } ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/mediatype/�����������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0016040�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/mediatype/corpus/����������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0017353�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/mediatype/corpus/1.corpus��������������������������������������������������������0000664�0000000�0000000�00000000062�14632123015�0020746�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/mediatype/corpus/2.corpus��������������������������������������������������������0000664�0000000�0000000�00000000216�14632123015�0020750�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:image/png;base64,iVBORw0KGgoAAA ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4 //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU 5ErkJggg== ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/mediatype/corpus/3.corpus��������������������������������������������������������0000664�0000000�0000000�00000000073�14632123015�0020752�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678 ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/mediatype/corpus/4.corpus��������������������������������������������������������0000664�0000000�0000000�00000001304�14632123015�0020751�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDADIiJSwlHzIsKSw4NTI7S31RS0VFS5ltc1p9tZ++u7Kfr6zI4f/zyNT/16yv+v/9////////wfD/////////////2wBDATU4OEtCS5NRUZP/zq/O////////////////////////////////////////////////////////////////////wAARCAAYAEADAREAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAQMAAgQF/8QAJRABAAIBBAEEAgMAAAAAAAAAAQIRAAMSITEEEyJBgTORUWFx/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AOgM52xQDrjvAV5Xv0vfKUALlTQfeBm0HThMNHXkL0Lw/swN5qgA8yT4MCS1OEOJV8mBz9Z05yfW8iSx7p4j+jA1aD6Wj7ZMzstsfvAas4UyRHvjrAkC9KhpLMClQntlqFc2X1gUj4viwVObKrddH9YDoHvuujAEuNV+bLwFS8XxdSr+Cq3Vf+4F5RgQl6ZR2p1eAzU/HX80YBYyJLCuexwJCO2O1bwCRidAfWBSctswbI12GAJT3yiwFR7+MBjGK2g/WAJR3FdF84E2rK5VR0YH/9k= ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/mediatype/main.go����������������������������������������������������������������0000664�0000000�0000000�00000000246�14632123015�0017315�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// +build gofuzz package fuzz import "github.com/tdewolff/parse/v2" // Fuzz is a fuzz test. func Fuzz(data []byte) int { _, _ = parse.Mediatype(data) return 1 } ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/number/��������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0015347�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/number/corpus/�������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0016662�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/number/corpus/1.corpus�����������������������������������������������������������0000664�0000000�0000000�00000000062�14632123015�0020255�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/number/corpus/2.corpus�����������������������������������������������������������0000664�0000000�0000000�00000000216�14632123015�0020257�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:image/png;base64,iVBORw0KGgoAAA ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4 //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU 5ErkJggg== ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/number/corpus/3.corpus�����������������������������������������������������������0000664�0000000�0000000�00000000073�14632123015�0020261�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678 ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/number/corpus/4.corpus�����������������������������������������������������������0000664�0000000�0000000�00000001304�14632123015�0020260�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDADIiJSwlHzIsKSw4NTI7S31RS0VFS5ltc1p9tZ++u7Kfr6zI4f/zyNT/16yv+v/9////////wfD/////////////2wBDATU4OEtCS5NRUZP/zq/O////////////////////////////////////////////////////////////////////wAARCAAYAEADAREAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAQMAAgQF/8QAJRABAAIBBAEEAgMAAAAAAAAAAQIRAAMSITEEEyJBgTORUWFx/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AOgM52xQDrjvAV5Xv0vfKUALlTQfeBm0HThMNHXkL0Lw/swN5qgA8yT4MCS1OEOJV8mBz9Z05yfW8iSx7p4j+jA1aD6Wj7ZMzstsfvAas4UyRHvjrAkC9KhpLMClQntlqFc2X1gUj4viwVObKrddH9YDoHvuujAEuNV+bLwFS8XxdSr+Cq3Vf+4F5RgQl6ZR2p1eAzU/HX80YBYyJLCuexwJCO2O1bwCRidAfWBSctswbI12GAJT3yiwFR7+MBjGK2g/WAJR3FdF84E2rK5VR0YH/9k= ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/number/main.go�������������������������������������������������������������������0000664�0000000�0000000�00000000240�14632123015�0016616�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// +build gofuzz package fuzz import "github.com/tdewolff/parse/v2" // Fuzz is a fuzz test. func Fuzz(data []byte) int { _ = parse.Number(data) return 1 } ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/replace-entities/����������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0017314�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/replace-entities/corpus/���������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0020627�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/replace-entities/corpus/1.corpus�������������������������������������������������0000664�0000000�0000000�00000000017�14632123015�0022222�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������test&test;test �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/replace-entities/corpus/2.corpus�������������������������������������������������0000664�0000000�0000000�00000000022�14632123015�0022217�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������&quot&quot;;&apos ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/replace-entities/corpus/3.corpus�������������������������������������������������0000664�0000000�0000000�00000000016�14632123015�0022223�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������&test4;&Long; ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/tests/replace-entities/main.go���������������������������������������������������������0000664�0000000�0000000�00000001031�14632123015�0020562�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// +build gofuzz package fuzz import "github.com/tdewolff/parse/v2" // Fuzz is a fuzz test. func Fuzz(data []byte) int { data = parse.Copy(data) // ignore const-input error for OSS-Fuzz newData := parse.ReplaceEntities(data, map[string][]byte{ "test": []byte("&t;"), "test3": []byte("&test;"), "test5": []byte("&#5;"), "quot": []byte("\""), "apos": []byte("'"), }, map[byte][]byte{ '\'': []byte("&#34;"), '"': []byte("&#39;"), }) if len(newData) > len(data) { panic("output longer than input") } return 1 } �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/util.go��������������������������������������������������������������������������������0000664�0000000�0000000�00000014077�14632123015�0014232�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package parse import ( "fmt" "io" "unicode" ) // Copy returns a copy of the given byte slice. func Copy(src []byte) (dst []byte) { dst = make([]byte, len(src)) copy(dst, src) return } // ToLower converts all characters in the byte slice from A-Z to a-z. func ToLower(src []byte) []byte { for i, c := range src { if c >= 'A' && c <= 'Z' { src[i] = c + ('a' - 'A') } } return src } // EqualFold returns true when s matches case-insensitively the targetLower (which must be lowercase). func EqualFold(s, targetLower []byte) bool { if len(s) != len(targetLower) { return false } for i, c := range targetLower { d := s[i] if d != c && (d < 'A' || d > 'Z' || d+('a'-'A') != c) { return false } } return true } // Printable returns a printable string for given rune func Printable(r rune) string { if unicode.IsGraphic(r) { return fmt.Sprintf("%c", r) } else if r < 128 { return fmt.Sprintf("0x%02X", r) } return fmt.Sprintf("%U", r) } var whitespaceTable = [256]bool{ // ASCII false, false, false, false, false, false, false, false, false, true, true, false, true, true, false, false, // tab, new line, form feed, carriage return false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, // space false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, // non-ASCII false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, } // IsWhitespace returns true for space, \n, \r, \t, \f. func IsWhitespace(c byte) bool { return whitespaceTable[c] } var newlineTable = [256]bool{ // ASCII false, false, false, false, false, false, false, false, false, false, true, false, false, true, false, false, // new line, carriage return false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, // non-ASCII false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, } // IsNewline returns true for \n, \r. func IsNewline(c byte) bool { return newlineTable[c] } // IsAllWhitespace returns true when the entire byte slice consists of space, \n, \r, \t, \f. func IsAllWhitespace(b []byte) bool { for _, c := range b { if !IsWhitespace(c) { return false } } return true } // TrimWhitespace removes any leading and trailing whitespace characters. func TrimWhitespace(b []byte) []byte { n := len(b) start := n for i := 0; i < n; i++ { if !IsWhitespace(b[i]) { start = i break } } end := n for i := n - 1; i >= start; i-- { if !IsWhitespace(b[i]) { end = i + 1 break } } return b[start:end] } type Indenter struct { io.Writer b []byte } func NewIndenter(w io.Writer, n int) Indenter { if wi, ok := w.(Indenter); ok { w = wi.Writer n += len(wi.b) } b := make([]byte, n) for i := range b { b[i] = ' ' } return Indenter{ Writer: w, b: b, } } func (in Indenter) Indent() int { return len(in.b) } func (in Indenter) Write(b []byte) (int, error) { n, j := 0, 0 for i, c := range b { if c == '\n' { m, _ := in.Writer.Write(b[j : i+1]) n += m m, _ = in.Writer.Write(in.b) n += m j = i + 1 } } m, err := in.Writer.Write(b[j:]) return n + m, err } �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/util_test.go���������������������������������������������������������������������������0000664�0000000�0000000�00000007105�14632123015�0015263�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package parse import ( "bytes" "math/rand" "testing" "github.com/tdewolff/test" ) func helperRandChars(n, m int, chars string) [][]byte { r := make([][]byte, n) for i := range r { for j := 0; j < m; j++ { r[i] = append(r[i], chars[rand.Intn(len(chars))]) } } return r } func helperRandStrings(n, m int, ss []string) [][]byte { r := make([][]byte, n) for i := range r { for j := 0; j < m; j++ { r[i] = append(r[i], []byte(ss[rand.Intn(len(ss))])...) } } return r } //////////////////////////////////////////////////////////////// var wsSlices [][]byte func init() { wsSlices = helperRandChars(10000, 50, "abcdefg \n\r\f\t") } func TestCopy(t *testing.T) { foo := []byte("abc") bar := Copy(foo) foo[0] = 'b' test.String(t, string(foo), "bbc") test.String(t, string(bar), "abc") } func TestToLower(t *testing.T) { foo := []byte("Abc") bar := ToLower(foo) bar[1] = 'B' test.String(t, string(foo), "aBc") test.String(t, string(bar), "aBc") } func TestEqualFold(t *testing.T) { test.That(t, EqualFold([]byte("Abc"), []byte("abc"))) test.That(t, !EqualFold([]byte("Abcd"), []byte("abc"))) test.That(t, !EqualFold([]byte("Bbc"), []byte("abc"))) test.That(t, !EqualFold([]byte("[]"), []byte("{}"))) // same distance in ASCII as 'a' and 'A' } func TestWhitespace(t *testing.T) { test.That(t, IsAllWhitespace([]byte("\t \r\n\f"))) test.That(t, !IsAllWhitespace([]byte("\t \r\n\fx"))) } func TestTrim(t *testing.T) { test.Bytes(t, TrimWhitespace([]byte("a")), []byte("a")) test.Bytes(t, TrimWhitespace([]byte(" a")), []byte("a")) test.Bytes(t, TrimWhitespace([]byte("a ")), []byte("a")) test.Bytes(t, TrimWhitespace([]byte(" ")), []byte("")) } func TestPrintable(t *testing.T) { var tests = []struct { s string printable string }{ {"a", "a"}, {"\x00", "0x00"}, {"\x7F", "0x7F"}, {"\u0800", "ࠀ"}, {"\u200F", "U+200F"}, } for _, tt := range tests { t.Run(tt.s, func(t *testing.T) { printable := "" for _, r := range tt.s { printable += Printable(r) } test.T(t, printable, tt.printable) }) } } //////////////////////////////////////////////////////////////// func BenchmarkBytesTrim(b *testing.B) { for i := 0; i < b.N; i++ { for _, e := range wsSlices { bytes.TrimSpace(e) } } } func BenchmarkTrim(b *testing.B) { for i := 0; i < b.N; i++ { for _, e := range wsSlices { TrimWhitespace(e) } } } func BenchmarkWhitespaceTable(b *testing.B) { n := 0 for i := 0; i < b.N; i++ { for _, e := range wsSlices { for _, c := range e { if IsWhitespace(c) { n++ } } } } } func BenchmarkWhitespaceIf1(b *testing.B) { n := 0 for i := 0; i < b.N; i++ { for _, e := range wsSlices { for _, c := range e { if c == ' ' { n++ } } } } } func BenchmarkWhitespaceIf2(b *testing.B) { n := 0 for i := 0; i < b.N; i++ { for _, e := range wsSlices { for _, c := range e { if c == ' ' || c == '\n' { n++ } } } } } func BenchmarkWhitespaceIf3(b *testing.B) { n := 0 for i := 0; i < b.N; i++ { for _, e := range wsSlices { for _, c := range e { if c == ' ' || c == '\n' || c == '\r' { n++ } } } } } func BenchmarkWhitespaceIf4(b *testing.B) { n := 0 for i := 0; i < b.N; i++ { for _, e := range wsSlices { for _, c := range e { if c == ' ' || c == '\n' || c == '\r' || c == '\t' { n++ } } } } } func BenchmarkWhitespaceIf5(b *testing.B) { n := 0 for i := 0; i < b.N; i++ { for _, e := range wsSlices { for _, c := range e { if c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f' { n++ } } } } } �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/xml/�����������������������������������������������������������������������������������0000775�0000000�0000000�00000000000�14632123015�0013515�5����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/xml/README.md��������������������������������������������������������������������������0000664�0000000�0000000�00000004070�14632123015�0014775�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������# XML [![API reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/tdewolff/parse/v2/xml?tab=doc) This package is an XML lexer written in [Go][1]. It follows the specification at [Extensible Markup Language (XML) 1.0 (Fifth Edition)](http://www.w3.org/TR/REC-xml/). The lexer takes an io.Reader and converts it into tokens until the EOF. ## Installation Run the following command go get -u github.com/tdewolff/parse/v2/xml or add the following import and run project with `go get` import "github.com/tdewolff/parse/v2/xml" ## Lexer ### Usage The following initializes a new Lexer with io.Reader `r`: ``` go l := xml.NewLexer(parse.NewInput(r)) ``` To tokenize until EOF an error, use: ``` go for { tt, data := l.Next() switch tt { case xml.ErrorToken: // error or EOF set in l.Err() return case xml.StartTagToken: // ... for { ttAttr, dataAttr := l.Next() if ttAttr != xml.AttributeToken { // handle StartTagCloseToken/StartTagCloseVoidToken/StartTagClosePIToken break } // ... } case xml.EndTagToken: // ... } } ``` All tokens: ``` go ErrorToken TokenType = iota // extra token when errors occur CommentToken CDATAToken StartTagToken StartTagCloseToken StartTagCloseVoidToken StartTagClosePIToken EndTagToken AttributeToken TextToken ``` ### Examples ``` go package main import ( "os" "github.com/tdewolff/parse/v2/xml" ) // Tokenize XML from stdin. func main() { l := xml.NewLexer(parse.NewInput(os.Stdin)) for { tt, data := l.Next() switch tt { case xml.ErrorToken: if l.Err() != io.EOF { fmt.Println("Error on line", l.Line(), ":", l.Err()) } return case xml.StartTagToken: fmt.Println("Tag", string(data)) for { ttAttr, dataAttr := l.Next() if ttAttr != xml.AttributeToken { break } key := dataAttr val := l.AttrVal() fmt.Println("Attribute", string(key), "=", string(val)) } // ... } } } ``` ## License Released under the [MIT license](https://github.com/tdewolff/parse/blob/master/LICENSE.md). [1]: http://golang.org/ "Go Language" ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/xml/lex.go�����������������������������������������������������������������������������0000664�0000000�0000000�00000016476�14632123015�0014652�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������// Package xml is an XML1.0 lexer following the specifications at http://www.w3.org/TR/xml/. package xml import ( "strconv" "github.com/tdewolff/parse/v2" ) // TokenType determines the type of token, eg. a number or a semicolon. type TokenType uint32 // TokenType values. const ( ErrorToken TokenType = iota // extra token when errors occur CommentToken DOCTYPEToken CDATAToken StartTagToken StartTagPIToken StartTagCloseToken StartTagCloseVoidToken StartTagClosePIToken EndTagToken AttributeToken TextToken ) // String returns the string representation of a TokenType. func (tt TokenType) String() string { switch tt { case ErrorToken: return "Error" case CommentToken: return "Comment" case DOCTYPEToken: return "DOCTYPE" case CDATAToken: return "CDATA" case StartTagToken: return "StartTag" case StartTagPIToken: return "StartTagPI" case StartTagCloseToken: return "StartTagClose" case StartTagCloseVoidToken: return "StartTagCloseVoid" case StartTagClosePIToken: return "StartTagClosePI" case EndTagToken: return "EndTag" case AttributeToken: return "Attribute" case TextToken: return "Text" } return "Invalid(" + strconv.Itoa(int(tt)) + ")" } //////////////////////////////////////////////////////////////// // Lexer is the state for the lexer. type Lexer struct { r *parse.Input err error inTag bool text []byte attrVal []byte } // NewLexer returns a new Lexer for a given io.Reader. func NewLexer(r *parse.Input) *Lexer { return &Lexer{ r: r, } } // Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned. func (l *Lexer) Err() error { if l.err != nil { return l.err } return l.r.Err() } // Text returns the textual representation of a token. This excludes delimiters and additional leading/trailing characters. func (l *Lexer) Text() []byte { return l.text } // AttrVal returns the attribute value when an AttributeToken was returned from Next. func (l *Lexer) AttrVal() []byte { return l.attrVal } // Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message. func (l *Lexer) Next() (TokenType, []byte) { l.text = nil var c byte if l.inTag { l.attrVal = nil for { // before attribute name state if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' { l.r.Move(1) continue } break } if c == 0 { if l.r.Err() == nil { l.err = parse.NewErrorLexer(l.r, "unexpected NULL character") } return ErrorToken, nil } else if c != '>' && (c != '/' && c != '?' || l.r.Peek(1) != '>') { return AttributeToken, l.shiftAttribute() } l.r.Skip() l.inTag = false if c == '/' { l.r.Move(2) return StartTagCloseVoidToken, l.r.Shift() } else if c == '?' { l.r.Move(2) return StartTagClosePIToken, l.r.Shift() } else { l.r.Move(1) return StartTagCloseToken, l.r.Shift() } } for { c = l.r.Peek(0) if c == '<' { if l.r.Pos() > 0 { l.text = l.r.Shift() return TextToken, l.text } c = l.r.Peek(1) if c == '/' { l.r.Move(2) return EndTagToken, l.shiftEndTag() } else if c == '!' { l.r.Move(2) if l.at('-', '-') { l.r.Move(2) return CommentToken, l.shiftCommentText() } else if l.at('[', 'C', 'D', 'A', 'T', 'A', '[') { l.r.Move(7) return CDATAToken, l.shiftCDATAText() } else if l.at('D', 'O', 'C', 'T', 'Y', 'P', 'E') { l.r.Move(7) return DOCTYPEToken, l.shiftDOCTYPEText() } l.r.Move(-2) } else if c == '?' { l.r.Move(2) l.inTag = true return StartTagPIToken, l.shiftStartTag() } l.r.Move(1) l.inTag = true return StartTagToken, l.shiftStartTag() } else if c == 0 { if l.r.Pos() > 0 { l.text = l.r.Shift() return TextToken, l.text } if l.r.Err() == nil { l.err = parse.NewErrorLexer(l.r, "unexpected NULL character") } return ErrorToken, nil } l.r.Move(1) } } //////////////////////////////////////////////////////////////// // The following functions follow the specifications at http://www.w3.org/html/wg/drafts/html/master/syntax.html func (l *Lexer) shiftDOCTYPEText() []byte { inString := false inBrackets := false for { c := l.r.Peek(0) if c == '"' { inString = !inString } else if (c == '[' || c == ']') && !inString { inBrackets = (c == '[') } else if c == '>' && !inString && !inBrackets { l.text = l.r.Lexeme()[9:] l.r.Move(1) return l.r.Shift() } else if c == 0 { l.text = l.r.Lexeme()[9:] return l.r.Shift() } l.r.Move(1) } } func (l *Lexer) shiftCDATAText() []byte { for { c := l.r.Peek(0) if c == ']' && l.r.Peek(1) == ']' && l.r.Peek(2) == '>' { l.text = l.r.Lexeme()[9:] l.r.Move(3) return l.r.Shift() } else if c == 0 { l.text = l.r.Lexeme()[9:] return l.r.Shift() } l.r.Move(1) } } func (l *Lexer) shiftCommentText() []byte { for { c := l.r.Peek(0) if c == '-' && l.r.Peek(1) == '-' && l.r.Peek(2) == '>' { l.text = l.r.Lexeme()[4:] l.r.Move(3) return l.r.Shift() } else if c == 0 { return l.r.Shift() } l.r.Move(1) } } func (l *Lexer) shiftStartTag() []byte { nameStart := l.r.Pos() for { if c := l.r.Peek(0); c == ' ' || c == '>' || (c == '/' || c == '?') && l.r.Peek(1) == '>' || c == '\t' || c == '\n' || c == '\r' || c == 0 { break } l.r.Move(1) } l.text = l.r.Lexeme()[nameStart:] return l.r.Shift() } func (l *Lexer) shiftAttribute() []byte { nameStart := l.r.Pos() var c byte for { // attribute name state if c = l.r.Peek(0); c == ' ' || c == '=' || c == '>' || (c == '/' || c == '?') && l.r.Peek(1) == '>' || c == '\t' || c == '\n' || c == '\r' || c == 0 { break } l.r.Move(1) } nameEnd := l.r.Pos() for { // after attribute name state if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' { l.r.Move(1) continue } break } if c == '=' { l.r.Move(1) for { // before attribute value state if c = l.r.Peek(0); c == ' ' || c == '\t' || c == '\n' || c == '\r' { l.r.Move(1) continue } break } attrPos := l.r.Pos() delim := c if delim == '"' || delim == '\'' { // attribute value single- and double-quoted state l.r.Move(1) for { c = l.r.Peek(0) if c == delim { l.r.Move(1) break } else if c == 0 { break } l.r.Move(1) if c == '\t' || c == '\n' || c == '\r' { l.r.Lexeme()[l.r.Pos()-1] = ' ' } } } else { // attribute value unquoted state for { if c = l.r.Peek(0); c == ' ' || c == '>' || (c == '/' || c == '?') && l.r.Peek(1) == '>' || c == '\t' || c == '\n' || c == '\r' || c == 0 { break } l.r.Move(1) } } l.attrVal = l.r.Lexeme()[attrPos:] } else { l.r.Rewind(nameEnd) l.attrVal = nil } l.text = l.r.Lexeme()[nameStart:nameEnd] return l.r.Shift() } func (l *Lexer) shiftEndTag() []byte { for { c := l.r.Peek(0) if c == '>' { l.text = l.r.Lexeme()[2:] l.r.Move(1) break } else if c == 0 { l.text = l.r.Lexeme()[2:] break } l.r.Move(1) } end := len(l.text) for end > 0 { if c := l.text[end-1]; c == ' ' || c == '\t' || c == '\n' || c == '\r' { end-- continue } break } l.text = l.text[:end] return l.r.Shift() } //////////////////////////////////////////////////////////////// func (l *Lexer) at(b ...byte) bool { for i, c := range b { if l.r.Peek(i) != c { return false } } return true } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/xml/lex_test.go������������������������������������������������������������������������0000664�0000000�0000000�00000017374�14632123015�0015707�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package xml import ( "fmt" "io" "testing" "github.com/tdewolff/parse/v2" "github.com/tdewolff/test" ) type TTs []TokenType func TestTokens(t *testing.T) { var tokenTests = []struct { xml string expected []TokenType }{ {"", TTs{}}, {"<!-- comment -->", TTs{CommentToken}}, {"<!-- comment \n multi \r line -->", TTs{CommentToken}}, {"<foo/>", TTs{StartTagToken, StartTagCloseVoidToken}}, {"<foo \t\r\n/>", TTs{StartTagToken, StartTagCloseVoidToken}}, {"<foo:bar.qux-norf/>", TTs{StartTagToken, StartTagCloseVoidToken}}, {"<foo></foo>", TTs{StartTagToken, StartTagCloseToken, EndTagToken}}, {"<foo>text</foo>", TTs{StartTagToken, StartTagCloseToken, TextToken, EndTagToken}}, {"<foo/> text", TTs{StartTagToken, StartTagCloseVoidToken, TextToken}}, {"<a> <b> <c>text</c> </b> </a>", TTs{StartTagToken, StartTagCloseToken, TextToken, StartTagToken, StartTagCloseToken, TextToken, StartTagToken, StartTagCloseToken, TextToken, EndTagToken, TextToken, EndTagToken, TextToken, EndTagToken}}, {"<foo a='a' b=\"b\" c=c/>", TTs{StartTagToken, AttributeToken, AttributeToken, AttributeToken, StartTagCloseVoidToken}}, {"<foo a=\"\"/>", TTs{StartTagToken, AttributeToken, StartTagCloseVoidToken}}, {"<foo a-b=\"\"/>", TTs{StartTagToken, AttributeToken, StartTagCloseVoidToken}}, {"<foo \nchecked \r\n value\r=\t'=/>\"' />", TTs{StartTagToken, AttributeToken, AttributeToken, StartTagCloseVoidToken}}, {"<?xml?>", TTs{StartTagPIToken, StartTagClosePIToken}}, {"<?xml a=\"a\" ?>", TTs{StartTagPIToken, AttributeToken, StartTagClosePIToken}}, {"<?xml a=a?>", TTs{StartTagPIToken, AttributeToken, StartTagClosePIToken}}, {"<![CDATA[ test ]]>", TTs{CDATAToken}}, {"<!DOCTYPE>", TTs{DOCTYPEToken}}, {"<!DOCTYPE note SYSTEM \"Note.dtd\">", TTs{DOCTYPEToken}}, {`<!DOCTYPE note [<!ENTITY nbsp "&#xA0;"><!ENTITY writer "Writer: Donald Duck."><!ENTITY copyright "Copyright:]> W3Schools.">]>`, TTs{DOCTYPEToken}}, {"<!foo>", TTs{StartTagToken, StartTagCloseToken}}, // early endings {"<!-- comment", TTs{CommentToken}}, {"<foo", TTs{StartTagToken}}, {"</foo", TTs{EndTagToken}}, {"<foo x", TTs{StartTagToken, AttributeToken}}, {"<foo x=", TTs{StartTagToken, AttributeToken}}, {"<foo x='", TTs{StartTagToken, AttributeToken}}, {"<foo x=''", TTs{StartTagToken, AttributeToken}}, {"<?xml", TTs{StartTagPIToken}}, {"<![CDATA[ test", TTs{CDATAToken}}, {"<!DOCTYPE note SYSTEM", TTs{DOCTYPEToken}}, // go fuzz {"</", TTs{EndTagToken}}, {"</\n", TTs{EndTagToken}}, } for _, tt := range tokenTests { t.Run(tt.xml, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.xml)) i := 0 for { token, _ := l.Next() if token == ErrorToken { test.T(t, l.Err(), io.EOF) test.T(t, i, len(tt.expected), "when error occurred we must be at the end") break } test.That(t, i < len(tt.expected), "index", i, "must not exceed expected token types size", len(tt.expected)) if i < len(tt.expected) { test.T(t, token, tt.expected[i], "token types must match") } i++ } }) } // coverage for i := 0; ; i++ { if TokenType(i).String() == fmt.Sprintf("Invalid(%d)", i) { break } } } func TestTags(t *testing.T) { var tagTests = []struct { xml string expected string }{ {"<foo:bar.qux-norf/>", "foo:bar.qux-norf"}, {"<?xml?>", "xml"}, {"<foo?bar/qux>", "foo?bar/qux"}, {"<!DOCTYPE note SYSTEM \"Note.dtd\">", " note SYSTEM \"Note.dtd\""}, // early endings {"<foo ", "foo"}, } for _, tt := range tagTests { t.Run(tt.xml, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.xml)) for { token, _ := l.Next() if token == ErrorToken { test.T(t, l.Err(), io.EOF) test.Fail(t, "when error occurred we must be at the end") break } else if token == StartTagToken || token == StartTagPIToken || token == EndTagToken || token == DOCTYPEToken { test.String(t, string(l.Text()), tt.expected, "tags must match") break } } }) } } func TestAttributes(t *testing.T) { var attributeTests = []struct { attr string expected []string }{ {"<foo a=\"b\" />", []string{"a", "\"b\""}}, {"<foo \nchecked \r\n value\r=\t'=/>\"' />", []string{"checked", "", "value", "'=/>\"'"}}, {"<foo bar=\" a \n\t\r b \" />", []string{"bar", "\" a b \""}}, {"<?xml a=b?>", []string{"a", "b"}}, {"<foo /=? >", []string{"/", "?"}}, // early endings {"<foo x", []string{"x", ""}}, {"<foo x=", []string{"x", ""}}, {"<foo x='", []string{"x", "'"}}, } for _, tt := range attributeTests { t.Run(tt.attr, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.attr)) i := 0 for { token, _ := l.Next() if token == ErrorToken { test.T(t, l.Err(), io.EOF) test.T(t, i, len(tt.expected), "when error occurred we must be at the end") break } else if token == AttributeToken { test.That(t, i+1 < len(tt.expected), "index", i+1, "must not exceed expected attributes size", len(tt.expected)) if i+1 < len(tt.expected) { test.String(t, string(l.Text()), tt.expected[i], "attribute keys must match") test.String(t, string(l.AttrVal()), tt.expected[i+1], "attribute keys must match") i += 2 } } } }) } } func TestErrors(t *testing.T) { var errorTests = []struct { xml string col int }{ {"a\x00b", 2}, {"<\x00 b='5'>", 2}, {"<a\x00b='5'>", 3}, {"<a \x00='5'>", 4}, {"<a b\x00'5'>", 5}, {"<a b=\x005'>", 6}, {"<a b='\x00'>", 7}, {"<a b='5\x00>", 8}, {"<a b='5'\x00", 9}, {"</\x00a>", 3}, {"</ \x00>", 4}, {"</ a\x00", 5}, {"<!\x00", 3}, {"<![CDATA[\x00", 10}, {"/*\x00", 3}, } for _, tt := range errorTests { t.Run(tt.xml, func(t *testing.T) { l := NewLexer(parse.NewInputString(tt.xml)) for { token, _ := l.Next() if token == ErrorToken { if perr, ok := l.Err().(*parse.Error); ok { _, col, _ := perr.Position() test.T(t, col, tt.col) } else { test.Fail(t, "bad error:", l.Err()) } break } } }) } } func TestTextAndAttrVal(t *testing.T) { l := NewLexer(parse.NewInputString(`<xml attr="val" >text<!--comment--><!DOCTYPE doctype><![CDATA[cdata]]>`)) _, data := l.Next() test.Bytes(t, data, []byte("<xml")) test.Bytes(t, l.Text(), []byte("xml")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte(` attr="val"`)) test.Bytes(t, l.Text(), []byte("attr")) test.Bytes(t, l.AttrVal(), []byte(`"val"`)) _, data = l.Next() test.Bytes(t, data, []byte(">")) test.Bytes(t, l.Text(), nil) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("text")) test.Bytes(t, l.Text(), []byte("text")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("<!--comment-->")) test.Bytes(t, l.Text(), []byte("comment")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("<!DOCTYPE doctype>")) test.Bytes(t, l.Text(), []byte(" doctype")) test.Bytes(t, l.AttrVal(), nil) _, data = l.Next() test.Bytes(t, data, []byte("<![CDATA[cdata]]>")) test.Bytes(t, l.Text(), []byte("cdata")) test.Bytes(t, l.AttrVal(), nil) } func TestOffset(t *testing.T) { z := parse.NewInputString(`<div attr="val">text</div>`) l := NewLexer(z) test.T(t, z.Offset(), 0) _, _ = l.Next() test.T(t, z.Offset(), 4) // <div _, _ = l.Next() test.T(t, z.Offset(), 15) // attr="val" _, _ = l.Next() test.T(t, z.Offset(), 16) // > _, _ = l.Next() test.T(t, z.Offset(), 20) // text _, _ = l.Next() test.T(t, z.Offset(), 26) // </div> } //////////////////////////////////////////////////////////////// func ExampleNewLexer() { l := NewLexer(parse.NewInputString("<span class='user'>John Doe</span>")) out := "" for { tt, data := l.Next() if tt == ErrorToken { break } out += string(data) } fmt.Println(out) // Output: <span class='user'>John Doe</span> } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/xml/util.go����������������������������������������������������������������������������0000664�0000000�0000000�00000003322�14632123015�0015021�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package xml var ( ltEntityBytes = []byte("&lt;") ampEntityBytes = []byte("&amp;") singleQuoteEntityBytes = []byte("&#39;") doubleQuoteEntityBytes = []byte("&#34;") ) // EscapeAttrVal returns the escape attribute value bytes without quotes. func EscapeAttrVal(buf *[]byte, b []byte) []byte { singles := 0 doubles := 0 for _, c := range b { if c == '"' { doubles++ } else if c == '\'' { singles++ } } n := len(b) + 2 var quote byte var escapedQuote []byte if doubles > singles { n += singles * 4 quote = '\'' escapedQuote = singleQuoteEntityBytes } else { n += doubles * 4 quote = '"' escapedQuote = doubleQuoteEntityBytes } if n > cap(*buf) { *buf = make([]byte, 0, n) // maximum size, not actual size } t := (*buf)[:n] // maximum size, not actual size t[0] = quote j := 1 start := 0 for i, c := range b { if c == quote { j += copy(t[j:], b[start:i]) j += copy(t[j:], escapedQuote) start = i + 1 } } j += copy(t[j:], b[start:]) t[j] = quote return t[:j+1] } // EscapeCDATAVal returns the escaped text bytes. func EscapeCDATAVal(buf *[]byte, b []byte) ([]byte, bool) { n := 0 for _, c := range b { if c == '<' || c == '&' { if c == '<' { n += 3 // &lt; } else { n += 4 // &amp; } if n > len("<![CDATA[]]>") { return b, false } } } if len(b)+n > cap(*buf) { *buf = make([]byte, 0, len(b)+n) } t := (*buf)[:len(b)+n] j := 0 start := 0 for i, c := range b { if c == '<' { j += copy(t[j:], b[start:i]) j += copy(t[j:], ltEntityBytes) start = i + 1 } else if c == '&' { j += copy(t[j:], b[start:i]) j += copy(t[j:], ampEntityBytes) start = i + 1 } } j += copy(t[j:], b[start:]) return t[:j], true } ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������parse-2.7.15/xml/util_test.go�����������������������������������������������������������������������0000664�0000000�0000000�00000003123�14632123015�0016057�0����������������������������������������������������������������������������������������������������ustar�00root����������������������������root����������������������������0000000�0000000������������������������������������������������������������������������������������������������������������������������������������������������������������������������package xml import ( "testing" "github.com/tdewolff/test" ) func TestEscapeAttrVal(t *testing.T) { var attrValTests = []struct { attrVal string expected string }{ {`xyz`, `"xyz"`}, {``, `""`}, {`x'z`, `"x'z"`}, {`x"z`, `'x"z'`}, {`a'b=""`, `'a&#39;b=""'`}, {`'x'"'z'`, `"x'&#34;'z"`}, {`"x"'"z"`, `'x"&#39;"z'`}, {`a'b=""`, `'a&#39;b=""'`}, } var buf []byte for _, tt := range attrValTests { t.Run(tt.attrVal, func(t *testing.T) { b := []byte(tt.attrVal) if len(b) > 1 && (b[0] == '"' || b[0] == '\'') && b[0] == b[len(b)-1] { b = b[1 : len(b)-1] } val := EscapeAttrVal(&buf, b) test.String(t, string(val), tt.expected) }) } } func TestEscapeCDATAVal(t *testing.T) { var CDATAValTests = []struct { CDATAVal string expected string }{ {"<![CDATA[<b>]]>", "&lt;b>"}, {"<![CDATA[abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz]]>", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"}, {"<![CDATA[ <b> ]]>", " &lt;b> "}, {"<![CDATA[<<<<<]]>", "<![CDATA[<<<<<]]>"}, {"<![CDATA[&]]>", "&amp;"}, {"<![CDATA[&&&&]]>", "<![CDATA[&&&&]]>"}, {"<![CDATA[ a ]]>", " a "}, {"<![CDATA[]]>", ""}, {"<![CDATA[ a ]]&gt; b ]]>", " a ]]&amp;gt; b "}, } var buf []byte for _, tt := range CDATAValTests { t.Run(tt.CDATAVal, func(t *testing.T) { b := []byte(tt.CDATAVal[len("<![CDATA[") : len(tt.CDATAVal)-len("]]>")]) data, useText := EscapeCDATAVal(&buf, b) text := string(data) if !useText { text = "<![CDATA[" + text + "]]>" } test.String(t, text, tt.expected) }) } } �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������