scanner.pas 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  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. }
  17. unit scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. type
  31. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  32. tscannerfile = class;
  33. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  34. tpreprocstack = class
  35. typ : preproctyp;
  36. accept : boolean;
  37. next : tpreprocstack;
  38. name : stringid;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TNamedIndexItem)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(const n:string;p:tdirectiveproc);
  49. constructor CreateCond(const n:string;p:tdirectiveproc);
  50. end;
  51. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  52. tscannerfile = class
  53. public
  54. inputfile : tinputfile; { current inputfile list }
  55. inputfilecount : longint;
  56. inputbuffer, { input buffer }
  57. inputpointer : pchar;
  58. inputstart : longint;
  59. line_no, { line }
  60. lastlinepos : longint;
  61. lasttokenpos : longint; { token }
  62. lasttoken,
  63. nexttoken : ttoken;
  64. comment_level,
  65. yylexcount : longint;
  66. lastasmgetchar : char;
  67. ignoredirectives : tstringlist; { ignore directives, used to give warnings only once }
  68. preprocstack : tpreprocstack;
  69. in_asm_string : boolean;
  70. preproc_pattern : string;
  71. preproc_token : ttoken;
  72. constructor Create(const fn:string);
  73. destructor Destroy;override;
  74. { File buffer things }
  75. function openinputfile:boolean;
  76. procedure closeinputfile;
  77. function tempopeninputfile:boolean;
  78. procedure tempcloseinputfile;
  79. procedure saveinputfile;
  80. procedure restoreinputfile;
  81. procedure firstfile;
  82. procedure nextfile;
  83. procedure addfile(hp:tinputfile);
  84. procedure reload;
  85. procedure insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  86. { Scanner things }
  87. procedure gettokenpos;
  88. procedure inc_comment_level;
  89. procedure dec_comment_level;
  90. procedure illegal_char(c:char);
  91. procedure end_of_file;
  92. procedure checkpreprocstack;
  93. procedure poppreprocstack;
  94. procedure ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  95. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  96. procedure elsepreprocstack;
  97. procedure handleconditional(p:tdirectiveitem);
  98. procedure handledirectives;
  99. procedure linebreak;
  100. procedure readchar;
  101. procedure readstring;
  102. procedure readnumber;
  103. function readid:string;
  104. function readval:longint;
  105. function readval_asstring:string;
  106. function readcomment:string;
  107. function readquotedstring:string;
  108. function readstate:char;
  109. function readstatedefault:char;
  110. procedure skipspace;
  111. procedure skipuntildirective;
  112. procedure skipcomment;
  113. procedure skipdelphicomment;
  114. procedure skipoldtpcomment;
  115. procedure readtoken;
  116. function readpreproc:ttoken;
  117. function asmgetcharstart : char;
  118. function asmgetchar:char;
  119. end;
  120. {$ifdef PREPROCWRITE}
  121. tpreprocfile=class
  122. f : text;
  123. buf : pointer;
  124. spacefound,
  125. eolfound : boolean;
  126. constructor create(const fn:string);
  127. destructor destroy;
  128. procedure Add(const s:string);
  129. procedure AddSpace;
  130. end;
  131. {$endif PREPROCWRITE}
  132. var
  133. { read strings }
  134. c : char;
  135. orgpattern,
  136. pattern : string;
  137. patternw : pcompilerwidestring;
  138. { token }
  139. token, { current token being parsed }
  140. idtoken : ttoken; { holds the token if the pattern is a known word }
  141. current_scanner : tscannerfile; { current scanner in use }
  142. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  143. {$ifdef PREPROCWRITE}
  144. preprocfile : tpreprocfile; { used with only preprocessing }
  145. {$endif PREPROCWRITE}
  146. type
  147. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  148. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  149. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  150. procedure InitScanner;
  151. procedure DoneScanner;
  152. {To be called when the language mode is finally determined}
  153. procedure ConsolidateMode;
  154. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  155. implementation
  156. uses
  157. dos,
  158. cutils,
  159. systems,
  160. switches,
  161. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  162. fmodule;
  163. var
  164. { dictionaries with the supported directives }
  165. turbo_scannerdirectives : tdictionary; { for other modes }
  166. mac_scannerdirectives : tdictionary; { for mode mac }
  167. {*****************************************************************************
  168. Helper routines
  169. *****************************************************************************}
  170. const
  171. { use any special name that is an invalid file name to avoid problems }
  172. preprocstring : array [preproctyp] of string[7]
  173. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  174. function is_keyword(const s:string):boolean;
  175. var
  176. low,high,mid : longint;
  177. begin
  178. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  179. not (s[1] in ['a'..'z','A'..'Z']) then
  180. begin
  181. is_keyword:=false;
  182. exit;
  183. end;
  184. low:=ord(tokenidx^[length(s),s[1]].first);
  185. high:=ord(tokenidx^[length(s),s[1]].last);
  186. while low<high do
  187. begin
  188. mid:=(high+low+1) shr 1;
  189. if pattern<tokeninfo^[ttoken(mid)].str then
  190. high:=mid-1
  191. else
  192. low:=mid;
  193. end;
  194. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  195. (tokeninfo^[ttoken(high)].keyword in aktmodeswitches);
  196. end;
  197. {To be called when the language mode is finally determined}
  198. procedure ConsolidateMode;
  199. begin
  200. if m_mac in aktmodeswitches then
  201. if current_module.is_unit and not assigned(current_module.globalmacrosymtable) then
  202. begin
  203. current_module.globalmacrosymtable:= tmacrosymtable.create(true);
  204. current_module.globalmacrosymtable.next:= current_module.localmacrosymtable;
  205. macrosymtablestack:=current_module.globalmacrosymtable;
  206. end;
  207. end;
  208. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  209. var
  210. b : boolean;
  211. oldaktmodeswitches : tmodeswitches;
  212. begin
  213. oldaktmodeswitches:=aktmodeswitches;
  214. b:=true;
  215. if s='DEFAULT' then
  216. aktmodeswitches:=initmodeswitches
  217. else
  218. if s='DELPHI' then
  219. aktmodeswitches:=delphimodeswitches
  220. else
  221. if s='TP' then
  222. aktmodeswitches:=tpmodeswitches
  223. else
  224. if s='FPC' then
  225. aktmodeswitches:=fpcmodeswitches
  226. else
  227. if s='OBJFPC' then
  228. aktmodeswitches:=objfpcmodeswitches
  229. else
  230. if s='GPC' then
  231. aktmodeswitches:=gpcmodeswitches
  232. else
  233. if s='MACPAS' then
  234. aktmodeswitches:=macmodeswitches
  235. else
  236. b:=false;
  237. if b and changeInit then
  238. initmodeswitches := aktmodeswitches;
  239. if b then
  240. begin
  241. { resolve all postponed switch changes }
  242. if localswitcheschanged then
  243. begin
  244. aktlocalswitches:=nextaktlocalswitches;
  245. localswitcheschanged:=false;
  246. end;
  247. { turn ansistrings on by default ? }
  248. if (m_delphi in aktmodeswitches) then
  249. begin
  250. include(aktlocalswitches,cs_ansistrings);
  251. if changeinit then
  252. include(initlocalswitches,cs_ansistrings);
  253. end
  254. else
  255. begin
  256. exclude(aktlocalswitches,cs_ansistrings);
  257. if changeinit then
  258. exclude(initlocalswitches,cs_ansistrings);
  259. end;
  260. { support goto/label by default in delphi/tp7/mac modes }
  261. if ([m_delphi,m_tp7,m_mac] * aktmodeswitches <> []) then
  262. begin
  263. include(aktmoduleswitches,cs_support_goto);
  264. if changeinit then
  265. include(initmoduleswitches,cs_support_goto);
  266. end;
  267. { Default enum packing for delphi/tp7 }
  268. if (m_tp7 in aktmodeswitches) or
  269. (m_delphi in aktmodeswitches) then
  270. aktpackenum:=1
  271. else if (m_mac in aktmodeswitches) then
  272. { compatible with Metrowerks Pascal }
  273. aktpackenum:=2
  274. else
  275. aktpackenum:=4;
  276. if changeinit then
  277. initpackenum:=aktpackenum;
  278. {$ifdef i386}
  279. { Default to intel assembler for delphi/tp7 on i386 }
  280. if (m_delphi in aktmodeswitches) or
  281. (m_tp7 in aktmodeswitches) then
  282. aktasmmode:=asmmode_i386_intel;
  283. if changeinit then
  284. initasmmode:=aktasmmode;
  285. {$endif i386}
  286. { Undefine old symbol }
  287. if (m_delphi in oldaktmodeswitches) then
  288. undef_system_macro('FPC_DELPHI')
  289. else if (m_tp7 in oldaktmodeswitches) then
  290. undef_system_macro('FPC_TP')
  291. else if (m_objfpc in oldaktmodeswitches) then
  292. undef_system_macro('FPC_OBJFPC')
  293. else if (m_gpc in oldaktmodeswitches) then
  294. undef_system_macro('FPC_GPC')
  295. else if (m_mac in oldaktmodeswitches) then
  296. undef_system_macro('FPC_MACPAS');
  297. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  298. if (m_delphi in aktmodeswitches) then
  299. def_system_macro('FPC_DELPHI')
  300. else if (m_tp7 in aktmodeswitches) then
  301. def_system_macro('FPC_TP')
  302. else if (m_objfpc in aktmodeswitches) then
  303. def_system_macro('FPC_OBJFPC')
  304. else if (m_gpc in aktmodeswitches) then
  305. def_system_macro('FPC_GPC')
  306. else if (m_mac in aktmodeswitches) then
  307. def_system_macro('FPC_MACPAS');
  308. end;
  309. SetCompileMode:=b;
  310. end;
  311. {*****************************************************************************
  312. Conditional Directives
  313. *****************************************************************************}
  314. procedure dir_else;
  315. begin
  316. current_scanner.elsepreprocstack;
  317. end;
  318. procedure dir_endif;
  319. begin
  320. current_scanner.poppreprocstack;
  321. end;
  322. function isdef(var valuedescr: String): Boolean;
  323. var
  324. hs : string;
  325. mac : tmacro;
  326. begin
  327. current_scanner.skipspace;
  328. hs:=current_scanner.readid;
  329. valuedescr:= hs;
  330. if hs='' then
  331. Message(scan_e_error_in_preproc_expr);
  332. mac:=tmacro(search_macro(hs));
  333. if assigned(mac) then
  334. mac.is_used:=true;
  335. isdef:= assigned(mac) and mac.defined;
  336. end;
  337. procedure dir_ifdef;
  338. begin
  339. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  340. end;
  341. function isnotdef(var valuedescr: String): Boolean;
  342. var
  343. hs : string;
  344. mac : tmacro;
  345. begin
  346. current_scanner.skipspace;
  347. hs:=current_scanner.readid;
  348. valuedescr:= hs;
  349. if hs='' then
  350. Message(scan_e_error_in_preproc_expr);
  351. mac:=tmacro(search_macro(hs));
  352. if assigned(mac) then
  353. mac.is_used:=true;
  354. isnotdef:= not (assigned(mac) and mac.defined);
  355. end;
  356. procedure dir_ifndef;
  357. begin
  358. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  359. end;
  360. function opt_check(var valuedescr: String): Boolean;
  361. var
  362. hs : string;
  363. state : char;
  364. begin
  365. opt_check:= false;
  366. current_scanner.skipspace;
  367. hs:=current_scanner.readid;
  368. valuedescr:= hs;
  369. if (length(hs)>1) then
  370. Message1(scan_w_illegal_switch,hs)
  371. else
  372. begin
  373. state:=current_scanner.ReadState;
  374. if state in ['-','+'] then
  375. opt_check:=CheckSwitch(hs[1],state)
  376. else
  377. Message(scan_e_error_in_preproc_expr);
  378. end;
  379. end;
  380. procedure dir_ifopt;
  381. begin
  382. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  383. end;
  384. procedure dir_libprefix;
  385. var
  386. s : string;
  387. begin
  388. current_scanner.skipspace;
  389. if c <> '''' then
  390. Message2(scan_f_syn_expected, '''', c);
  391. s := current_scanner.readquotedstring;
  392. stringdispose(outputprefix);
  393. outputprefix := stringdup(s);
  394. with current_module do
  395. setfilename(paramfn^, paramallowoutput);
  396. end;
  397. procedure dir_libsuffix;
  398. var
  399. s : string;
  400. begin
  401. current_scanner.skipspace;
  402. if c <> '''' then
  403. Message2(scan_f_syn_expected, '''', c);
  404. s := current_scanner.readquotedstring;
  405. stringdispose(outputsuffix);
  406. outputsuffix := stringdup(s);
  407. with current_module do
  408. setfilename(paramfn^, paramallowoutput);
  409. end;
  410. procedure dir_extension;
  411. var
  412. s : string;
  413. begin
  414. current_scanner.skipspace;
  415. if c <> '''' then
  416. Message2(scan_f_syn_expected, '''', c);
  417. s := current_scanner.readquotedstring;
  418. outputextension := '.'+s;
  419. with current_module do
  420. setfilename(paramfn^, paramallowoutput);
  421. end;
  422. {
  423. Compile time expression type check
  424. ----------------------------------
  425. Each subexpression returns its type to the caller, which then can
  426. do type check. Since data types of compile time expressions is
  427. not well defined, the type system does a best effort. The drawback is
  428. that some errors might not be detected.
  429. Instead of returning a particular data type, a set of possible data types
  430. are returned. This way ambigouos types can be handled. For instance a
  431. value of 1 can be both a boolean and and integer.
  432. Booleans
  433. --------
  434. The following forms of boolean values are supported:
  435. * C coded, that is 0 is false, non-zero is true.
  436. * TRUE/FALSE for mac style compile time variables
  437. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  438. When a compile time expression is evaluated, they are then translated
  439. to C coded booleans (0/1), to simplify for the expression evaluator.
  440. Note that this scheme then also of support mac compile time variables which
  441. are 0/1 but with a boolean meaning.
  442. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  443. means that units which is not recompiled, and thus stores
  444. compile time variables as the old format (0/1), continue to work.
  445. Short circuit evaluation
  446. ------------------------
  447. For this to work, the part of a compile time expression which is short
  448. circuited, should not be evaluated, while it still should be parsed.
  449. Therefor there is a parameter eval, telling whether evaluation is needed.
  450. In case not, the value returned can be arbitrary.
  451. }
  452. type
  453. {Compile time expression types}
  454. TCTEType = (ctetBoolean, ctetInteger, ctetString, ctetSet);
  455. TCTETypeSet = set of TCTEType;
  456. const
  457. cteTypeNames : array[TCTEType] of string[10] = (
  458. 'BOOLEAN','INTEGER','STRING','SET');
  459. {Subset of types which can be elements in sets.}
  460. setElementTypes = [ctetBoolean, ctetInteger, ctetString];
  461. function GetCTETypeName(t: TCTETypeSet): String;
  462. var
  463. i: TCTEType;
  464. begin
  465. result:= '';
  466. for i:= Low(TCTEType) to High(TCTEType) do
  467. if i in t then
  468. if result = '' then
  469. result:= cteTypeNames[i]
  470. else
  471. result:= result + ' or ' + cteTypeNames[i];
  472. end;
  473. procedure CTEError(actType, desiredExprType: TCTETypeSet; place: String);
  474. begin
  475. Message3(scan_e_compile_time_typeerror,
  476. GetCTETypeName(desiredExprType),
  477. GetCTETypeName(actType),
  478. place
  479. );
  480. end;
  481. function parse_compiler_expr(var compileExprType: TCTETypeSet):string;
  482. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string; forward;
  483. procedure preproc_consume(t : ttoken);
  484. begin
  485. if t<>current_scanner.preproc_token then
  486. Message(scan_e_preproc_syntax_error);
  487. current_scanner.preproc_token:=current_scanner.readpreproc;
  488. end;
  489. function preproc_substitutedtoken(var macroType: TCTETypeSet; eval : Boolean): string;
  490. { Currently this parses identifiers as well as numbers.
  491. The result from this procedure can either be that the token
  492. itself is a value, or that it is a compile time variable/macro,
  493. which then is substituted for another value (for macros
  494. recursivelly substituted).}
  495. var
  496. hs: string;
  497. mac : tmacro;
  498. macrocount,
  499. len : integer;
  500. numres : longint;
  501. w: word;
  502. begin
  503. result := current_scanner.preproc_pattern;
  504. if not eval then
  505. exit;
  506. mac:= nil;
  507. { Substitue macros and compiler variables with their content/value.
  508. For real macros also do recursive substitution. }
  509. macrocount:=0;
  510. repeat
  511. mac:=tmacro(search_macro(result));
  512. inc(macrocount);
  513. if macrocount>max_macro_nesting then
  514. begin
  515. Message(scan_w_macro_too_deep);
  516. break;
  517. end;
  518. if assigned(mac) and mac.defined then
  519. if assigned(mac.buftext) then
  520. begin
  521. if mac.buflen>255 then
  522. begin
  523. len:=255;
  524. Message(scan_w_macro_cut_after_255_chars);
  525. end
  526. else
  527. len:=mac.buflen;
  528. hs[0]:=char(len);
  529. move(mac.buftext^,hs[1],len);
  530. result:=upcase(hs);
  531. mac.is_used:=true;
  532. end
  533. else
  534. begin
  535. Message1(scan_e_error_macro_lacks_value, result);
  536. break;
  537. end
  538. else
  539. begin
  540. break;
  541. end;
  542. if mac.is_compiler_var then
  543. break;
  544. until false;
  545. { At this point, result do contain the value. Do some decoding and
  546. determine the type.}
  547. val(result,numres,w);
  548. if (w=0) then {It is an integer}
  549. begin
  550. if (numres = 0) or (numres = 1) then
  551. macroType := [ctetInteger, ctetBoolean]
  552. else
  553. macroType := [ctetInteger];
  554. end
  555. else if assigned(mac) and (m_mac in aktmodeswitches) and (result='FALSE') then
  556. begin
  557. result:= '0';
  558. macroType:= [ctetBoolean];
  559. end
  560. else if assigned(mac) and (m_mac in aktmodeswitches) and (result='TRUE') then
  561. begin
  562. result:= '1';
  563. macroType:= [ctetBoolean];
  564. end
  565. else if (m_mac in aktmodeswitches) and
  566. (not assigned(mac) or not mac.defined) and
  567. (macrocount = 1) then
  568. begin
  569. {Errors in mode mac is issued here. For non macpas modes there is
  570. more liberty, but the error will eventually be caught at a later stage.}
  571. Message1(scan_e_error_macro_undefined, result);
  572. macroType:= [ctetString]; {Just to have something}
  573. end
  574. else
  575. macroType:= [ctetString];
  576. end;
  577. function read_factor(var factorType: TCTETypeSet; eval : Boolean) : string;
  578. var
  579. hs : string;
  580. mac: tmacro;
  581. srsym : tsym;
  582. srsymtable : tsymtable;
  583. l : longint;
  584. w : integer;
  585. hasKlammer: Boolean;
  586. setElemType : TCTETypeSet;
  587. begin
  588. if current_scanner.preproc_token=_ID then
  589. begin
  590. if current_scanner.preproc_pattern='DEFINED' then
  591. begin
  592. factorType:= [ctetBoolean];
  593. preproc_consume(_ID);
  594. current_scanner.skipspace;
  595. if current_scanner.preproc_token =_LKLAMMER then
  596. begin
  597. preproc_consume(_LKLAMMER);
  598. current_scanner.skipspace;
  599. hasKlammer:= true;
  600. end
  601. else if (m_mac in aktmodeswitches) then
  602. hasKlammer:= false
  603. else
  604. Message(scan_e_error_in_preproc_expr);
  605. if current_scanner.preproc_token =_ID then
  606. begin
  607. hs := current_scanner.preproc_pattern;
  608. mac := tmacro(search_macro(hs));
  609. if assigned(mac) and mac.defined then
  610. begin
  611. hs := '1';
  612. mac.is_used:=true;
  613. end
  614. else
  615. hs := '0';
  616. read_factor := hs;
  617. preproc_consume(_ID);
  618. current_scanner.skipspace;
  619. end
  620. else
  621. Message(scan_e_error_in_preproc_expr);
  622. if hasKlammer then
  623. if current_scanner.preproc_token =_RKLAMMER then
  624. preproc_consume(_RKLAMMER)
  625. else
  626. Message(scan_e_error_in_preproc_expr);
  627. end
  628. else
  629. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  630. begin
  631. factorType:= [ctetBoolean];
  632. preproc_consume(_ID);
  633. current_scanner.skipspace;
  634. if current_scanner.preproc_token =_ID then
  635. begin
  636. hs := current_scanner.preproc_pattern;
  637. mac := tmacro(search_macro(hs));
  638. if assigned(mac) then
  639. begin
  640. hs := '0';
  641. mac.is_used:=true;
  642. end
  643. else
  644. hs := '1';
  645. read_factor := hs;
  646. preproc_consume(_ID);
  647. current_scanner.skipspace;
  648. end
  649. else
  650. Message(scan_e_error_in_preproc_expr);
  651. end
  652. else
  653. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='OPTION') then
  654. begin
  655. factorType:= [ctetBoolean];
  656. preproc_consume(_ID);
  657. current_scanner.skipspace;
  658. if current_scanner.preproc_token =_LKLAMMER then
  659. begin
  660. preproc_consume(_LKLAMMER);
  661. current_scanner.skipspace;
  662. end
  663. else
  664. Message(scan_e_error_in_preproc_expr);
  665. if not (current_scanner.preproc_token = _ID) then
  666. Message(scan_e_error_in_preproc_expr);
  667. hs:=current_scanner.preproc_pattern;
  668. if (length(hs) > 1) then
  669. {This is allowed in Metrowerks Pascal}
  670. Message(scan_e_error_in_preproc_expr)
  671. else
  672. begin
  673. if CheckSwitch(hs[1],'+') then
  674. read_factor := '1'
  675. else
  676. read_factor := '0';
  677. end;
  678. preproc_consume(_ID);
  679. current_scanner.skipspace;
  680. if current_scanner.preproc_token =_RKLAMMER then
  681. preproc_consume(_RKLAMMER)
  682. else
  683. Message(scan_e_error_in_preproc_expr);
  684. end
  685. else
  686. if current_scanner.preproc_pattern='SIZEOF' then
  687. begin
  688. factorType:= [ctetInteger];
  689. preproc_consume(_ID);
  690. current_scanner.skipspace;
  691. if current_scanner.preproc_token =_LKLAMMER then
  692. begin
  693. preproc_consume(_LKLAMMER);
  694. current_scanner.skipspace;
  695. end
  696. else
  697. Message(scan_e_preproc_syntax_error);
  698. if eval then
  699. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  700. begin
  701. l:=0;
  702. case srsym.typ of
  703. globalvarsym,
  704. localvarsym,
  705. paravarsym :
  706. l:=tabstractvarsym(srsym).getsize;
  707. typedconstsym :
  708. l:=ttypedconstsym(srsym).getsize;
  709. typesym:
  710. l:=ttypesym(srsym).restype.def.size;
  711. else
  712. Message(scan_e_error_in_preproc_expr);
  713. end;
  714. str(l,read_factor);
  715. end
  716. else
  717. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  718. preproc_consume(_ID);
  719. current_scanner.skipspace;
  720. if current_scanner.preproc_token =_RKLAMMER then
  721. preproc_consume(_RKLAMMER)
  722. else
  723. Message(scan_e_preproc_syntax_error);
  724. end
  725. else
  726. if current_scanner.preproc_pattern='DECLARED' then
  727. begin
  728. factorType:= [ctetBoolean];
  729. preproc_consume(_ID);
  730. current_scanner.skipspace;
  731. if current_scanner.preproc_token =_LKLAMMER then
  732. begin
  733. preproc_consume(_LKLAMMER);
  734. current_scanner.skipspace;
  735. end
  736. else
  737. Message(scan_e_error_in_preproc_expr);
  738. if current_scanner.preproc_token =_ID then
  739. begin
  740. hs := upper(current_scanner.preproc_pattern);
  741. if searchsym(hs,srsym,srsymtable) then
  742. hs := '1'
  743. else
  744. hs := '0';
  745. read_factor := hs;
  746. preproc_consume(_ID);
  747. current_scanner.skipspace;
  748. end
  749. else
  750. Message(scan_e_error_in_preproc_expr);
  751. if current_scanner.preproc_token =_RKLAMMER then
  752. preproc_consume(_RKLAMMER)
  753. else
  754. Message(scan_e_error_in_preproc_expr);
  755. end
  756. else
  757. if current_scanner.preproc_pattern='NOT' then
  758. begin
  759. factorType:= [ctetBoolean];
  760. preproc_consume(_ID);
  761. hs:=read_factor(factorType, eval);
  762. if eval then
  763. begin
  764. if not (ctetBoolean in factorType) then
  765. CTEError(factorType, [ctetBoolean], 'NOT');
  766. val(hs,l,w);
  767. if l<>0 then
  768. read_factor:='0'
  769. else
  770. read_factor:='1';
  771. end
  772. else
  773. read_factor:='0'; {Just to have something}
  774. end
  775. else
  776. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='TRUE') then
  777. begin
  778. factorType:= [ctetBoolean];
  779. preproc_consume(_ID);
  780. read_factor:='1';
  781. end
  782. else
  783. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='FALSE') then
  784. begin
  785. factorType:= [ctetBoolean];
  786. preproc_consume(_ID);
  787. read_factor:='0';
  788. end
  789. else
  790. begin
  791. hs:=preproc_substitutedtoken(factorType, eval);
  792. { Default is to return the original symbol }
  793. read_factor:=hs;
  794. if eval and (m_delphi in aktmodeswitches) and (ctetString in factorType) then
  795. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  796. begin
  797. case srsym.typ of
  798. constsym :
  799. begin
  800. with tconstsym(srsym) do
  801. begin
  802. case consttyp of
  803. constord :
  804. begin
  805. case consttype.def.deftype of
  806. orddef:
  807. begin
  808. if is_integer(consttype.def) then
  809. begin
  810. read_factor:=tostr(value.valueord);
  811. factorType:= [ctetInteger];
  812. end
  813. else if is_boolean(consttype.def) then
  814. begin
  815. read_factor:=tostr(value.valueord);
  816. factorType:= [ctetBoolean];
  817. end
  818. else if is_char(consttype.def) then
  819. begin
  820. read_factor:=chr(value.valueord);
  821. factorType:= [ctetString];
  822. end
  823. end;
  824. enumdef:
  825. begin
  826. read_factor:=tostr(value.valueord);
  827. factorType:= [ctetInteger];
  828. end;
  829. end;
  830. end;
  831. conststring :
  832. begin
  833. read_factor := upper(pchar(value.valueptr));
  834. factorType:= [ctetString];
  835. end;
  836. constset :
  837. begin
  838. hs:=',';
  839. for l:=0 to 255 do
  840. if l in pconstset(tconstsym(srsym).value.valueptr)^ then
  841. hs:=hs+tostr(l)+',';
  842. read_factor := hs;
  843. factorType:= [ctetSet];
  844. end;
  845. end;
  846. end;
  847. end;
  848. enumsym :
  849. begin
  850. read_factor:=tostr(tenumsym(srsym).value);
  851. factorType:= [ctetInteger];
  852. end;
  853. end;
  854. end;
  855. preproc_consume(_ID);
  856. current_scanner.skipspace;
  857. end
  858. end
  859. else if current_scanner.preproc_token =_LKLAMMER then
  860. begin
  861. preproc_consume(_LKLAMMER);
  862. read_factor:=read_expr(factorType, eval);
  863. preproc_consume(_RKLAMMER);
  864. end
  865. else if current_scanner.preproc_token = _LECKKLAMMER then
  866. begin
  867. preproc_consume(_LECKKLAMMER);
  868. read_factor := ',';
  869. while current_scanner.preproc_token = _ID do
  870. begin
  871. read_factor := read_factor+read_factor(setElemType, eval)+',';
  872. if current_scanner.preproc_token = _COMMA then
  873. preproc_consume(_COMMA);
  874. end;
  875. // TODO Add check of setElemType
  876. preproc_consume(_RECKKLAMMER);
  877. factorType:= [ctetSet];
  878. end
  879. else
  880. Message(scan_e_error_in_preproc_expr);
  881. end;
  882. function read_term(var termType: TCTETypeSet; eval : Boolean) : string;
  883. var
  884. hs1,hs2 : string;
  885. l1,l2 : longint;
  886. w : integer;
  887. termType2: TCTETypeSet;
  888. begin
  889. hs1:=read_factor(termType, eval);
  890. repeat
  891. if (current_scanner.preproc_token<>_ID) then
  892. break;
  893. if current_scanner.preproc_pattern<>'AND' then
  894. break;
  895. val(hs1,l1,w);
  896. if l1=0 then
  897. eval:= false; {Short circuit evaluation of OR}
  898. if eval then
  899. begin
  900. {Check if first expr is boolean. Must be done here, after we know
  901. it is an AND expression.}
  902. if not (ctetBoolean in termType) then
  903. CTEError(termType, [ctetBoolean], 'AND');
  904. termType:= [ctetBoolean];
  905. end;
  906. preproc_consume(_ID);
  907. hs2:=read_factor(termType2, eval);
  908. if eval then
  909. begin
  910. if not (ctetBoolean in termType2) then
  911. CTEError(termType2, [ctetBoolean], 'AND');
  912. val(hs2,l2,w);
  913. if (l1<>0) and (l2<>0) then
  914. hs1:='1'
  915. else
  916. hs1:='0';
  917. end;
  918. until false;
  919. read_term:=hs1;
  920. end;
  921. function read_simple_expr(var simpleExprType: TCTETypeSet; eval : Boolean) : string;
  922. var
  923. hs1,hs2 : string;
  924. l1,l2 : longint;
  925. w : integer;
  926. simpleExprType2: TCTETypeSet;
  927. begin
  928. hs1:=read_term(simpleExprType, eval);
  929. repeat
  930. if (current_scanner.preproc_token<>_ID) then
  931. break;
  932. if current_scanner.preproc_pattern<>'OR' then
  933. break;
  934. val(hs1,l1,w);
  935. if l1<>0 then
  936. eval:= false; {Short circuit evaluation of OR}
  937. if eval then
  938. begin
  939. {Check if first expr is boolean. Must be done here, after we know
  940. it is an OR expression.}
  941. if not (ctetBoolean in simpleExprType) then
  942. CTEError(simpleExprType, [ctetBoolean], 'OR');
  943. simpleExprType:= [ctetBoolean];
  944. end;
  945. preproc_consume(_ID);
  946. hs2:=read_term(simpleExprType2, eval);
  947. if eval then
  948. begin
  949. if not (ctetBoolean in simpleExprType2) then
  950. CTEError(simpleExprType2, [ctetBoolean], 'OR');
  951. val(hs2,l2,w);
  952. if (l1<>0) or (l2<>0) then
  953. hs1:='1'
  954. else
  955. hs1:='0';
  956. end;
  957. until false;
  958. read_simple_expr:=hs1;
  959. end;
  960. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string;
  961. var
  962. hs1,hs2 : string;
  963. b : boolean;
  964. op : ttoken;
  965. w : integer;
  966. l1,l2 : longint;
  967. exprType2: TCTETypeSet;
  968. begin
  969. hs1:=read_simple_expr(exprType, eval);
  970. op:=current_scanner.preproc_token;
  971. if (op = _ID) and (current_scanner.preproc_pattern = 'IN') then
  972. op := _IN;
  973. if not (op in [_IN,_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  974. begin
  975. read_expr:=hs1;
  976. exit;
  977. end;
  978. if (op = _IN) then
  979. preproc_consume(_ID)
  980. else
  981. preproc_consume(op);
  982. hs2:=read_simple_expr(exprType2, eval);
  983. if eval then
  984. begin
  985. if op = _IN then
  986. begin
  987. if exprType2 <> [ctetSet] then
  988. CTEError(exprType2, [ctetSet], 'IN');
  989. if exprType = [ctetSet] then
  990. CTEError(exprType, setElementTypes, 'IN');
  991. if is_number(hs1) and is_number(hs2) then
  992. Message(scan_e_preproc_syntax_error)
  993. else if hs2[1] = ',' then
  994. b:=pos(','+hs1+',', hs2) > 0 { TODO For integer sets, perhaps check for numeric equivalence so that 0 = 00 }
  995. else
  996. Message(scan_e_preproc_syntax_error);
  997. end
  998. else
  999. begin
  1000. if (exprType * exprType2) = [] then
  1001. CTEError(exprType2, exprType, tokeninfo^[op].str);
  1002. if is_number(hs1) and is_number(hs2) then
  1003. begin
  1004. val(hs1,l1,w);
  1005. val(hs2,l2,w);
  1006. case op of
  1007. _EQUAL :
  1008. b:=l1=l2;
  1009. _UNEQUAL :
  1010. b:=l1<>l2;
  1011. _LT :
  1012. b:=l1<l2;
  1013. _GT :
  1014. b:=l1>l2;
  1015. _GTE :
  1016. b:=l1>=l2;
  1017. _LTE :
  1018. b:=l1<=l2;
  1019. end;
  1020. end
  1021. else
  1022. begin
  1023. case op of
  1024. _EQUAL :
  1025. b:=hs1=hs2;
  1026. _UNEQUAL :
  1027. b:=hs1<>hs2;
  1028. _LT :
  1029. b:=hs1<hs2;
  1030. _GT :
  1031. b:=hs1>hs2;
  1032. _GTE :
  1033. b:=hs1>=hs2;
  1034. _LTE :
  1035. b:=hs1<=hs2;
  1036. end;
  1037. end;
  1038. end;
  1039. end
  1040. else
  1041. b:= false; {Just to have something}
  1042. if b then
  1043. read_expr:='1'
  1044. else
  1045. read_expr:='0';
  1046. exprType:= [ctetBoolean];
  1047. end;
  1048. begin
  1049. current_scanner.skipspace;
  1050. { start preproc expression scanner }
  1051. current_scanner.preproc_token:=current_scanner.readpreproc;
  1052. parse_compiler_expr:=read_expr(compileExprType, true);
  1053. end;
  1054. function boolean_compile_time_expr(var valuedescr: String): Boolean;
  1055. var
  1056. hs : string;
  1057. exprType: TCTETypeSet;
  1058. begin
  1059. hs:=parse_compiler_expr(exprType);
  1060. if (exprType * [ctetBoolean]) = [] then
  1061. CTEError(exprType, [ctetBoolean], 'IF or ELSEIF');
  1062. boolean_compile_time_expr:= hs <> '0';
  1063. valuedescr:= hs;
  1064. end;
  1065. procedure dir_if;
  1066. begin
  1067. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1068. end;
  1069. procedure dir_elseif;
  1070. begin
  1071. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1072. end;
  1073. procedure dir_define_impl(macstyle: boolean);
  1074. var
  1075. hs : string;
  1076. bracketcount : longint;
  1077. mac : tmacro;
  1078. macropos : longint;
  1079. macrobuffer : pmacrobuffer;
  1080. begin
  1081. current_scanner.skipspace;
  1082. hs:=current_scanner.readid;
  1083. mac:=tmacro(search_macro(hs));
  1084. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  1085. begin
  1086. mac:=tmacro.create(hs);
  1087. mac.defined:=true;
  1088. Message1(parser_c_macro_defined,mac.name);
  1089. macrosymtablestack.insert(mac);
  1090. end
  1091. else
  1092. begin
  1093. Message1(parser_c_macro_defined,mac.name);
  1094. mac.defined:=true;
  1095. mac.is_compiler_var:=false;
  1096. { delete old definition }
  1097. if assigned(mac.buftext) then
  1098. begin
  1099. freemem(mac.buftext,mac.buflen);
  1100. mac.buftext:=nil;
  1101. end;
  1102. end;
  1103. mac.is_used:=true;
  1104. if (cs_support_macro in aktmoduleswitches) then
  1105. begin
  1106. { !!!!!! handle macro params, need we this? }
  1107. current_scanner.skipspace;
  1108. if not macstyle then
  1109. begin
  1110. { may be a macro? }
  1111. if c <> ':' then
  1112. exit;
  1113. current_scanner.readchar;
  1114. if c <> '=' then
  1115. exit;
  1116. current_scanner.readchar;
  1117. current_scanner.skipspace;
  1118. end;
  1119. { key words are never substituted }
  1120. if is_keyword(hs) then
  1121. Message(scan_e_keyword_cant_be_a_macro);
  1122. new(macrobuffer);
  1123. macropos:=0;
  1124. { parse macro, brackets are counted so it's possible
  1125. to have a $ifdef etc. in the macro }
  1126. bracketcount:=0;
  1127. repeat
  1128. case c of
  1129. '}' :
  1130. if (bracketcount=0) then
  1131. break
  1132. else
  1133. dec(bracketcount);
  1134. '{' :
  1135. inc(bracketcount);
  1136. #10,#13 :
  1137. current_scanner.linebreak;
  1138. #26 :
  1139. current_scanner.end_of_file;
  1140. end;
  1141. macrobuffer^[macropos]:=c;
  1142. inc(macropos);
  1143. if macropos>=maxmacrolen then
  1144. Message(scan_f_macro_buffer_overflow);
  1145. current_scanner.readchar;
  1146. until false;
  1147. { free buffer of macro ?}
  1148. if assigned(mac.buftext) then
  1149. freemem(mac.buftext,mac.buflen);
  1150. { get new mem }
  1151. getmem(mac.buftext,macropos);
  1152. mac.buflen:=macropos;
  1153. { copy the text }
  1154. move(macrobuffer^,mac.buftext^,macropos);
  1155. dispose(macrobuffer);
  1156. end
  1157. else
  1158. begin
  1159. { check if there is an assignment, then we need to give a
  1160. warning }
  1161. current_scanner.skipspace;
  1162. if c=':' then
  1163. begin
  1164. current_scanner.readchar;
  1165. if c='=' then
  1166. Message(scan_w_macro_support_turned_off);
  1167. end;
  1168. end;
  1169. end;
  1170. procedure dir_define;
  1171. begin
  1172. dir_define_impl(false);
  1173. end;
  1174. procedure dir_definec;
  1175. begin
  1176. dir_define_impl(true);
  1177. end;
  1178. procedure dir_setc;
  1179. var
  1180. hs : string;
  1181. mac : tmacro;
  1182. exprType: TCTETypeSet;
  1183. l : longint;
  1184. w : integer;
  1185. begin
  1186. current_scanner.skipspace;
  1187. hs:=current_scanner.readid;
  1188. mac:=tmacro(search_macro(hs));
  1189. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  1190. begin
  1191. mac:=tmacro.create(hs);
  1192. mac.defined:=true;
  1193. mac.is_compiler_var:=true;
  1194. Message1(parser_c_macro_defined,mac.name);
  1195. macrosymtablestack.insert(mac);
  1196. end
  1197. else
  1198. begin
  1199. mac.defined:=true;
  1200. mac.is_compiler_var:=true;
  1201. { delete old definition }
  1202. if assigned(mac.buftext) then
  1203. begin
  1204. freemem(mac.buftext,mac.buflen);
  1205. mac.buftext:=nil;
  1206. end;
  1207. end;
  1208. mac.is_used:=true;
  1209. { key words are never substituted }
  1210. if is_keyword(hs) then
  1211. Message(scan_e_keyword_cant_be_a_macro);
  1212. { !!!!!! handle macro params, need we this? }
  1213. current_scanner.skipspace;
  1214. { may be a macro? }
  1215. { assignment can be both := and = }
  1216. if c=':' then
  1217. current_scanner.readchar;
  1218. if c='=' then
  1219. begin
  1220. current_scanner.readchar;
  1221. hs:= parse_compiler_expr(exprType);
  1222. if (exprType * [ctetBoolean, ctetInteger]) = [] then
  1223. CTEError(exprType, [ctetBoolean, ctetInteger], 'SETC');
  1224. if length(hs) <> 0 then
  1225. begin
  1226. {If we are absolutely shure it is boolean, translate
  1227. to TRUE/FALSE to increase possibility to do future type check}
  1228. if exprType = [ctetBoolean] then
  1229. begin
  1230. val(hs,l,w);
  1231. if l<>0 then
  1232. hs:='TRUE'
  1233. else
  1234. hs:='FALSE';
  1235. end;
  1236. Message2(parser_c_macro_set_to,mac.name,hs);
  1237. { free buffer of macro ?}
  1238. if assigned(mac.buftext) then
  1239. freemem(mac.buftext,mac.buflen);
  1240. { get new mem }
  1241. getmem(mac.buftext,length(hs));
  1242. mac.buflen:=length(hs);
  1243. { copy the text }
  1244. move(hs[1],mac.buftext^,mac.buflen);
  1245. end
  1246. else
  1247. Message(scan_e_preproc_syntax_error);
  1248. end
  1249. else
  1250. Message(scan_e_preproc_syntax_error);
  1251. end;
  1252. procedure dir_undef;
  1253. var
  1254. hs : string;
  1255. mac : tmacro;
  1256. begin
  1257. current_scanner.skipspace;
  1258. hs:=current_scanner.readid;
  1259. mac:=tmacro(search_macro(hs));
  1260. if not assigned(mac) or (mac.owner <> macrosymtablestack) then
  1261. begin
  1262. mac:=tmacro.create(hs);
  1263. Message1(parser_c_macro_undefined,mac.name);
  1264. mac.defined:=false;
  1265. macrosymtablestack.insert(mac);
  1266. end
  1267. else
  1268. begin
  1269. Message1(parser_c_macro_undefined,mac.name);
  1270. mac.defined:=false;
  1271. mac.is_compiler_var:=false;
  1272. { delete old definition }
  1273. if assigned(mac.buftext) then
  1274. begin
  1275. freemem(mac.buftext,mac.buflen);
  1276. mac.buftext:=nil;
  1277. end;
  1278. end;
  1279. mac.is_used:=true;
  1280. end;
  1281. procedure dir_include;
  1282. function findincludefile(const path,name,ext:string;var foundfile:string):boolean;
  1283. var
  1284. found : boolean;
  1285. hpath : string;
  1286. begin
  1287. (* look for the include file
  1288. If path was specified as part of {$I } then
  1289. 1. specified path (expanded with path of inputfile if relative)
  1290. else
  1291. 1. path of current inputfile,current dir
  1292. 2. local includepath
  1293. 3. global includepath *)
  1294. found:=false;
  1295. foundfile:='';
  1296. hpath:='';
  1297. if path<>'' then
  1298. begin
  1299. if not path_absolute(path) then
  1300. hpath:=current_scanner.inputfile.path^+path
  1301. else
  1302. hpath:=path;
  1303. found:=FindFile(name+ext, hpath,foundfile);
  1304. end
  1305. else
  1306. begin
  1307. hpath:=current_scanner.inputfile.path^+';'+CurDirRelPath(source_info);
  1308. found:=FindFile(name+ext, hpath,foundfile);
  1309. if not found then
  1310. found:=current_module.localincludesearchpath.FindFile(name+ext,foundfile);
  1311. if not found then
  1312. found:=includesearchpath.FindFile(name+ext,foundfile);
  1313. end;
  1314. findincludefile:=found;
  1315. end;
  1316. var
  1317. args,
  1318. foundfile,
  1319. hs : string;
  1320. path : dirstr;
  1321. name : namestr;
  1322. ext : extstr;
  1323. hp : tinputfile;
  1324. found : boolean;
  1325. begin
  1326. current_scanner.skipspace;
  1327. args:=current_scanner.readcomment;
  1328. hs:=GetToken(args,' ');
  1329. if hs='' then
  1330. exit;
  1331. if (hs[1]='%') then
  1332. begin
  1333. { case insensitive }
  1334. hs:=upper(hs);
  1335. { remove %'s }
  1336. Delete(hs,1,1);
  1337. if hs[length(hs)]='%' then
  1338. Delete(hs,length(hs),1);
  1339. { save old }
  1340. path:=hs;
  1341. { first check for internal macros }
  1342. if hs='TIME' then
  1343. hs:=gettimestr
  1344. else
  1345. if hs='DATE' then
  1346. hs:=getdatestr
  1347. else
  1348. if hs='FILE' then
  1349. hs:=current_module.sourcefiles.get_file_name(aktfilepos.fileindex)
  1350. else
  1351. if hs='LINE' then
  1352. hs:=tostr(aktfilepos.line)
  1353. else
  1354. if hs='FPCVERSION' then
  1355. hs:=version_string
  1356. else
  1357. if hs='FPCDATE' then
  1358. hs:=date_string
  1359. else
  1360. if hs='FPCTARGET' then
  1361. hs:=target_cpu_string
  1362. else
  1363. if hs='FPCTARGETCPU' then
  1364. hs:=target_cpu_string
  1365. else
  1366. if hs='FPCTARGETOS' then
  1367. hs:=target_info.shortname
  1368. else
  1369. hs:=getenv(hs);
  1370. if hs='' then
  1371. Message1(scan_w_include_env_not_found,path);
  1372. { make it a stringconst }
  1373. hs:=''''+hs+'''';
  1374. current_scanner.insertmacro(path,@hs[1],length(hs),
  1375. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1376. end
  1377. else
  1378. begin
  1379. hs:=FixFileName(hs);
  1380. fsplit(hs,path,name,ext);
  1381. { try to find the file }
  1382. found:=findincludefile(path,name,ext,foundfile);
  1383. if (ext='') then
  1384. begin
  1385. { try default extensions .inc , .pp and .pas }
  1386. if (not found) then
  1387. found:=findincludefile(path,name,'.inc',foundfile);
  1388. if (not found) then
  1389. found:=findincludefile(path,name,sourceext,foundfile);
  1390. if (not found) then
  1391. found:=findincludefile(path,name,pasext,foundfile);
  1392. end;
  1393. if current_scanner.inputfilecount<max_include_nesting then
  1394. begin
  1395. inc(current_scanner.inputfilecount);
  1396. { we need to reread the current char }
  1397. dec(current_scanner.inputpointer);
  1398. { shutdown current file }
  1399. current_scanner.tempcloseinputfile;
  1400. { load new file }
  1401. hp:=do_openinputfile(foundfile);
  1402. current_scanner.addfile(hp);
  1403. current_module.sourcefiles.register_file(hp);
  1404. if (not found) then
  1405. Message1(scan_f_cannot_open_includefile,hs);
  1406. if (not current_scanner.openinputfile) then
  1407. Message1(scan_f_cannot_open_includefile,hs);
  1408. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  1409. current_scanner.reload;
  1410. end
  1411. else
  1412. Message(scan_f_include_deep_ten);
  1413. end;
  1414. end;
  1415. {*****************************************************************************
  1416. Preprocessor writting
  1417. *****************************************************************************}
  1418. {$ifdef PREPROCWRITE}
  1419. constructor tpreprocfile.create(const fn:string);
  1420. begin
  1421. { open outputfile }
  1422. assign(f,fn);
  1423. {$I-}
  1424. rewrite(f);
  1425. {$I+}
  1426. if ioresult<>0 then
  1427. Comment(V_Fatal,'can''t create file '+fn);
  1428. getmem(buf,preprocbufsize);
  1429. settextbuf(f,buf^,preprocbufsize);
  1430. { reset }
  1431. eolfound:=false;
  1432. spacefound:=false;
  1433. end;
  1434. destructor tpreprocfile.destroy;
  1435. begin
  1436. close(f);
  1437. freemem(buf,preprocbufsize);
  1438. end;
  1439. procedure tpreprocfile.add(const s:string);
  1440. begin
  1441. write(f,s);
  1442. end;
  1443. procedure tpreprocfile.addspace;
  1444. begin
  1445. if eolfound then
  1446. begin
  1447. writeln(f,'');
  1448. eolfound:=false;
  1449. spacefound:=false;
  1450. end
  1451. else
  1452. if spacefound then
  1453. begin
  1454. write(f,' ');
  1455. spacefound:=false;
  1456. end;
  1457. end;
  1458. {$endif PREPROCWRITE}
  1459. {*****************************************************************************
  1460. TPreProcStack
  1461. *****************************************************************************}
  1462. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1463. begin
  1464. accept:=a;
  1465. typ:=atyp;
  1466. next:=n;
  1467. end;
  1468. {*****************************************************************************
  1469. TDirectiveItem
  1470. *****************************************************************************}
  1471. constructor TDirectiveItem.Create(const n:string;p:tdirectiveproc);
  1472. begin
  1473. inherited CreateName(n);
  1474. is_conditional:=false;
  1475. proc:=p;
  1476. end;
  1477. constructor TDirectiveItem.CreateCond(const n:string;p:tdirectiveproc);
  1478. begin
  1479. inherited CreateName(n);
  1480. is_conditional:=true;
  1481. proc:=p;
  1482. end;
  1483. {****************************************************************************
  1484. TSCANNERFILE
  1485. ****************************************************************************}
  1486. constructor tscannerfile.create(const fn:string);
  1487. begin
  1488. inputfile:=do_openinputfile(fn);
  1489. if assigned(current_module) then
  1490. current_module.sourcefiles.register_file(inputfile);
  1491. { reset localinput }
  1492. inputbuffer:=nil;
  1493. inputpointer:=nil;
  1494. inputstart:=0;
  1495. { reset scanner }
  1496. preprocstack:=nil;
  1497. comment_level:=0;
  1498. yylexcount:=0;
  1499. block_type:=bt_general;
  1500. line_no:=0;
  1501. lastlinepos:=0;
  1502. lasttokenpos:=0;
  1503. lasttoken:=NOTOKEN;
  1504. nexttoken:=NOTOKEN;
  1505. lastasmgetchar:=#0;
  1506. ignoredirectives:=TStringList.Create;
  1507. in_asm_string:=false;
  1508. end;
  1509. procedure tscannerfile.firstfile;
  1510. begin
  1511. { load block }
  1512. if not openinputfile then
  1513. Message1(scan_f_cannot_open_input,inputfile.name^);
  1514. reload;
  1515. end;
  1516. destructor tscannerfile.destroy;
  1517. begin
  1518. if assigned(current_module) and
  1519. (current_module.state=ms_compiled) and
  1520. (status.errorcount=0) then
  1521. checkpreprocstack
  1522. else
  1523. begin
  1524. while assigned(preprocstack) do
  1525. poppreprocstack;
  1526. end;
  1527. if not inputfile.closed then
  1528. closeinputfile;
  1529. ignoredirectives.free;
  1530. end;
  1531. function tscannerfile.openinputfile:boolean;
  1532. begin
  1533. openinputfile:=inputfile.open;
  1534. { load buffer }
  1535. inputbuffer:=inputfile.buf;
  1536. inputpointer:=inputfile.buf;
  1537. inputstart:=inputfile.bufstart;
  1538. { line }
  1539. line_no:=0;
  1540. lastlinepos:=0;
  1541. lasttokenpos:=0;
  1542. end;
  1543. procedure tscannerfile.closeinputfile;
  1544. begin
  1545. inputfile.close;
  1546. { reset buffer }
  1547. inputbuffer:=nil;
  1548. inputpointer:=nil;
  1549. inputstart:=0;
  1550. { reset line }
  1551. line_no:=0;
  1552. lastlinepos:=0;
  1553. lasttokenpos:=0;
  1554. end;
  1555. function tscannerfile.tempopeninputfile:boolean;
  1556. begin
  1557. if inputfile.is_macro then
  1558. exit;
  1559. tempopeninputfile:=inputfile.tempopen;
  1560. { reload buffer }
  1561. inputbuffer:=inputfile.buf;
  1562. inputpointer:=inputfile.buf;
  1563. inputstart:=inputfile.bufstart;
  1564. end;
  1565. procedure tscannerfile.tempcloseinputfile;
  1566. begin
  1567. if inputfile.closed or inputfile.is_macro then
  1568. exit;
  1569. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1570. inputfile.tempclose;
  1571. { reset buffer }
  1572. inputbuffer:=nil;
  1573. inputpointer:=nil;
  1574. inputstart:=0;
  1575. end;
  1576. procedure tscannerfile.saveinputfile;
  1577. begin
  1578. inputfile.saveinputpointer:=inputpointer;
  1579. inputfile.savelastlinepos:=lastlinepos;
  1580. inputfile.saveline_no:=line_no;
  1581. end;
  1582. procedure tscannerfile.restoreinputfile;
  1583. begin
  1584. inputpointer:=inputfile.saveinputpointer;
  1585. lastlinepos:=inputfile.savelastlinepos;
  1586. line_no:=inputfile.saveline_no;
  1587. if not inputfile.is_macro then
  1588. parser_current_file:=inputfile.name^;
  1589. end;
  1590. procedure tscannerfile.nextfile;
  1591. var
  1592. to_dispose : tinputfile;
  1593. begin
  1594. if assigned(inputfile.next) then
  1595. begin
  1596. if inputfile.is_macro then
  1597. to_dispose:=inputfile
  1598. else
  1599. begin
  1600. to_dispose:=nil;
  1601. dec(inputfilecount);
  1602. end;
  1603. { we can allways close the file, no ? }
  1604. inputfile.close;
  1605. inputfile:=inputfile.next;
  1606. if assigned(to_dispose) then
  1607. to_dispose.free;
  1608. restoreinputfile;
  1609. end;
  1610. end;
  1611. procedure tscannerfile.addfile(hp:tinputfile);
  1612. begin
  1613. saveinputfile;
  1614. { add to list }
  1615. hp.next:=inputfile;
  1616. inputfile:=hp;
  1617. { load new inputfile }
  1618. restoreinputfile;
  1619. end;
  1620. procedure tscannerfile.reload;
  1621. begin
  1622. with inputfile do
  1623. begin
  1624. { when nothing more to read then leave immediatly, so we
  1625. don't change the aktfilepos and leave it point to the last
  1626. char }
  1627. if (c=#26) and (not assigned(next)) then
  1628. exit;
  1629. repeat
  1630. { still more to read?, then change the #0 to a space so its seen
  1631. as a seperator, this can't be used for macro's which can change
  1632. the place of the #0 in the buffer with tempopen }
  1633. if (c=#0) and (bufsize>0) and
  1634. not(inputfile.is_macro) and
  1635. (inputpointer-inputbuffer<bufsize) then
  1636. begin
  1637. c:=' ';
  1638. inc(inputpointer);
  1639. exit;
  1640. end;
  1641. { can we read more from this file ? }
  1642. if (c<>#26) and (not endoffile) then
  1643. begin
  1644. readbuf;
  1645. inputpointer:=buf;
  1646. inputbuffer:=buf;
  1647. inputstart:=bufstart;
  1648. { first line? }
  1649. if line_no=0 then
  1650. begin
  1651. c:=inputpointer^;
  1652. { eat utf-8 signature? }
  1653. if (ord(inputpointer^)=$ef) and
  1654. (ord((inputpointer+1)^)=$bb) and
  1655. (ord((inputpointer+2)^)=$bf) then
  1656. begin
  1657. inc(inputpointer,3);
  1658. message(scan_c_switching_to_utf8);
  1659. aktsourcecodepage:='utf8';
  1660. end;
  1661. line_no:=1;
  1662. if cs_asm_source in aktglobalswitches then
  1663. inputfile.setline(line_no,bufstart);
  1664. end;
  1665. end
  1666. else
  1667. begin
  1668. { load eof position in tokenpos/aktfilepos }
  1669. gettokenpos;
  1670. { close file }
  1671. closeinputfile;
  1672. { no next module, than EOF }
  1673. if not assigned(inputfile.next) then
  1674. begin
  1675. c:=#26;
  1676. exit;
  1677. end;
  1678. { load next file and reopen it }
  1679. nextfile;
  1680. tempopeninputfile;
  1681. { status }
  1682. Message1(scan_t_back_in,inputfile.name^);
  1683. end;
  1684. { load next char }
  1685. c:=inputpointer^;
  1686. inc(inputpointer);
  1687. until c<>#0; { if also end, then reload again }
  1688. end;
  1689. end;
  1690. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1691. var
  1692. hp : tinputfile;
  1693. begin
  1694. { save old postion }
  1695. dec(inputpointer);
  1696. tempcloseinputfile;
  1697. { create macro 'file' }
  1698. { use special name to dispose after !! }
  1699. hp:=do_openinputfile('_Macro_.'+macname);
  1700. addfile(hp);
  1701. with inputfile do
  1702. begin
  1703. setmacro(p,len);
  1704. { local buffer }
  1705. inputbuffer:=buf;
  1706. inputpointer:=buf;
  1707. inputstart:=bufstart;
  1708. ref_index:=fileindex;
  1709. end;
  1710. { reset line }
  1711. line_no:=line;
  1712. lastlinepos:=0;
  1713. lasttokenpos:=0;
  1714. { load new c }
  1715. c:=inputpointer^;
  1716. inc(inputpointer);
  1717. end;
  1718. procedure tscannerfile.gettokenpos;
  1719. { load the values of tokenpos and lasttokenpos }
  1720. begin
  1721. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1722. akttokenpos.line:=line_no;
  1723. akttokenpos.column:=lasttokenpos-lastlinepos;
  1724. akttokenpos.fileindex:=inputfile.ref_index;
  1725. aktfilepos:=akttokenpos;
  1726. end;
  1727. procedure tscannerfile.inc_comment_level;
  1728. var
  1729. oldaktfilepos : tfileposinfo;
  1730. begin
  1731. if (m_nested_comment in aktmodeswitches) then
  1732. inc(comment_level)
  1733. else
  1734. comment_level:=1;
  1735. if (comment_level>1) then
  1736. begin
  1737. oldaktfilepos:=aktfilepos;
  1738. gettokenpos; { update for warning }
  1739. Message1(scan_w_comment_level,tostr(comment_level));
  1740. aktfilepos:=oldaktfilepos;
  1741. end;
  1742. end;
  1743. procedure tscannerfile.dec_comment_level;
  1744. begin
  1745. if (m_nested_comment in aktmodeswitches) then
  1746. dec(comment_level)
  1747. else
  1748. comment_level:=0;
  1749. end;
  1750. procedure tscannerfile.linebreak;
  1751. var
  1752. cur : char;
  1753. oldtokenpos,
  1754. oldaktfilepos : tfileposinfo;
  1755. begin
  1756. with inputfile do
  1757. begin
  1758. if (byte(inputpointer^)=0) and not(endoffile) then
  1759. begin
  1760. cur:=c;
  1761. reload;
  1762. if byte(cur)+byte(c)<>23 then
  1763. dec(inputpointer);
  1764. end
  1765. else
  1766. begin
  1767. { Support all combination of #10 and #13 as line break }
  1768. if (byte(inputpointer^)+byte(c)=23) then
  1769. inc(inputpointer);
  1770. end;
  1771. { Always return #10 as line break }
  1772. c:=#10;
  1773. { increase line counters }
  1774. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1775. inc(line_no);
  1776. { update linebuffer }
  1777. if cs_asm_source in aktglobalswitches then
  1778. inputfile.setline(line_no,lastlinepos);
  1779. { update for status and call the show status routine,
  1780. but don't touch aktfilepos ! }
  1781. oldaktfilepos:=aktfilepos;
  1782. oldtokenpos:=akttokenpos;
  1783. gettokenpos; { update for v_status }
  1784. inc(status.compiledlines);
  1785. ShowStatus;
  1786. aktfilepos:=oldaktfilepos;
  1787. akttokenpos:=oldtokenpos;
  1788. end;
  1789. end;
  1790. procedure tscannerfile.illegal_char(c:char);
  1791. var
  1792. s : string;
  1793. begin
  1794. if c in [#32..#255] then
  1795. s:=''''+c+''''
  1796. else
  1797. s:='#'+tostr(ord(c));
  1798. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1799. end;
  1800. procedure tscannerfile.end_of_file;
  1801. begin
  1802. checkpreprocstack;
  1803. Message(scan_f_end_of_file);
  1804. end;
  1805. {-------------------------------------------
  1806. IF Conditional Handling
  1807. -------------------------------------------}
  1808. procedure tscannerfile.checkpreprocstack;
  1809. begin
  1810. { check for missing ifdefs }
  1811. while assigned(preprocstack) do
  1812. begin
  1813. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1814. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1815. poppreprocstack;
  1816. end;
  1817. end;
  1818. procedure tscannerfile.poppreprocstack;
  1819. var
  1820. hp : tpreprocstack;
  1821. begin
  1822. if assigned(preprocstack) then
  1823. begin
  1824. Message1(scan_c_endif_found,preprocstack.name);
  1825. hp:=preprocstack.next;
  1826. preprocstack.free;
  1827. preprocstack:=hp;
  1828. end
  1829. else
  1830. Message(scan_e_endif_without_if);
  1831. end;
  1832. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  1833. var
  1834. condition: Boolean;
  1835. valuedescr: String;
  1836. begin
  1837. if (preprocstack=nil) or preprocstack.accept then
  1838. condition:= compile_time_predicate(valuedescr)
  1839. else
  1840. begin
  1841. condition:= false;
  1842. valuedescr:= '';
  1843. end;
  1844. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  1845. preprocstack.name:=valuedescr;
  1846. preprocstack.line_nb:=line_no;
  1847. preprocstack.owner:=self;
  1848. if preprocstack.accept then
  1849. Message2(messid,preprocstack.name,'accepted')
  1850. else
  1851. Message2(messid,preprocstack.name,'rejected');
  1852. end;
  1853. procedure tscannerfile.elsepreprocstack;
  1854. begin
  1855. if assigned(preprocstack) and
  1856. (preprocstack.typ<>pp_else) then
  1857. begin
  1858. if (preprocstack.typ=pp_elseif) then
  1859. preprocstack.accept:=false
  1860. else
  1861. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  1862. preprocstack.accept:=not preprocstack.accept;
  1863. preprocstack.typ:=pp_else;
  1864. preprocstack.line_nb:=line_no;
  1865. if preprocstack.accept then
  1866. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1867. else
  1868. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1869. end
  1870. else
  1871. Message(scan_e_endif_without_if);
  1872. end;
  1873. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  1874. var
  1875. valuedescr: String;
  1876. begin
  1877. if assigned(preprocstack) and
  1878. (preprocstack.typ in [pp_if,pp_elseif]) then
  1879. begin
  1880. { when the branch is accepted we use pp_elseif so we know that
  1881. all the next branches need to be rejected. when this branch is still
  1882. not accepted then leave it at pp_if }
  1883. if (preprocstack.typ=pp_elseif) then
  1884. preprocstack.accept:=false
  1885. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  1886. begin
  1887. preprocstack.accept:=false;
  1888. preprocstack.typ:=pp_elseif;
  1889. end
  1890. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  1891. and compile_time_predicate(valuedescr) then
  1892. begin
  1893. preprocstack.name:=valuedescr;
  1894. preprocstack.accept:=true;
  1895. preprocstack.typ:=pp_elseif;
  1896. end;
  1897. preprocstack.line_nb:=line_no;
  1898. if preprocstack.accept then
  1899. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1900. else
  1901. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1902. end
  1903. else
  1904. Message(scan_e_endif_without_if);
  1905. end;
  1906. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1907. var
  1908. oldaktfilepos : tfileposinfo;
  1909. begin
  1910. oldaktfilepos:=aktfilepos;
  1911. repeat
  1912. current_scanner.gettokenpos;
  1913. p.proc();
  1914. { accept the text ? }
  1915. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1916. break
  1917. else
  1918. begin
  1919. current_scanner.gettokenpos;
  1920. Message(scan_c_skipping_until);
  1921. repeat
  1922. current_scanner.skipuntildirective;
  1923. if not (m_mac in aktmodeswitches) then
  1924. p:=tdirectiveitem(turbo_scannerdirectives.search(current_scanner.readid))
  1925. else
  1926. p:=tdirectiveitem(mac_scannerdirectives.search(current_scanner.readid));
  1927. until assigned(p) and (p.is_conditional);
  1928. current_scanner.gettokenpos;
  1929. Message1(scan_d_handling_switch,'$'+p.name);
  1930. end;
  1931. until false;
  1932. aktfilepos:=oldaktfilepos;
  1933. end;
  1934. procedure tscannerfile.handledirectives;
  1935. var
  1936. t : tdirectiveitem;
  1937. hs : string;
  1938. begin
  1939. gettokenpos;
  1940. readchar; {Remove the $}
  1941. hs:=readid;
  1942. {$ifdef PREPROCWRITE}
  1943. if parapreprocess then
  1944. begin
  1945. t:=Get_Directive(hs);
  1946. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1947. begin
  1948. preprocfile^.AddSpace;
  1949. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1950. exit;
  1951. end;
  1952. end;
  1953. {$endif PREPROCWRITE}
  1954. { skip this directive? }
  1955. if (ignoredirectives.find(hs)<>nil) then
  1956. begin
  1957. if (comment_level>0) then
  1958. readcomment;
  1959. { we've read the whole comment }
  1960. aktcommentstyle:=comment_none;
  1961. exit;
  1962. end;
  1963. if hs='' then
  1964. begin
  1965. Message1(scan_w_illegal_switch,'$'+hs);
  1966. end;
  1967. { Check for compiler switches }
  1968. while (length(hs)=1) and (c in ['-','+']) do
  1969. begin
  1970. HandleSwitch(hs[1],c);
  1971. current_scanner.readchar; {Remove + or -}
  1972. if c=',' then
  1973. begin
  1974. current_scanner.readchar; {Remove , }
  1975. { read next switch, support $v+,$+}
  1976. hs:=current_scanner.readid;
  1977. if (hs='') then
  1978. begin
  1979. if (c='$') and (m_fpc in aktmodeswitches) then
  1980. begin
  1981. current_scanner.readchar; { skip $ }
  1982. hs:=current_scanner.readid;
  1983. end;
  1984. if (hs='') then
  1985. Message1(scan_w_illegal_directive,'$'+c);
  1986. end
  1987. else
  1988. Message1(scan_d_handling_switch,'$'+hs);
  1989. end
  1990. else
  1991. hs:='';
  1992. end;
  1993. { directives may follow switches after a , }
  1994. if hs<>'' then
  1995. begin
  1996. if not (m_mac in aktmodeswitches) then
  1997. t:=tdirectiveitem(turbo_scannerdirectives.search(hs))
  1998. else
  1999. t:=tdirectiveitem(mac_scannerdirectives.search(hs));
  2000. if assigned(t) then
  2001. begin
  2002. if t.is_conditional then
  2003. handleconditional(t)
  2004. else
  2005. begin
  2006. Message1(scan_d_handling_switch,'$'+hs);
  2007. t.proc();
  2008. end;
  2009. end
  2010. else
  2011. begin
  2012. current_scanner.ignoredirectives.insert(hs);
  2013. Message1(scan_w_illegal_directive,'$'+hs);
  2014. end;
  2015. { conditionals already read the comment }
  2016. if (current_scanner.comment_level>0) then
  2017. current_scanner.readcomment;
  2018. { we've read the whole comment }
  2019. aktcommentstyle:=comment_none;
  2020. end;
  2021. end;
  2022. procedure tscannerfile.readchar;
  2023. begin
  2024. c:=inputpointer^;
  2025. if c=#0 then
  2026. reload
  2027. else
  2028. inc(inputpointer);
  2029. end;
  2030. procedure tscannerfile.readstring;
  2031. var
  2032. i : longint;
  2033. err : boolean;
  2034. begin
  2035. err:=false;
  2036. i:=0;
  2037. repeat
  2038. case c of
  2039. '_',
  2040. '0'..'9',
  2041. 'A'..'Z' :
  2042. begin
  2043. if i<255 then
  2044. begin
  2045. inc(i);
  2046. orgpattern[i]:=c;
  2047. pattern[i]:=c;
  2048. end
  2049. else
  2050. begin
  2051. if not err then
  2052. begin
  2053. Message(scan_e_string_exceeds_255_chars);
  2054. err:=true;
  2055. end;
  2056. end;
  2057. c:=inputpointer^;
  2058. inc(inputpointer);
  2059. end;
  2060. 'a'..'z' :
  2061. begin
  2062. if i<255 then
  2063. begin
  2064. inc(i);
  2065. orgpattern[i]:=c;
  2066. pattern[i]:=chr(ord(c)-32)
  2067. end
  2068. else
  2069. begin
  2070. if not err then
  2071. begin
  2072. Message(scan_e_string_exceeds_255_chars);
  2073. err:=true;
  2074. end;
  2075. end;
  2076. c:=inputpointer^;
  2077. inc(inputpointer);
  2078. end;
  2079. #0 :
  2080. reload;
  2081. else
  2082. break;
  2083. end;
  2084. until false;
  2085. orgpattern[0]:=chr(i);
  2086. pattern[0]:=chr(i);
  2087. end;
  2088. procedure tscannerfile.readnumber;
  2089. var
  2090. base,
  2091. i : longint;
  2092. begin
  2093. case c of
  2094. '%' :
  2095. begin
  2096. readchar;
  2097. base:=2;
  2098. pattern[1]:='%';
  2099. i:=1;
  2100. end;
  2101. '&' :
  2102. begin
  2103. readchar;
  2104. base:=8;
  2105. pattern[1]:='&';
  2106. i:=1;
  2107. end;
  2108. '$' :
  2109. begin
  2110. readchar;
  2111. base:=16;
  2112. pattern[1]:='$';
  2113. i:=1;
  2114. end;
  2115. else
  2116. begin
  2117. base:=10;
  2118. i:=0;
  2119. end;
  2120. end;
  2121. while ((base>=10) and (c in ['0'..'9'])) or
  2122. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  2123. ((base=8) and (c in ['0'..'7'])) or
  2124. ((base=2) and (c in ['0'..'1'])) do
  2125. begin
  2126. if i<255 then
  2127. begin
  2128. inc(i);
  2129. pattern[i]:=c;
  2130. end;
  2131. readchar;
  2132. end;
  2133. pattern[0]:=chr(i);
  2134. end;
  2135. function tscannerfile.readid:string;
  2136. begin
  2137. readstring;
  2138. readid:=pattern;
  2139. end;
  2140. function tscannerfile.readval:longint;
  2141. var
  2142. l : longint;
  2143. w : integer;
  2144. begin
  2145. readnumber;
  2146. val(pattern,l,w);
  2147. readval:=l;
  2148. end;
  2149. function tscannerfile.readval_asstring:string;
  2150. begin
  2151. readnumber;
  2152. readval_asstring:=pattern;
  2153. end;
  2154. function tscannerfile.readcomment:string;
  2155. var
  2156. i : longint;
  2157. begin
  2158. i:=0;
  2159. repeat
  2160. case c of
  2161. '{' :
  2162. begin
  2163. if aktcommentstyle=comment_tp then
  2164. inc_comment_level;
  2165. end;
  2166. '}' :
  2167. begin
  2168. if aktcommentstyle=comment_tp then
  2169. begin
  2170. readchar;
  2171. dec_comment_level;
  2172. if comment_level=0 then
  2173. break
  2174. else
  2175. continue;
  2176. end;
  2177. end;
  2178. '*' :
  2179. begin
  2180. if aktcommentstyle=comment_oldtp then
  2181. begin
  2182. readchar;
  2183. if c=')' then
  2184. begin
  2185. readchar;
  2186. dec_comment_level;
  2187. break;
  2188. end
  2189. else
  2190. { Add both characters !!}
  2191. if (i<255) then
  2192. begin
  2193. inc(i);
  2194. readcomment[i]:='*';
  2195. if (i<255) then
  2196. begin
  2197. inc(i);
  2198. readcomment[i]:='*';
  2199. end;
  2200. end;
  2201. end
  2202. else
  2203. { Not old TP comment, so add...}
  2204. begin
  2205. if (i<255) then
  2206. begin
  2207. inc(i);
  2208. readcomment[i]:='*';
  2209. end;
  2210. end;
  2211. end;
  2212. #10,#13 :
  2213. linebreak;
  2214. #26 :
  2215. end_of_file;
  2216. else
  2217. begin
  2218. if (i<255) then
  2219. begin
  2220. inc(i);
  2221. readcomment[i]:=c;
  2222. end;
  2223. end;
  2224. end;
  2225. readchar;
  2226. until false;
  2227. readcomment[0]:=chr(i);
  2228. end;
  2229. function tscannerfile.readquotedstring:string;
  2230. var
  2231. i : longint;
  2232. msgwritten : boolean;
  2233. begin
  2234. i:=0;
  2235. msgwritten:=false;
  2236. if (c='''') then
  2237. begin
  2238. repeat
  2239. readchar;
  2240. case c of
  2241. #26 :
  2242. end_of_file;
  2243. #10,#13 :
  2244. Message(scan_f_string_exceeds_line);
  2245. '''' :
  2246. begin
  2247. readchar;
  2248. if c<>'''' then
  2249. break;
  2250. end;
  2251. end;
  2252. if i<255 then
  2253. begin
  2254. inc(i);
  2255. result[i]:=c;
  2256. end
  2257. else
  2258. begin
  2259. if not msgwritten then
  2260. begin
  2261. Message(scan_e_string_exceeds_255_chars);
  2262. msgwritten:=true;
  2263. end;
  2264. end;
  2265. until false;
  2266. end;
  2267. result[0]:=chr(i);
  2268. end;
  2269. function tscannerfile.readstate:char;
  2270. var
  2271. state : char;
  2272. begin
  2273. state:=' ';
  2274. if c=' ' then
  2275. begin
  2276. current_scanner.skipspace;
  2277. current_scanner.readid;
  2278. if pattern='ON' then
  2279. state:='+'
  2280. else
  2281. if pattern='OFF' then
  2282. state:='-';
  2283. end
  2284. else
  2285. state:=c;
  2286. if not (state in ['+','-']) then
  2287. Message(scan_e_wrong_switch_toggle);
  2288. readstate:=state;
  2289. end;
  2290. function tscannerfile.readstatedefault:char;
  2291. var
  2292. state : char;
  2293. begin
  2294. state:=' ';
  2295. if c=' ' then
  2296. begin
  2297. current_scanner.skipspace;
  2298. current_scanner.readid;
  2299. if pattern='ON' then
  2300. state:='+'
  2301. else
  2302. if pattern='OFF' then
  2303. state:='-'
  2304. else
  2305. if pattern='DEFAULT' then
  2306. state:='*';
  2307. end
  2308. else
  2309. state:=c;
  2310. if not (state in ['+','-','*']) then
  2311. Message(scan_e_wrong_switch_toggle_default);
  2312. readstatedefault:=state;
  2313. end;
  2314. procedure tscannerfile.skipspace;
  2315. begin
  2316. repeat
  2317. case c of
  2318. #26 :
  2319. begin
  2320. reload;
  2321. if (c=#26) and not assigned(inputfile.next) then
  2322. break;
  2323. continue;
  2324. end;
  2325. #10,
  2326. #13 :
  2327. linebreak;
  2328. #9,#11,#12,' ' :
  2329. ;
  2330. else
  2331. break;
  2332. end;
  2333. readchar;
  2334. until false;
  2335. end;
  2336. procedure tscannerfile.skipuntildirective;
  2337. var
  2338. found : longint;
  2339. next_char_loaded : boolean;
  2340. begin
  2341. found:=0;
  2342. next_char_loaded:=false;
  2343. repeat
  2344. case c of
  2345. #10,
  2346. #13 :
  2347. linebreak;
  2348. #26 :
  2349. begin
  2350. reload;
  2351. if (c=#26) and not assigned(inputfile.next) then
  2352. end_of_file;
  2353. continue;
  2354. end;
  2355. '{' :
  2356. begin
  2357. if (aktcommentstyle in [comment_tp,comment_none]) then
  2358. begin
  2359. aktcommentstyle:=comment_tp;
  2360. if (comment_level=0) then
  2361. found:=1;
  2362. inc_comment_level;
  2363. end;
  2364. end;
  2365. '*' :
  2366. begin
  2367. if (aktcommentstyle=comment_oldtp) then
  2368. begin
  2369. readchar;
  2370. if c=')' then
  2371. begin
  2372. dec_comment_level;
  2373. found:=0;
  2374. aktcommentstyle:=comment_none;
  2375. end
  2376. else
  2377. next_char_loaded:=true;
  2378. end
  2379. else
  2380. found := 0;
  2381. end;
  2382. '}' :
  2383. begin
  2384. if (aktcommentstyle=comment_tp) then
  2385. begin
  2386. dec_comment_level;
  2387. if (comment_level=0) then
  2388. aktcommentstyle:=comment_none;
  2389. found:=0;
  2390. end;
  2391. end;
  2392. '$' :
  2393. begin
  2394. if found=1 then
  2395. found:=2;
  2396. end;
  2397. '''' :
  2398. if (aktcommentstyle=comment_none) then
  2399. begin
  2400. repeat
  2401. readchar;
  2402. case c of
  2403. #26 :
  2404. end_of_file;
  2405. #10,#13 :
  2406. break;
  2407. '''' :
  2408. begin
  2409. readchar;
  2410. if c<>'''' then
  2411. begin
  2412. next_char_loaded:=true;
  2413. break;
  2414. end;
  2415. end;
  2416. end;
  2417. until false;
  2418. end;
  2419. '(' :
  2420. begin
  2421. if (aktcommentstyle=comment_none) then
  2422. begin
  2423. readchar;
  2424. if c='*' then
  2425. begin
  2426. readchar;
  2427. if c='$' then
  2428. begin
  2429. found:=2;
  2430. inc_comment_level;
  2431. aktcommentstyle:=comment_oldtp;
  2432. end
  2433. else
  2434. begin
  2435. skipoldtpcomment;
  2436. next_char_loaded:=true;
  2437. end;
  2438. end
  2439. else
  2440. next_char_loaded:=true;
  2441. end
  2442. else
  2443. found:=0;
  2444. end;
  2445. '/' :
  2446. begin
  2447. if (aktcommentstyle=comment_none) then
  2448. begin
  2449. readchar;
  2450. if c='/' then
  2451. skipdelphicomment;
  2452. next_char_loaded:=true;
  2453. end
  2454. else
  2455. found:=0;
  2456. end;
  2457. else
  2458. found:=0;
  2459. end;
  2460. if next_char_loaded then
  2461. next_char_loaded:=false
  2462. else
  2463. readchar;
  2464. until (found=2);
  2465. end;
  2466. {****************************************************************************
  2467. Comment Handling
  2468. ****************************************************************************}
  2469. procedure tscannerfile.skipcomment;
  2470. begin
  2471. aktcommentstyle:=comment_tp;
  2472. readchar;
  2473. inc_comment_level;
  2474. { handle compiler switches }
  2475. if (c='$') then
  2476. handledirectives;
  2477. { handle_switches can dec comment_level, }
  2478. while (comment_level>0) do
  2479. begin
  2480. case c of
  2481. '{' :
  2482. inc_comment_level;
  2483. '}' :
  2484. dec_comment_level;
  2485. #10,#13 :
  2486. linebreak;
  2487. #26 :
  2488. begin
  2489. reload;
  2490. if (c=#26) and not assigned(inputfile.next) then
  2491. end_of_file;
  2492. continue;
  2493. end;
  2494. end;
  2495. readchar;
  2496. end;
  2497. aktcommentstyle:=comment_none;
  2498. end;
  2499. procedure tscannerfile.skipdelphicomment;
  2500. begin
  2501. aktcommentstyle:=comment_delphi;
  2502. inc_comment_level;
  2503. readchar;
  2504. { this is not supported }
  2505. if c='$' then
  2506. Message(scan_w_wrong_styled_switch);
  2507. { skip comment }
  2508. while not (c in [#10,#13,#26]) do
  2509. readchar;
  2510. dec_comment_level;
  2511. aktcommentstyle:=comment_none;
  2512. end;
  2513. procedure tscannerfile.skipoldtpcomment;
  2514. var
  2515. found : longint;
  2516. begin
  2517. aktcommentstyle:=comment_oldtp;
  2518. inc_comment_level;
  2519. { only load a char if last already processed,
  2520. was cause of bug1634 PM }
  2521. if c=#0 then
  2522. readchar;
  2523. { this is now supported }
  2524. if (c='$') then
  2525. handledirectives;
  2526. { skip comment }
  2527. while (comment_level>0) do
  2528. begin
  2529. found:=0;
  2530. repeat
  2531. case c of
  2532. #26 :
  2533. begin
  2534. reload;
  2535. if (c=#26) and not assigned(inputfile.next) then
  2536. end_of_file;
  2537. continue;
  2538. end;
  2539. #10,#13 :
  2540. linebreak;
  2541. '*' :
  2542. begin
  2543. if found=3 then
  2544. found:=4
  2545. else
  2546. found:=1;
  2547. end;
  2548. ')' :
  2549. begin
  2550. if found in [1,4] then
  2551. begin
  2552. dec_comment_level;
  2553. if comment_level=0 then
  2554. found:=2
  2555. else
  2556. found:=0;
  2557. end;
  2558. end;
  2559. '(' :
  2560. begin
  2561. if found=4 then
  2562. inc_comment_level;
  2563. found:=3;
  2564. end;
  2565. else
  2566. begin
  2567. if found=4 then
  2568. inc_comment_level;
  2569. found:=0;
  2570. end;
  2571. end;
  2572. readchar;
  2573. until (found=2);
  2574. end;
  2575. aktcommentstyle:=comment_none;
  2576. end;
  2577. {****************************************************************************
  2578. Token Scanner
  2579. ****************************************************************************}
  2580. procedure tscannerfile.readtoken;
  2581. var
  2582. code : integer;
  2583. len,
  2584. low,high,mid : longint;
  2585. w : word;
  2586. m : longint;
  2587. mac : tmacro;
  2588. asciinr : string[6];
  2589. msgwritten,
  2590. iswidestring : boolean;
  2591. label
  2592. exit_label;
  2593. begin
  2594. if localswitcheschanged then
  2595. begin
  2596. aktlocalswitches:=nextaktlocalswitches;
  2597. localswitcheschanged:=false;
  2598. end;
  2599. { was there already a token read, then return that token }
  2600. if nexttoken<>NOTOKEN then
  2601. begin
  2602. token:=nexttoken;
  2603. nexttoken:=NOTOKEN;
  2604. goto exit_label;
  2605. end;
  2606. { Skip all spaces and comments }
  2607. repeat
  2608. case c of
  2609. '{' :
  2610. skipcomment;
  2611. #26 :
  2612. begin
  2613. reload;
  2614. if (c=#26) and not assigned(inputfile.next) then
  2615. break;
  2616. end;
  2617. ' ',#9..#13 :
  2618. begin
  2619. {$ifdef PREPROCWRITE}
  2620. if parapreprocess then
  2621. begin
  2622. if c=#10 then
  2623. preprocfile.eolfound:=true
  2624. else
  2625. preprocfile.spacefound:=true;
  2626. end;
  2627. {$endif PREPROCWRITE}
  2628. skipspace;
  2629. end
  2630. else
  2631. break;
  2632. end;
  2633. until false;
  2634. { Save current token position, for EOF its already loaded }
  2635. if c<>#26 then
  2636. gettokenpos;
  2637. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  2638. if c in ['A'..'Z','a'..'z','_'] then
  2639. begin
  2640. readstring;
  2641. token:=_ID;
  2642. idtoken:=_ID;
  2643. { keyword or any other known token,
  2644. pattern is always uppercased }
  2645. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  2646. begin
  2647. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  2648. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  2649. while low<high do
  2650. begin
  2651. mid:=(high+low+1) shr 1;
  2652. if pattern<tokeninfo^[ttoken(mid)].str then
  2653. high:=mid-1
  2654. else
  2655. low:=mid;
  2656. end;
  2657. with tokeninfo^[ttoken(high)] do
  2658. if pattern=str then
  2659. begin
  2660. if keyword in aktmodeswitches then
  2661. if op=NOTOKEN then
  2662. token:=ttoken(high)
  2663. else
  2664. token:=op;
  2665. idtoken:=ttoken(high);
  2666. end;
  2667. end;
  2668. { Only process identifiers and not keywords }
  2669. if token=_ID then
  2670. begin
  2671. { this takes some time ... }
  2672. if (cs_support_macro in aktmoduleswitches) then
  2673. begin
  2674. mac:=tmacro(search_macro(pattern));
  2675. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  2676. begin
  2677. if yylexcount<max_macro_nesting then
  2678. begin
  2679. mac.is_used:=true;
  2680. inc(yylexcount);
  2681. insertmacro(pattern,mac.buftext,mac.buflen,
  2682. mac.fileinfo.line,mac.fileinfo.fileindex);
  2683. { handle empty macros }
  2684. if c=#0 then
  2685. reload;
  2686. readtoken;
  2687. { that's all folks }
  2688. dec(yylexcount);
  2689. exit;
  2690. end
  2691. else
  2692. Message(scan_w_macro_too_deep);
  2693. end;
  2694. end;
  2695. end;
  2696. { return token }
  2697. goto exit_label;
  2698. end
  2699. else
  2700. begin
  2701. idtoken:=_NOID;
  2702. case c of
  2703. '$' :
  2704. begin
  2705. readnumber;
  2706. token:=_INTCONST;
  2707. goto exit_label;
  2708. end;
  2709. '%' :
  2710. begin
  2711. if not(m_fpc in aktmodeswitches) then
  2712. Illegal_Char(c)
  2713. else
  2714. begin
  2715. readnumber;
  2716. token:=_INTCONST;
  2717. goto exit_label;
  2718. end;
  2719. end;
  2720. '&' :
  2721. begin
  2722. if m_fpc in aktmodeswitches then
  2723. begin
  2724. readnumber;
  2725. token:=_INTCONST;
  2726. goto exit_label;
  2727. end
  2728. else if m_mac in aktmodeswitches then
  2729. begin
  2730. readchar;
  2731. token:=_AMPERSAND;
  2732. goto exit_label;
  2733. end
  2734. else
  2735. Illegal_Char(c);
  2736. end;
  2737. '0'..'9' :
  2738. begin
  2739. readnumber;
  2740. if (c in ['.','e','E']) then
  2741. begin
  2742. { first check for a . }
  2743. if c='.' then
  2744. begin
  2745. readchar;
  2746. { is it a .. from a range? }
  2747. case c of
  2748. '.' :
  2749. begin
  2750. readchar;
  2751. token:=_INTCONST;
  2752. nexttoken:=_POINTPOINT;
  2753. goto exit_label;
  2754. end;
  2755. ')' :
  2756. begin
  2757. readchar;
  2758. token:=_INTCONST;
  2759. nexttoken:=_RECKKLAMMER;
  2760. goto exit_label;
  2761. end;
  2762. end;
  2763. { insert the number after the . }
  2764. pattern:=pattern+'.';
  2765. while c in ['0'..'9'] do
  2766. begin
  2767. pattern:=pattern+c;
  2768. readchar;
  2769. end;
  2770. end;
  2771. { E can also follow after a point is scanned }
  2772. if c in ['e','E'] then
  2773. begin
  2774. pattern:=pattern+'E';
  2775. readchar;
  2776. if c in ['-','+'] then
  2777. begin
  2778. pattern:=pattern+c;
  2779. readchar;
  2780. end;
  2781. if not(c in ['0'..'9']) then
  2782. Illegal_Char(c);
  2783. while c in ['0'..'9'] do
  2784. begin
  2785. pattern:=pattern+c;
  2786. readchar;
  2787. end;
  2788. end;
  2789. token:=_REALNUMBER;
  2790. goto exit_label;
  2791. end;
  2792. token:=_INTCONST;
  2793. goto exit_label;
  2794. end;
  2795. ';' :
  2796. begin
  2797. readchar;
  2798. token:=_SEMICOLON;
  2799. goto exit_label;
  2800. end;
  2801. '[' :
  2802. begin
  2803. readchar;
  2804. token:=_LECKKLAMMER;
  2805. goto exit_label;
  2806. end;
  2807. ']' :
  2808. begin
  2809. readchar;
  2810. token:=_RECKKLAMMER;
  2811. goto exit_label;
  2812. end;
  2813. '(' :
  2814. begin
  2815. readchar;
  2816. case c of
  2817. '*' :
  2818. begin
  2819. c:=#0;{Signal skipoldtpcomment to reload a char }
  2820. skipoldtpcomment;
  2821. readtoken;
  2822. exit;
  2823. end;
  2824. '.' :
  2825. begin
  2826. readchar;
  2827. token:=_LECKKLAMMER;
  2828. goto exit_label;
  2829. end;
  2830. end;
  2831. token:=_LKLAMMER;
  2832. goto exit_label;
  2833. end;
  2834. ')' :
  2835. begin
  2836. readchar;
  2837. token:=_RKLAMMER;
  2838. goto exit_label;
  2839. end;
  2840. '+' :
  2841. begin
  2842. readchar;
  2843. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2844. begin
  2845. readchar;
  2846. token:=_PLUSASN;
  2847. goto exit_label;
  2848. end;
  2849. token:=_PLUS;
  2850. goto exit_label;
  2851. end;
  2852. '-' :
  2853. begin
  2854. readchar;
  2855. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2856. begin
  2857. readchar;
  2858. token:=_MINUSASN;
  2859. goto exit_label;
  2860. end;
  2861. token:=_MINUS;
  2862. goto exit_label;
  2863. end;
  2864. ':' :
  2865. begin
  2866. readchar;
  2867. if c='=' then
  2868. begin
  2869. readchar;
  2870. token:=_ASSIGNMENT;
  2871. goto exit_label;
  2872. end;
  2873. token:=_COLON;
  2874. goto exit_label;
  2875. end;
  2876. '*' :
  2877. begin
  2878. readchar;
  2879. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2880. begin
  2881. readchar;
  2882. token:=_STARASN;
  2883. end
  2884. else
  2885. if c='*' then
  2886. begin
  2887. readchar;
  2888. token:=_STARSTAR;
  2889. end
  2890. else
  2891. token:=_STAR;
  2892. goto exit_label;
  2893. end;
  2894. '/' :
  2895. begin
  2896. readchar;
  2897. case c of
  2898. '=' :
  2899. begin
  2900. if (cs_support_c_operators in aktmoduleswitches) then
  2901. begin
  2902. readchar;
  2903. token:=_SLASHASN;
  2904. goto exit_label;
  2905. end;
  2906. end;
  2907. '/' :
  2908. begin
  2909. skipdelphicomment;
  2910. readtoken;
  2911. exit;
  2912. end;
  2913. end;
  2914. token:=_SLASH;
  2915. goto exit_label;
  2916. end;
  2917. '|' :
  2918. if m_mac in aktmodeswitches then
  2919. begin
  2920. readchar;
  2921. token:=_PIPE;
  2922. goto exit_label;
  2923. end
  2924. else
  2925. Illegal_Char(c);
  2926. '=' :
  2927. begin
  2928. readchar;
  2929. token:=_EQUAL;
  2930. goto exit_label;
  2931. end;
  2932. '.' :
  2933. begin
  2934. readchar;
  2935. case c of
  2936. '.' :
  2937. begin
  2938. readchar;
  2939. case c of
  2940. '.' :
  2941. begin
  2942. readchar;
  2943. token:=_POINTPOINTPOINT;
  2944. goto exit_label;
  2945. end;
  2946. else
  2947. begin
  2948. token:=_POINTPOINT;
  2949. goto exit_label;
  2950. end;
  2951. end;
  2952. end;
  2953. ')' :
  2954. begin
  2955. readchar;
  2956. token:=_RECKKLAMMER;
  2957. goto exit_label;
  2958. end;
  2959. end;
  2960. token:=_POINT;
  2961. goto exit_label;
  2962. end;
  2963. '@' :
  2964. begin
  2965. readchar;
  2966. token:=_KLAMMERAFFE;
  2967. goto exit_label;
  2968. end;
  2969. ',' :
  2970. begin
  2971. readchar;
  2972. token:=_COMMA;
  2973. goto exit_label;
  2974. end;
  2975. '''','#','^' :
  2976. begin
  2977. len:=0;
  2978. msgwritten:=false;
  2979. pattern:='';
  2980. iswidestring:=false;
  2981. if c='^' then
  2982. begin
  2983. readchar;
  2984. c:=upcase(c);
  2985. if (block_type=bt_type) or
  2986. (lasttoken=_ID) or (lasttoken=_NIL) or
  2987. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  2988. begin
  2989. token:=_CARET;
  2990. goto exit_label;
  2991. end
  2992. else
  2993. begin
  2994. inc(len);
  2995. if c<#64 then
  2996. pattern[len]:=chr(ord(c)+64)
  2997. else
  2998. pattern[len]:=chr(ord(c)-64);
  2999. readchar;
  3000. end;
  3001. end;
  3002. repeat
  3003. case c of
  3004. '#' :
  3005. begin
  3006. readchar; { read # }
  3007. if c='$' then
  3008. begin
  3009. readchar; { read leading $ }
  3010. asciinr:='$';
  3011. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  3012. begin
  3013. asciinr:=asciinr+c;
  3014. readchar;
  3015. end;
  3016. end
  3017. else
  3018. begin
  3019. asciinr:='';
  3020. while (c in ['0'..'9']) and (length(asciinr)<6) do
  3021. begin
  3022. asciinr:=asciinr+c;
  3023. readchar;
  3024. end;
  3025. end;
  3026. val(asciinr,m,code);
  3027. if (asciinr='') or (code<>0) then
  3028. Message(scan_e_illegal_char_const)
  3029. else if (m<0) or (m>255) or (length(asciinr)>3) then
  3030. begin
  3031. if (m>=0) and (m<=65535) then
  3032. begin
  3033. if not iswidestring then
  3034. begin
  3035. ascii2unicode(@pattern[1],len,patternw);
  3036. iswidestring:=true;
  3037. len:=0;
  3038. end;
  3039. concatwidestringchar(patternw,tcompilerwidechar(m));
  3040. end
  3041. else
  3042. Message(scan_e_illegal_char_const)
  3043. end
  3044. else if iswidestring then
  3045. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  3046. else
  3047. begin
  3048. if len<255 then
  3049. begin
  3050. inc(len);
  3051. pattern[len]:=chr(m);
  3052. end
  3053. else
  3054. begin
  3055. if not msgwritten then
  3056. begin
  3057. Message(scan_e_string_exceeds_255_chars);
  3058. msgwritten:=true;
  3059. end;
  3060. end;
  3061. end;
  3062. end;
  3063. '''' :
  3064. begin
  3065. repeat
  3066. readchar;
  3067. case c of
  3068. #26 :
  3069. end_of_file;
  3070. #10,#13 :
  3071. Message(scan_f_string_exceeds_line);
  3072. '''' :
  3073. begin
  3074. readchar;
  3075. if c<>'''' then
  3076. break;
  3077. end;
  3078. end;
  3079. { interpret as utf-8 string? }
  3080. if (ord(c)>=$80) and (aktsourcecodepage='utf8') then
  3081. begin
  3082. { convert existing string to an utf-8 string }
  3083. if not iswidestring then
  3084. begin
  3085. ascii2unicode(@pattern[1],len,patternw);
  3086. iswidestring:=true;
  3087. len:=0;
  3088. end;
  3089. { four or more chars aren't handled }
  3090. if (ord(c) and $f0)=$f0 then
  3091. message(scan_e_utf8_bigger_than_65535)
  3092. { three chars }
  3093. else if (ord(c) and $e0)=$e0 then
  3094. begin
  3095. w:=ord(c) and $f;
  3096. readchar;
  3097. if (ord(c) and $c0)<>$80 then
  3098. message(scan_e_utf8_malformed);
  3099. w:=(w shl 6) or (ord(c) and $3f);
  3100. readchar;
  3101. if (ord(c) and $c0)<>$80 then
  3102. message(scan_e_utf8_malformed);
  3103. w:=(w shl 6) or (ord(c) and $3f);
  3104. concatwidestringchar(patternw,w);
  3105. end
  3106. { two chars }
  3107. else if (ord(c) and $c0)<>0 then
  3108. begin
  3109. w:=ord(c) and $1f;
  3110. readchar;
  3111. if (ord(c) and $c0)<>$80 then
  3112. message(scan_e_utf8_malformed);
  3113. w:=(w shl 6) or (ord(c) and $3f);
  3114. concatwidestringchar(patternw,w);
  3115. end
  3116. { illegal }
  3117. else if (ord(c) and $80)<>0 then
  3118. message(scan_e_utf8_malformed)
  3119. else
  3120. concatwidestringchar(patternw,tcompilerwidechar(c))
  3121. end
  3122. else if iswidestring then
  3123. begin
  3124. if aktsourcecodepage='utf8' then
  3125. concatwidestringchar(patternw,ord(c))
  3126. else
  3127. concatwidestringchar(patternw,asciichar2unicode(c))
  3128. end
  3129. else
  3130. begin
  3131. if len<255 then
  3132. begin
  3133. inc(len);
  3134. pattern[len]:=c;
  3135. end
  3136. else
  3137. begin
  3138. if not msgwritten then
  3139. begin
  3140. Message(scan_e_string_exceeds_255_chars);
  3141. msgwritten:=true;
  3142. end;
  3143. end;
  3144. end;
  3145. until false;
  3146. end;
  3147. '^' :
  3148. begin
  3149. readchar;
  3150. c:=upcase(c);
  3151. if c<#64 then
  3152. c:=chr(ord(c)+64)
  3153. else
  3154. c:=chr(ord(c)-64);
  3155. if iswidestring then
  3156. concatwidestringchar(patternw,asciichar2unicode(c))
  3157. else
  3158. begin
  3159. if len<255 then
  3160. begin
  3161. inc(len);
  3162. pattern[len]:=c;
  3163. end
  3164. else
  3165. begin
  3166. if not msgwritten then
  3167. begin
  3168. Message(scan_e_string_exceeds_255_chars);
  3169. msgwritten:=true;
  3170. end;
  3171. end;
  3172. end;
  3173. readchar;
  3174. end;
  3175. else
  3176. break;
  3177. end;
  3178. until false;
  3179. { strings with length 1 become const chars }
  3180. if iswidestring then
  3181. begin
  3182. if patternw^.len=1 then
  3183. token:=_CWCHAR
  3184. else
  3185. token:=_CWSTRING;
  3186. end
  3187. else
  3188. begin
  3189. pattern[0]:=chr(len);
  3190. if len=1 then
  3191. token:=_CCHAR
  3192. else
  3193. token:=_CSTRING;
  3194. end;
  3195. goto exit_label;
  3196. end;
  3197. '>' :
  3198. begin
  3199. readchar;
  3200. case c of
  3201. '=' :
  3202. begin
  3203. readchar;
  3204. token:=_GTE;
  3205. goto exit_label;
  3206. end;
  3207. '>' :
  3208. begin
  3209. readchar;
  3210. token:=_OP_SHR;
  3211. goto exit_label;
  3212. end;
  3213. '<' :
  3214. begin { >< is for a symetric diff for sets }
  3215. readchar;
  3216. token:=_SYMDIF;
  3217. goto exit_label;
  3218. end;
  3219. end;
  3220. token:=_GT;
  3221. goto exit_label;
  3222. end;
  3223. '<' :
  3224. begin
  3225. readchar;
  3226. case c of
  3227. '>' :
  3228. begin
  3229. readchar;
  3230. token:=_UNEQUAL;
  3231. goto exit_label;
  3232. end;
  3233. '=' :
  3234. begin
  3235. readchar;
  3236. token:=_LTE;
  3237. goto exit_label;
  3238. end;
  3239. '<' :
  3240. begin
  3241. readchar;
  3242. token:=_OP_SHL;
  3243. goto exit_label;
  3244. end;
  3245. end;
  3246. token:=_LT;
  3247. goto exit_label;
  3248. end;
  3249. #26 :
  3250. begin
  3251. token:=_EOF;
  3252. checkpreprocstack;
  3253. goto exit_label;
  3254. end;
  3255. else
  3256. Illegal_Char(c);
  3257. end;
  3258. end;
  3259. exit_label:
  3260. lasttoken:=token;
  3261. end;
  3262. function tscannerfile.readpreproc:ttoken;
  3263. begin
  3264. skipspace;
  3265. case c of
  3266. '_',
  3267. 'A'..'Z',
  3268. 'a'..'z' :
  3269. begin
  3270. current_scanner.preproc_pattern:=readid;
  3271. readpreproc:=_ID;
  3272. end;
  3273. '0'..'9' :
  3274. begin
  3275. current_scanner.preproc_pattern:=readval_asstring;
  3276. { realnumber? }
  3277. if c='.' then
  3278. begin
  3279. readchar;
  3280. while c in ['0'..'9'] do
  3281. begin
  3282. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  3283. readchar;
  3284. end;
  3285. end;
  3286. readpreproc:=_ID;
  3287. end;
  3288. '$','%','&' :
  3289. begin
  3290. current_scanner.preproc_pattern:=readval_asstring;
  3291. readpreproc:=_ID;
  3292. end;
  3293. ',' :
  3294. begin
  3295. readchar;
  3296. readpreproc:=_COMMA;
  3297. end;
  3298. '}' :
  3299. begin
  3300. readpreproc:=_END;
  3301. end;
  3302. '(' :
  3303. begin
  3304. readchar;
  3305. readpreproc:=_LKLAMMER;
  3306. end;
  3307. ')' :
  3308. begin
  3309. readchar;
  3310. readpreproc:=_RKLAMMER;
  3311. end;
  3312. '[' :
  3313. begin
  3314. readchar;
  3315. readpreproc:=_LECKKLAMMER;
  3316. end;
  3317. ']' :
  3318. begin
  3319. readchar;
  3320. readpreproc:=_RECKKLAMMER;
  3321. end;
  3322. '+' :
  3323. begin
  3324. readchar;
  3325. readpreproc:=_PLUS;
  3326. end;
  3327. '-' :
  3328. begin
  3329. readchar;
  3330. readpreproc:=_MINUS;
  3331. end;
  3332. '*' :
  3333. begin
  3334. readchar;
  3335. readpreproc:=_STAR;
  3336. end;
  3337. '/' :
  3338. begin
  3339. readchar;
  3340. readpreproc:=_SLASH;
  3341. end;
  3342. '=' :
  3343. begin
  3344. readchar;
  3345. readpreproc:=_EQUAL;
  3346. end;
  3347. '>' :
  3348. begin
  3349. readchar;
  3350. if c='=' then
  3351. begin
  3352. readchar;
  3353. readpreproc:=_GTE;
  3354. end
  3355. else
  3356. readpreproc:=_GT;
  3357. end;
  3358. '<' :
  3359. begin
  3360. readchar;
  3361. case c of
  3362. '>' :
  3363. begin
  3364. readchar;
  3365. readpreproc:=_UNEQUAL;
  3366. end;
  3367. '=' :
  3368. begin
  3369. readchar;
  3370. readpreproc:=_LTE;
  3371. end;
  3372. else
  3373. readpreproc:=_LT;
  3374. end;
  3375. end;
  3376. #26 :
  3377. begin
  3378. readpreproc:=_EOF;
  3379. checkpreprocstack;
  3380. end;
  3381. else
  3382. Illegal_Char(c);
  3383. end;
  3384. end;
  3385. function tscannerfile.asmgetcharstart : char;
  3386. begin
  3387. { return first the character already
  3388. available in c }
  3389. lastasmgetchar:=c;
  3390. result:=asmgetchar;
  3391. end;
  3392. function tscannerfile.asmgetchar : char;
  3393. begin
  3394. if lastasmgetchar<>#0 then
  3395. begin
  3396. c:=lastasmgetchar;
  3397. lastasmgetchar:=#0;
  3398. end
  3399. else
  3400. readchar;
  3401. if in_asm_string then
  3402. begin
  3403. asmgetchar:=c;
  3404. exit;
  3405. end;
  3406. repeat
  3407. case c of
  3408. {$ifndef arm}
  3409. // the { ... } is used in ARM assembler to define register sets, so we can't used
  3410. // it as comment, either (* ... *), /* ... */ or // ... should be used instead
  3411. '{' :
  3412. skipcomment;
  3413. {$endif arm}
  3414. #10,#13 :
  3415. begin
  3416. linebreak;
  3417. asmgetchar:=c;
  3418. exit;
  3419. end;
  3420. #26 :
  3421. begin
  3422. reload;
  3423. if (c=#26) and not assigned(inputfile.next) then
  3424. end_of_file;
  3425. continue;
  3426. end;
  3427. '/' :
  3428. begin
  3429. readchar;
  3430. if c='/' then
  3431. skipdelphicomment
  3432. else
  3433. begin
  3434. asmgetchar:='/';
  3435. lastasmgetchar:=c;
  3436. exit;
  3437. end;
  3438. end;
  3439. '(' :
  3440. begin
  3441. readchar;
  3442. if c='*' then
  3443. begin
  3444. c:=#0;{Signal skipoldtpcomment to reload a char }
  3445. skipoldtpcomment;
  3446. end
  3447. else
  3448. begin
  3449. asmgetchar:='(';
  3450. lastasmgetchar:=c;
  3451. exit;
  3452. end;
  3453. end;
  3454. else
  3455. begin
  3456. asmgetchar:=c;
  3457. exit;
  3458. end;
  3459. end;
  3460. until false;
  3461. end;
  3462. {*****************************************************************************
  3463. Helpers
  3464. *****************************************************************************}
  3465. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3466. begin
  3467. if dm in [directive_all, directive_turbo] then
  3468. turbo_scannerdirectives.insert(tdirectiveitem.create(s,p));
  3469. if dm in [directive_all, directive_mac] then
  3470. mac_scannerdirectives.insert(tdirectiveitem.create(s,p));
  3471. end;
  3472. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3473. begin
  3474. if dm in [directive_all, directive_turbo] then
  3475. turbo_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  3476. if dm in [directive_all, directive_mac] then
  3477. mac_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  3478. end;
  3479. {*****************************************************************************
  3480. Initialization
  3481. *****************************************************************************}
  3482. procedure InitScanner;
  3483. begin
  3484. InitWideString(patternw);
  3485. turbo_scannerdirectives:=TDictionary.Create;
  3486. mac_scannerdirectives:=TDictionary.Create;
  3487. { Common directives and conditionals }
  3488. AddDirective('I',directive_all, @dir_include);
  3489. AddDirective('DEFINE',directive_all, @dir_define);
  3490. AddDirective('UNDEF',directive_all, @dir_undef);
  3491. AddConditional('IF',directive_all, @dir_if);
  3492. AddConditional('IFDEF',directive_all, @dir_ifdef);
  3493. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  3494. AddConditional('ELSE',directive_all, @dir_else);
  3495. AddConditional('ELSEIF',directive_all, @dir_elseif);
  3496. AddConditional('ENDIF',directive_all, @dir_endif);
  3497. { Directives and conditionals for all modes except mode macpas}
  3498. AddDirective('INCLUDE',directive_turbo, @dir_include);
  3499. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  3500. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  3501. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  3502. AddConditional('IFEND',directive_turbo, @dir_endif);
  3503. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  3504. { Directives and conditionals for mode macpas: }
  3505. AddDirective('SETC',directive_mac, @dir_setc);
  3506. AddDirective('DEFINEC',directive_mac, @dir_definec);
  3507. AddDirective('UNDEFC',directive_mac, @dir_undef);
  3508. AddConditional('IFC',directive_mac, @dir_if);
  3509. AddConditional('ELSEC',directive_mac, @dir_else);
  3510. AddConditional('ELIFC',directive_mac, @dir_elseif);
  3511. AddConditional('ENDC',directive_mac, @dir_endif);
  3512. end;
  3513. procedure DoneScanner;
  3514. begin
  3515. turbo_scannerdirectives.Free;
  3516. mac_scannerdirectives.Free;
  3517. DoneWideString(patternw);
  3518. end;
  3519. end.