lexrules.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. {
  2. Parser for Lex grammar rules.
  3. This module implements a parser for Lex grammar rules. It should
  4. probably be reimplemented using Lex and Yacc, but the irregular
  5. lexical structure of the Lex language makes that rather tedious,
  6. so I decided to use a conventional recursive-descent-parser
  7. instead.
  8. Copyright (c) 1990-92 Albert Graef <[email protected]>
  9. Copyright (C) 1996 Berend de Boer <[email protected]>
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. $Revision$
  22. $Modtime: 96-08-01 6:30 $
  23. $History: LEXRULES.PAS $
  24. *
  25. * ***************** Version 2 *****************
  26. * User: Berend Date: 96-10-10 Time: 21:16
  27. * Updated in $/Lex and Yacc/tply
  28. * Updated for protected mode, windows and Delphi 1.X and 2.X.
  29. }
  30. unit LexRules;
  31. interface
  32. uses LexBase, LexTable;
  33. procedure parse_rule ( rule_no : Integer );
  34. (* rule parser (rule_no=number of parsed rule) *)
  35. (* Return values of rule parser: *)
  36. var
  37. expr, stmt : String;
  38. (* expression and statement part of rule *)
  39. cf : Boolean;
  40. (* caret flag *)
  41. n_st : Integer;
  42. (* number of start states in prefix *)
  43. st : array [1..max_states] of Integer;
  44. (* start states *)
  45. r : RegExpr;
  46. (* parsed expression *)
  47. implementation
  48. uses LexMsgs;
  49. (* Scanner routines:
  50. The following routines provide access to the source line and handle
  51. macro substitutions. To perform macro substitution, an input buffer
  52. is maintained which contains the rest of the line to be parsed, plus
  53. any pending macro substitutions. The input buffer is organized as
  54. a stack onto which null-terminated replacement strings are pushed
  55. as macro substitutions are processed (the terminating null-character
  56. is used as an endmarker for macros, in order to keep track of the
  57. number of pending macro substitutions); characters are popped from the
  58. stack via calls to the get_char routine.
  59. In order to perform macro substitution, the scanner also has to
  60. maintain some state information to be able to determine when it
  61. is scanning quoted characters, strings or character classes (s.t.
  62. no macro substitution is performed in such cases).
  63. The scanner also keeps track of the current source line position in
  64. variable act_pos; if there are any macro substitutions on the stack,
  65. act_pos will point to the position of the original macro call in the
  66. source line. This is needed to give proper error diagnostics. *)
  67. const max_chars = 2048;
  68. var
  69. act_pos, bufptr : Integer;
  70. (* current position in source line and input stack pointer *)
  71. buf : array [1..max_chars] of Char;
  72. (* input buffer *)
  73. str_state, cclass_state, quote_state : Boolean;
  74. (* state information *)
  75. n_macros : Integer;
  76. (* number of macros currently on stack *)
  77. procedure mark_error ( msg : String; offset : Integer );
  78. (* mark error position (offset=offset of error position (to the left of
  79. act_pos) *)
  80. begin
  81. if n_macros=0 then
  82. error(msg, act_pos-offset)
  83. else
  84. error(msg+' in regular definition', act_pos)
  85. end(*mark_error*);
  86. procedure put_str(str : String);
  87. (* push str onto input stack *)
  88. var i : Integer;
  89. begin
  90. inc(bufptr, length(str));
  91. if bufptr>max_chars then fatal(macro_stack_overflow);
  92. for i := 1 to length(str) do
  93. buf[bufptr-i+1] := str[i];
  94. end(*put_str*);
  95. procedure init_scanner;
  96. (* initialize the scanner *)
  97. begin
  98. act_pos := 1; bufptr := 0;
  99. str_state := false; cclass_state := false; quote_state := false;
  100. n_macros := 0;
  101. put_str(line);
  102. end(*init_scanner*);
  103. function act_char : Char;
  104. (* current character (#0 if none) *)
  105. function push_macro : Boolean;
  106. (* check for macro call at current position in input buffer *)
  107. function scan_macro ( var name : String ) : Boolean;
  108. var i : Integer;
  109. begin
  110. if (bufptr>1) and
  111. (buf[bufptr]='{') and (buf[bufptr-1] in letters) then
  112. begin
  113. name := '{'; i := bufptr-1;
  114. while (i>0) and (buf[i] in alphanums) do
  115. begin
  116. name := name+buf[i];
  117. dec(i);
  118. end;
  119. if (i>0) and (buf[i]='}') then
  120. begin
  121. scan_macro := true;
  122. name := name+'}';
  123. bufptr := i-1;
  124. end
  125. else
  126. begin
  127. scan_macro := false;
  128. mark_error(syntax_error, -length(name));
  129. bufptr := i;
  130. end
  131. end
  132. else
  133. scan_macro := false
  134. end(*scan_macro*);
  135. var name : String;
  136. begin
  137. if scan_macro(name) then
  138. begin
  139. push_macro := true;
  140. {$ifdef fpc}
  141. with sym_table^[key(name, max_keys, @lookup, @entry)] do
  142. {$else}
  143. with sym_table^[key(name, max_keys, lookup, entry)] do
  144. {$endif}
  145. if sym_type=macro_sym then
  146. begin
  147. put_str(subst^+#0);
  148. inc(n_macros);
  149. end
  150. else
  151. mark_error(undefined_symbol, -1)
  152. end
  153. else
  154. push_macro := false
  155. end(*push_macro*);
  156. function pop_macro : Boolean;
  157. (* check for macro endmarker *)
  158. begin
  159. if (bufptr>0) and (buf[bufptr]=#0) then
  160. begin
  161. dec(bufptr);
  162. dec(n_macros);
  163. if n_macros=0 then act_pos := length(line)-bufptr+1;
  164. pop_macro := true;
  165. end
  166. else
  167. pop_macro := false
  168. end(*pop_macro*);
  169. begin
  170. if not (str_state or cclass_state or quote_state) then
  171. while push_macro do while pop_macro do ;
  172. if bufptr=0 then
  173. act_char := #0
  174. else
  175. begin
  176. while pop_macro do ;
  177. act_char := buf[bufptr];
  178. end
  179. end(*act_char*);
  180. procedure get_char;
  181. (* get next character *)
  182. begin
  183. if bufptr>0 then
  184. begin
  185. case buf[bufptr] of
  186. '\' : quote_state := not quote_state;
  187. '"' : if quote_state then
  188. quote_state := false
  189. else if not cclass_state then
  190. str_state := not str_state;
  191. '[' : if quote_state then
  192. quote_state := false
  193. else if not str_state then
  194. cclass_state := true;
  195. ']' : if quote_state then
  196. quote_state := false
  197. else if not str_state then
  198. cclass_state := false;
  199. else quote_state := false;
  200. end;
  201. dec(bufptr);
  202. if n_macros=0 then
  203. act_pos := length(line)-bufptr+1;
  204. end
  205. end(*get_char*);
  206. (* Semantic routines: *)
  207. procedure add_start_state ( symbol : String );
  208. (* add start state to st array *)
  209. begin
  210. {$ifdef fpc}
  211. with sym_table^[key(symbol, max_keys, @lookup, @entry)] do
  212. {$else}
  213. with sym_table^[key(symbol, max_keys, lookup, entry)] do
  214. {$endif}
  215. if sym_type=start_state_sym then
  216. begin
  217. if n_st>=max_start_states then exit; { this shouldn't happen }
  218. inc(n_st);
  219. st[n_st] := start_state;
  220. end
  221. else
  222. mark_error(undefined_symbol, length(symbol))
  223. end(*add_start_state*);
  224. (* Parser: *)
  225. procedure parse_rule ( rule_no : Integer );
  226. procedure rule ( var done : Boolean );
  227. (* parse rule according to syntax:
  228. rule : start_state_prefix caret
  229. expr [ '$' | '/' expr ]
  230. ;
  231. start_state_prefix : /* empty */
  232. | '<' start_state_list '>'
  233. ;
  234. start_state_list : ident { ',' ident }
  235. ;
  236. caret : /* empty */
  237. | '^'
  238. ;
  239. expr : term { '|' term }
  240. ;
  241. term : factor { factor }
  242. ;
  243. factor : char
  244. | string
  245. | cclass
  246. | '.'
  247. | '(' expr ')'
  248. | factor '*'
  249. | factor '+'
  250. | factor '?'
  251. | factor '{' num [ ',' num ] '}'
  252. ;
  253. *)
  254. procedure start_state_prefix ( var done : Boolean );
  255. procedure start_state_list ( var done : Boolean );
  256. procedure ident ( var done : Boolean );
  257. var idstr : String;
  258. begin(*ident*)
  259. done := act_char in letters; if not done then exit;
  260. idstr := act_char;
  261. get_char;
  262. while act_char in alphanums do
  263. begin
  264. idstr := idstr+act_char;
  265. get_char;
  266. end;
  267. add_start_state(idstr);
  268. end(*ident*);
  269. begin(*start_state_list*)
  270. ident(done); if not done then exit;
  271. while act_char=',' do
  272. begin
  273. get_char;
  274. ident(done); if not done then exit;
  275. end;
  276. end(*start_state_list*);
  277. begin(*start_state_prefix*)
  278. n_st := 0;
  279. if act_char='<' then
  280. begin
  281. get_char;
  282. start_state_list(done); if not done then exit;
  283. if act_char='>' then
  284. begin
  285. done := true;
  286. get_char;
  287. end
  288. else
  289. done := false
  290. end
  291. else
  292. done := true
  293. end(*start_state_prefix*);
  294. procedure caret( var done : Boolean );
  295. begin(*caret*)
  296. done := true;
  297. cf := act_char='^';
  298. if act_char='^' then get_char;
  299. end(*caret*);
  300. procedure scan_char ( var done : Boolean; var c : Char );
  301. var
  302. oct_val : Byte;
  303. count : Integer;
  304. begin
  305. done := true;
  306. if act_char='\' then
  307. begin
  308. get_char;
  309. case act_char of
  310. #0 : done := false;
  311. 'n' : begin
  312. c := nl;
  313. get_char
  314. end;
  315. 'r' : begin
  316. c := cr;
  317. get_char
  318. end;
  319. 't' : begin
  320. c := tab;
  321. get_char
  322. end;
  323. 'b' : begin
  324. c := bs;
  325. get_char
  326. end;
  327. 'f' : begin
  328. c := ff;
  329. get_char
  330. end;
  331. '0'..'7' : begin
  332. oct_val := ord(act_char)-ord('0');
  333. get_char;
  334. count := 1;
  335. while ('0'<=act_char) and
  336. (act_char<='7') and
  337. (count<3) do
  338. begin
  339. inc(count);
  340. oct_val := oct_val*8+ord(act_char)-ord('0');
  341. get_char
  342. end;
  343. c := chr(oct_val);
  344. end
  345. else begin
  346. c := act_char;
  347. get_char
  348. end
  349. end
  350. end
  351. else
  352. begin
  353. c := act_char;
  354. get_char
  355. end
  356. end(*scan_char*);
  357. procedure scan_str ( var done : Boolean; var str : String );
  358. var c : Char;
  359. begin
  360. str := '';
  361. get_char;
  362. while (act_char<>#0) and (act_char<>'"') do
  363. begin
  364. scan_char(done, c); if not done then exit;
  365. str := str+c;
  366. end;
  367. if act_char=#0 then
  368. done := false
  369. else
  370. begin
  371. get_char;
  372. done := true;
  373. end
  374. end(*scan_str*);
  375. procedure scan_cclass( var done : Boolean; var cc : CClass );
  376. (* scan a character class *)
  377. var
  378. caret : boolean;
  379. c, c1,cl : Char;
  380. begin
  381. cc := [];
  382. get_char;
  383. if act_char='^' then
  384. begin
  385. caret := true;
  386. get_char;
  387. end
  388. else
  389. caret := false;
  390. while (act_char<>#0) and (act_char<>']') do
  391. begin
  392. scan_char(done, c); if not done then exit;
  393. if act_char='-' then
  394. begin
  395. get_char;
  396. if (act_char<>#0) and (act_char<>']') then
  397. begin
  398. scan_char(done, c1); if not done then exit;
  399. for cl:=c to c1 do
  400. cc:=cc+[cl];
  401. {cc := cc+[c..c1];}
  402. end
  403. else
  404. cc := cc+[c,'-'];
  405. end
  406. else
  407. cc := cc+[c];
  408. end;
  409. if act_char=#0 then
  410. done := false
  411. else
  412. begin
  413. get_char;
  414. done := true;
  415. end;
  416. if caret then cc := [#1..#255]-cc;
  417. end(*scan_cclass*);
  418. procedure scan_num( var done : Boolean; var n : Integer );
  419. var str : String;
  420. begin
  421. if act_char in digits then
  422. begin
  423. str := act_char;
  424. get_char;
  425. while act_char in digits do
  426. begin
  427. str := str+act_char;
  428. get_char;
  429. end;
  430. done := isInt(str, n);
  431. end
  432. else
  433. done := false
  434. end(*scan_num*);
  435. procedure DoExpr ( var done : Boolean; var r : RegExpr );
  436. procedure term ( var done : Boolean; var r : RegExpr );
  437. procedure factor ( var done : Boolean; var r : RegExpr );
  438. var str : String;
  439. cc : CClass;
  440. c : Char;
  441. n, m : Integer;
  442. begin(*factor*)
  443. case act_char of
  444. '"' : begin
  445. scan_str(done, str); if not done then exit;
  446. r := strExpr(newStr(str));
  447. end;
  448. '[' : begin
  449. scan_cclass(done, cc); if not done then exit;
  450. r := cclassExpr(newCClass(cc));
  451. end;
  452. '.' : begin
  453. get_char;
  454. r := cclassExpr(newCClass([#1..#255]-[nl]));
  455. done := true;
  456. end;
  457. '(' : begin
  458. get_char;
  459. DoExpr(done, r); if not done then exit;
  460. if act_char=')' then
  461. begin
  462. get_char;
  463. done := true;
  464. end
  465. else
  466. done := false
  467. end;
  468. else begin
  469. scan_char(done, c); if not done then exit;
  470. r := charExpr(c);
  471. end;
  472. end;
  473. while done and (act_char in ['*','+','?','{']) do
  474. case act_char of
  475. '*' : begin
  476. get_char;
  477. r := starExpr(r);
  478. end;
  479. '+' : begin
  480. get_char;
  481. r := plusExpr(r);
  482. end;
  483. '?' : begin
  484. get_char;
  485. r := optExpr(r);
  486. end;
  487. '{' : begin
  488. get_char;
  489. scan_num(done, m); if not done then exit;
  490. if act_char=',' then
  491. begin
  492. get_char;
  493. scan_num(done, n); if not done then exit;
  494. r := mnExpr(r, m, n);
  495. end
  496. else
  497. r := mnExpr(r, m, m);
  498. if act_char='}' then
  499. begin
  500. get_char;
  501. done := true;
  502. end
  503. else
  504. done := false
  505. end;
  506. end
  507. end(*factor*);
  508. const term_delim : CClass = [#0,' ',tab,'$','|',')','/'];
  509. var r1 : RegExpr;
  510. begin(*term*)
  511. if not (act_char in term_delim) then
  512. begin
  513. factor(done, r); if not done then exit;
  514. while not (act_char in term_delim) do
  515. begin
  516. factor(done, r1); if not done then exit;
  517. r := catExpr(r, r1);
  518. end
  519. end
  520. else
  521. begin
  522. r := epsExpr;
  523. done := true;
  524. end
  525. end(*term*);
  526. var r1 : RegExpr;
  527. begin(*expr*)
  528. term(done, r); if not done then exit;
  529. while act_char='|' do
  530. begin
  531. get_char;
  532. term(done, r1); if not done then exit;
  533. r := altExpr(r, r1);
  534. end
  535. end(*expr*);
  536. var r1, r2 : RegExpr;
  537. begin(*rule*)
  538. start_state_prefix(done); if not done then exit;
  539. caret(done); if not done then exit;
  540. DoExpr(done, r1); if not done then exit;
  541. if act_char='$' then
  542. begin
  543. r := catExpr(catExpr(r1,
  544. markExpr(rule_no, 1)),
  545. cclassExpr(newCClass([nl])));
  546. get_char;
  547. end
  548. else if act_char='/' then
  549. begin
  550. get_char;
  551. DoExpr(done, r2); if not done then exit;
  552. r := catExpr(catExpr(r1,
  553. markExpr(rule_no, 1)), r2);
  554. end
  555. else
  556. r := catExpr(r1, markExpr(rule_no, 1));
  557. r := catExpr(r, markExpr(rule_no, 0));
  558. done := (act_char=#0) or (act_char=' ') or (act_char=tab);
  559. end(*rule*);
  560. var done : Boolean;
  561. begin(*parse_rule*)
  562. init_scanner;
  563. rule(done);
  564. if done then
  565. begin
  566. expr := copy(line, 1, act_pos-1);
  567. stmt := copy(line, act_pos, length(line));
  568. end
  569. else
  570. mark_error(syntax_error, 0)
  571. end(*parse_rule*);
  572. end(*LexRules*).