# Original Parser

## version 1 by Ron Newcomb

Section - A Small Snippet Extension

[ This snippet mini-extension is really handy. ]

[ Snippets as a unit ]
To decide what number is the position part of (snp - a snippet): (- ({snp} / 100) -).
To decide what number is the length in words part of (snp - a snippet): (- ({snp} % 100) -).

[ Useful little phrases. ]
To decide if (snp - a snippet) is/are invalid: (- ( ({snp} < 101) || (({snp} % 100) < 1) ) -).
To decide what number is the ending position of (snp - a snippet): (- ((({snp}) / 100) + (({snp}) % 100) - 1) -).
To decide what snippet is the empty snippet: (- 100 -).
To decide what snippet is (snip - a snippet) lengthened by (n - a number): (- ({snip} + {n}) -).
To decide what snippet is (snip - a snippet) shortened by (n - a number): (- ({snip} - {n}) -).
To decide what snippet is the single word at/-- (position - a number): (- ((({position}) * 100) + 1) -).
To decide what snippet is the insertion point at/-- (position - a number): (- (({position}) * 100) -).

[ Turning a snippet of words into a "snippet of letters". ]
To decide what [letter-wise position] number is the left edge of (snip - a snippet):
let leftmost [word-wise position] be the [word-wise] position part of the snip;
decide on the leftmost word's [letter-wise] position of the player's parsed command;

To decide what [letter-wise position] number is the right edge of (snip - a snippet):
[ say "length in words of snip [length in words part of the snip].";]
if the length in words part of the snip is 0, decide on the left edge of the snip - 2;
let rightmost [word-wise position] be the [word-wise] ending position of the snip;
let the last word's first letter's position be the rightmost word's [letter-wise] position of the player's parsed command;
decide on the last word's first letter's position + the rightmost word's length of the player's parsed command - 1.

[ OOPS processing also uses this, as it's essentially a call to "replace (snippet) with (text)". ]
[ Old word(s) are in the player's input buffer. New word(s) are in the secondary input buffer. ]
To replace (old word - snippet) with (new word's length - a number) in letters from (new word's position - a number):
let the old word's position be the left edge of the old word;
let the old word's length be (the right edge of the old word) - the old word's position + 1;
[ say "[right edge of the old word] - old position [old word's position] + 1 = old len [old word's length]; new len [new word's length].";]
[ if the new word's length < the old word's length:
fill the player's input buffer with spaces starting at the old word's position for old word's length elements; [ fill? or shift left? And retokenize? ]]
if the new word's length > the old word's length:
[ say " [player's input buffer][line break]";]
insert (new word's length - old word's length) elements into the player's input buffer at the old word's position;
[ say " [player's input buffer][line break]";]
copy (the new word's length) elements FROM the secondary input buffer at the new word's position TO the player's input buffer at the old word's position;
if the new word's length < the old word's length:
shift the player's input buffer left by (old word's length - new word's length) starting at the old word's position + the old word's length;
PARSE the player's input buffer into the player's parsed command;
now the player's command is the empty snippet lengthened by the word count.

[Every turn:
say "[the player's command].";
if the player's command includes "blue/red", replace the matched text with "yellow";
say "[the player's command].";
if the player's command includes "yellow", cut the matched text;
say "[the player's command].";
change the text of the player's command to "";
say "[the player's command].";]

[Before printing a parser error, say " [the player's input buffer].".]