15.7. The Scanner Unit

The next, and by far the most important, version of the scanner is the one that handles the multi-character tokens that all real languages must have. Only the two functions, GetName and GetNumber, change between the two units, but just to be sure there are no mistakes, I've reproduced the entire unit here. This is unit Scanner:

unit Scanner;

interface
uses Input, Errors;

function IsAlpha(c: char): boolean;
function IsDigit(c: char): boolean;
function IsAlNum(c: char): boolean;
function IsAddop(c: char): boolean;
function IsMulop(c: char): boolean;

procedure Match(x: char);
function GetName: string;
function GetNumber: longint;

implementation

{ Recognize an Alpha Character }
function IsAlpha(c: char): boolean;
begin
        IsAlpha := UpCase(c) in ['A'..'Z'];
end;

{ Recognize a Numeric Character }
function IsDigit(c: char): boolean;
begin
        IsDigit := c in ['0'..'9'];
end;

{ Recognize an Alphanumeric Character }
function IsAlnum(c: char): boolean;
begin
        IsAlnum := IsAlpha(c) or IsDigit(c);
end;

{ Recognize an Addition Operator }
function IsAddop(c: char): boolean;
begin
        IsAddop := c in ['+','-'];
end;

{ Recognize a Multiplication Operator }
function IsMulop(c: char): boolean;
begin
        IsMulop := c in ['*','/'];
end;

{ Match One Character }
procedure Match(x: char);
begin
        if Look = x then GetChar
        else Expected('''' + x + '''');
end;

{ Get an Identifier }
function GetName: string;
var n: string;
begin
        n := '';
        if not IsAlpha(Look) then Expected('Name');
        while IsAlnum(Look) do begin
                n := n + Look;
                GetChar;
        end;
        GetName := n;
end;

{ Get a Number }
function GetNumber: string;
var n: string;
begin
        n := '';
        if not IsDigit(Look) then Expected('Integer');
        while IsDigit(Look) do begin
                n := n + Look;
                GetChar;
        end;
        GetNumber := n;
end;

end.

The same test program will test this scanner, also. Simply change the "uses" clause to use Scanner instead of Scanner1. Now you should be able to type multi-character names and numbers.