53 lines
1.8 KiB
Go
53 lines
1.8 KiB
Go
package parsekit
|
|
|
|
// P holds the internal state of the parser.
|
|
type P struct {
|
|
state StateFn // the function that handles the current state
|
|
nextState StateFn // the function that will handle the next state
|
|
stack []StateFn // state function stack, for nested parsing
|
|
input string // the scanned input
|
|
len int // the total length of the input in bytes
|
|
pos int // current byte scanning position in the input
|
|
newline bool // keep track of when we have scanned a newline
|
|
cursorRow int // current row number in the input
|
|
cursorColumn int // current column position in the input
|
|
buffer stringBuffer // an efficient buffer, used to build string values
|
|
items chan Item // channel of resulting Parser items
|
|
item Item // the current item as reached by Next() and retrieved by Get()
|
|
err *Error // an error when lexing failed, retrieved by Error()
|
|
}
|
|
|
|
// StateFn represents the state of the parser as a function
|
|
// that returns the next state.
|
|
type StateFn func(*P)
|
|
|
|
// ItemType represents the type of a parser Item.
|
|
type ItemType int
|
|
|
|
// ItemEOF is a built-in parser item type that is used for flagging that the
|
|
// end of the input was reached.
|
|
const ItemEOF ItemType = -1
|
|
|
|
// ItemError is a built-in parser item type that is used for flagging that
|
|
// an error has occurred during parsing.
|
|
const ItemError ItemType = -2
|
|
|
|
// Item represents an item returned from the parser.
|
|
type Item struct {
|
|
Type ItemType
|
|
Value string
|
|
}
|
|
|
|
// Error is used as the error type when parsing errors occur.
|
|
// The error includes some extra meta information to allow for useful
|
|
// error messages to the user.
|
|
type Error struct {
|
|
Message string
|
|
Row int
|
|
Column int
|
|
}
|
|
|
|
func (err *Error) Error() string {
|
|
return err.Message
|
|
}
|