The three figures that follow are to help you understand the concept of parsing. Please note that the figures do not include error cases.
┌────────────────────────────────────────┐ ↓ │ ┌────────────────────────────────┐ │ │START │ │ │Token is first one in template. │ │ │Length=length(source string) │ │ │Match start=1. Match end=1. │ │ └─────────┬──────────────────────┘ │ ┌──────────→ │ │ │ ↓ │ │ ┌───────────────────┐yes ┌────────────────────┐ │ │ │End of template? ├───→│Parsing complete. │ │ │ └─────────┬─────────┘ └────────────────────┘ │ │ ↓ no │ │ ┌───────────────────┐ │ │ │CALL Find Next │ │ │ │ Pattern. │ │ │ └─────────┬─────────┘ │ │ ↓ │ │ ┌───────────────────┐ │ │ │CALL Word Parsing. │ │ │ └─────────┬─────────┘ │ │ ↓ │ │ ┌───────────────────┐ │ │ │Step to next token.│ │ │ └─────────┬─────────┘ │ │ ↓ │ │ ┌───────────────────┐ yes ┌────────────────────┐ │ │ │Token a comma? ├────→│Set next source │ │ │ └─────────┬─────────┘ │string and template.├───┘ │ │ no └────────────────────┘ └────────────┘
┌────────────────────────────────────────────────┐ ↓ │ ┌─────────────┐ ┌────────────────────────────────┐ │ │Start: │yes │String start=match end. │ │ │End of ├───→│Match start=length + 1. │ │ │template? │ │Match end=length + 1. Return. │ │ └─────┬───────┘ └────────────────────────────────┘ │ ↓ no │ ┌─────────────┐ ┌────────────────────────────────┐ │ │Token period │yes │ │ │ │or variable? ├───→│Step to next token. ├───┘ └─────┬───────┘ └────────────────────────────────┘ ↓ no ┌─────────────┐ ┌─────────┐ ┌──────────┐ ┌───────────────────────────────────┐ │Token a plus?│yes │Variable │yes │Resolve │ │String start=match start. │ │ ├───→│form? ├───→│its value.├──→│Match start=min(length + 1, │ └─────┬───────┘ └────┬────┘ └──────────┘↑ │ match start + value). │ │ no │ no │ │Match end=match start. Return. │ ↓ └─────────────────────┘ └───────────────────────────────────┘ ┌─────────────┐ ┌─────────┐ ┌──────────┐ ┌───────────────────────────────────┐ │Token a │yes │Variable │yes │Resolve │ │String start=match start. │ │minus? ├───→│form? ├───→│its value.├──→│Match start=max(1, match │ └─────┬───────┘ └────┬────┘ └──────────┘↑ │ start ─ value). │ │ no │ no │ │Match end=match start. Return. │ ↓ └─────────────────────┘ └───────────────────────────────────┘ ┌─────────────┐ ┌─────────┐ ┌──────────┐ ┌───────────────────────────────────┐ │Token an │yes │Variable │yes │Resolve │ │String start=match end. │ │equal? ├───→│form? ├───→│its value.├──→│Match start=min(length+1, value). │ └─────┬───────┘ └────┬────┘ └──────────┘↑ │Match end=match start. Return. │ │ no │ no │ └───────────────────────────────────┘ ↓ └─────────────────────┘ ┌─────────────┐ ┌───────────────────────────────────┐ │Token a │yes │String start=match end. │ │number? ├───→│Match start=min(length+1, value). │ └─────┬───────┘ │Match end=match start. Return. │ ↓ no └───────────────────────────────────┘ ┌─────────────┐ │Token a lit─ │yes │eral string? ├──────────────────────────┐ └─────┬───────┘ │ │ no │ ↓ ↓ ┌─────────────┐ ┌──────────┐ ┌───────────────┐ ┌─────────────────────────────┐ │Token a var─ │yes │Resolve │ │Match found in │yes │String start=match end. │ │iable string?├───→│its value.├──→│rest of string?├───→│Match start=match position. │ └─────┬───────┘ └──────────┘ └──────┬────────┘ │Match end=match position + │ │ no │ no │ pattern length. Return. │ │ ↓ └─────────────────────────────┘ │ ┌────────────────────────────────┐ │ │String start=match end. │ │ │Match start=length + 1. │ │ │Match end=length + 1. Return. │ ↓ └────────────────────────────────┘ ┌─────────────┐ ┌────────────────────────────────┐ │Token a │yes │Match start=length + 1. │ │ comma? ├─────────→│Match end=length + 1. Return. │ └─────────────┘ └────────────────────────────────┘
┌─────────────────────────┐ ┌────────────────────────┐ │Start: Match end <= │no │ │ │ string start? ├───→│String end=match start. │ └───────────┬─────────────┘ └────────────────────────┘ ↓ yes ┌─────────────────────────┐ │String end=length + 1. │ └───────────┬─────────────┘ ↓ ┌──────────────────────────────────────────────────────────────────────┐ │Substring=substr(source string,string start,(string end─string start))│ │Token=previous pattern. │ └───────────┬──────────────────────────────────────────────────────────┘ ↓ ←───────────────────────────────────────────────┐ ┌─────────────────────────┐no │ │Any more tokens? ├─────────────┐ │ └───────────┬─────────────┘ │ │ ↓ yes │ │ ┌─────────────────────────┐ │ │ │Step to next token. │ │ │ └───────────┬─────────────┘ │ │ ↓ ↓ │ ┌─────────────────────────┐no ┌────────────────────────┐ │ │Token a variable or a ├───→│Return. │ │ │period? │ └────────────────────────┘ │ └───────────┬─────────────┘ │ ↓ yes │ ┌─────────────────────────┐no │ │Any more tokens? ├─────────────┐ │ └───────────┬─────────────┘ │ │ ↓ yes ↓ │ ┌─────────────────────────┐ ┌────────────────────────┐ │ │Next token a variable or │ no │Assign rest of substring│ │ │period? ├───→│to variable. │ │ └───────────┬─────────────┘ └─────────────┬──────────┘ │ ↓ yes └───────────────→│ ┌─────────────────────────┐ no ┌────────────────────────┐ │ │Any substring left? ├───→│Assign null string to │ │ └───────────┬─────────────┘ │variable. │ │ ↓ yes └─────────────┬──────────┘ │ ┌─────────────────────────┐ └───────────────→│ │Strip any leading blanks.│ │ └───────────┬─────────────┘ │ ↓ │ ┌─────────────────────────┐ no ┌────────────────────────┐ │ │Any substring left? ├───→│Assign null string to │ │ └───────────┬─────────────┘ │variable. │ │ │ └─────────────┬──────────┘ │ ↓ yes └───────────────→│ ┌─────────────────────────┐ no ┌────────────────────────┐ │ │Blank found in substring?├───→│Assign rest of substring│ │ │ │ │to variable. │ │ └───────────┬─────────────┘ └─────────────┬──────────┘ │ ↓ yes └───────────────→│ ┌───────────────────────────────────────────────────────────┐ │ │Assign word from substring to variable and step past blank.│ │ └───────────────────┬───────────────────────────────────────┘ │ └─────────────────────────────────────────┘