Janet 1.0.0-dev Documentation

Parser Module


parser/byte 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/consume cfunction
(parser/consume parser bytes [, 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/insert 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)

Returns a string representation of the internal state of the parser. 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.

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)

Returns the current line number and column number of the parser's location in the byte stream as a tuple (line, column). Lines and columns are counted from 1, (the first byte is line 1, column 1) and a newline is considered ASCII 0x0A.