There is soql-parser-js in npm/javascript if you want to break out a query into its different parts as an AST JSON object. Once you have the query parsed, you can do all sorts of transformations and what not to convert that object into whatever your desired POJO model is. I am not aware of anything similar that exists in apex, nor do i feel that something such as a tokenizer and lexer belong somewhere like apex.
Here is an example of a query with some nested queries and relationship fields for a quick reference:
Query:
SELECT Id, Name FROM Account WHERE Id IN ( SELECT AccountId FROM
Contact WHERE LastName LIKE 'apple%' ) AND Id IN ( SELECT AccountId, Account.CreatedBy.Name
FROM Opportunity WHERE isClosed = FALSE )
Parsed:
{
"fields": [
{
"type": "Field",
"field": "Id"
},
{
"type": "Field",
"field": "Name"
}
],
"sObject": "Account",
"where": {
"left": {
"field": "Id",
"operator": "IN",
"valueQuery": {
"fields": [
{
"type": "Field",
"field": "AccountId"
}
],
"sObject": "Contact",
"where": {
"left": {
"field": "LastName",
"operator": "LIKE",
"value": "'apple%'",
"literalType": "STRING"
}
}
}
},
"operator": "AND",
"right": {
"left": {
"field": "Id",
"operator": "IN",
"valueQuery": {
"fields": [
{
"type": "Field",
"field": "AccountId"
},
{
"type": "FieldRelationship",
"field": "Name",
"relationships": [
"Account",
"CreatedBy"
],
"rawValue": "Account.CreatedBy.Name"
}
],
"sObject": "Opportunity",
"where": {
"left": {
"field": "isClosed",
"operator": "=",
"value": "FALSE",
"literalType": "BOOLEAN"
}
}
}
}
}
}
}