sqlparser

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Simple non-performant recursive descent parser for purposes of sqlcode; currently only supports the special @Enum declarations used by sqlcode. We only allow these on the top, and parsing will stop without any errors at the point hitting anything else.

Index

Constants

This section is empty.

Variables

View Source
var CycleError = errors.New("Detected a dependency cycle")

Functions

func AdvanceAndCopy

func AdvanceAndCopy(s *Scanner, target *[]Unparsed)

AdvanceAndCopy is like NextToken; advance to next token that is not whitespace and return Note: The 'go' and EOF tokens are *not* copied

func CopyToken

func CopyToken(s *Scanner, target *[]Unparsed)

func IsSqlcodeConstVariable

func IsSqlcodeConstVariable(varname string) bool

func NextTokenCopyingWhitespace

func NextTokenCopyingWhitespace(s *Scanner, target *[]Unparsed)

NextTokenCopyingWhitespace is like s.NextToken(), but if whitespace is encountered it is simply copied into `target`. Upon return, the scanner is located at a non-whitespace token, and target is either unmodified or filled with some whitespace nodes.

func Parse

func Parse(s *Scanner, result *Document)

func TopologicalSort

func TopologicalSort(input []Create) (output []Create, errpos Pos, err error)

Types

type Create

type Create struct {
	CreateType string    // "procedure", "function" or "type"
	QuotedName PosString // proc/func/type name, including []
	Body       []Unparsed
	DependsOn  []PosString
	Docstring  []PosString // comment lines before the create statement. Note: this is also part of Body
}

func (Create) DependsOnStrings

func (c Create) DependsOnStrings() (result []string)

func (Create) DocstringAsString added in v0.4.0

func (c Create) DocstringAsString() string

func (Create) DocstringYamldoc added in v0.4.0

func (c Create) DocstringYamldoc() (string, error)

func (Create) ParseYamlInDocstring added in v0.4.0

func (c Create) ParseYamlInDocstring(out any) error

func (Create) Serialize

func (c Create) Serialize(w io.StringWriter) error

func (Create) SerializeBytes

func (c Create) SerializeBytes(w io.Writer) error

func (Create) String

func (c Create) String() string

func (Create) WithoutPos

func (c Create) WithoutPos() Create

type Declare

type Declare struct {
	Start        Pos
	Stop         Pos
	VariableName string
	Datatype     Type
	Literal      Unparsed
}

func (Declare) String

func (d Declare) String() string

func (Declare) WithoutPos

func (d Declare) WithoutPos() Declare

type Document

type Document struct {
	PragmaIncludeIf []string
	Creates         []Create
	Declares        []Declare
	Errors          []Error
}

func ParseFilesystems

func ParseFilesystems(fslst []fs.FS, includeTags []string) (filenames []string, result Document, err error)

ParseFileystems iterates through a list of filesystems and parses all files matching `*.sql`, determines which one are sqlcode files from the contents, and returns the combination of all of them.

err will only return errors related to filesystems/reading. Errors related to parsing/sorting will be in result.Errors.

ParseFilesystems will also sort create statements topologically.

func ParseString

func ParseString(filename FileRef, input string) (result Document)

func (Document) Empty

func (d Document) Empty() bool

func (*Document) Include

func (d *Document) Include(other Document)

func (Document) WithoutPos

func (d Document) WithoutPos() Document

Transform a Document to remove all Position information; this is used to 'unclutter' a DOM to more easily write assertions on it.

type Error

type Error struct {
	Pos     Pos
	Message string
}

func (Error) Error added in v0.4.0

func (e Error) Error() string

func (Error) WithoutPos

func (e Error) WithoutPos() Error

type FileRef

type FileRef string

dedicated type for reference to file, in case we need to refactor this later..

type NotFoundError

type NotFoundError struct {
	Name string
}

func (NotFoundError) Error

func (n NotFoundError) Error() string

type Pos

type Pos struct {
	File      FileRef
	Line, Col int
}

type PosString

type PosString struct {
	Pos
	Value string
}

A string that has a Pos-ition in a source document

func (PosString) String

func (p PosString) String() string

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

We don't do the lexer/parser split / token stream, but simply use the Scanner directly from the recursive descent parser; it is simply a cursor in the buffer with associated utility methods

func (Scanner) Clone

func (s Scanner) Clone() *Scanner

Returns a clone of the scanner; this is used to do look-ahead parsing

func (*Scanner) NextNonWhitespaceCommentToken added in v0.4.0

func (s *Scanner) NextNonWhitespaceCommentToken() TokenType

func (*Scanner) NextNonWhitespaceToken

func (s *Scanner) NextNonWhitespaceToken() TokenType

func (*Scanner) NextToken

func (s *Scanner) NextToken() TokenType

NextToken scans the NextToken token and advances the Scanner's position to after the token

func (*Scanner) ReservedWord

func (s *Scanner) ReservedWord() string

func (*Scanner) SkipWhitespace

func (s *Scanner) SkipWhitespace()

func (*Scanner) SkipWhitespaceComments added in v0.4.0

func (s *Scanner) SkipWhitespaceComments()

func (*Scanner) Start

func (s *Scanner) Start() Pos

func (*Scanner) Stop

func (s *Scanner) Stop() Pos

func (*Scanner) Token

func (s *Scanner) Token() string

func (*Scanner) TokenLower

func (s *Scanner) TokenLower() string

func (*Scanner) TokenType

func (s *Scanner) TokenType() TokenType

type TokenType

type TokenType int
const (
	WhitespaceToken TokenType = iota + 1

	LeftParenToken
	RightParenToken
	SemicolonToken
	EqualToken
	CommaToken
	DotToken

	VarcharLiteralToken
	NVarcharLiteralToken

	MultilineCommentToken
	SinglelineCommentToken

	// PragmaToken is like SinglelineCommentToken but starting with `--sqlcode:`.
	// It is useful to scan this as a separate token type because then this comment
	// anywhere else than the top of the file will not be treated as whitespace,
	// but give an error.
	PragmaToken

	NumberToken

	// Note: A lot of stuff pass as identifiers that should really have been
	// reserved words
	ReservedWordToken
	VariableIdentifierToken
	QuotedIdentifierToken
	UnquotedIdentifierToken
	OtherToken

	UnterminatedVarcharLiteralErrorToken
	UnterminatedQuotedIdentifierErrorToken
	DoubleQuoteErrorToken // we don't want to support double quotes, for simplicity, so that is an error and stops parsing
	UnexpectedCharacterToken
	NonUTF8ErrorToken

	BatchSeparatorToken
	MalformedBatchSeparatorToken
	EOFToken
)

func (TokenType) GoString

func (tt TokenType) GoString() string

func (TokenType) String

func (tt TokenType) String() string

type Type

type Type struct {
	BaseType string
	Args     []string
}

func (Type) String

func (t Type) String() (result string)

type Unparsed

type Unparsed struct {
	Type        TokenType
	Start, Stop Pos
	RawValue    string
}

func CreateUnparsed

func CreateUnparsed(s *Scanner) Unparsed

func (Unparsed) WithoutPos

func (u Unparsed) WithoutPos() Unparsed

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL