Class Scanner

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class Scanner extends ProxyInstance

GScanner provides a general-purpose lexical scanner.

You should set inputName 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 userData and maxParseErrors 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 msgHandler field. The type of the message handler function is declared by GScannerMsgFunc.

  • Constructor Details

    • Scanner

      public Scanner(MemorySegment address)
      Create a Scanner proxy instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Scanner

      public Scanner(Arena arena)
      Allocate a new Scanner.
      Parameters:
      arena - to control the memory allocation scope
    • Scanner

      public Scanner()
      Allocate a new Scanner. The memory is allocated with Arena.ofAuto().
    • Scanner

      public Scanner(MemorySegment userData, int maxParseErrors, int parseErrors, String inputName, ScannerConfig config, TokenType token, TokenValue value, int line, int position, TokenType nextToken, TokenValue nextValue, int nextLine, int nextPosition, HashTable<MemorySegment, MemorySegment> symbolTable, int inputFd, String text, String textEnd, String buffer, int scopeId, ScannerMsgFunc msgHandler, Arena arena)
      Allocate a new Scanner with the fields set to the provided values.
      Parameters:
      userData - value for the field userData
      maxParseErrors - value for the field maxParseErrors
      parseErrors - value for the field parseErrors
      inputName - value for the field inputName
      config - value for the field config
      token - value for the field token
      value - value for the field value
      line - value for the field line
      position - value for the field position
      nextToken - value for the field nextToken
      nextValue - value for the field nextValue
      nextLine - value for the field nextLine
      nextPosition - value for the field nextPosition
      symbolTable - value for the field symbolTable
      inputFd - value for the field inputFd
      text - value for the field text
      textEnd - value for the field textEnd
      buffer - value for the field buffer
      scopeId - value for the field scopeId
      msgHandler - value for the field msgHandler
      arena - to control the memory allocation scope
    • Scanner

      public Scanner(MemorySegment userData, int maxParseErrors, int parseErrors, String inputName, ScannerConfig config, TokenType token, TokenValue value, int line, int position, TokenType nextToken, TokenValue nextValue, int nextLine, int nextPosition, HashTable<MemorySegment, MemorySegment> symbolTable, int inputFd, String text, String textEnd, String buffer, int scopeId, ScannerMsgFunc msgHandler)
      Allocate a new Scanner with the fields set to the provided values. The memory is allocated with Arena.ofAuto().
      Parameters:
      userData - value for the field userData
      maxParseErrors - value for the field maxParseErrors
      parseErrors - value for the field parseErrors
      inputName - value for the field inputName
      config - value for the field config
      token - value for the field token
      value - value for the field value
      line - value for the field line
      position - value for the field position
      nextToken - value for the field nextToken
      nextValue - value for the field nextValue
      nextLine - value for the field nextLine
      nextPosition - value for the field nextPosition
      symbolTable - value for the field symbolTable
      inputFd - value for the field inputFd
      text - value for the field text
      textEnd - value for the field textEnd
      buffer - value for the field buffer
      scopeId - value for the field scopeId
      msgHandler - value for the field msgHandler
  • Method Details

    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • readUserData

      public MemorySegment readUserData()
      Read the value of the field user_data.
      Returns:
      The value of the field user_data
    • writeUserData

      public void writeUserData(MemorySegment userData)
      Write a value in the field user_data.
      Parameters:
      userData - The new value for the field user_data
    • readMaxParseErrors

      public int readMaxParseErrors()
      Read the value of the field max_parse_errors.
      Returns:
      The value of the field max_parse_errors
    • writeMaxParseErrors

      public void writeMaxParseErrors(int maxParseErrors)
      Write a value in the field max_parse_errors.
      Parameters:
      maxParseErrors - The new value for the field max_parse_errors
    • readParseErrors

      public int readParseErrors()
      Read the value of the field parse_errors.
      Returns:
      The value of the field parse_errors
    • writeParseErrors

      public void writeParseErrors(int parseErrors)
      Write a value in the field parse_errors.
      Parameters:
      parseErrors - The new value for the field parse_errors
    • readInputName

      public String readInputName()
      Read the value of the field input_name.
      Returns:
      The value of the field input_name
    • writeInputName

      public void writeInputName(String inputName, Arena _arena)
      Write a value in the field input_name.
      Parameters:
      inputName - The new value for the field input_name
      _arena - to control the memory allocation scope
    • readConfig

      public ScannerConfig readConfig()
      Read the value of the field config.
      Returns:
      The value of the field config
    • writeConfig

      public void writeConfig(ScannerConfig config)
      Write a value in the field config.
      Parameters:
      config - The new value for the field config
    • readToken

      public TokenType readToken()
      Read the value of the field token.
      Returns:
      The value of the field token
    • writeToken

      public void writeToken(TokenType token)
      Write a value in the field token.
      Parameters:
      token - The new value for the field token
    • readValue

      public @Nullable TokenValue readValue()
      Read the value of the field value.
      Returns:
      The value of the field value
    • writeValue

      public void writeValue(@Nullable TokenValue value)
      Write a value in the field value.
      Parameters:
      value - The new value for the field value
    • readLine

      public int readLine()
      Read the value of the field line.
      Returns:
      The value of the field line
    • writeLine

      public void writeLine(int line)
      Write a value in the field line.
      Parameters:
      line - The new value for the field line
    • readPosition

      public int readPosition()
      Read the value of the field position.
      Returns:
      The value of the field position
    • writePosition

      public void writePosition(int position)
      Write a value in the field position.
      Parameters:
      position - The new value for the field position
    • readNextToken

      public TokenType readNextToken()
      Read the value of the field next_token.
      Returns:
      The value of the field next_token
    • writeNextToken

      public void writeNextToken(TokenType nextToken)
      Write a value in the field next_token.
      Parameters:
      nextToken - The new value for the field next_token
    • readNextValue

      public @Nullable TokenValue readNextValue()
      Read the value of the field next_value.
      Returns:
      The value of the field next_value
    • writeNextValue

      public void writeNextValue(@Nullable TokenValue nextValue)
      Write a value in the field next_value.
      Parameters:
      nextValue - The new value for the field next_value
    • readNextLine

      public int readNextLine()
      Read the value of the field next_line.
      Returns:
      The value of the field next_line
    • writeNextLine

      public void writeNextLine(int nextLine)
      Write a value in the field next_line.
      Parameters:
      nextLine - The new value for the field next_line
    • readNextPosition

      public int readNextPosition()
      Read the value of the field next_position.
      Returns:
      The value of the field next_position
    • writeNextPosition

      public void writeNextPosition(int nextPosition)
      Write a value in the field next_position.
      Parameters:
      nextPosition - The new value for the field next_position
    • readSymbolTable

      public HashTable<MemorySegment, MemorySegment> readSymbolTable()
      Read the value of the field symbol_table.
      Returns:
      The value of the field symbol_table
    • writeSymbolTable

      public void writeSymbolTable(HashTable<MemorySegment, MemorySegment> symbolTable)
      Write a value in the field symbol_table.
      Parameters:
      symbolTable - The new value for the field symbol_table
    • readInputFd

      public int readInputFd()
      Read the value of the field input_fd.
      Returns:
      The value of the field input_fd
    • writeInputFd

      public void writeInputFd(int inputFd)
      Write a value in the field input_fd.
      Parameters:
      inputFd - The new value for the field input_fd
    • readText

      public String readText()
      Read the value of the field text.
      Returns:
      The value of the field text
    • writeText

      public void writeText(String text, Arena _arena)
      Write a value in the field text.
      Parameters:
      text - The new value for the field text
      _arena - to control the memory allocation scope
    • readTextEnd

      public String readTextEnd()
      Read the value of the field text_end.
      Returns:
      The value of the field text_end
    • writeTextEnd

      public void writeTextEnd(String textEnd, Arena _arena)
      Write a value in the field text_end.
      Parameters:
      textEnd - The new value for the field text_end
      _arena - to control the memory allocation scope
    • readBuffer

      public String readBuffer()
      Read the value of the field buffer.
      Returns:
      The value of the field buffer
    • writeBuffer

      public void writeBuffer(String buffer, Arena _arena)
      Write a value in the field buffer.
      Parameters:
      buffer - The new value for the field buffer
      _arena - to control the memory allocation scope
    • readScopeId

      public int readScopeId()
      Read the value of the field scope_id.
      Returns:
      The value of the field scope_id
    • writeScopeId

      public void writeScopeId(int scopeId)
      Write a value in the field scope_id.
      Parameters:
      scopeId - The new value for the field scope_id
    • readMsgHandler

      public @Nullable ScannerMsgFunc readMsgHandler()
      Read the value of the field msg_handler.
      Returns:
      The value of the field msg_handler
    • writeMsgHandler

      public void writeMsgHandler(@Nullable ScannerMsgFunc msgHandler, Arena _arena)
      Write a value in the field msg_handler.
      Parameters:
      msgHandler - The new value for the field msg_handler
      _arena - to control the memory allocation scope
    • new_

      public static Scanner new_(ScannerConfig configTempl)

      Creates a new GScanner.

      The configTempl structure specifies the initial settings of the scanner, which are copied into the GScanner config field. If you pass null then the default settings are used.

      Parameters:
      configTempl - the initial scanner settings
      Returns:
      the new GScanner
    • curLine

      public int curLine()
      Returns the current line in the input stream (counting from 1). This is the line of the last token parsed via g_scanner_get_next_token().
      Returns:
      the current line
    • curPosition

      public int curPosition()
      Returns the current position in the current line (counting from 0). This is the position of the last token parsed via g_scanner_get_next_token().
      Returns:
      the current position on the line
    • curToken

      public TokenType curToken()
      Gets the current token type. This is simply the token field in the GScanner structure.
      Returns:
      the current token type
    • curValue

      public @Nullable TokenValue curValue()
      Gets the current token value. This is simply the value field in the GScanner structure.
      Returns:
      the current token value
    • destroy

      public void destroy()
      Frees all memory used by the GScanner.
    • eof

      public boolean eof()
      Returns true if the scanner has reached the end of the file or text buffer.
      Returns:
      true if the scanner has reached the end of the file or text buffer
    • error

      public void error(String format, Object... varargs)
      Outputs an error message, via the GScanner message handler.
      Parameters:
      format - the message format. See the printf() documentation
      varargs - the parameters to insert into the format string
    • getNextToken

      public TokenType getNextToken()
      Parses the next token just like g_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 GScanner structure.
      Returns:
      the type of the token
    • inputFile

      public void inputFile(int inputFd)
      Prepares to scan a file.
      Parameters:
      inputFd - a file descriptor
    • inputText

      public void inputText(String text, int textLen)
      Prepares to scan a text buffer.
      Parameters:
      text - the text buffer to scan
      textLen - the length of the text buffer
    • lookupSymbol

      public @Nullable MemorySegment lookupSymbol(String symbol)
      Looks up a symbol in the current scope and return its value. If the symbol is not bound in the current scope, null is returned.
      Parameters:
      symbol - the symbol to look up
      Returns:
      the value of symbol in the current scope, or null if symbol is not bound in the current scope
    • peekNextToken

      public TokenType peekNextToken()

      Parses the next token, without removing it from the input stream. The token data is placed in the nextToken, nextValue, nextLine, and nextPosition fields of the GScanner structure.

      Note that, while the token is not removed from the input stream (i.e. the next call to g_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.

      Returns:
      the type of the token
    • scopeAddSymbol

      public void scopeAddSymbol(int scopeId, String symbol, @Nullable MemorySegment value)
      Adds a symbol to the given scope.
      Parameters:
      scopeId - the scope id
      symbol - the symbol to add
      value - the value of the symbol
    • scopeForeachSymbol

      public void scopeForeachSymbol(int scopeId, @Nullable HFunc func)
      Calls the given function for each of the symbol/value pairs in the given scope of the GScanner. The function is passed the symbol and value of each pair, and the given userData parameter.
      Parameters:
      scopeId - the scope id
      func - the function to call for each symbol/value pair
    • scopeLookupSymbol

      public @Nullable MemorySegment scopeLookupSymbol(int scopeId, String symbol)
      Looks up a symbol in a scope and return its value. If the symbol is not bound in the scope, null is returned.
      Parameters:
      scopeId - the scope id
      symbol - the symbol to look up
      Returns:
      the value of symbol in the given scope, or null if symbol is not bound in the given scope.
    • scopeRemoveSymbol

      public void scopeRemoveSymbol(int scopeId, String symbol)
      Removes a symbol from a scope.
      Parameters:
      scopeId - the scope id
      symbol - the symbol to remove
    • setScope

      public int setScope(int scopeId)
      Sets the current scope.
      Parameters:
      scopeId - the new scope id
      Returns:
      the old scope id
    • syncFileOffset

      public void syncFileOffset()
      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.
    • unexpToken

      public void unexpToken(TokenType expectedToken, String identifierSpec, String symbolSpec, String symbolName, String message, int isError)
      Outputs a message through the scanner's msg_handler, resulting from an unexpected token in the input stream. Note that you should not call g_scanner_peek_next_token() followed by g_scanner_unexp_token() without an intermediate call to g_scanner_get_next_token(), as g_scanner_unexp_token() evaluates the scanner's current token (not the peeked token) to construct part of the message.
      Parameters:
      expectedToken - the expected token
      identifierSpec - a string describing how the scanner's user refers to identifiers (null defaults to "identifier"). This is used if expectedToken is TokenType.IDENTIFIER or TokenType.IDENTIFIER_NULL.
      symbolSpec - a string describing how the scanner's user refers to symbols (null defaults to "symbol"). This is used if expectedToken is TokenType.SYMBOL or any token value greater than G_TOKEN_LAST.
      symbolName - the name of the symbol, if the scanner's current token is a symbol.
      message - a message string to output at the end of the warning/error, or null.
      isError - if true it is output as an error. If false it is output as a warning.
    • warn

      public void warn(String format, Object... varargs)
      Outputs a warning message, via the GScanner message handler.
      Parameters:
      format - the message format. See the printf() documentation
      varargs - the parameters to insert into the format string