scanner.pas 138 KB

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