Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ func (SizedSchemaType) isSchemaType() {}
func (ArraySchemaType) isSchemaType() {}
func (StructType) isSchemaType() {}
func (NamedType) isSchemaType() {}
func (BadType) isSchemaType() {}

// IndexAlteration represents ALTER INDEX action.
type IndexAlteration interface {
Expand Down
6 changes: 6 additions & 0 deletions parse_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func ParseType(filepath, s string) (ast.Type, error) {
return newParser(filepath, s).ParseType()
}

// ParseSchemaType parses an input string containing a schema type.
// filepath can be empty, it is only used in error message.
func ParseSchemaType(filepath, s string) (ast.SchemaType, error) {
return newParser(filepath, s).ParseSchemaType()
}

// ParseDDL parses an input string containing a DDL statement.
// filepath can be empty, it is only used in error message.
func ParseDDL(filepath, s string) (ast.DDL, error) {
Expand Down
13 changes: 13 additions & 0 deletions parse_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func ExampleParseType() {
// Output:
// ARRAY<STRUCT<n INT64, s STRING>>
}

func ExampleParseSchemaType() {
typ, err := memefish.ParseSchemaType("", "ARRAY<STRING(MAX)>")
if err != nil {
panic(err)
}

fmt.Println(typ.SQL())

// Output:
// ARRAY<STRING(MAX)>
}

func ExampleParseDDL() {
sql := heredoc.Doc(`
CREATE TABLE foo (
Expand Down
23 changes: 23 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ func (p *Parser) ParseType() (ast.Type, error) {
return t, nil
}

// ParseSchemaType parses a schema type.
func (p *Parser) ParseSchemaType() (t ast.SchemaType, err error) {
p.nextToken()
l := p.Clone()
defer func() {
if r := recover(); r != nil {
t = p.handleParseTypeError(r, l)
}
if len(p.errors) > 0 {
// Reset the errors and allow processing to continue
err = MultiError(p.errors)
p.errors = nil
}
}()

t = p.parseSchemaType()
if p.Token.Kind != token.TokenEOF {
p.errors = append(p.errors, p.errorfAtToken(&p.Token, "expected token: <eof>, but: %s", p.Token.Kind))
}

return t, nil
}

// ParseDDL parses a CREATE/ALTER/DROP statement.
func (p *Parser) ParseDDL() (ast.DDL, error) {
p.nextToken()
Expand Down