scanner.pas 136 KB

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