h2plexlib.pas 11 KB

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