GLib.Scanner

Fields

Name Type Access Description
buffer str r  
config GLib.ScannerConfig r/w link into the scanner configuration
input_fd int r  
input_name str r/w name of input stream, featured by the default message handler
line int r/w line number of the last token from GLib.Scanner.get_next_token()
max_parse_errors int r/w unused
msg_handler GLib.ScannerMsgFunc r/w handler function for _warn and _error
next_line int r/w line number of the last token from GLib.Scanner.peek_next_token()
next_position int r/w str number of the last token from GLib.Scanner.peek_next_token()
next_token GLib.TokenType r/w token parsed by the last GLib.Scanner.peek_next_token()
next_value GLib.TokenValue r/w value of the last token from GLib.Scanner.peek_next_token()
parse_errors int r/w g_scanner_error() increments this field
position int r/w str number of the last token from GLib.Scanner.get_next_token()
qdata GLib.Data r/w quarked data
scope_id int r  
symbol_table {object: object} r  
text str r  
text_end str r  
token GLib.TokenType r/w token parsed by the last GLib.Scanner.get_next_token()
user_data object r/w unused
value GLib.TokenValue r/w value of the last token from GLib.Scanner.get_next_token()

Methods

  cur_line ()
  cur_position ()
  cur_token ()
  destroy ()
  eof ()
  get_next_token ()
  input_file (input_fd)
  input_text (text, text_len)
  lookup_symbol (symbol)
  peek_next_token ()
  scope_add_symbol (scope_id, symbol, value)
  scope_lookup_symbol (scope_id, symbol)
  scope_remove_symbol (scope_id, symbol)
  set_scope (scope_id)
  sync_file_offset ()
  unexp_token (expected_token, identifier_spec, symbol_spec, symbol_name, message, is_error)

Details

class GLib.Scanner

The data structure representing a lexical scanner.

You should set input_name after creating the scanner, since it is used by the default message handler when displaying warnings and errors. If you are scanning a file, the filename would be a good choice.

The user_data and max_parse_errors fields are not used. If you need to associate extra data with the scanner you can place them here.

If you want to use your own message handler you can set the msg_handler field. The type of the message handler function is declared by GLib.ScannerMsgFunc.

cur_line()[source]
Returns:the current line
Return type:int

Returns the current line in the input stream (counting from 1). This is the line of the last token parsed via GLib.Scanner.get_next_token().

cur_position()[source]
Returns:the current position on the line
Return type:int

Returns the current position in the current line (counting from 0). This is the position of the last token parsed via GLib.Scanner.get_next_token().

cur_token()[source]
Returns:the current token type
Return type:GLib.TokenType

Gets the current token type. This is simply the token field in the GLib.Scanner structure.

destroy()[source]

Frees all memory used by the GLib.Scanner.

eof()[source]
Returns:True if the scanner has reached the end of the file or text buffer
Return type:bool

Returns True if the scanner has reached the end of the file or text buffer.

get_next_token()[source]
Returns:the type of the token
Return type:GLib.TokenType

Parses the next token just like GLib.Scanner.peek_next_token() and also removes it from the input stream. The token data is placed in the token, value, line, and position fields of the GLib.Scanner structure.

input_file(input_fd)[source]
Parameters:input_fd (int) – a file descriptor

Prepares to scan a file.

input_text(text, text_len)[source]
Parameters:
  • text (str) – the text buffer to scan
  • text_len (int) – the length of the text buffer

Prepares to scan a text buffer.

lookup_symbol(symbol)[source]
Parameters:symbol (str) – the symbol to look up
Returns:the value of symbol in the current scope, or None if symbol is not bound in the current scope
Return type:object or None

Looks up a symbol in the current scope and return its value. If the symbol is not bound in the current scope, None is returned.

peek_next_token()[source]
Returns:the type of the token
Return type:GLib.TokenType

Parses the next token, without removing it from the input stream. The token data is placed in the next_token, next_value, next_line, and next_position fields of the GLib.Scanner structure.

Note that, while the token is not removed from the input stream (i.e. the next call to GLib.Scanner.get_next_token() will return the same token), it will not be reevaluated. This can lead to surprising results when changing scope or the scanner configuration after peeking the next token. Getting the next token after switching the scope or configuration will return whatever was peeked before, regardless of any symbols that may have been added or removed in the new scope.

scope_add_symbol(scope_id, symbol, value)[source]
Parameters:
  • scope_id (int) – the scope id
  • symbol (str) – the symbol to add
  • value (object or None) – the value of the symbol

Adds a symbol to the given scope.

scope_lookup_symbol(scope_id, symbol)[source]
Parameters:
  • scope_id (int) – the scope id
  • symbol (str) – the symbol to look up
Returns:

the value of symbol in the given scope, or None if symbol is not bound in the given scope.

Return type:

object or None

Looks up a symbol in a scope and return its value. If the symbol is not bound in the scope, None is returned.

scope_remove_symbol(scope_id, symbol)[source]
Parameters:
  • scope_id (int) – the scope id
  • symbol (str) – the symbol to remove

Removes a symbol from a scope.

set_scope(scope_id)[source]
Parameters:scope_id (int) – the new scope id
Returns:the old scope id
Return type:int

Sets the current scope.

sync_file_offset()[source]

Rewinds the filedescriptor to the current buffer position and blows the file read ahead buffer. This is useful for third party uses of the scanners filedescriptor, which hooks onto the current scanning position.

unexp_token(expected_token, identifier_spec, symbol_spec, symbol_name, message, is_error)[source]
Parameters:
  • expected_token (GLib.TokenType) – the expected token
  • identifier_spec (str) – a string describing how the scanner’s user refers to identifiers (None defaults to “identifier”). This is used if expected_token is GLib.TokenType.IDENTIFIER or GLib.TokenType.IDENTIFIER_NULL.
  • symbol_spec (str) – a string describing how the scanner’s user refers to symbols (None defaults to “symbol”). This is used if expected_token is GLib.TokenType.SYMBOL or any token value greater than %G_TOKEN_LAST.
  • symbol_name (str) – the name of the symbol, if the scanner’s current token is a symbol.
  • message (str) – a message string to output at the end of the warning/error, or None.
  • is_error (int) – if True it is output as an error. If False it is output as a warning.

Outputs a message through the scanner’s msg_handler, resulting from an unexpected token in the input stream. Note that you should not call GLib.Scanner.peek_next_token() followed by GLib.Scanner.unexp_token() without an intermediate call to GLib.Scanner.get_next_token(), as GLib.Scanner.unexp_token() evaluates the scanner’s current token (not the peeked token) to construct part of the message.