Janet 1.3.1 Documentation

Parser Module


parser/byte parser/clone parser/consume parser/eof parser/error parser/flush parser/has-more parser/insert parser/new parser/produce parser/state parser/status parser/where

parser/byte cfunction
(parser/byte parser b)

Input a single byte into the parser byte stream. Returns the parser.

parser/clone cfunction
(parser/clone p)

Creates a deep clone of a parser that is identical to the input parser. This cloned parser can be used to continue parsing from a good checkpoint if parsing later fails. Returns a new parser.

parser/consume cfunction
(parser/consume parser bytes &opt index)

Input bytes into the parser and parse them. Will not throw errors if there is a parse error. Starts at the byte index given by index. Returns the number of bytes read.

parser/eof cfunction
(parser/eof parser)

Indicate that the end of file was reached to the parser. This puts the parser in the :dead state.

parser/error cfunction
(parser/error parser)

If the parser is in the error state, returns the message associated with that error. Otherwise, returns nil. Also flushes the parser state and parser queue, so be sure to handle everything in the queue before calling parser/error.

parser/flush cfunction
(parser/flush parser)

Clears the parser state and parse queue. Can be used to reset the parser if an error was encountered. Does not reset the line and column counter, so to begin parsing in a new context, create a new parser.

parser/has-more cfunction
(parser/has-more parser)

Check if the parser has more values in the value queue.

parser/insert cfunction
(parser/insert parser value)

Insert a value into the parser. This means that the parser state can be manipulated in between chunks of bytes. This would allow a user to add extra elements to arrays and tuples, for example. Returns the parser.

parser/new cfunction

Creates and returns a new parser object. Parsers are state machines that can receive bytes, and generate a stream of janet values.

parser/produce cfunction
(parser/produce parser)

Dequeue the next value in the parse queue. Will return nil if no parsed values are in the queue, otherwise will dequeue the next value.

parser/state cfunction
(parser/state parser &opt key)

Returns a representation of the internal state of the parser. If a key is passed, only that information about the state is returned. Allowed keys are:

	:delimiters - Each byte in the string represents a nested data structure. For example, if the parser state is '(["', then the parser is in the middle of parsing a string inside of square brackets inside parentheses. Can be used to augment a REPL prompt.	:frames - Each table in the array represents a 'frame' in the parser state. Frames contain information about the start of the expression being parsed as well as the type of that expression and some type-specific information.

parser/status cfunction
(parser/status parser)

Gets the current status of the parser state machine. The status will be one of:

	:pending - a value is being parsed.
	:error - a parsing error was encountered.
	:root - the parser can either read more values or safely terminate.

parser/where cfunction
(parser/where parser &opt offset)

Returns the current line number and column number of the parser's location in the byte stream as an index, counted from 0. If offset is supplied, then the byte offset is updated to that new value.