Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 4764cae

Browse files
Document new util functions for parsing
Signed-off-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
1 parent 7de54c5 commit 4764cae

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

sql/parse/util.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ func readLetter(r *bufio.Reader, buf *bytes.Buffer) error {
127127
return nil
128128
}
129129

130+
// Parses a single rune from the reader and consumes it, copying it to the
131+
// buffer, if it is either a letter or a point
130132
func readLetterOrPoint(r *bufio.Reader, buf *bytes.Buffer) error {
131133
ru, _, err := r.ReadRune()
132134
if err != nil {
@@ -165,6 +167,8 @@ func readValidIdentRune(r *bufio.Reader, buf *bytes.Buffer) error {
165167
return nil
166168
}
167169

170+
// Parses a single rune from the reader and consumes it, copying it to the
171+
// buffer, if is a letter, a digit, an underscore or the specified separator.
168172
func readValidScopedIdentRune(r *bufio.Reader, separator rune, buf *bytes.Buffer) error {
169173
ru, _, err := r.ReadRune()
170174
if err != nil {
@@ -237,6 +241,14 @@ func readIdent(ident *string) parseFunc {
237241
}
238242
}
239243

244+
245+
// Reads a scoped identifier, populating the specified slice with the different
246+
// parts of the identifier if it is correctly formed.
247+
// A scoped identifier is a sequence of identifiers separated by the specified
248+
// rune in separator. An identifier is a string of runes whose first character
249+
// is a letter and the following ones are either letters, digits or underscores.
250+
// An example of a correctly formed scoped identifier is "dbName.tableName",
251+
// that would populate the slice with the values ["dbName", "tableName"]
240252
func readScopedIdent(separator rune, idents *[]string) parseFunc {
241253
return func(r *bufio.Reader) error {
242254
var buf bytes.Buffer
@@ -370,6 +382,8 @@ func expectQuote(r *bufio.Reader) error {
370382
return nil
371383
}
372384

385+
// Tries to read the specified string, consuming the reader if the string is
386+
// found. The `matched` boolean is set to true if the string is found
373387
func maybe(matched *bool, str string) parseFunc {
374388
return func(rd *bufio.Reader) error {
375389
*matched = false
@@ -400,6 +414,9 @@ func maybe(matched *bool, str string) parseFunc {
400414
}
401415
}
402416

417+
// Tries to read the specified strings, one after the other, separeted by an
418+
// arbitrary number of spaces. It consumes the reader if and only all the
419+
// strings are found.
403420
func multiMaybe(matched *bool, strings ...string) parseFunc {
404421
return func(rd *bufio.Reader) error {
405422
*matched = false
@@ -429,11 +446,11 @@ func multiMaybe(matched *bool, strings ...string) parseFunc {
429446
}
430447
}
431448

432-
// Read a list of strings separated by the specified separator, with a rune
449+
// Reads a list of strings separated by the specified separator, with a rune
433450
// indicating the opening of the list and another one specifying its closing.
434451
// For example, readList('(', ',', ')', list) parses "(uno, dos,tres)" and
435452
// populates list with the array of strings ["uno", "dos", "tres"]
436-
// If the opening is not found, do not advance the reader
453+
// If the opening is not found, this does not consumes any rune from the reader.
437454
func maybeList(opening, separator, closing rune, list *[]string) parseFunc {
438455
return func(rd *bufio.Reader) error {
439456
r, _, err := rd.ReadRune()

0 commit comments

Comments
 (0)