scan.l 39 KB

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