2019-10-06 19:55:12 +02:00
|
|
|
(*
|
2019-03-11 09:59:55 +01:00
|
|
|
BSD 2-Clause License
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
Copyright (c) 2018-2019, Anton Krotov
|
2019-03-11 09:59:55 +01:00
|
|
|
All rights reserved.
|
2016-10-24 01:30:27 +02:00
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE SCAN;
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
IMPORT TXT := TEXTDRV, AVL := AVLTREES, ARITH, S := STRINGS;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
CONST
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
LEXLEN = 1024;
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxUNDEF* = 0; lxIDENT* = 1; lxINTEGER* = 2; lxHEX* = 3;
|
|
|
|
lxCHAR* = 4; lxFLOAT* = 5; lxSTRING* = 6; lxCOMMENT* = 7;
|
|
|
|
lxEOF* = 8;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxPLUS* = 21; lxMINUS* = 22; lxMUL* = 23; lxSLASH* = 24;
|
|
|
|
lxNOT* = 25; lxAND* = 26; lxPOINT* = 27; lxCOMMA* = 28;
|
|
|
|
lxSEMI* = 29; lxBAR* = 30; lxLROUND* = 31; lxLSQUARE* = 32;
|
|
|
|
lxLCURLY* = 33; lxCARET* = 34; lxEQ* = 35; lxNE* = 36;
|
|
|
|
lxLT* = 37; lxGT* = 38; lxCOLON* = 39; lxRROUND* = 40;
|
|
|
|
lxRSQUARE* = 41; lxRCURLY* = 42; lxLE* = 43; lxGE* = 44;
|
|
|
|
lxASSIGN* = 45; lxRANGE* = 46;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxKW = 51;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxARRAY* = 51; lxBEGIN* = 52; lxBY* = 53; lxCASE* = 54;
|
|
|
|
lxCONST* = 55; lxDIV* = 56; lxDO* = 57; lxELSE* = 58;
|
|
|
|
lxELSIF* = 59; lxEND* = 60; lxFALSE* = 61; lxFOR* = 62;
|
|
|
|
lxIF* = 63; lxIMPORT* = 64; lxIN* = 65; lxIS* = 66;
|
|
|
|
lxMOD* = 67; lxMODULE* = 68; lxNIL* = 69; lxOF* = 70;
|
|
|
|
lxOR* = 71; lxPOINTER* = 72; lxPROCEDURE* = 73; lxRECORD* = 74;
|
|
|
|
lxREPEAT* = 75; lxRETURN* = 76; lxTHEN* = 77; lxTO* = 78;
|
|
|
|
lxTRUE* = 79; lxTYPE* = 80; lxUNTIL* = 81; lxVAR* = 82;
|
|
|
|
lxWHILE* = 83;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
lxERROR01* = -1; lxERROR02* = -2; lxERROR03* = -3; lxERROR04* = -4;
|
|
|
|
lxERROR05* = -5; lxERROR06* = -6; lxERROR07* = -7; lxERROR08* = -8;
|
|
|
|
lxERROR09* = -9; lxERROR10* = -10; lxERROR11* = -11; lxERROR12* = -12;
|
|
|
|
lxERROR13* = -13;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
LEXSTR* = ARRAY LEXLEN OF CHAR;
|
|
|
|
|
|
|
|
IDENT* = POINTER TO RECORD (AVL.DATA)
|
|
|
|
|
|
|
|
s*: LEXSTR;
|
2019-10-06 19:55:12 +02:00
|
|
|
offset*, offsetW*: INTEGER;
|
|
|
|
key: INTEGER
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
POSITION* = RECORD
|
|
|
|
|
|
|
|
line*, col*: INTEGER
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
LEX* = RECORD
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
s*: LEXSTR;
|
|
|
|
length*: INTEGER;
|
|
|
|
sym*: INTEGER;
|
|
|
|
pos*: POSITION;
|
|
|
|
ident*: IDENT;
|
|
|
|
string*: IDENT;
|
|
|
|
value*: ARITH.VALUE;
|
|
|
|
error*: INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
over: BOOLEAN
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
SCANNER* = TXT.TEXT;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
VAR
|
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
idents: AVL.NODE;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
delimiters: ARRAY 256 OF BOOLEAN;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
NewIdent: IDENT;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
upto: BOOLEAN;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
PROCEDURE nodecmp (a, b: AVL.DATA): INTEGER;
|
|
|
|
RETURN ORD(a(IDENT).s > b(IDENT).s) - ORD(a(IDENT).s < b(IDENT).s)
|
|
|
|
END nodecmp;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE enterid* (s: LEXSTR): IDENT;
|
|
|
|
VAR
|
|
|
|
newnode: BOOLEAN;
|
|
|
|
node: AVL.NODE;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
BEGIN
|
2019-10-06 19:55:12 +02:00
|
|
|
NewIdent.s := s;
|
|
|
|
idents := AVL.insert(idents, NewIdent, nodecmp, newnode, node);
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
IF newnode THEN
|
2019-10-06 19:55:12 +02:00
|
|
|
NEW(NewIdent);
|
|
|
|
NewIdent.offset := -1;
|
|
|
|
NewIdent.offsetW := -1;
|
|
|
|
NewIdent.key := 0
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
2016-10-24 01:30:27 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
RETURN node.data(IDENT)
|
|
|
|
END enterid;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE putchar (VAR lex: LEX; c: CHAR);
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
IF lex.length < LEXLEN - 1 THEN
|
|
|
|
lex.s[lex.length] := c;
|
|
|
|
INC(lex.length);
|
|
|
|
lex.s[lex.length] := 0X
|
2016-10-24 01:30:27 +02:00
|
|
|
ELSE
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.over := TRUE
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
END putchar;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE nextc (text: TXT.TEXT): CHAR;
|
|
|
|
BEGIN
|
|
|
|
TXT.next(text)
|
|
|
|
RETURN text.peak
|
|
|
|
END nextc;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE ident (text: TXT.TEXT; VAR lex: LEX);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
2019-03-11 09:59:55 +01:00
|
|
|
ASSERT(S.letter(c));
|
|
|
|
|
|
|
|
WHILE S.letter(c) OR S.digit(c) DO
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
IF lex.over THEN
|
|
|
|
lex.sym := lxERROR06
|
2016-10-24 01:30:27 +02:00
|
|
|
ELSE
|
2019-10-06 19:55:12 +02:00
|
|
|
lex.ident := enterid(lex.s);
|
|
|
|
IF lex.ident.key # 0 THEN
|
|
|
|
lex.sym := lex.ident.key
|
|
|
|
ELSE
|
|
|
|
lex.sym := lxIDENT
|
|
|
|
END
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END ident;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE number (text: TXT.TEXT; VAR lex: LEX);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
|
|
|
hex: BOOLEAN;
|
|
|
|
error: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
2019-03-11 09:59:55 +01:00
|
|
|
ASSERT(S.digit(c));
|
|
|
|
|
|
|
|
error := 0;
|
|
|
|
|
|
|
|
lex.sym := lxINTEGER;
|
|
|
|
hex := FALSE;
|
|
|
|
|
|
|
|
WHILE S.digit(c) DO
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
WHILE S.hexdigit(c) DO
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
hex := TRUE
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = "H" THEN
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.sym := lxHEX
|
|
|
|
|
|
|
|
ELSIF c = "X" THEN
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.sym := lxCHAR
|
|
|
|
|
|
|
|
ELSIF c = "." THEN
|
|
|
|
|
|
|
|
IF hex THEN
|
|
|
|
lex.sym := lxERROR01
|
2016-10-24 01:30:27 +02:00
|
|
|
ELSE
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c # "." THEN
|
|
|
|
putchar(lex, ".");
|
|
|
|
lex.sym := lxFLOAT
|
|
|
|
ELSE
|
|
|
|
lex.sym := lxINTEGER;
|
2019-09-26 22:23:06 +02:00
|
|
|
text.peak := 7FX;
|
|
|
|
upto := TRUE
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
WHILE S.digit(c) DO
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
IF c = "E" THEN
|
|
|
|
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
IF (c = "+") OR (c = "-") THEN
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
IF S.digit(c) THEN
|
|
|
|
WHILE S.digit(c) DO
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
lex.sym := lxERROR02
|
|
|
|
END
|
|
|
|
|
|
|
|
END
|
|
|
|
|
|
|
|
END
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
ELSIF hex THEN
|
|
|
|
lex.sym := lxERROR01
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF lex.over & (lex.sym >= 0) THEN
|
|
|
|
lex.sym := lxERROR07
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF lex.sym = lxINTEGER THEN
|
|
|
|
ARITH.iconv(lex.s, lex.value, error)
|
|
|
|
ELSIF (lex.sym = lxHEX) OR (lex.sym = lxCHAR) THEN
|
|
|
|
ARITH.hconv(lex.s, lex.value, error)
|
|
|
|
ELSIF lex.sym = lxFLOAT THEN
|
|
|
|
ARITH.fconv(lex.s, lex.value, error)
|
|
|
|
END;
|
|
|
|
|
|
|
|
CASE error OF
|
|
|
|
|0:
|
|
|
|
|1: lex.sym := lxERROR08
|
|
|
|
|2: lex.sym := lxERROR09
|
|
|
|
|3: lex.sym := lxERROR10
|
|
|
|
|4: lex.sym := lxERROR11
|
|
|
|
|5: lex.sym := lxERROR12
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END number;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE string (text: TXT.TEXT; VAR lex: LEX; quot: CHAR);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
2019-09-26 22:23:06 +02:00
|
|
|
c: CHAR;
|
|
|
|
n: INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
|
|
|
n := 0;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
WHILE (c # quot) & (c # 0X) & ~text.eol & ~text.eof DO
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
INC(n)
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = quot THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
IF lex.over THEN
|
|
|
|
lex.sym := lxERROR05
|
|
|
|
ELSE
|
|
|
|
IF n # 1 THEN
|
|
|
|
lex.sym := lxSTRING
|
|
|
|
ELSE
|
|
|
|
lex.sym := lxCHAR;
|
2019-09-26 22:23:06 +02:00
|
|
|
ARITH.setChar(lex.value, ORD(lex.s[0]))
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
lex.sym := lxERROR03
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF lex.sym = lxSTRING THEN
|
|
|
|
lex.string := enterid(lex.s);
|
|
|
|
lex.value.typ := ARITH.tSTRING;
|
|
|
|
lex.value.string := lex.string
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END string;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE comment (text: TXT.TEXT);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
|
|
|
cond, depth: INTEGER;
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
cond := 0;
|
2019-03-11 09:59:55 +01:00
|
|
|
depth := 1;
|
|
|
|
|
|
|
|
REPEAT
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = "*" THEN
|
|
|
|
IF cond = 1 THEN
|
|
|
|
cond := 0;
|
|
|
|
INC(depth)
|
|
|
|
ELSE
|
|
|
|
cond := 2
|
|
|
|
END
|
|
|
|
ELSIF c = ")" THEN
|
|
|
|
IF cond = 2 THEN
|
|
|
|
DEC(depth)
|
|
|
|
END;
|
|
|
|
cond := 0
|
|
|
|
ELSIF c = "(" THEN
|
|
|
|
cond := 1
|
|
|
|
ELSE
|
|
|
|
cond := 0
|
|
|
|
END
|
|
|
|
|
|
|
|
UNTIL (depth = 0) OR text.eof
|
|
|
|
|
|
|
|
END comment;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE delimiter (text: TXT.TEXT; VAR lex: LEX; c: CHAR);
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
CASE lex.s[0] OF
|
2019-03-11 09:59:55 +01:00
|
|
|
|"+":
|
|
|
|
lex.sym := lxPLUS
|
|
|
|
|
|
|
|
|"-":
|
|
|
|
lex.sym := lxMINUS
|
|
|
|
|
|
|
|
|"*":
|
|
|
|
lex.sym := lxMUL
|
|
|
|
|
|
|
|
|"/":
|
|
|
|
lex.sym := lxSLASH;
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
IF c = "/" THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.sym := lxCOMMENT;
|
|
|
|
REPEAT
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
UNTIL text.eol OR text.eof
|
|
|
|
END
|
|
|
|
|
|
|
|
|"~":
|
|
|
|
lex.sym := lxNOT
|
|
|
|
|
|
|
|
|"&":
|
|
|
|
lex.sym := lxAND
|
|
|
|
|
|
|
|
|".":
|
2019-09-26 22:23:06 +02:00
|
|
|
lex.sym := lxPOINT;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
IF c = "." THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.sym := lxRANGE;
|
2019-09-26 22:23:06 +02:00
|
|
|
putchar(lex, c);
|
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
|
|
|
|
|",":
|
|
|
|
lex.sym := lxCOMMA
|
|
|
|
|
|
|
|
|";":
|
|
|
|
lex.sym := lxSEMI
|
|
|
|
|
|
|
|
|"|":
|
|
|
|
lex.sym := lxBAR
|
|
|
|
|
|
|
|
|"(":
|
|
|
|
lex.sym := lxLROUND;
|
|
|
|
|
|
|
|
IF c = "*" THEN
|
|
|
|
lex.sym := lxCOMMENT;
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
comment(text)
|
|
|
|
END
|
|
|
|
|
|
|
|
|"[":
|
|
|
|
lex.sym := lxLSQUARE
|
|
|
|
|
|
|
|
|"{":
|
|
|
|
lex.sym := lxLCURLY
|
|
|
|
|
|
|
|
|"^":
|
|
|
|
lex.sym := lxCARET
|
|
|
|
|
|
|
|
|"=":
|
|
|
|
lex.sym := lxEQ
|
|
|
|
|
|
|
|
|"#":
|
|
|
|
lex.sym := lxNE
|
|
|
|
|
|
|
|
|"<":
|
|
|
|
lex.sym := lxLT;
|
|
|
|
|
|
|
|
IF c = "=" THEN
|
|
|
|
lex.sym := lxLE;
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|">":
|
|
|
|
lex.sym := lxGT;
|
|
|
|
|
|
|
|
IF c = "=" THEN
|
|
|
|
lex.sym := lxGE;
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
|
|
|
|
|":":
|
|
|
|
lex.sym := lxCOLON;
|
|
|
|
|
|
|
|
IF c = "=" THEN
|
|
|
|
lex.sym := lxASSIGN;
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
|
|
|
|
|")":
|
|
|
|
lex.sym := lxRROUND
|
|
|
|
|
|
|
|
|"]":
|
|
|
|
lex.sym := lxRSQUARE
|
|
|
|
|
|
|
|
|"}":
|
|
|
|
lex.sym := lxRCURLY
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
END
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
END delimiter;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE Next* (text: SCANNER; VAR lex: LEX);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
REPEAT
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
WHILE S.space(c) DO
|
2019-09-26 22:23:06 +02:00
|
|
|
c := nextc(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
|
|
|
|
lex.s[0] := 0X;
|
|
|
|
lex.length := 0;
|
|
|
|
lex.pos.line := text.line;
|
|
|
|
lex.pos.col := text.col;
|
|
|
|
lex.ident := NIL;
|
|
|
|
lex.over := FALSE;
|
|
|
|
|
|
|
|
IF S.letter(c) THEN
|
|
|
|
ident(text, lex)
|
|
|
|
ELSIF S.digit(c) THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
number(text, lex)
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSIF (c = '"') OR (c = "'") THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
string(text, lex, c)
|
2019-10-06 19:55:12 +02:00
|
|
|
ELSIF delimiters[ORD(c)] THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
delimiter(text, lex, c)
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSIF c = 0X THEN
|
|
|
|
lex.sym := lxEOF;
|
|
|
|
IF text.eof THEN
|
|
|
|
INC(lex.pos.col)
|
|
|
|
END
|
2019-09-26 22:23:06 +02:00
|
|
|
ELSIF (c = 7FX) & upto THEN
|
|
|
|
upto := FALSE;
|
|
|
|
lex.sym := lxRANGE;
|
|
|
|
putchar(lex, ".");
|
|
|
|
putchar(lex, ".");
|
|
|
|
DEC(lex.pos.col);
|
|
|
|
TXT.next(text)
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
|
|
|
putchar(lex, c);
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.next(text);
|
2019-03-11 09:59:55 +01:00
|
|
|
lex.sym := lxERROR04
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF lex.sym < 0 THEN
|
|
|
|
lex.error := -lex.sym
|
|
|
|
ELSE
|
|
|
|
lex.error := 0
|
|
|
|
END
|
|
|
|
|
|
|
|
UNTIL lex.sym # lxCOMMENT
|
|
|
|
|
|
|
|
END Next;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE open* (name: ARRAY OF CHAR): SCANNER;
|
2019-09-26 22:23:06 +02:00
|
|
|
RETURN TXT.open(name)
|
2019-03-11 09:59:55 +01:00
|
|
|
END open;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE close* (VAR scanner: SCANNER);
|
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
TXT.close(scanner)
|
2019-03-11 09:59:55 +01:00
|
|
|
END close;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE init;
|
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
delim: ARRAY 23 OF CHAR;
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
PROCEDURE enterkw (key: INTEGER; kw: LEXSTR);
|
|
|
|
VAR
|
|
|
|
id: IDENT;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
2019-10-06 19:55:12 +02:00
|
|
|
id := enterid(kw);
|
|
|
|
id.key := key
|
2019-03-11 09:59:55 +01:00
|
|
|
END enterkw;
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
upto := FALSE;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
FOR i := 0 TO 255 DO
|
2019-10-06 19:55:12 +02:00
|
|
|
delimiters[i] := FALSE
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
delim := "+-*/~&.,;|([{^=#<>:)]}";
|
|
|
|
|
|
|
|
FOR i := 0 TO LEN(delim) - 2 DO
|
2019-10-06 19:55:12 +02:00
|
|
|
delimiters[ORD(delim[i])] := TRUE
|
2016-10-24 01:30:27 +02:00
|
|
|
END;
|
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
NEW(NewIdent);
|
|
|
|
NewIdent.s := "";
|
|
|
|
NewIdent.offset := -1;
|
|
|
|
NewIdent.offsetW := -1;
|
|
|
|
NewIdent.key := 0;
|
|
|
|
|
|
|
|
idents := NIL;
|
|
|
|
|
|
|
|
enterkw(lxARRAY, "ARRAY");
|
|
|
|
enterkw(lxBEGIN, "BEGIN");
|
|
|
|
enterkw(lxBY, "BY");
|
|
|
|
enterkw(lxCASE, "CASE");
|
|
|
|
enterkw(lxCONST, "CONST");
|
|
|
|
enterkw(lxDIV, "DIV");
|
|
|
|
enterkw(lxDO, "DO");
|
|
|
|
enterkw(lxELSE, "ELSE");
|
|
|
|
enterkw(lxELSIF, "ELSIF");
|
|
|
|
enterkw(lxEND, "END");
|
|
|
|
enterkw(lxFALSE, "FALSE");
|
|
|
|
enterkw(lxFOR, "FOR");
|
|
|
|
enterkw(lxIF, "IF");
|
|
|
|
enterkw(lxIMPORT, "IMPORT");
|
|
|
|
enterkw(lxIN, "IN");
|
|
|
|
enterkw(lxIS, "IS");
|
|
|
|
enterkw(lxMOD, "MOD");
|
|
|
|
enterkw(lxMODULE, "MODULE");
|
|
|
|
enterkw(lxNIL, "NIL");
|
|
|
|
enterkw(lxOF, "OF");
|
|
|
|
enterkw(lxOR, "OR");
|
|
|
|
enterkw(lxPOINTER, "POINTER");
|
|
|
|
enterkw(lxPROCEDURE, "PROCEDURE");
|
|
|
|
enterkw(lxRECORD, "RECORD");
|
|
|
|
enterkw(lxREPEAT, "REPEAT");
|
|
|
|
enterkw(lxRETURN, "RETURN");
|
|
|
|
enterkw(lxTHEN, "THEN");
|
|
|
|
enterkw(lxTO, "TO");
|
|
|
|
enterkw(lxTRUE, "TRUE");
|
|
|
|
enterkw(lxTYPE, "TYPE");
|
|
|
|
enterkw(lxUNTIL, "UNTIL");
|
|
|
|
enterkw(lxVAR, "VAR");
|
|
|
|
enterkw(lxWHILE, "WHILE")
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
END init;
|
|
|
|
|
2016-10-24 01:30:27 +02:00
|
|
|
|
|
|
|
BEGIN
|
2019-03-11 09:59:55 +01:00
|
|
|
init
|
2019-10-06 19:55:12 +02:00
|
|
|
END SCAN.
|