scanner.pas 137 KB

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