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
ConstructorsConstructorDescriptionScanner()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, 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.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. -
Method Summary
Modifier and TypeMethodDescriptionintcurLine()Returns the current line in the input stream (counting from 1).intReturns the current position in the current line (counting from 0).curToken()Gets the current token type.@Nullable TokenValuecurValue()Gets the current token value.voiddestroy()Frees all memory used by theGScanner.booleaneof()Returnstrueif the scanner has reached the end of the file or text buffer.voidOutputs an error message, via theGScannermessage handler.static MemoryLayoutThe 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.voidinputFile(int inputFd) Prepares to scan a file.voidPrepares to scan a text buffer.@Nullable MemorySegmentlookupSymbol(String symbol) Looks up a symbol in the current scope and return its value.static Scannernew_(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.intRead the value of the fieldinput_fd.Read the value of the fieldinput_name.intreadLine()Read the value of the fieldline.intRead the value of the fieldmax_parse_errors.@Nullable ScannerMsgFuncRead the value of the fieldmsg_handler.intRead the value of the fieldnext_line.intRead the value of the fieldnext_position.Read the value of the fieldnext_token.@Nullable TokenValueRead the value of the fieldnext_value.intRead the value of the fieldparse_errors.intRead the value of the fieldposition.intRead the value of the fieldscope_id.Read the value of the fieldsymbol_table.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.@Nullable TokenValueRead the value of the fieldvalue.voidscopeAddSymbol(int scopeId, String symbol, @Nullable MemorySegment value) Adds a symbol to the given scope.voidscopeForeachSymbol(int scopeId, @Nullable HFunc func) Calls the given function for each of the symbol/value pairs in the given scope of theGScanner.@Nullable MemorySegmentscopeLookupSymbol(int scopeId, String symbol) Looks up a symbol in a scope and return its value.voidscopeRemoveSymbol(int scopeId, String symbol) Removes a symbol from a scope.intsetScope(int scopeId) Sets the current scope.voidRewinds the filedescriptor to the current buffer position and blows the file read ahead buffer.voidunexpToken(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.voidOutputs a warning message, via theGScannermessage handler.voidwriteBuffer(String buffer, Arena _arena) Write a value in the fieldbuffer.voidwriteConfig(ScannerConfig config) Write a value in the fieldconfig.voidwriteInputFd(int inputFd) Write a value in the fieldinput_fd.voidwriteInputName(String inputName, Arena _arena) Write a value in the fieldinput_name.voidwriteLine(int line) Write a value in the fieldline.voidwriteMaxParseErrors(int maxParseErrors) Write a value in the fieldmax_parse_errors.voidwriteMsgHandler(@Nullable ScannerMsgFunc msgHandler, Arena _arena) Write a value in the fieldmsg_handler.voidwriteNextLine(int nextLine) Write a value in the fieldnext_line.voidwriteNextPosition(int nextPosition) Write a value in the fieldnext_position.voidwriteNextToken(TokenType nextToken) Write a value in the fieldnext_token.voidwriteNextValue(@Nullable TokenValue nextValue) Write a value in the fieldnext_value.voidwriteParseErrors(int parseErrors) Write a value in the fieldparse_errors.voidwritePosition(int position) Write a value in the fieldposition.voidwriteScopeId(int scopeId) Write a value in the fieldscope_id.voidwriteSymbolTable(HashTable<MemorySegment, MemorySegment> symbolTable) Write a value in the fieldsymbol_table.voidWrite a value in the fieldtext.voidwriteTextEnd(String textEnd, Arena _arena) Write a value in the fieldtext_end.voidwriteToken(TokenType token) Write a value in the fieldtoken.voidwriteUserData(MemorySegment userData) Write a value in the fielduser_data.voidwriteValue(@Nullable TokenValue value) Write a value in the fieldvalue.Methods inherited from class org.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, 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 fielduserDatamaxParseErrors- value for the fieldmaxParseErrorsparseErrors- value for the fieldparseErrorsinputName- value for the fieldinputNameconfig- value for the fieldconfigtoken- value for the fieldtokenvalue- value for the fieldvalueline- value for the fieldlineposition- value for the fieldpositionnextToken- value for the fieldnextTokennextValue- value for the fieldnextValuenextLine- value for the fieldnextLinenextPosition- value for the fieldnextPositionsymbolTable- value for the fieldsymbolTableinputFd- value for the fieldinputFdtext- value for the fieldtexttextEnd- value for the fieldtextEndbuffer- value for the fieldbufferscopeId- value for the fieldscopeIdmsgHandler- value for the fieldmsgHandlerarena- 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 withArena.ofAuto().- Parameters:
userData- value for the fielduserDatamaxParseErrors- value for the fieldmaxParseErrorsparseErrors- value for the fieldparseErrorsinputName- value for the fieldinputNameconfig- value for the fieldconfigtoken- value for the fieldtokenvalue- value for the fieldvalueline- value for the fieldlineposition- value for the fieldpositionnextToken- value for the fieldnextTokennextValue- value for the fieldnextValuenextLine- value for the fieldnextLinenextPosition- value for the fieldnextPositionsymbolTable- value for the fieldsymbolTableinputFd- value for the fieldinputFdtext- value for the fieldtexttextEnd- value for the fieldtextEndbuffer- value for the fieldbufferscopeId- value for the fieldscopeIdmsgHandler- 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
-
readSymbolTable
Read the value of the fieldsymbol_table.- Returns:
- The value of the field
symbol_table
-
writeSymbolTable
Write a value in the fieldsymbol_table.- Parameters:
symbolTable- The new value for the fieldsymbol_table
-
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
configTemplstructure specifies the initial settings of the scanner, which are copied into theGScannerconfigfield. If you passnullthen 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 thetokenfield in theGScannerstructure.- Returns:
- the current token type
-
curValue
Gets the current token value. This is simply thevaluefield in theGScannerstructure.- Returns:
- the current token value
-
destroy
public void destroy()Frees all memory used by theGScanner. -
eof
public boolean eof()Returnstrueif the scanner has reached the end of the file or text buffer.- Returns:
trueif 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,andpositionfields of theGScannerstructure.- 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,nullis returned.- Parameters:
symbol- the symbol to look up- Returns:
- the value of
symbolin the current scope, ornullifsymbolis 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,andnextPositionfields of theGScannerstructure.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 givenuserDataparameter.- 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,nullis returned.- Parameters:
scopeId- the scope idsymbol- the symbol to look up- Returns:
- the value of
symbolin the given scope, ornullifsymbolis 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 (nulldefaults to "identifier"). This is used ifexpectedTokenisTokenType.IDENTIFIERorTokenType.IDENTIFIER_NULL.symbolSpec- a string describing how the scanner's user refers to symbols (nulldefaults to "symbol"). This is used ifexpectedTokenisTokenType.SYMBOLor 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- iftrueit is output as an error. Iffalseit is output as a warning.
-
warn
-