scanner.pas 135 KB

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