lexlib.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. {$I-}
  2. unit LexLib;
  3. (* Standard Lex library unit for TP Lex Version 3.0.
  4. 2-11-91 AG *)
  5. interface
  6. (* The Lex library unit supplies a collection of variables and routines
  7. needed by the lexical analyzer routine yylex and application programs
  8. using Lex-generated lexical analyzers. It also provides access to the
  9. input/output streams used by the lexical analyzer and the text of the
  10. matched string, and provides some utility functions which may be used
  11. in actions.
  12. This `standard' version of the LexLib unit is used to implement lexical
  13. analyzers which read from and write to MS-DOS files (using standard input
  14. and output, by default). It is suitable for many standard applications
  15. for lexical analyzers, such as text conversion tools or compilers.
  16. However, you may create your own version of the LexLib unit, tailored to
  17. your target applications. In particular, you may wish to provide another
  18. set of I/O functions, e.g., if you want to read from or write to memory
  19. instead to files, or want to use different file types. *)
  20. (* Variables:
  21. The variable yytext contains the current match, yyleng its length.
  22. The variable yyline contains the current input line, and yylineno and
  23. yycolno denote the current input position (line, column). These values
  24. are often used in giving error diagnostics (however, they will only be
  25. meaningful if there is no rescanning across line ends).
  26. The variables yyinput and yyoutput are the text files which are used
  27. by the lexical analyzer. By default, they are assigned to standard
  28. input and output, but you may change these assignments to fit your
  29. target application (use the Turbo Pascal standard routines assign,
  30. reset, and rewrite for this purpose). *)
  31. var
  32. yyinput, yyoutput : Text; (* input and output file *)
  33. yyline : String; (* current input line *)
  34. yylineno, yycolno : Integer; (* current input position *)
  35. yytext : String; (* matched text (should be considered r/o) *)
  36. yyleng : Byte (* length of matched text *)
  37. absolute yytext;
  38. (* I/O routines:
  39. The following routines get_char, unget_char and put_char are used to
  40. implement access to the input and output files. Since \n (newline) for
  41. Lex means line end, the I/O routines have to translate MS-DOS line ends
  42. (carriage-return/line-feed) into newline characters and vice versa. Input
  43. is buffered to allow rescanning text (via unput_char).
  44. The input buffer holds the text of the line to be scanned. When the input
  45. buffer empties, a new line is obtained from the input stream. Characters
  46. can be returned to the input buffer by calls to unget_char. At end-of-
  47. file a null character is returned.
  48. The input routines also keep track of the input position and set the
  49. yyline, yylineno, yycolno variables accordingly.
  50. Since the rest of the Lex library only depends on these three routines
  51. (there are no direct references to the yyinput and yyoutput files or
  52. to the input buffer), you can easily replace get_char, unget_char and
  53. put_char by another suitable set of routines, e.g. if you want to read
  54. from/write to memory, etc. *)
  55. function get_char : Char;
  56. (* obtain one character from the input file (null character at end-of-
  57. file) *)
  58. procedure unget_char ( c : Char );
  59. (* return one character to the input file to be reread in subsequent calls
  60. to get_char *)
  61. procedure put_char ( c : Char );
  62. (* write one character to the output file *)
  63. (* Utility routines: *)
  64. procedure echo;
  65. (* echoes the current match to the output stream *)
  66. procedure yymore;
  67. (* append the next match to the current one *)
  68. procedure yyless ( n : Integer );
  69. (* truncate yytext to size n and return the remaining characters to the
  70. input stream *)
  71. procedure reject;
  72. (* reject the current match and execute the next one *)
  73. (* reject does not actually cause the input to be rescanned; instead,
  74. internal state information is used to find the next match. Hence
  75. you should not try to modify the input stream or the yytext variable
  76. when rejecting a match. *)
  77. procedure return ( n : Integer );
  78. procedure returnc ( c : Char );
  79. (* sets the return value of yylex *)
  80. procedure start ( state : Integer );
  81. (* puts the lexical analyzer in the given start state; state=0 denotes
  82. the default start state, other values are user-defined *)
  83. (* yywrap:
  84. The yywrap function is called by yylex at end-of-file (unless you have
  85. specified a rule matching end-of-file). You may redefine this routine
  86. in your Lex program to do application-dependent processing at end of
  87. file. In particular, yywrap may arrange for more input and return false
  88. in which case the yylex routine resumes lexical analysis. *)
  89. type
  90. yywrap_t = function (): Boolean;
  91. var
  92. yywrap: yywrap_t;
  93. (* The default yywrap routine supplied here closes input and output files
  94. and returns true (causing yylex to terminate). *)
  95. (* The following are the internal data structures and routines used by the
  96. lexical analyzer routine yylex; they should not be used directly. *)
  97. var
  98. yystate : Integer; (* current state of lexical analyzer *)
  99. yyactchar : Char; (* current character *)
  100. yylastchar : Char; (* last matched character (#0 if none) *)
  101. yyrule : Integer; (* matched rule *)
  102. yyreject : Boolean; (* current match rejected? *)
  103. yydone : Boolean; (* yylex return value set? *)
  104. yyretval : Integer; (* yylex return value *)
  105. procedure yynew;
  106. (* starts next match; initializes state information of the lexical
  107. analyzer *)
  108. procedure yyscan;
  109. (* gets next character from the input stream and updates yytext and
  110. yyactchar accordingly *)
  111. procedure yymark ( n : Integer );
  112. (* marks position for rule no. n *)
  113. procedure yymatch ( n : Integer );
  114. (* declares a match for rule number n *)
  115. function yyfind ( var n : Integer ) : Boolean;
  116. (* finds the last match and the corresponding marked position and adjusts
  117. the matched string accordingly; returns:
  118. - true if a rule has been matched, false otherwise
  119. - n: the number of the matched rule *)
  120. function yydefault : Boolean;
  121. (* executes the default action (copy character); returns true unless
  122. at end-of-file *)
  123. procedure yyclear;
  124. (* reinitializes state information after lexical analysis has been
  125. finished *)
  126. implementation
  127. procedure fatal ( msg : String );
  128. (* writes a fatal error message and halts program *)
  129. begin
  130. writeln('LexLib: ', msg);
  131. halt(1);
  132. end(*fatal*);
  133. (* I/O routines: *)
  134. const nl = #10; (* newline character *)
  135. const max_chars = 2048;
  136. var
  137. bufptr : Integer;
  138. buf : array [1..max_chars] of Char;
  139. function get_char : Char;
  140. var i : Integer;
  141. begin
  142. if (bufptr=0) and not eof(yyinput) then
  143. begin
  144. readln(yyinput, yyline);
  145. inc(yylineno); yycolno := 1;
  146. buf[1] := nl;
  147. for i := 1 to length(yyline) do
  148. buf[i+1] := yyline[length(yyline)-i+1];
  149. inc(bufptr, length(yyline)+1);
  150. end;
  151. if bufptr>0 then
  152. begin
  153. get_char := buf[bufptr];
  154. dec(bufptr);
  155. inc(yycolno);
  156. end
  157. else
  158. get_char := #0;
  159. end(*get_char*);
  160. procedure unget_char ( c : Char );
  161. begin
  162. if bufptr=max_chars then fatal('input buffer overflow');
  163. inc(bufptr);
  164. dec(yycolno);
  165. buf[bufptr] := c;
  166. end(*unget_char*);
  167. procedure put_char ( c : Char );
  168. begin
  169. if c=#0 then
  170. { ignore }
  171. else if c=nl then
  172. writeln(yyoutput)
  173. else
  174. write(yyoutput, c)
  175. end(*put_char*);
  176. (* Variables:
  177. Some state information is maintained to keep track with calls to yymore,
  178. yyless, reject, start and yymatch/yymark, and to initialize state
  179. information used by the lexical analyzer.
  180. - yystext: contains the initial contents of the yytext variable; this
  181. will be the empty string, unless yymore is called which sets yystext
  182. to the current yytext
  183. - yysstate: start state of lexical analyzer (set to 0 during
  184. initialization, and modified in calls to the start routine)
  185. - yylstate: line state information (1 if at beginning of line, 0
  186. otherwise)
  187. - yystack: stack containing matched rules; yymatches contains the number of
  188. matches
  189. - yypos: for each rule the last marked position (yymark); zeroed when rule
  190. has already been considered
  191. - yysleng: copy of the original yyleng used to restore state information
  192. when reject is used *)
  193. const
  194. max_matches = 1024;
  195. max_rules = 256;
  196. var
  197. yystext : String;
  198. yysstate, yylstate : Integer;
  199. yymatches : Integer;
  200. yystack : array [1..max_matches] of Integer;
  201. yypos : array [1..max_rules] of Integer;
  202. yysleng : Byte;
  203. (* Utilities: *)
  204. procedure echo;
  205. var i : Integer;
  206. begin
  207. for i := 1 to yyleng do
  208. put_char(yytext[i])
  209. end(*echo*);
  210. procedure yymore;
  211. begin
  212. yystext := yytext;
  213. end(*yymore*);
  214. procedure yyless ( n : Integer );
  215. var i : Integer;
  216. begin
  217. for i := yyleng downto n+1 do
  218. unget_char(yytext[i]);
  219. yyleng := n;
  220. end(*yyless*);
  221. procedure reject;
  222. var i : Integer;
  223. begin
  224. yyreject := true;
  225. for i := yyleng+1 to yysleng do
  226. yytext := yytext+get_char;
  227. dec(yymatches);
  228. end(*reject*);
  229. procedure return ( n : Integer );
  230. begin
  231. yyretval := n;
  232. yydone := true;
  233. end(*return*);
  234. procedure returnc ( c : Char );
  235. begin
  236. yyretval := ord(c);
  237. yydone := true;
  238. end(*returnc*);
  239. procedure start ( state : Integer );
  240. begin
  241. yysstate := state;
  242. end(*start*);
  243. (* yywrap: *)
  244. function lexlib_yywrap : Boolean;
  245. begin
  246. close(yyinput);
  247. close(yyoutput);
  248. lexlib_yywrap := true;
  249. end(*yywrap*);
  250. (* Internal routines: *)
  251. procedure yynew;
  252. begin
  253. if yylastchar<>#0 then
  254. if yylastchar=nl then
  255. yylstate := 1
  256. else
  257. yylstate := 0;
  258. yystate := yysstate+yylstate;
  259. yytext := yystext;
  260. yystext := '';
  261. yymatches := 0;
  262. yydone := false;
  263. end(*yynew*);
  264. procedure yyscan;
  265. begin
  266. if yyleng=255 then fatal('yytext overflow');
  267. yyactchar := get_char;
  268. inc(yyleng);
  269. yytext[yyleng] := yyactchar;
  270. end(*yyscan*);
  271. procedure yymark ( n : Integer );
  272. begin
  273. if n>max_rules then fatal('too many rules');
  274. yypos[n] := yyleng;
  275. end(*yymark*);
  276. procedure yymatch ( n : Integer );
  277. begin
  278. inc(yymatches);
  279. if yymatches>max_matches then fatal('match stack overflow');
  280. yystack[yymatches] := n;
  281. end(*yymatch*);
  282. function yyfind ( var n : Integer ) : Boolean;
  283. begin
  284. yyreject := false;
  285. while (yymatches>0) and (yypos[yystack[yymatches]]=0) do
  286. dec(yymatches);
  287. if yymatches>0 then
  288. begin
  289. yysleng := yyleng;
  290. n := yystack[yymatches];
  291. yyless(yypos[n]);
  292. yypos[n] := 0;
  293. if yyleng>0 then
  294. yylastchar := yytext[yyleng]
  295. else
  296. yylastchar := #0;
  297. yyfind := true;
  298. end
  299. else
  300. begin
  301. yyless(0);
  302. yylastchar := #0;
  303. yyfind := false;
  304. end
  305. end(*yyfind*);
  306. function yydefault : Boolean;
  307. begin
  308. yyreject := false;
  309. yyactchar := get_char;
  310. if yyactchar<>#0 then
  311. begin
  312. put_char(yyactchar);
  313. yydefault := true;
  314. end
  315. else
  316. begin
  317. yylstate := 1;
  318. yydefault := false;
  319. end;
  320. yylastchar := yyactchar;
  321. end(*yydefault*);
  322. procedure yyclear;
  323. begin
  324. bufptr := 0;
  325. yysstate := 0;
  326. yylstate := 1;
  327. yylastchar := #0;
  328. yytext := '';
  329. yystext := '';
  330. end(*yyclear*);
  331. begin
  332. yywrap := @lexlib_yywrap;
  333. assign(yyinput, '');
  334. assign(yyoutput, '');
  335. reset(yyinput); rewrite(yyoutput);
  336. yylineno := 0;
  337. yyclear;
  338. end(*LexLib*).