scan.l 24 KB

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