scan.l 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. %{
  2. {
  3. $Id$
  4. Copyright (c) 1998-2000 by Florian Klaempfl
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. unit scan;
  18. interface
  19. uses
  20. strings,
  21. lexlib,yacclib;
  22. const
  23. version = '0.99.16';
  24. type
  25. Char=system.char;
  26. ttyp = (
  27. t_id,
  28. { p contains the string }
  29. t_arraydef,
  30. { }
  31. t_pointerdef,
  32. { p1 contains the definition
  33. if in type overrider
  34. or nothing for args
  35. }
  36. t_addrdef,
  37. t_void,
  38. { no field }
  39. t_dec,
  40. { }
  41. t_declist,
  42. { p1 is t_dec
  43. next if exists }
  44. t_memberdec,
  45. { p1 is type specifier
  46. p2 is declarator_list }
  47. t_structdef,
  48. { }
  49. t_memberdeclist,
  50. { p1 is memberdec
  51. next is next if it exist }
  52. t_procdef,
  53. { }
  54. t_uniondef,
  55. { }
  56. t_enumdef,
  57. { }
  58. t_enumlist,
  59. { }
  60. t_preop,
  61. { p contains the operator string
  62. p1 contains the right expr }
  63. t_bop,
  64. { p contains the operator string
  65. p1 contains the left expr
  66. p2 contains the right expr }
  67. t_arrayop,
  68. {
  69. p1 contains the array expr
  70. p2 contains the index expressions }
  71. t_callop,
  72. {
  73. p1 contains the proc expr
  74. p2 contains the index expressions }
  75. t_arg,
  76. {
  77. p1 contain the typedef
  78. p2 the declarator (t_dec)
  79. }
  80. t_arglist,
  81. { }
  82. t_funexprlist,
  83. { }
  84. t_exprlist,
  85. { p1 contains the expr
  86. next contains the next if it exists }
  87. t_ifexpr,
  88. { p1 contains the condition expr
  89. p2 contains the if branch
  90. p3 contains the else branch }
  91. t_funcname,
  92. { p1 contains the function dname
  93. p2 contains the funexprlist
  94. p3 possibly contains the return type }
  95. t_typespec,
  96. { p1 is the type itself
  97. p2 the typecast expr }
  98. t_size_specifier,
  99. { p1 expr for size }
  100. t_default_value
  101. { p1 expr for value }
  102. );
  103. const
  104. ttypstr: array[ttyp] of string =
  105. (
  106. 't_id',
  107. 't_arraydef',
  108. 't_pointerdef',
  109. 't_addrdef',
  110. 't_void',
  111. 't_dec',
  112. 't_declist',
  113. 't_memberdec',
  114. 't_structdef',
  115. 't_memberdeclist',
  116. 't_procdef',
  117. 't_uniondef',
  118. 't_enumdef',
  119. 't_enumlist',
  120. 't_preop',
  121. 't_bop',
  122. 't_arrayop',
  123. 't_callop',
  124. 't_arg',
  125. 't_arglist',
  126. 't_funexprlist',
  127. 't_exprlist',
  128. 't_ifexpr',
  129. 't_funcname',
  130. 't_typespec',
  131. 't_size_specifier',
  132. 't_default_value'
  133. );
  134. type
  135. presobject = ^tresobject;
  136. tresobject = object
  137. typ : ttyp;
  138. p : pchar;
  139. next : presobject;
  140. p1,p2,p3 : presobject;
  141. { name of int/real, then no T prefix is required }
  142. intname : boolean;
  143. constructor init_no(t : ttyp);
  144. constructor init_one(t : ttyp;_p1 : presobject);
  145. constructor init_two(t : ttyp;_p1,_p2 : presobject);
  146. constructor init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  147. constructor init_id(const s : string);
  148. constructor init_intid(const s : string);
  149. constructor init_bop(const s : string;_p1,_p2 : presobject);
  150. constructor init_preop(const s : string;_p1 : presobject);
  151. procedure setstr(const s:string);
  152. function str : string;
  153. function strlength : byte;
  154. function get_copy : presobject;
  155. { can this ve considered as a constant ? }
  156. function is_const : boolean;
  157. destructor done;
  158. end;
  159. tblocktype = (bt_type,bt_const,bt_var,bt_func,bt_no);
  160. var
  161. infile : string;
  162. outfile : text;
  163. c : char;
  164. aktspace : string;
  165. block_type : tblocktype;
  166. commentstr: string;
  167. const
  168. in_define : boolean = false;
  169. { True if define spans to the next line }
  170. cont_line : boolean = false;
  171. { 1 after define; 2 after the ID to print the first separating space }
  172. in_space_define : byte = 0;
  173. arglevel : longint = 0;
  174. function yylex : integer;
  175. function act_token : string;
  176. procedure internalerror(i : integer);
  177. function strpnew(const s : string) : pchar;
  178. procedure writetree(p: presobject);
  179. implementation
  180. uses
  181. options,converu;
  182. const
  183. newline = #10;
  184. procedure writeentry(p: presobject; var currentlevel: integer);
  185. begin
  186. if assigned(p^.p1) then
  187. begin
  188. WriteLn(' Entry p1[',ttypstr[p^.p1^.typ],']',p^.p1^.str);
  189. end;
  190. if assigned(p^.p2) then
  191. begin
  192. WriteLn(' Entry p2[',ttypstr[p^.p2^.typ],']',p^.p2^.str);
  193. end;
  194. if assigned(p^.p3) then
  195. begin
  196. WriteLn(' Entry p3[',ttypstr[p^.p3^.typ],']',p^.p3^.str);
  197. end;
  198. end;
  199. procedure writetree(p: presobject);
  200. var
  201. i : integer;
  202. localp: presobject;
  203. localp1: presobject;
  204. currentlevel : integer;
  205. begin
  206. localp:=p;
  207. currentlevel:=0;
  208. while assigned(localp) do
  209. begin
  210. WriteLn('Entry[',ttypstr[localp^.typ],']',localp^.str);
  211. case localp^.typ of
  212. { Some arguments sharing the same type }
  213. t_arglist:
  214. begin
  215. localp1:=localp;
  216. while assigned(localp1) do
  217. begin
  218. writeentry(localp1,currentlevel);
  219. localp1:=localp1^.p1;
  220. end;
  221. end;
  222. end;
  223. localp:=localp^.next;
  224. end;
  225. end;
  226. procedure internalerror(i : integer);
  227. begin
  228. writeln('Internal error ',i,' in line ',yylineno);
  229. halt(1);
  230. end;
  231. procedure commenteof;
  232. begin
  233. writeln('unexpected EOF inside comment at line ',yylineno);
  234. end;
  235. procedure copy_until_eol;
  236. begin
  237. c:=get_char;
  238. while c<>newline do
  239. begin
  240. write(outfile,c);
  241. c:=get_char;
  242. end;
  243. end;
  244. procedure skip_until_eol;
  245. begin
  246. c:=get_char;
  247. while c<>newline do
  248. c:=get_char;
  249. end;
  250. function strpnew(const s : string) : pchar;
  251. var
  252. p : pchar;
  253. begin
  254. getmem(p,length(s)+1);
  255. strpcopy(p,s);
  256. strpnew:=p;
  257. end;
  258. constructor tresobject.init_preop(const s : string;_p1 : presobject);
  259. begin
  260. typ:=t_preop;
  261. p:=strpnew(s);
  262. p1:=_p1;
  263. p2:=nil;
  264. p3:=nil;
  265. next:=nil;
  266. intname:=false;
  267. end;
  268. constructor tresobject.init_bop(const s : string;_p1,_p2 : presobject);
  269. begin
  270. typ:=t_bop;
  271. p:=strpnew(s);
  272. p1:=_p1;
  273. p2:=_p2;
  274. p3:=nil;
  275. next:=nil;
  276. intname:=false;
  277. end;
  278. constructor tresobject.init_id(const s : string);
  279. begin
  280. typ:=t_id;
  281. p:=strpnew(s);
  282. p1:=nil;
  283. p2:=nil;
  284. p3:=nil;
  285. next:=nil;
  286. intname:=false;
  287. end;
  288. constructor tresobject.init_intid(const s : string);
  289. begin
  290. typ:=t_id;
  291. p:=strpnew(s);
  292. p1:=nil;
  293. p2:=nil;
  294. p3:=nil;
  295. next:=nil;
  296. intname:=true;
  297. end;
  298. constructor tresobject.init_two(t : ttyp;_p1,_p2 : presobject);
  299. begin
  300. typ:=t;
  301. p1:=_p1;
  302. p2:=_p2;
  303. p3:=nil;
  304. p:=nil;
  305. next:=nil;
  306. intname:=false;
  307. end;
  308. constructor tresobject.init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  309. begin
  310. typ:=t;
  311. p1:=_p1;
  312. p2:=_p2;
  313. p3:=_p3;
  314. p:=nil;
  315. next:=nil;
  316. intname:=false;
  317. end;
  318. constructor tresobject.init_one(t : ttyp;_p1 : presobject);
  319. begin
  320. typ:=t;
  321. p1:=_p1;
  322. p2:=nil;
  323. p3:=nil;
  324. next:=nil;
  325. p:=nil;
  326. intname:=false;
  327. end;
  328. constructor tresobject.init_no(t : ttyp);
  329. begin
  330. typ:=t;
  331. p:=nil;
  332. p1:=nil;
  333. p2:=nil;
  334. p3:=nil;
  335. next:=nil;
  336. intname:=false;
  337. end;
  338. procedure tresobject.setstr(const s : string);
  339. begin
  340. if assigned(p) then
  341. strdispose(p);
  342. p:=strpnew(s);
  343. end;
  344. function tresobject.str : string;
  345. begin
  346. str:=strpas(p);
  347. end;
  348. function tresobject.strlength : byte;
  349. begin
  350. if assigned(p) then
  351. strlength:=strlen(p)
  352. else
  353. strlength:=0;
  354. end;
  355. { can this ve considered as a constant ? }
  356. function tresobject.is_const : boolean;
  357. begin
  358. case typ of
  359. t_id,t_void :
  360. is_const:=true;
  361. t_preop :
  362. is_const:= ((str='-') or (str=' not ')) and p1^.is_const;
  363. t_bop :
  364. is_const:= p2^.is_const and p1^.is_const;
  365. else
  366. is_const:=false;
  367. end;
  368. end;
  369. function tresobject.get_copy : presobject;
  370. var
  371. newres : presobject;
  372. begin
  373. newres:=new(presobject,init_no(typ));
  374. newres^.intname:=intname;
  375. if assigned(p) then
  376. newres^.p:=strnew(p);
  377. if assigned(p1) then
  378. newres^.p1:=p1^.get_copy;
  379. if assigned(p2) then
  380. newres^.p2:=p2^.get_copy;
  381. if assigned(p3) then
  382. newres^.p3:=p3^.get_copy;
  383. if assigned(next) then
  384. newres^.next:=next^.get_copy;
  385. get_copy:=newres;
  386. end;
  387. destructor tresobject.done;
  388. begin
  389. (* writeln('disposing ',byte(typ)); *)
  390. if assigned(p)then strdispose(p);
  391. if assigned(p1) then
  392. dispose(p1,done);
  393. if assigned(p2) then
  394. dispose(p2,done);
  395. if assigned(p3) then
  396. dispose(p3,done);
  397. if assigned(next) then
  398. dispose(next,done);
  399. end;
  400. %}
  401. D [0-9]
  402. %%
  403. "/*" begin
  404. if not stripcomment then
  405. write(outfile,aktspace,'{');
  406. repeat
  407. c:=get_char;
  408. case c of
  409. '*' :
  410. begin
  411. c:=get_char;
  412. if c='/' then
  413. begin
  414. if not stripcomment then
  415. write(outfile,' }');
  416. c:=get_char;
  417. if (c=newline) then
  418. begin
  419. writeln(outfile);
  420. unget_char(c);
  421. end;
  422. flush(outfile);
  423. exit;
  424. end
  425. else
  426. begin
  427. if not stripcomment then
  428. write(outfile,'*');
  429. unget_char(c)
  430. end;
  431. end;
  432. newline :
  433. begin
  434. if not stripcomment then
  435. begin
  436. writeln(outfile);
  437. write(outfile,aktspace);
  438. end;
  439. end;
  440. { Don't write this thing out, to
  441. avoid nested comments.
  442. }
  443. '{','}' :
  444. begin
  445. end;
  446. #0 :
  447. commenteof;
  448. else
  449. if not stripcomment then
  450. write(outfile,c);
  451. end;
  452. until false;
  453. flush(outfile);
  454. end;
  455. "//" begin
  456. commentstr:='';
  457. if (in_define) and not (stripcomment) then
  458. begin
  459. commentstr:='{';
  460. end
  461. else
  462. If not stripcomment then
  463. write(outfile,aktspace,'{');
  464. repeat
  465. c:=get_char;
  466. case c of
  467. newline :
  468. begin
  469. unget_char(c);
  470. if not stripcomment then
  471. begin
  472. if in_define then
  473. begin
  474. commentstr:=commentstr+' }';
  475. end
  476. else
  477. begin
  478. write(outfile,' }');
  479. writeln(outfile);
  480. end;
  481. end;
  482. flush(outfile);
  483. exit;
  484. end;
  485. { Don't write this comment out,
  486. to avoid nested comment problems
  487. }
  488. '{','}' :
  489. begin
  490. end;
  491. #0 :
  492. commenteof;
  493. else
  494. if not stripcomment then
  495. begin
  496. if in_define then
  497. begin
  498. commentstr:=commentstr+c;
  499. end
  500. else
  501. write(outfile,c);
  502. end;
  503. end;
  504. until false;
  505. flush(outfile);
  506. end;
  507. \"[^\"]*\" return(CSTRING);
  508. \'[^\']*\' return(CSTRING);
  509. "L"\"[^\"]*\" if win32headers then
  510. return(CSTRING)
  511. else
  512. return(256);
  513. "L"\'[^\']*\' if win32headers then
  514. return(CSTRING)
  515. else
  516. return(256);
  517. {D}+[Uu]?[Ll]? begin
  518. while yytext[length(yytext)] in ['L','U','l','u'] do
  519. Delete(yytext,length(yytext),1);
  520. return(NUMBER);
  521. end;
  522. "0x"[0-9A-Fa-f]*[Uu]?[Ll]?
  523. begin
  524. (* handle pre- and postfixes *)
  525. if copy(yytext,1,2)='0x' then
  526. begin
  527. delete(yytext,1,2);
  528. yytext:='$'+yytext;
  529. end;
  530. while yytext[length(yytext)] in ['L','U','l','u'] do
  531. Delete(yytext,length(yytext),1);
  532. return(NUMBER);
  533. end;
  534. {D}+(\.{D}+)?([Ee][+-]?{D}+)?
  535. begin
  536. return(NUMBER);
  537. end;
  538. "->" if in_define then
  539. return(DEREF)
  540. else
  541. return(256);
  542. "-" return(MINUS);
  543. "==" return(EQUAL);
  544. "!=" return(UNEQUAL);
  545. ">=" return(GTE);
  546. "<=" return(LTE);
  547. ">>" return(_SHR);
  548. "##" return(STICK);
  549. "<<" return(_SHL);
  550. ">" return(GT);
  551. "<" return(LT);
  552. "|" return(_OR);
  553. "&" return(_AND);
  554. "~" return(_NOT); (* inverse, but handled as not operation *)
  555. "!" return(_NOT);
  556. "/" return(_SLASH);
  557. "+" return(_PLUS);
  558. "?" return(QUESTIONMARK);
  559. ":" return(COLON);
  560. "," return(COMMA);
  561. "[" return(LECKKLAMMER);
  562. "]" return(RECKKLAMMER);
  563. "(" begin
  564. inc(arglevel);
  565. return(LKLAMMER);
  566. end;
  567. ")" begin
  568. dec(arglevel);
  569. return(RKLAMMER);
  570. end;
  571. "*" return(STAR);
  572. "..." return(ELLIPSIS);
  573. "." if in_define then
  574. return(POINT)
  575. else
  576. return(256);
  577. "=" return(_ASSIGN);
  578. "extern" return(EXTERN);
  579. "STDCALL" if Win32headers then
  580. return(STDCALL)
  581. else
  582. return(ID);
  583. "CDECL" if not Win32headers then
  584. return(ID)
  585. else
  586. return(CDECL);
  587. "PASCAL" if not Win32headers then
  588. return(ID)
  589. else
  590. return(PASCAL);
  591. "PACKED" if not Win32headers then
  592. return(ID)
  593. else
  594. return(_PACKED);
  595. "WINAPI" if not Win32headers then
  596. return(ID)
  597. else
  598. return(WINAPI);
  599. "SYS_TRAP" if not palmpilot then
  600. return(ID)
  601. else
  602. return(SYS_TRAP);
  603. "WINGDIAPI" if not Win32headers then
  604. return(ID)
  605. else
  606. return(WINGDIAPI);
  607. "CALLBACK" if not Win32headers then
  608. return(ID)
  609. else
  610. return(CALLBACK);
  611. "EXPENTRY" if not Win32headers then
  612. return(ID)
  613. else
  614. return(CALLBACK);
  615. "void" return(VOID);
  616. "VOID" return(VOID);
  617. "#ifdef __cplusplus"[ \t]*\n"extern \"C\" {"\n"#endif"
  618. begin
  619. if not stripinfo then
  620. writeln(outfile,'{ C++ extern C conditionnal removed }');
  621. end;
  622. "#ifdef __cplusplus"[ \t]*\n"}"\n"#endif"
  623. begin
  624. if not stripinfo then
  625. writeln(outfile,'{ C++ end of extern C conditionnal removed }');
  626. end;
  627. "#"[ \t]*"else" begin
  628. writeln(outfile,'{$else}');
  629. block_type:=bt_no;
  630. flush(outfile);
  631. end;
  632. "#"[ \t]*"endif" begin
  633. writeln(outfile,'{$endif}');
  634. block_type:=bt_no;
  635. flush(outfile);
  636. end;
  637. "#"[ \t]*"elif" begin
  638. if not stripinfo then
  639. write(outfile,'(*** was #elif ****)');
  640. write(outfile,'{$else');
  641. copy_until_eol;
  642. writeln(outfile,'}');
  643. block_type:=bt_no;
  644. flush(outfile);
  645. end;
  646. "#"[ \t]*"undef" begin
  647. write(outfile,'{$undef');
  648. copy_until_eol;
  649. writeln(outfile,'}');
  650. flush(outfile);
  651. end;
  652. "#"[ \t]*"error" begin
  653. write(outfile,'{$error');
  654. copy_until_eol;
  655. writeln(outfile,'}');
  656. flush(outfile);
  657. end;
  658. "#"[ \t]*"include" begin
  659. write(outfile,'{$include');
  660. copy_until_eol;
  661. writeln(outfile,'}');
  662. flush(outfile);
  663. block_type:=bt_no;
  664. end;
  665. "#"[ \t]*"if" begin
  666. write(outfile,'{$if');
  667. copy_until_eol;
  668. writeln(outfile,'}');
  669. flush(outfile);
  670. block_type:=bt_no;
  671. end;
  672. "# "[0-9]+" " begin
  673. (* preprocessor line info *)
  674. repeat
  675. c:=get_char;
  676. case c of
  677. newline :
  678. begin
  679. unget_char(c);
  680. exit;
  681. end;
  682. #0 :
  683. commenteof;
  684. end;
  685. until false;
  686. end;
  687. "#"[ \t]*"pragma" begin
  688. if not stripinfo then
  689. begin
  690. write(outfile,'(** unsupported pragma');
  691. write(outfile,'#pragma');
  692. copy_until_eol;
  693. writeln(outfile,'*)');
  694. flush(outfile);
  695. end
  696. else
  697. skip_until_eol;
  698. block_type:=bt_no;
  699. end;
  700. "#"[ \t]*"define" begin
  701. commentstr:='';
  702. in_define:=true;
  703. in_space_define:=1;
  704. return(DEFINE);
  705. end;
  706. "char" return(_CHAR);
  707. "union" return(UNION);
  708. "enum" return(ENUM);
  709. "struct" return(STRUCT);
  710. "{" return(LGKLAMMER);
  711. "}" return(RGKLAMMER);
  712. "typedef" return(TYPEDEF);
  713. "int" return(INT);
  714. "short" return(SHORT);
  715. "long" return(LONG);
  716. "signed" return(SIGNED);
  717. "unsigned" return(UNSIGNED);
  718. "float" return(REAL);
  719. "const" return(_CONST);
  720. "CONST" return(_CONST);
  721. "FAR" return(_FAR);
  722. "far" return(_FAR);
  723. "NEAR" return(_NEAR);
  724. "near" return(_NEAR);
  725. "HUGE" return(_HUGE);
  726. "huge" return(_HUGE);
  727. [A-Za-z_][A-Za-z0-9_]* begin
  728. if in_space_define=1 then
  729. in_space_define:=2;
  730. return(ID);
  731. end;
  732. ";" return(SEMICOLON);
  733. [ \f\t] begin
  734. if (arglevel=0) and (in_space_define=2) then
  735. begin
  736. in_space_define:=0;
  737. return(SPACE_DEFINE);
  738. end;
  739. end;
  740. \n begin
  741. if in_define then
  742. begin
  743. in_space_define:=0;
  744. if cont_line then
  745. begin
  746. cont_line:=false;
  747. end
  748. else
  749. begin
  750. in_define:=false;
  751. return(NEW_LINE);
  752. end;
  753. end;
  754. end;
  755. \\$ begin
  756. if in_define then
  757. begin
  758. cont_line:=true;
  759. end
  760. else
  761. begin
  762. writeln('Unexpected wrap of line ',yylineno);
  763. writeln('"',yyline,'"');
  764. return(256);
  765. end;
  766. end;
  767. . begin
  768. writeln('Illegal character in line ',yylineno);
  769. writeln('"',yyline,'"');
  770. return(256);
  771. end;
  772. %%
  773. function act_token : string;
  774. begin
  775. act_token:=yytext;
  776. end;
  777. end.