scannerc.frm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. IMPLEMENTATION MODULE -->modulename;
  2. (* Scanner generated by Coco/R - using the FileIO library supplied with this project. *)
  3. (* allows extraction of comments *)
  4. IMPORT FileIO, Storage, SYSTEM (* TSIZE only *);
  5. CONST
  6. noSYMB = -->unknownsym; (*error token code*)
  7. (* not only for errors but also for not finished states of scanner analysis *)
  8. eof = 32C (* MS-DOS Keyboard eof char *);
  9. EOF = 0C;
  10. EOL = 15C;
  11. CR = 15C;
  12. LF = 12C;
  13. Long0 = 0;
  14. Long1 = 1;
  15. BlkSize = 16384;
  16. TYPE
  17. BufBlock = ARRAY [0 .. BlkSize-1] OF CHAR;
  18. Buffer = ARRAY [0 .. 31] OF POINTER TO BufBlock;
  19. StartTable = ARRAY [0 .. 255] OF INTEGER;
  20. GetCH = PROCEDURE (INT32): CHAR;
  21. CommentPtr = POINTER TO CommentRec;
  22. CommentRec = RECORD
  23. begCom, endCom: INT32;
  24. next: CommentPtr;
  25. END;
  26. VAR
  27. lastCh,
  28. ch: CHAR; (*current input character*)
  29. curLine: INTEGER; (*current input line (may be higher than line)*)
  30. lineStart: INT32; (*start position of current line*)
  31. apx: INT32; (*length of appendix (CONTEXT phrase)*)
  32. oldEols: INTEGER; (*number of EOLs in a comment*)
  33. bp, bp0: INT32; (*current position in buf
  34. (bp0: position of current token)*)
  35. inputLen: INT32; (*source file size*)
  36. buf: Buffer; (*source buffer for low-level access*)
  37. start: StartTable; (*start state for every character*)
  38. CurrentCh: GetCH;
  39. firstComment, lastComment: CommentPtr;
  40. PROCEDURE Err (nr, line, col: INTEGER; pos: INT32);
  41. BEGIN
  42. INC(errors)
  43. END Err;
  44. PROCEDURE NextCh;
  45. (* Return global variable ch *)
  46. BEGIN
  47. lastCh := ch; INC(bp); ch := CurrentCh(bp);
  48. IF (ch = EOL) OR (ch = LF) AND (lastCh # EOL) THEN
  49. INC(curLine); lineStart := bp
  50. END
  51. END NextCh;
  52. PROCEDURE HaveComment (): BOOLEAN;
  53. VAR
  54. level, startLine: INTEGER;
  55. oldLineStart: INT32;
  56. BEGIN
  57. level := 1; startLine := curLine; oldLineStart := lineStart;
  58. -->commentRETURN FALSE;
  59. END HaveComment;
  60. PROCEDURE Comment (): BOOLEAN;
  61. VAR
  62. SeenComment: BOOLEAN;
  63. beginC, endC: INT32;
  64. nextComment: CommentPtr;
  65. BEGIN
  66. beginC := bp;
  67. SeenComment := HaveComment();
  68. endC := bp; DEC(endC);
  69. IF endC > beginC THEN
  70. seenComment := TRUE;
  71. Storage.ALLOCATE(nextComment, SYSTEM.TSIZE(CommentRec));
  72. nextComment^.begCom := beginC;
  73. nextComment^.endCom := endC;
  74. nextComment^.next := NIL;
  75. IF firstComment = NIL
  76. THEN firstComment := nextComment
  77. ELSE lastComment^.next := nextComment
  78. END;
  79. lastComment := nextComment;
  80. END;
  81. RETURN SeenComment;
  82. END Comment;
  83. PROCEDURE GetComment (VAR comment: ARRAY OF CHAR; pos: CARDINAL; VAR length: CARDINAL);
  84. VAR
  85. this: CommentPtr;
  86. BEGIN
  87. length := 0;
  88. WHILE firstComment # NIL DO
  89. this := firstComment;
  90. WHILE (pos + length <= HIGH(comment)) AND (this^.begCom <= this^.endCom) DO
  91. comment[pos + length] := CharAt(this^.begCom);
  92. INC(length); INC(this^.begCom);
  93. END;
  94. firstComment := firstComment^.next;
  95. Storage.DEALLOCATE(this, SYSTEM.TSIZE(CommentRec))
  96. END;
  97. seenComment := FALSE;
  98. END GetComment;
  99. PROCEDURE Get (VAR sym: CARDINAL);
  100. VAR
  101. state: CARDINAL;
  102. PROCEDURE Equal (s: ARRAY OF CHAR): BOOLEAN;
  103. VAR
  104. i: CARDINAL;
  105. q: INT32;
  106. BEGIN
  107. IF nextLen # LENGTH(s) THEN RETURN FALSE END;
  108. i := 1; q := bp0; INC(q);
  109. WHILE i < nextLen DO
  110. IF CurrentCh(q) # s[i] THEN RETURN FALSE END;
  111. INC(i); INC(q)
  112. END;
  113. RETURN TRUE
  114. END Equal;
  115. PROCEDURE CheckLiteral;
  116. BEGIN
  117. -->literals
  118. END CheckLiteral;
  119. BEGIN (*Get*)
  120. -->GetSy1
  121. pos := nextPos; nextPos := bp;
  122. col := nextCol; nextCol := VAL(INTEGER, bp - lineStart);
  123. line := nextLine; nextLine := curLine;
  124. len := nextLen; nextLen := 0;
  125. apx := 0; state := start[ORD(ch)]; bp0 := bp;
  126. LOOP
  127. NextCh; INC(nextLen);
  128. CASE state OF
  129. -->GetSy2
  130. ELSE sym := noSYMB; RETURN (*NextCh already done*)
  131. END
  132. END
  133. END Get;
  134. PROCEDURE GetString (pos: INT32; len: CARDINAL; VAR s: ARRAY OF CHAR);
  135. VAR
  136. i: CARDINAL;
  137. p: INT32;
  138. BEGIN
  139. IF len > HIGH(s) THEN len := HIGH(s) END;
  140. p := pos; i := 0;
  141. WHILE i < len DO
  142. s[i] := CharAt(p); INC(i); INC(p)
  143. END;
  144. s[len] := 0C;
  145. END GetString;
  146. PROCEDURE GetName (pos: INT32; len: CARDINAL; VAR s: ARRAY OF CHAR);
  147. VAR
  148. i: CARDINAL;
  149. p: INT32;
  150. BEGIN
  151. IF len > HIGH(s) THEN len := HIGH(s) END;
  152. p := pos; i := 0;
  153. WHILE i < len DO
  154. s[i] := CurrentCh(p); INC(i); INC(p)
  155. END;
  156. s[len] := 0C;
  157. END GetName;
  158. PROCEDURE CharAt (pos: INT32): CHAR;
  159. VAR
  160. ch: CHAR;
  161. BEGIN
  162. IF pos >= inputLen THEN RETURN EOF END;
  163. ch := buf[ORD(pos DIV BlkSize)]^[ORD(pos MOD BlkSize)];
  164. IF ch # eof THEN RETURN ch ELSE RETURN EOF END
  165. END CharAt;
  166. PROCEDURE CapChAt (pos: INT32): CHAR;
  167. VAR
  168. ch: CHAR;
  169. BEGIN
  170. IF pos >= inputLen THEN RETURN EOF END;
  171. ch := CAP(buf[ORD(pos DIV BlkSize)]^[ORD(pos MOD BlkSize)]);
  172. IF ch # eof THEN RETURN ch ELSE RETURN EOF END
  173. END CapChAt;
  174. PROCEDURE Reset;
  175. VAR
  176. i, read: CARDINAL;
  177. BEGIN (*assert: src has been opened*)
  178. i := 0; inputLen := 0;
  179. REPEAT
  180. Storage.ALLOCATE(buf[i], BlkSize);
  181. read := BlkSize; FileIO.ReadBytes(src, buf[i]^, read);
  182. INC(i); INC(inputLen, VAL(INT32, read))
  183. UNTIL read # BlkSize;
  184. buf[i-1]^[read] := EOF;
  185. curLine := 1; lineStart := -2; bp := -1;
  186. oldEols := 0; apx := 0; errors := 0;
  187. NextCh;
  188. END Reset;
  189. BEGIN
  190. -->initializations
  191. Error := Err; lastCh := EOF;
  192. firstComment := NIL; seenComment := FALSE;
  193. END -->modulename.
  194. -->definitionDEFINITION MODULE -->modulename;
  195. (* Scanner generated by Coco/R - using the FileIO library supplied with this project. *)
  196. IMPORT FileIO;
  197. TYPE
  198. INT32 = FileIO.INT32 (* need 32 bit integers *);
  199. VAR
  200. src, lst: FileIO.File;(*source/list files. To be opened by the main pgm*)
  201. directory: ARRAY [0 .. 255] OF CHAR (*of source file*);
  202. line, col: INTEGER; (*line and column of current symbol*)
  203. len: CARDINAL; (*length of current symbol*)
  204. pos: INT32; (*file position of current symbol*)
  205. nextLine: INTEGER; (*line of lookahead symbol*)
  206. nextCol: INTEGER; (*column of lookahead symbol*)
  207. nextLen: CARDINAL; (*length of lookahead symbol*)
  208. nextPos: INT32; (*file position of lookahead symbol*)
  209. errors: INTEGER; (*number of detected errors*)
  210. seenComment: BOOLEAN; (*TRUE if comments have been registered*)
  211. Error: PROCEDURE ((*nr*)INTEGER, (*line*)INTEGER, (*col*)INTEGER,
  212. (*pos*)INT32);
  213. PROCEDURE Get (VAR sym: CARDINAL);
  214. (* Gets next symbol from source file *)
  215. PROCEDURE GetString (pos: INT32; len: CARDINAL; VAR name: ARRAY OF CHAR);
  216. (* Retrieves exact string of max length len from position pos in source file *)
  217. PROCEDURE GetName (pos: INT32; len: CARDINAL; VAR name: ARRAY OF CHAR);
  218. (* Retrieves name of symbol of length len at position pos in source file *)
  219. PROCEDURE CharAt (pos: INT32): CHAR;
  220. (* Returns exact character at position pos in source file *)
  221. PROCEDURE GetComment (VAR comment: ARRAY OF CHAR; pos: CARDINAL; VAR length: CARDINAL);
  222. (* IF seenComment
  223. THEN concatenates and extracts previously scanned comments into comment,
  224. starting at comment[pos], and computes length; seenComment := FALSE
  225. ELSE returns length := 0 *)
  226. PROCEDURE Reset;
  227. (* Reads and stores source file internally *)
  228. END -->modulename.