scan.l 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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.15';
  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. presobject = ^tresobject;
  104. tresobject = object
  105. typ : ttyp;
  106. p : pchar;
  107. next : presobject;
  108. p1,p2,p3 : presobject;
  109. { name of int/real, then no T prefix is required }
  110. intname : boolean;
  111. constructor init_no(t : ttyp);
  112. constructor init_one(t : ttyp;_p1 : presobject);
  113. constructor init_two(t : ttyp;_p1,_p2 : presobject);
  114. constructor init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  115. constructor init_id(const s : string);
  116. constructor init_intid(const s : string);
  117. constructor init_bop(const s : string;_p1,_p2 : presobject);
  118. constructor init_preop(const s : string;_p1 : presobject);
  119. procedure setstr(const s:string);
  120. function str : string;
  121. function strlength : byte;
  122. function get_copy : presobject;
  123. { can this ve considered as a constant ? }
  124. function is_const : boolean;
  125. destructor done;
  126. end;
  127. tblocktype = (bt_type,bt_const,bt_var,bt_func,bt_no);
  128. var
  129. infile : string;
  130. outfile : text;
  131. c : char;
  132. aktspace : string;
  133. block_type : tblocktype;
  134. const
  135. in_define : boolean = false;
  136. { True if define spans to the next line }
  137. cont_line : boolean = false;
  138. { 1 after define; 2 after the ID to print the first separating space }
  139. in_space_define : byte = 0;
  140. arglevel : longint = 0;
  141. function yylex : integer;
  142. function act_token : string;
  143. procedure internalerror(i : integer);
  144. function strpnew(const s : string) : pchar;
  145. implementation
  146. uses
  147. options,converu;
  148. const
  149. newline = #10;
  150. procedure internalerror(i : integer);
  151. begin
  152. writeln('Internal error ',i,' in line ',yylineno);
  153. halt(1);
  154. end;
  155. procedure commenteof;
  156. begin
  157. writeln('unexpected EOF inside comment at line ',yylineno);
  158. end;
  159. procedure copy_until_eol;
  160. begin
  161. c:=get_char;
  162. while c<>newline do
  163. begin
  164. write(outfile,c);
  165. c:=get_char;
  166. end;
  167. end;
  168. procedure skip_until_eol;
  169. begin
  170. c:=get_char;
  171. while c<>newline do
  172. c:=get_char;
  173. end;
  174. function strpnew(const s : string) : pchar;
  175. var
  176. p : pchar;
  177. begin
  178. getmem(p,length(s)+1);
  179. strpcopy(p,s);
  180. strpnew:=p;
  181. end;
  182. constructor tresobject.init_preop(const s : string;_p1 : presobject);
  183. begin
  184. typ:=t_preop;
  185. p:=strpnew(s);
  186. p1:=_p1;
  187. p2:=nil;
  188. p3:=nil;
  189. next:=nil;
  190. intname:=false;
  191. end;
  192. constructor tresobject.init_bop(const s : string;_p1,_p2 : presobject);
  193. begin
  194. typ:=t_bop;
  195. p:=strpnew(s);
  196. p1:=_p1;
  197. p2:=_p2;
  198. p3:=nil;
  199. next:=nil;
  200. intname:=false;
  201. end;
  202. constructor tresobject.init_id(const s : string);
  203. begin
  204. typ:=t_id;
  205. p:=strpnew(s);
  206. p1:=nil;
  207. p2:=nil;
  208. p3:=nil;
  209. next:=nil;
  210. intname:=false;
  211. end;
  212. constructor tresobject.init_intid(const s : string);
  213. begin
  214. typ:=t_id;
  215. p:=strpnew(s);
  216. p1:=nil;
  217. p2:=nil;
  218. p3:=nil;
  219. next:=nil;
  220. intname:=true;
  221. end;
  222. constructor tresobject.init_two(t : ttyp;_p1,_p2 : presobject);
  223. begin
  224. typ:=t;
  225. p1:=_p1;
  226. p2:=_p2;
  227. p3:=nil;
  228. p:=nil;
  229. next:=nil;
  230. intname:=false;
  231. end;
  232. constructor tresobject.init_three(t : ttyp;_p1,_p2,_p3 : presobject);
  233. begin
  234. typ:=t;
  235. p1:=_p1;
  236. p2:=_p2;
  237. p3:=_p3;
  238. p:=nil;
  239. next:=nil;
  240. intname:=false;
  241. end;
  242. constructor tresobject.init_one(t : ttyp;_p1 : presobject);
  243. begin
  244. typ:=t;
  245. p1:=_p1;
  246. p2:=nil;
  247. p3:=nil;
  248. next:=nil;
  249. p:=nil;
  250. intname:=false;
  251. end;
  252. constructor tresobject.init_no(t : ttyp);
  253. begin
  254. typ:=t;
  255. p:=nil;
  256. p1:=nil;
  257. p2:=nil;
  258. p3:=nil;
  259. next:=nil;
  260. intname:=false;
  261. end;
  262. procedure tresobject.setstr(const s : string);
  263. begin
  264. if assigned(p) then
  265. strdispose(p);
  266. p:=strpnew(s);
  267. end;
  268. function tresobject.str : string;
  269. begin
  270. str:=strpas(p);
  271. end;
  272. function tresobject.strlength : byte;
  273. begin
  274. if assigned(p) then
  275. strlength:=strlen(p)
  276. else
  277. strlength:=0;
  278. end;
  279. { can this ve considered as a constant ? }
  280. function tresobject.is_const : boolean;
  281. begin
  282. case typ of
  283. t_id,t_void :
  284. is_const:=true;
  285. t_preop :
  286. is_const:= ((str='-') or (str=' not ')) and p1^.is_const;
  287. t_bop :
  288. is_const:= p2^.is_const and p1^.is_const;
  289. else
  290. is_const:=false;
  291. end;
  292. end;
  293. function tresobject.get_copy : presobject;
  294. var
  295. newres : presobject;
  296. begin
  297. newres:=new(presobject,init_no(typ));
  298. newres^.intname:=intname;
  299. if assigned(p) then
  300. newres^.p:=strnew(p);
  301. if assigned(p1) then
  302. newres^.p1:=p1^.get_copy;
  303. if assigned(p2) then
  304. newres^.p2:=p2^.get_copy;
  305. if assigned(p3) then
  306. newres^.p3:=p3^.get_copy;
  307. if assigned(next) then
  308. newres^.next:=next^.get_copy;
  309. get_copy:=newres;
  310. end;
  311. destructor tresobject.done;
  312. begin
  313. (* writeln('disposing ',byte(typ)); *)
  314. if assigned(p)then strdispose(p);
  315. if assigned(p1) then
  316. dispose(p1,done);
  317. if assigned(p2) then
  318. dispose(p2,done);
  319. if assigned(p3) then
  320. dispose(p3,done);
  321. if assigned(next) then
  322. dispose(next,done);
  323. end;
  324. %}
  325. D [0-9]
  326. %%
  327. "/*" begin
  328. if not stripcomment then
  329. write(outfile,aktspace,'{');
  330. repeat
  331. c:=get_char;
  332. case c of
  333. '*' :
  334. begin
  335. c:=get_char;
  336. if c='/' then
  337. begin
  338. if not stripcomment then
  339. writeln(outfile,' }');
  340. flush(outfile);
  341. exit;
  342. end
  343. else
  344. begin
  345. if not stripcomment then
  346. write(outfile,' ');
  347. unget_char(c)
  348. end;
  349. end;
  350. newline :
  351. begin
  352. if not stripcomment then
  353. begin
  354. writeln(outfile);
  355. write(outfile,aktspace);
  356. end;
  357. end;
  358. #0 :
  359. commenteof;
  360. else
  361. if not stripcomment then
  362. write(outfile,c);
  363. end;
  364. until false;
  365. flush(outfile);
  366. end;
  367. "//" begin
  368. If not stripcomment then
  369. write(outfile,aktspace,'{');
  370. repeat
  371. c:=get_char;
  372. case c of
  373. newline :
  374. begin
  375. unget_char(c);
  376. if not stripcomment then
  377. writeln(outfile,' }');
  378. flush(outfile);
  379. exit;
  380. end;
  381. #0 :
  382. commenteof;
  383. else
  384. if not stripcomment then
  385. write(outfile,c);
  386. end;
  387. until false;
  388. flush(outfile);
  389. end;
  390. \"[^\"]*\" return(CSTRING);
  391. \'[^\']*\' return(CSTRING);
  392. "L"\"[^\"]*\" if win32headers then
  393. return(CSTRING)
  394. else
  395. return(256);
  396. "L"\'[^\']*\' if win32headers then
  397. return(CSTRING)
  398. else
  399. return(256);
  400. {D}+[Uu]?[Ll]? begin
  401. while yytext[length(yytext)] in ['L','U','l','u'] do
  402. Delete(yytext,length(yytext),1);
  403. return(NUMBER);
  404. end;
  405. "0x"[0-9A-Fa-f]*[Uu]?[Ll]?
  406. begin
  407. (* handle pre- and postfixes *)
  408. if copy(yytext,1,2)='0x' then
  409. begin
  410. delete(yytext,1,2);
  411. yytext:='$'+yytext;
  412. end;
  413. while yytext[length(yytext)] in ['L','U','l','u'] do
  414. Delete(yytext,length(yytext),1);
  415. return(NUMBER);
  416. end;
  417. {D}+(\.{D}+)?([Ee][+-]?{D}+)?
  418. begin
  419. return(NUMBER);
  420. end;
  421. "->" if in_define then
  422. return(DEREF)
  423. else
  424. return(256);
  425. "-" return(MINUS);
  426. "==" return(EQUAL);
  427. "!=" return(UNEQUAL);
  428. ">=" return(GTE);
  429. "<=" return(LTE);
  430. ">>" return(_SHR);
  431. "##" return(STICK);
  432. "<<" return(_SHL);
  433. ">" return(GT);
  434. "<" return(LT);
  435. "|" return(_OR);
  436. "&" return(_AND);
  437. "~" return(_NOT); (* inverse, but handled as not operation *)
  438. "!" return(_NOT);
  439. "/" return(_SLASH);
  440. "+" return(_PLUS);
  441. "?" return(QUESTIONMARK);
  442. ":" return(COLON);
  443. "," return(COMMA);
  444. "[" return(LECKKLAMMER);
  445. "]" return(RECKKLAMMER);
  446. "(" begin
  447. inc(arglevel);
  448. return(LKLAMMER);
  449. end;
  450. ")" begin
  451. dec(arglevel);
  452. return(RKLAMMER);
  453. end;
  454. "*" return(STAR);
  455. "..." return(ELLIPSIS);
  456. "." if in_define then
  457. return(POINT)
  458. else
  459. return(256);
  460. "=" return(_ASSIGN);
  461. "extern" return(EXTERN);
  462. "STDCALL" if Win32headers then
  463. return(STDCALL)
  464. else
  465. return(ID);
  466. "CDECL" if not Win32headers then
  467. return(ID)
  468. else
  469. return(CDECL);
  470. "PASCAL" if not Win32headers then
  471. return(ID)
  472. else
  473. return(PASCAL);
  474. "PACKED" if not Win32headers then
  475. return(ID)
  476. else
  477. return(_PACKED);
  478. "WINAPI" if not Win32headers then
  479. return(ID)
  480. else
  481. return(WINAPI);
  482. "SYS_TRAP" if not palmpilot then
  483. return(ID)
  484. else
  485. return(SYS_TRAP);
  486. "WINGDIAPI" if not Win32headers then
  487. return(ID)
  488. else
  489. return(WINGDIAPI);
  490. "CALLBACK" if not Win32headers then
  491. return(ID)
  492. else
  493. return(CALLBACK);
  494. "EXPENTRY" if not Win32headers then
  495. return(ID)
  496. else
  497. return(CALLBACK);
  498. "void" return(VOID);
  499. "VOID" return(VOID);
  500. "#ifdef __cplusplus"[ \t]*\n"extern \"C\" {"\n"#endif"
  501. begin
  502. if not stripinfo then
  503. writeln(outfile,'{ C++ extern C conditionnal removed }');
  504. end;
  505. "#ifdef __cplusplus"[ \t]*\n"}"\n"#endif"
  506. begin
  507. if not stripinfo then
  508. writeln(outfile,'{ C++ end of extern C conditionnal removed }');
  509. end;
  510. "#"[ \t]*"else" begin
  511. writeln(outfile,'{$else}');
  512. block_type:=bt_no;
  513. flush(outfile);
  514. end;
  515. "#"[ \t]*"endif" begin
  516. writeln(outfile,'{$endif}');
  517. block_type:=bt_no;
  518. flush(outfile);
  519. end;
  520. "#"[ \t]*"elif" begin
  521. if not stripinfo then
  522. write(outfile,'(*** was #elif ****)');
  523. write(outfile,'{$else');
  524. copy_until_eol;
  525. writeln(outfile,'}');
  526. block_type:=bt_no;
  527. flush(outfile);
  528. end;
  529. "#"[ \t]*"undef" begin
  530. write(outfile,'{$undef');
  531. copy_until_eol;
  532. writeln(outfile,'}');
  533. flush(outfile);
  534. end;
  535. "#"[ \t]*"error" begin
  536. write(outfile,'{$error');
  537. copy_until_eol;
  538. writeln(outfile,'}');
  539. flush(outfile);
  540. end;
  541. "#"[ \t]*"include" begin
  542. write(outfile,'{$include');
  543. copy_until_eol;
  544. writeln(outfile,'}');
  545. flush(outfile);
  546. block_type:=bt_no;
  547. end;
  548. "#"[ \t]*"if" begin
  549. write(outfile,'{$if');
  550. copy_until_eol;
  551. writeln(outfile,'}');
  552. flush(outfile);
  553. block_type:=bt_no;
  554. end;
  555. "# "[0-9]+" " begin
  556. (* preprocessor line info *)
  557. repeat
  558. c:=get_char;
  559. case c of
  560. newline :
  561. begin
  562. unget_char(c);
  563. exit;
  564. end;
  565. #0 :
  566. commenteof;
  567. end;
  568. until false;
  569. end;
  570. "#"[ \t]*"pragma" begin
  571. if not stripinfo then
  572. begin
  573. write(outfile,'(** unsupported pragma');
  574. write(outfile,'#pragma');
  575. copy_until_eol;
  576. writeln(outfile,'*)');
  577. flush(outfile);
  578. end
  579. else
  580. skip_until_eol;
  581. block_type:=bt_no;
  582. end;
  583. "#"[ \t]*"define" begin
  584. in_define:=true;
  585. in_space_define:=1;
  586. return(DEFINE);
  587. end;
  588. "char" return(_CHAR);
  589. "union" return(UNION);
  590. "enum" return(ENUM);
  591. "struct" return(STRUCT);
  592. "{" return(LGKLAMMER);
  593. "}" return(RGKLAMMER);
  594. "typedef" return(TYPEDEF);
  595. "int" return(INT);
  596. "short" return(SHORT);
  597. "long" return(LONG);
  598. "signed" return(SIGNED);
  599. "unsigned" return(UNSIGNED);
  600. "float" return(REAL);
  601. "const" return(_CONST);
  602. "CONST" return(_CONST);
  603. "FAR" return(_FAR);
  604. "far" return(_FAR);
  605. "NEAR" return(_NEAR);
  606. "near" return(_NEAR);
  607. "HUGE" return(_HUGE);
  608. "huge" return(_HUGE);
  609. [A-Za-z_][A-Za-z0-9_]* begin
  610. if in_space_define=1 then
  611. in_space_define:=2;
  612. return(ID);
  613. end;
  614. ";" return(SEMICOLON);
  615. [ \f\t] begin
  616. if (arglevel=0) and (in_space_define=2) then
  617. begin
  618. in_space_define:=0;
  619. return(SPACE_DEFINE);
  620. end;
  621. end;
  622. \n begin
  623. if in_define then
  624. begin
  625. in_space_define:=0;
  626. if cont_line then
  627. begin
  628. cont_line:=false;
  629. end
  630. else
  631. begin
  632. in_define:=false;
  633. return(NEW_LINE);
  634. end;
  635. end;
  636. end;
  637. \\$ begin
  638. if in_define then
  639. begin
  640. cont_line:=true;
  641. end
  642. else
  643. begin
  644. writeln('Unexpected wrap of line ',yylineno);
  645. writeln('"',yyline,'"');
  646. return(256);
  647. end;
  648. end;
  649. . begin
  650. writeln('Illegal character in line ',yylineno);
  651. writeln('"',yyline,'"');
  652. return(256);
  653. end;
  654. %%
  655. function act_token : string;
  656. begin
  657. act_token:=yytext;
  658. end;
  659. end.