123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- {
- Copyright (c) 1998-2002 by Florian Klaempfl
- Contains some helper routines for the parser
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit pbase;
- {$i fpcdefs.inc}
- interface
- uses
- cutils,cclasses,
- tokens,globtype,
- symconst,symbase,symtype,symdef,symsym,symtable
- ;
- const
- { tokens that end a block or statement. And don't require
- a ; on the statement before }
- endtokens = [_SEMICOLON,_END,_ELSE,_UNTIL,_EXCEPT,_FINALLY];
- { true, if we are after an assignement }
- afterassignment : boolean = false;
- { true, if we are parsing arguments }
- in_args : boolean = false;
- { true, if we are parsing arguments allowing named parameters }
- named_args_allowed : boolean = false;
- { true, if we got an @ to get the address }
- got_addrn : boolean = false;
- { special for handling procedure vars }
- getprocvardef : tprocvardef = nil;
- var
- { for operators }
- optoken : ttoken;
- { true, if only routine headers should be parsed }
- parse_only : boolean;
- { true, if we found a name for a named arg }
- found_arg_name : boolean;
- { true, if we are parsing generic declaration }
- parse_generic : boolean;
- procedure identifier_not_found(const s:string);
- { function tokenstring(i : ttoken):string;}
- { consumes token i, if the current token is unequal i }
- { a syntax error is written }
- procedure consume(i : ttoken);
- {Tries to consume the token i, and returns true if it was consumed:
- if token=i.}
- function try_to_consume(i:Ttoken):boolean;
- { consumes all tokens til atoken (for error recovering }
- procedure consume_all_until(atoken : ttoken);
- { consumes tokens while they are semicolons }
- procedure consume_emptystats;
- { reads a list of identifiers into a string list }
- { consume a symbol, if not found give an error and
- and return an errorsym }
- function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
- function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
- function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume : ttoken):boolean;
- function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
- { just for an accurate position of the end of a procedure (PM) }
- var
- last_endtoken_filepos: tfileposinfo;
- implementation
- uses
- globals,htypechk,scanner,systems,verbose,fmodule;
- {****************************************************************************
- Token Parsing
- ****************************************************************************}
- procedure identifier_not_found(const s:string);
- begin
- Message1(sym_e_id_not_found,s);
- { show a fatal that you need -S2 or -Sd, but only
- if we just parsed the a token that has m_class }
- if not(m_class in current_settings.modeswitches) and
- (Upper(s)=pattern) and
- (tokeninfo^[idtoken].keyword=m_class) then
- Message(parser_f_need_objfpc_or_delphi_mode);
- end;
- { consumes token i, write error if token is different }
- procedure consume(i : ttoken);
- begin
- if (token<>i) and (idtoken<>i) then
- if token=_id then
- Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
- else
- Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
- else
- begin
- if token=_END then
- last_endtoken_filepos:=current_tokenpos;
- current_scanner.readtoken(true);
- end;
- end;
- function try_to_consume(i:Ttoken):boolean;
- begin
- try_to_consume:=false;
- if (token=i) or (idtoken=i) then
- begin
- try_to_consume:=true;
- if token=_END then
- last_endtoken_filepos:=current_tokenpos;
- current_scanner.readtoken(true);
- end;
- end;
- procedure consume_all_until(atoken : ttoken);
- begin
- while (token<>atoken) and (idtoken<>atoken) do
- begin
- Consume(token);
- if token=_EOF then
- begin
- Consume(atoken);
- Message(scan_f_end_of_file);
- exit;
- end;
- end;
- end;
- procedure consume_emptystats;
- begin
- repeat
- until not try_to_consume(_SEMICOLON);
- end;
- { check if a symbol contains the hint directive, and if so gives out a hint
- if required.
- If this code is changed, it's likly that consume_sym_orgid and factor_read_id
- must be changed as well (FK)
- }
- function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
- var
- t : ttoken;
- begin
- { first check for identifier }
- if token<>_ID then
- begin
- consume(_ID);
- srsym:=generrorsym;
- srsymtable:=nil;
- result:=false;
- exit;
- end;
- searchsym(pattern,srsym,srsymtable);
- { handle unit specification like System.Writeln }
- try_consume_unitsym(srsym,srsymtable,t);
- { if nothing found give error and return errorsym }
- if assigned(srsym) then
- check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
- else
- begin
- identifier_not_found(orgpattern);
- srsym:=generrorsym;
- srsymtable:=nil;
- end;
- consume(t);
- result:=assigned(srsym);
- end;
- { check if a symbol contains the hint directive, and if so gives out a hint
- if required and returns the id with it's original casing
- }
- function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
- var
- t : ttoken;
- begin
- { first check for identifier }
- if token<>_ID then
- begin
- consume(_ID);
- srsym:=generrorsym;
- srsymtable:=nil;
- result:=false;
- exit;
- end;
- searchsym(pattern,srsym,srsymtable);
- { handle unit specification like System.Writeln }
- try_consume_unitsym(srsym,srsymtable,t);
- { if nothing found give error and return errorsym }
- if assigned(srsym) then
- check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
- else
- begin
- identifier_not_found(orgpattern);
- srsym:=generrorsym;
- srsymtable:=nil;
- end;
- s:=orgpattern;
- consume(t);
- result:=assigned(srsym);
- end;
- function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume : ttoken):boolean;
- var
- hmodule: tmodule;
- begin
- result:=false;
- tokentoconsume:=_ID;
- if assigned(srsym) and
- (srsym.typ=unitsym) then
- begin
- if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
- internalerror(200501154);
- { only allow unit.symbol access if the name was
- found in the current module
- we can use iscurrentunit because generic specializations does not
- change current_unit variable }
- hmodule:=find_module_from_symtable(srsym.Owner);
- if not Assigned(hmodule) then
- internalerror(201001120);
- if hmodule.unit_index=current_filepos.moduleindex then
- begin
- consume(_ID);
- consume(_POINT);
- case token of
- _ID:
- searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
- _STRING:
- begin
- { system.string? }
- if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
- begin
- if cs_ansistrings in current_settings.localswitches then
- searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
- else
- searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
- tokentoconsume:=_STRING;
- end;
- end
- end;
- end
- else
- begin
- srsym:=nil;
- srsymtable:=nil;
- end;
- result:=true;
- end;
- end;
- function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
- var
- last_is_deprecated:boolean;
- begin
- try_consume_hintdirective:=false;
- if not(m_hintdirective in current_settings.modeswitches) then
- exit;
- repeat
- last_is_deprecated:=false;
- case idtoken of
- _LIBRARY :
- begin
- include(symopt,sp_hint_library);
- try_consume_hintdirective:=true;
- end;
- _DEPRECATED :
- begin
- include(symopt,sp_hint_deprecated);
- try_consume_hintdirective:=true;
- last_is_deprecated:=true;
- end;
- _EXPERIMENTAL :
- begin
- include(symopt,sp_hint_experimental);
- try_consume_hintdirective:=true;
- end;
- _PLATFORM :
- begin
- include(symopt,sp_hint_platform);
- try_consume_hintdirective:=true;
- end;
- _UNIMPLEMENTED :
- begin
- include(symopt,sp_hint_unimplemented);
- try_consume_hintdirective:=true;
- end;
- else
- break;
- end;
- consume(Token);
- { handle deprecated message }
- if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
- begin
- if deprecatedmsg<>nil then
- internalerror(200910181);
- if token=_CSTRING then
- deprecatedmsg:=stringdup(cstringpattern)
- else
- deprecatedmsg:=stringdup(pattern);
- consume(token);
- include(symopt,sp_has_deprecated_msg);
- end;
- until false;
- end;
- end.
|