matchRegions function
dynamic
matchRegions(
- List tokens,
- dynamic options
)
Implementation
dynamic matchRegions(List tokens, options) {
List regions = [];
if (checkBlacklist(tokens)) return regions;
int i = 0;
late var currentRegion;
int tokensCount = tokens.length;
while (i < tokensCount) {
var token = tokens[i];
var tokenFits = checkIfTokenFitsRegion(currentRegion, token, options);
switch (tokenFits) {
case SKIP:
{
break;
}
case ADD:
{
if (currentRegion) {
currentRegion['end'] = token['end'];
currentRegion['tokens'].add(token);
if (token['type'] == TOKEN_TYPE['DECIMAL']) {
currentRegion['hasDecimal'] = true;
}
}
break;
}
case START_NEW_REGION:
{
currentRegion = {
'start': token['start'],
'end': token['end'],
'tokens': [token]
};
regions.add(currentRegion);
if (token['tyep'] == TOKEN_TYPE['DECIMAL']) {
currentRegion['hasDecimal'] = true;
}
break;
}
case NOPE:
{
const doNothing = 'do nothing';
break;
}
default:
{
currentRegion = null;
break;
}
}
i++;
}
return regions.map((region) => ({
'start': region['start'],
'end': region['end'],
'tokens': region['tokens'],
'subRegion': getSubRegions(region, options)
}));
}