Class Scanner
- All Implemented Interfaces:
Proxy
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 Summary
ConstructorDescriptionScanner()
Allocate a new Scanner.Allocate a new Scanner.Scanner
(MemorySegment address) Create a Scanner proxy instance for the provided memory address.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, int inputFd, String text, String textEnd, String buffer, int scopeId, ScannerMsgFunc msgHandler) Allocate a new Scanner with the fields set to the provided values.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, 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. -
Method Summary
Modifier and TypeMethodDescriptionint
curLine()
Returns the current line in the input stream (counting from 1).int
Returns the current position in the current line (counting from 0).curToken()
Gets the current token type.curValue()
Gets the current token value.void
destroy()
Frees all memory used by theGScanner
.boolean
eof()
Returnstrue
if the scanner has reached the end of the file or text buffer.void
Outputs an error message, via theGScanner
message handler.static MemoryLayout
The memory layout of the native struct.Parses the next token just like g_scanner_peek_next_token() and also removes it from the input stream.void
inputFile
(int inputFd) Prepares to scan a file.void
Prepares to scan a text buffer.lookupSymbol
(String symbol) Looks up a symbol in the current scope and return its value.static Scanner
new_
(ScannerConfig configTempl) Creates a newGScanner
.Parses the next token, without removing it from the input stream.Read the value of the fieldbuffer
.Read the value of the fieldconfig
.int
Read the value of the fieldinput_fd
.Read the value of the fieldinput_name
.int
readLine()
Read the value of the fieldline
.int
Read the value of the fieldmax_parse_errors
.Read the value of the fieldmsg_handler
.int
Read the value of the fieldnext_line
.int
Read the value of the fieldnext_position
.Read the value of the fieldnext_token
.Read the value of the fieldnext_value
.int
Read the value of the fieldparse_errors
.int
Read the value of the fieldposition
.int
Read the value of the fieldscope_id
.readText()
Read the value of the fieldtext
.Read the value of the fieldtext_end
.Read the value of the fieldtoken
.Read the value of the fielduser_data
.Read the value of the fieldvalue
.void
scopeAddSymbol
(int scopeId, String symbol, @Nullable MemorySegment value) Adds a symbol to the given scope.void
scopeForeachSymbol
(int scopeId, HFunc func) Calls the given function for each of the symbol/value pairs in the given scope of theGScanner
.scopeLookupSymbol
(int scopeId, String symbol) Looks up a symbol in a scope and return its value.void
scopeRemoveSymbol
(int scopeId, String symbol) Removes a symbol from a scope.int
setScope
(int scopeId) Sets the current scope.void
Rewinds the filedescriptor to the current buffer position and blows the file read ahead buffer.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.void
Outputs a warning message, via theGScanner
message handler.void
writeBuffer
(String buffer, Arena _arena) Write a value in the fieldbuffer
.void
writeConfig
(ScannerConfig config) Write a value in the fieldconfig
.void
writeInputFd
(int inputFd) Write a value in the fieldinput_fd
.void
writeInputName
(String inputName, Arena _arena) Write a value in the fieldinput_name
.void
writeLine
(int line) Write a value in the fieldline
.void
writeMaxParseErrors
(int maxParseErrors) Write a value in the fieldmax_parse_errors
.void
writeMsgHandler
(ScannerMsgFunc msgHandler, Arena _arena) Write a value in the fieldmsg_handler
.void
writeNextLine
(int nextLine) Write a value in the fieldnext_line
.void
writeNextPosition
(int nextPosition) Write a value in the fieldnext_position
.void
writeNextToken
(TokenType nextToken) Write a value in the fieldnext_token
.void
writeNextValue
(TokenValue nextValue) Write a value in the fieldnext_value
.void
writeParseErrors
(int parseErrors) Write a value in the fieldparse_errors
.void
writePosition
(int position) Write a value in the fieldposition
.void
writeScopeId
(int scopeId) Write a value in the fieldscope_id
.void
Write a value in the fieldtext
.void
writeTextEnd
(String textEnd, Arena _arena) Write a value in the fieldtext_end
.void
writeToken
(TokenType token) Write a value in the fieldtoken
.void
writeUserData
(MemorySegment userData) Write a value in the fielduser_data
.void
writeValue
(TokenValue value) Write a value in the fieldvalue
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Scanner
Create a Scanner proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Scanner
Allocate a new Scanner.- Parameters:
arena
- to control the memory allocation scope
-
Scanner
public Scanner()Allocate a new Scanner. The memory is allocated withArena.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, 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 fielduserData
maxParseErrors
- value for the fieldmaxParseErrors
parseErrors
- value for the fieldparseErrors
inputName
- value for the fieldinputName
config
- value for the fieldconfig
token
- value for the fieldtoken
value
- value for the fieldvalue
line
- value for the fieldline
position
- value for the fieldposition
nextToken
- value for the fieldnextToken
nextValue
- value for the fieldnextValue
nextLine
- value for the fieldnextLine
nextPosition
- value for the fieldnextPosition
inputFd
- value for the fieldinputFd
text
- value for the fieldtext
textEnd
- value for the fieldtextEnd
buffer
- value for the fieldbuffer
scopeId
- value for the fieldscopeId
msgHandler
- value for the fieldmsgHandler
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, 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 withArena.ofAuto()
.- Parameters:
userData
- value for the fielduserData
maxParseErrors
- value for the fieldmaxParseErrors
parseErrors
- value for the fieldparseErrors
inputName
- value for the fieldinputName
config
- value for the fieldconfig
token
- value for the fieldtoken
value
- value for the fieldvalue
line
- value for the fieldline
position
- value for the fieldposition
nextToken
- value for the fieldnextToken
nextValue
- value for the fieldnextValue
nextLine
- value for the fieldnextLine
nextPosition
- value for the fieldnextPosition
inputFd
- value for the fieldinputFd
text
- value for the fieldtext
textEnd
- value for the fieldtextEnd
buffer
- value for the fieldbuffer
scopeId
- value for the fieldscopeId
msgHandler
- value for the fieldmsgHandler
-
-
Method Details
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readUserData
Read the value of the fielduser_data
.- Returns:
- The value of the field
user_data
-
writeUserData
Write a value in the fielduser_data
.- Parameters:
userData
- The new value for the fielduser_data
-
readMaxParseErrors
public int readMaxParseErrors()Read the value of the fieldmax_parse_errors
.- Returns:
- The value of the field
max_parse_errors
-
writeMaxParseErrors
public void writeMaxParseErrors(int maxParseErrors) Write a value in the fieldmax_parse_errors
.- Parameters:
maxParseErrors
- The new value for the fieldmax_parse_errors
-
readParseErrors
public int readParseErrors()Read the value of the fieldparse_errors
.- Returns:
- The value of the field
parse_errors
-
writeParseErrors
public void writeParseErrors(int parseErrors) Write a value in the fieldparse_errors
.- Parameters:
parseErrors
- The new value for the fieldparse_errors
-
readInputName
Read the value of the fieldinput_name
.- Returns:
- The value of the field
input_name
-
writeInputName
-
readConfig
Read the value of the fieldconfig
.- Returns:
- The value of the field
config
-
writeConfig
Write a value in the fieldconfig
.- Parameters:
config
- The new value for the fieldconfig
-
readToken
Read the value of the fieldtoken
.- Returns:
- The value of the field
token
-
writeToken
Write a value in the fieldtoken
.- Parameters:
token
- The new value for the fieldtoken
-
readValue
Read the value of the fieldvalue
.- Returns:
- The value of the field
value
-
writeValue
Write a value in the fieldvalue
.- Parameters:
value
- The new value for the fieldvalue
-
readLine
public int readLine()Read the value of the fieldline
.- Returns:
- The value of the field
line
-
writeLine
public void writeLine(int line) Write a value in the fieldline
.- Parameters:
line
- The new value for the fieldline
-
readPosition
public int readPosition()Read the value of the fieldposition
.- Returns:
- The value of the field
position
-
writePosition
public void writePosition(int position) Write a value in the fieldposition
.- Parameters:
position
- The new value for the fieldposition
-
readNextToken
Read the value of the fieldnext_token
.- Returns:
- The value of the field
next_token
-
writeNextToken
Write a value in the fieldnext_token
.- Parameters:
nextToken
- The new value for the fieldnext_token
-
readNextValue
Read the value of the fieldnext_value
.- Returns:
- The value of the field
next_value
-
writeNextValue
Write a value in the fieldnext_value
.- Parameters:
nextValue
- The new value for the fieldnext_value
-
readNextLine
public int readNextLine()Read the value of the fieldnext_line
.- Returns:
- The value of the field
next_line
-
writeNextLine
public void writeNextLine(int nextLine) Write a value in the fieldnext_line
.- Parameters:
nextLine
- The new value for the fieldnext_line
-
readNextPosition
public int readNextPosition()Read the value of the fieldnext_position
.- Returns:
- The value of the field
next_position
-
writeNextPosition
public void writeNextPosition(int nextPosition) Write a value in the fieldnext_position
.- Parameters:
nextPosition
- The new value for the fieldnext_position
-
readInputFd
public int readInputFd()Read the value of the fieldinput_fd
.- Returns:
- The value of the field
input_fd
-
writeInputFd
public void writeInputFd(int inputFd) Write a value in the fieldinput_fd
.- Parameters:
inputFd
- The new value for the fieldinput_fd
-
readText
-
writeText
-
readTextEnd
Read the value of the fieldtext_end
.- Returns:
- The value of the field
text_end
-
writeTextEnd
-
readBuffer
Read the value of the fieldbuffer
.- Returns:
- The value of the field
buffer
-
writeBuffer
-
readScopeId
public int readScopeId()Read the value of the fieldscope_id
.- Returns:
- The value of the field
scope_id
-
writeScopeId
public void writeScopeId(int scopeId) Write a value in the fieldscope_id
.- Parameters:
scopeId
- The new value for the fieldscope_id
-
readMsgHandler
Read the value of the fieldmsg_handler
.- Returns:
- The value of the field
msg_handler
-
writeMsgHandler
Write a value in the fieldmsg_handler
.- Parameters:
msgHandler
- The new value for the fieldmsg_handler
_arena
- to control the memory allocation scope
-
new_
Creates a newGScanner
.The
configTempl
structure specifies the initial settings of the scanner, which are copied into theGScanner
config
field. If you passnull
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
Gets the current token type. This is simply thetoken
field in theGScanner
structure.- Returns:
- the current token type
-
curValue
Gets the current token value. This is simply thevalue
field in theGScanner
structure.- Returns:
- the current token value
-
destroy
public void destroy()Frees all memory used by theGScanner
. -
eof
public boolean eof()Returnstrue
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
-
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 thetoken
,value
,line
, andposition
fields of theGScanner
structure.- Returns:
- the type of the token
-
inputFile
public void inputFile(int inputFd) Prepares to scan a file.- Parameters:
inputFd
- a file descriptor
-
inputText
Prepares to scan a text buffer.- Parameters:
text
- the text buffer to scantextLen
- the length of the text buffer
-
lookupSymbol
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, ornull
ifsymbol
is not bound in the current scope
-
peekNextToken
Parses the next token, without removing it from the input stream. The token data is placed in thenextToken
,nextValue
,nextLine
, andnextPosition
fields of theGScanner
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
Adds a symbol to the given scope.- Parameters:
scopeId
- the scope idsymbol
- the symbol to addvalue
- the value of the symbol
-
scopeForeachSymbol
Calls the given function for each of the symbol/value pairs in the given scope of theGScanner
. The function is passed the symbol and value of each pair, and the givenuserData
parameter.- Parameters:
scopeId
- the scope idfunc
- the function to call for each symbol/value pair
-
scopeLookupSymbol
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 idsymbol
- the symbol to look up- Returns:
- the value of
symbol
in the given scope, ornull
ifsymbol
is not bound in the given scope.
-
scopeRemoveSymbol
Removes a symbol from a scope.- Parameters:
scopeId
- the scope idsymbol
- 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 tokenidentifierSpec
- a string describing how the scanner's user refers to identifiers (null
defaults to "identifier"). This is used ifexpectedToken
isTokenType.IDENTIFIER
orTokenType.IDENTIFIER_NULL
.symbolSpec
- a string describing how the scanner's user refers to symbols (null
defaults to "symbol"). This is used ifexpectedToken
isTokenType.SYMBOL
or any token value greater thanG_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, ornull
.isError
- iftrue
it is output as an error. Iffalse
it is output as a warning.
-
warn
-