FoxParser.Mod 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  1. MODULE FoxParser; (** AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Parser"; **)
  2. (* (c) fof ETH Zurich, 2009 *)
  3. IMPORT Basic := FoxBasic, Scanner := FoxScanner, D := Debugging, SyntaxTree := FoxSyntaxTree, Global := FoxGlobal, Diagnostics;
  4. CONST
  5. Trace = FALSE;
  6. CascadedWithSupport = TRUE;
  7. (** the parser reflects the following EBNF:
  8. Module = ('module' | 'cellnet' [Flags]) Identifier ['in' Identifier]';'
  9. [ImportList] DeclarationSequence [Body]
  10. 'end' Identifier '.'.
  11. ImportList = 'import' Import { ',' Import } ';'.
  12. Import = Identifier [':=' Identifier] ['in' Identifier].
  13. DeclarationSequence = {
  14. 'const' [ConstDeclaration] {';' [ConstDeclaration]}
  15. |'type' [TypeDeclaration] {';' [TypeDeclaration]}
  16. |'var' [VariableDeclaration] {';' [VariableDeclaration]}
  17. | ProcedureDeclaration
  18. | OperatorDeclaration
  19. | ';'
  20. }
  21. Declaration = IdentifierDefinition '=' Expression.
  22. TypeDeclaration = IdentifierDefinition '=' Type.
  23. VariableDeclaration = VariableNameList ':' Type.
  24. VariableNameList = IdentifierDefinition [Flags] [':=' Expression | 'extern' String] {',' IdentifierDefinition [Flags] [':=' Expression | 'extern' String] }.
  25. OperatorDeclaration = 'operator' [Flags] ['-'] String ['*'|'-'] FormalParameters ';'
  26. DeclarationSequence [Body] 'end' String.
  27. ProcedureDeclaration = 'procedure' ['^'|'&'|'~'|'-'|Flags ['-']] IdentifierDefinition [FormalParameters]';'
  28. DeclarationSequence [Body] 'end' Identifier.
  29. Flags = '{' [Identifier ['(' Expression ')'|'=' Expression] {',' Identifier ['(' Expression ')' | '=' Expression ] } ] '}'.
  30. FormalParameters = '(' [ParameterDeclaration {';' ParameterDeclaration}] ')' [':' [Flags] Type].
  31. ParameterDeclaration = ['var'|'const'] Identifier [Flags] ['= Expression] {',' Identifier [Flags] ['= Expression]}':' Type.
  32. PortList = '(' [PortDeclaration {';' PortDeclaration}] ')'.
  33. PortDeclaration = Identifier [Flags] {',' Identifier [Flags]}':' Type.
  34. Type = ArrayType | RecordType | PointerType | ObjectType | CellType | CellnetType | PortType
  35. | ProcedureType | EnumerationType | QualifiedIdentifier.
  36. PortType = 'port' ('in'|'out') ['(' Expression ')']
  37. EnumerationType = 'enum' ['('QualifiedIdentifier')'] IdentifierDefinition ['=' Expression]
  38. {',' IdentifierDefinition ['=' Expression]} 'end'.
  39. ArrayType = 'array' 'of' Type | 'array' Expression {',' Expression} 'of' Type
  40. | 'array' '[' MathArraySize {',' MathArraySize} ']' ['of' Type].
  41. MathArraySize = Expression | '*' | '?'.
  42. RecordType = 'record' [Flags] ['(' QualifiedIdentifier ')'] [VariableDeclaration {';' VariableDeclaration}] 'end'.
  43. PointerType = 'pointer' [Flags] 'to' Type.
  44. CellType = 'cell' [Flags] [PortList] [';'] DeclarationSequence [Body] 'end' [Identifier].
  45. ObjectType = 'object' | 'object' [Flags] ['(' (QualifiedIdentifier | ArrayType) ')'] DeclarationSequence [Body] 'end' [Identifier] .
  46. ProcedureType = 'procedure' [Flags] [FormalParameters].
  47. Body = 'begin' [Flags] StatementSequence ['finally' StatementSequence]
  48. | 'code' Code.
  49. Code = { any \ 'end' \ 'with' } ['with' {('in'|'out') StatementSequence}] .
  50. StatementBlock = [Flags] StatementSequence.
  51. StatementSequence = Statement {';' Statement}.
  52. Statement =
  53. [
  54. Designator [':=' Expression |'!' Expression | '?' Expression | '<<' Expresssion | '>>' Expression]
  55. | 'if' Expression 'then' StatementSequence
  56. {'elsif' Expression 'then' StatementSequence}
  57. ['else' StatementSequence]
  58. 'end'
  59. | 'with' Identifier ':' QualifiedIdentifier 'do' StatementSequence
  60. {'|' Identifier ':' QualifiedIdentifier 'do' StatementSequence}
  61. [else StatementSequence]
  62. 'end'
  63. | 'case' Expression 'of' ['|'] Case {'|' Case} ['else' StatementSequence] 'end'
  64. | 'while' Expression 'do' StatementSequence 'end'
  65. | 'repeat' StatementSequence 'until' Expression
  66. | 'for' Identifier ':=' Expression 'to' Expression ['by' Expression] 'do'
  67. StatementSequence 'end'
  68. | 'loop' StatementSequence 'end'
  69. | 'exit'
  70. | 'return' [Expression]
  71. | 'await' Expression
  72. | 'begin' StatementBlock 'end'
  73. | 'code' {any} 'end'
  74. ].
  75. Case = RangeExpression {',' RangeExpression} ':' StatementSequence.
  76. Expression = RangeExpression [RelationOp RangeExpression].
  77. RelationOp = '=' | '.=' | '#' | '.#'
  78. | '<' | '.<' | '<=' | '.<=' | '>' | '.>' | '>=' | '.>='
  79. | '??' | '!!' | '<<?' | '>>?'
  80. | 'in' | 'is'
  81. SimpleExpression = ['+'|'-'] Term {AddOp Term}.
  82. AddOp = '+' | '-' | 'or'.
  83. Term = Factor {MulOp Factor}.
  84. MulOp = '*' | '**' | '.*' | '+*' | '/' | '\' | './' | 'div' | 'mod' | '&'.
  85. Factor = Number | Character | String | 'nil' | 'imag' | 'true' | 'false' | Set
  86. | '(' Expression ')' | '~' Factor | Factor '`' | Designator | MathArray.
  87. | 'SIZE' 'OF' Designator | 'ADDRESS' 'OF' Designator
  88. MathArray = '[' Expression {',' Expression} ']'.
  89. Set = '{' [ RangeExpression {',' RangeExpression} ] '}'.
  90. Designator = ('self' | 'result' | Identifier)
  91. {'.' Identifier | '[' IndexList ']' | '(' [ExpressionList] ')' | '^'} [Flags].
  92. RangeExpression = SimpleExpression | [SimpleExpression] '..' [SimpleExpression] ['by' SimpleExpression] | '*'.
  93. IndexList = '?' [',' ExpressionList ] | ExpressionList [',' '?'].
  94. ExpressionList = Expression { ',' Expression }.
  95. IdentifierDefinition = Identifier [ '*' | '-' ].
  96. QualifiedIdentifier = Identifier ['.' Identifier].
  97. Identifier = Letter {Letter | Digit | '_'}.
  98. Letter = 'A' | 'B' | .. | 'Z' | 'a' | 'b' | .. | 'z'.
  99. String = '"' {Character} '"' | "'" {Character} "'".
  100. Number = Integer | Real.
  101. Integer = Digit {Digit} | '0' 'x' {HexDigit} | Digit {HexDigit} 'H' .
  102. Real = Digit {Digit} '.' {Digit} [ScaleFactor].
  103. ScaleFactor = ('E' | 'D') ['+' | '-'] digit {digit}.
  104. HexDigit = Digit | 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
  105. Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' .
  106. **)
  107. TYPE
  108. Position*=Scanner.Position;
  109. Parser* = OBJECT
  110. VAR scanner-: Scanner.Scanner;
  111. symbol-: Scanner.Symbol;
  112. diagnostics: Diagnostics.Diagnostics;
  113. currentScope: SyntaxTree.Scope;
  114. recentCommentItem: ANY; recentLine: LONGINT;
  115. recentComment: SyntaxTree.Comment;
  116. moduleScope: SyntaxTree.ModuleScope;
  117. error-: BOOLEAN;
  118. Lax-: BOOLEAN;
  119. indent: LONGINT; (* for debugging purposes only *)
  120. hasSymbol: BOOLEAN;
  121. PROCEDURE S( CONST s: ARRAY OF CHAR ); (* for debugging purposes only *)
  122. VAR i: LONGINT;
  123. BEGIN
  124. D.Ln; INC( indent ); D.Int( indent,1 );
  125. FOR i := 1 TO indent DO D.Str( " " ); END;
  126. D.Str( "start: " ); D.Str( s ); D.Str( " at pos " ); D.Int( symbol.position.start,1 );
  127. END S;
  128. PROCEDURE E( CONST s: ARRAY OF CHAR ); (* for debugging purposes only *)
  129. VAR i: LONGINT;
  130. BEGIN
  131. D.Ln; D.Int( indent,1 );
  132. FOR i := 1 TO indent DO D.Str( " " ); END;
  133. D.Str( "end : " ); D.Str( s ); D.Str( " at pos " ); D.Int( symbol.position.start,1 );
  134. DEC(indent);
  135. END E;
  136. PROCEDURE EE( CONST s, t: ARRAY OF CHAR ); (* for debugging purposes only *)
  137. VAR i: LONGINT;
  138. BEGIN
  139. D.Ln; D.Int( indent,1 );
  140. FOR i := 1 TO indent DO D.Str( " " ); END;
  141. D.Str( "end : " ); D.Str( s ); D.Str( " (" ); D.Str( t ); D.Str( ") at pos " );
  142. DEC(indent);
  143. END EE;
  144. (** constructor, init parser with scanner providing input and with diagnostics for error output *)
  145. PROCEDURE & Init*( scanner: Scanner.Scanner; diagnostics: Diagnostics.Diagnostics );
  146. BEGIN
  147. SELF.scanner := scanner;
  148. SELF.diagnostics := diagnostics;
  149. error := ~scanner.GetNextSymbol(symbol);
  150. hasSymbol := TRUE;
  151. IF error THEN Basic.Error(diagnostics, scanner.source^, Basic.invalidPosition, "no input stream") END;
  152. recentCommentItem := NIL; recentComment := NIL;
  153. (* debugging *)
  154. indent := 0;
  155. Lax := FALSE;
  156. END Init;
  157. PROCEDURE Reset*;
  158. BEGIN
  159. error := FALSE;
  160. END Reset;
  161. PROCEDURE SetLax*;
  162. BEGIN
  163. Lax := TRUE;
  164. END SetLax;
  165. (** output error message and / or given code *)
  166. PROCEDURE Error(position: Position; code: LONGINT; CONST message: ARRAY OF CHAR);
  167. BEGIN
  168. Basic.ErrorC(diagnostics, scanner.source^, position, code, message);
  169. error := TRUE
  170. END Error;
  171. (** helper procedures interfacing to the scanner **)
  172. PROCEDURE SkipComments(b: BOOLEAN);
  173. VAR comment: SyntaxTree.Comment;
  174. BEGIN
  175. WHILE ~error & (b & (TokenB()= Scanner.Comment) OR ~b & (Token() = Scanner.Comment)) DO
  176. comment := SyntaxTree.NewComment(symbol.position, currentScope, symbol.string^,symbol.stringLength);
  177. IF moduleScope # NIL THEN
  178. moduleScope.AddComment(comment);
  179. END;
  180. IF recentComment = NIL THEN
  181. recentComment := comment;
  182. IF symbol.position.line = recentLine THEN
  183. IF recentCommentItem # NIL THEN
  184. IF (recentCommentItem IS SyntaxTree.Symbol) THEN
  185. IF recentCommentItem(SyntaxTree.Symbol).comment = NIL THEN
  186. recentCommentItem(SyntaxTree.Symbol).SetComment(comment)
  187. END;
  188. ELSIF (recentCommentItem IS SyntaxTree.Statement) THEN
  189. IF recentCommentItem(SyntaxTree.Statement).comment = NIL THEN
  190. recentCommentItem(SyntaxTree.Statement).SetComment(comment)
  191. END;
  192. ELSIF (recentCommentItem IS SyntaxTree.IfPart) THEN
  193. IF recentCommentItem(SyntaxTree.IfPart).comment = NIL THEN
  194. recentCommentItem(SyntaxTree.IfPart).SetComment(comment)
  195. END;
  196. ELSIF (recentCommentItem IS SyntaxTree.CasePart) THEN
  197. IF recentCommentItem(SyntaxTree.CasePart).comment = NIL THEN
  198. recentCommentItem(SyntaxTree.CasePart).SetComment(comment)
  199. END;
  200. ELSIF (recentCommentItem IS SyntaxTree.WithPart) THEN
  201. IF recentCommentItem(SyntaxTree.WithPart).comment = NIL THEN
  202. recentCommentItem(SyntaxTree.WithPart).SetComment(comment)
  203. END;
  204. END;
  205. comment.SetItem(recentCommentItem,TRUE);
  206. recentComment := NIL;
  207. recentCommentItem := NIL
  208. END;
  209. END;
  210. END;
  211. NextSymbol;
  212. (*error := ~scanner.GetNextSymbol(symbol);*)
  213. END;
  214. END SkipComments;
  215. (** Get next symbol from scanner and store it in object-local variable 'symbol' *)
  216. PROCEDURE NextSymbol*;
  217. VAR comment: SyntaxTree.Comment;
  218. BEGIN
  219. (*
  220. error := ~scanner.GetNextSymbol(symbol) OR error;
  221. hasSymbol := TRUE;
  222. SkipComments();
  223. *)
  224. hasSymbol := FALSE;
  225. END NextSymbol;
  226. PROCEDURE Token*(): LONGINT;
  227. BEGIN
  228. IF ~hasSymbol OR (symbol.token = Scanner.Escape) THEN
  229. error := ~scanner.GetNextSymbol(symbol) OR error;
  230. IF symbol.token = Scanner.Escape THEN
  231. error := ~scanner.GetNextSymbol(symbol) OR error;
  232. END;
  233. hasSymbol := TRUE;
  234. SkipComments(FALSE);
  235. END;
  236. RETURN symbol.token;
  237. END Token;
  238. (* stop on escape token *)
  239. PROCEDURE TokenB*(): LONGINT;
  240. BEGIN
  241. IF ~hasSymbol THEN
  242. error := ~scanner.GetNextSymbol(symbol) OR error;
  243. hasSymbol := TRUE;
  244. SkipComments(TRUE);
  245. END;
  246. RETURN symbol.token;
  247. END TokenB;
  248. (** Check if current symbol equals sym. If yes then return true, return false otherwise *)
  249. PROCEDURE PeekB*(token: Scanner.Token): BOOLEAN;
  250. VAR comment: SyntaxTree.Comment;
  251. BEGIN
  252. RETURN TokenB() = token
  253. END PeekB;
  254. (** Check if current symbol equals sym. If yes then return true, return false otherwise *)
  255. PROCEDURE Peek*(token: Scanner.Token): BOOLEAN;
  256. VAR comment: SyntaxTree.Comment;
  257. BEGIN
  258. SkipComments(FALSE);
  259. RETURN Token() = token
  260. END Peek;
  261. (** Check if the current symbol equals sym.If yes then read next symbol, report error otherwise. returns success value *)
  262. PROCEDURE Mandatory*( token: Scanner.Token): BOOLEAN;
  263. BEGIN
  264. ASSERT( token # Scanner.Identifier ); ASSERT( token # Scanner.String ); ASSERT( token # Scanner.Number ); (* because of NextSymbol ! *)
  265. IF ~Peek(token) THEN
  266. Error( symbol.position, token, "" );
  267. RETURN FALSE
  268. ELSE
  269. NextSymbol;
  270. RETURN TRUE
  271. END
  272. END Mandatory;
  273. (** Check if the current symbol equals sym. If yes then read next symbol, report error otherwise *)
  274. PROCEDURE Check( token: Scanner.Token );
  275. VAR b: BOOLEAN;
  276. BEGIN
  277. b := Mandatory( token );
  278. END Check;
  279. (** Check if current symbol is an identifier. If yes then copy identifier to name and get next symbol,
  280. report error otherwise and set name to empty name. returns success value *)
  281. PROCEDURE MandatoryIdentifier( VAR name: SyntaxTree.Identifier): BOOLEAN;
  282. BEGIN
  283. IF Peek(Scanner.Identifier) THEN
  284. name := symbol.identifier;
  285. NextSymbol;
  286. RETURN TRUE
  287. ELSE
  288. Error( symbol.position, Scanner.Identifier, "" );
  289. name := SyntaxTree.invalidIdentifier;
  290. RETURN FALSE
  291. END
  292. END MandatoryIdentifier;
  293. (** Expect an identifier (using MandatoryIdentifier) and return identifier object **)
  294. PROCEDURE Identifier(VAR position: Position): SyntaxTree.Identifier;
  295. VAR name: SyntaxTree.Identifier; identifier: SyntaxTree.Identifier;
  296. BEGIN
  297. position := symbol.position;
  298. IF MandatoryIdentifier(name) THEN
  299. identifier := name;
  300. ELSE
  301. identifier := SyntaxTree.invalidIdentifier;
  302. END;
  303. RETURN identifier
  304. END Identifier;
  305. (** Check if current symbol is a string (or string-like character). If yes then copy identifier to name and get next symbol,
  306. report error otherwise and set name to empty name. returns success value*)
  307. PROCEDURE MandatoryString*( VAR name: Scanner.StringType ): BOOLEAN;
  308. BEGIN
  309. IF Peek( Scanner.String) THEN
  310. name := symbol.string;
  311. NextSymbol;
  312. RETURN TRUE
  313. ELSIF Peek( Scanner.Character) THEN (* for compatibility with release: characters treated as strings *)
  314. name := symbol.string;
  315. NextSymbol;
  316. RETURN TRUE
  317. ELSE
  318. Error( symbol.position, Scanner.String, "" );
  319. NEW(name,1); name^ := "";
  320. RETURN FALSE
  321. END
  322. END MandatoryString;
  323. (** Check if current symbol is an identifier and if the name matches. If yes then get next symbol, report error otherwise. returns success value*)
  324. PROCEDURE ExpectThisIdentifier( name: SyntaxTree.Identifier ): BOOLEAN;
  325. VAR string: ARRAY 64 OF CHAR;
  326. BEGIN
  327. IF name = SyntaxTree.invalidIdentifier THEN (* nothing to be expected *)
  328. RETURN TRUE
  329. ELSIF (Token() # Scanner.Identifier) OR (symbol.identifier # name) THEN
  330. Basic.GetString(name,string);
  331. Error( symbol.position, Scanner.Identifier, string );
  332. RETURN FALSE
  333. ELSE
  334. NextSymbol;
  335. RETURN TRUE
  336. END
  337. END ExpectThisIdentifier;
  338. (** Check if current symbol is an identifier and if the name matches. If yes then get next symbol, report error otherwise. returns success value*)
  339. PROCEDURE ExpectThisString( CONST name: ARRAY OF CHAR ): BOOLEAN;
  340. BEGIN
  341. IF Peek(Scanner.String) & (symbol.string^ = name) THEN
  342. NextSymbol;
  343. RETURN TRUE
  344. ELSE
  345. Error( symbol.position, Scanner.String, name );
  346. RETURN FALSE
  347. END
  348. END ExpectThisString;
  349. (** Check if current symbol equals sym. If yes then get next symbol, return false otherwise *)
  350. PROCEDURE Optional*( token: Scanner.Token ): BOOLEAN;
  351. BEGIN
  352. (* do not use for Identifier, String or Number, if the result is needed ! *)
  353. IF Peek(token) THEN
  354. NextSymbol;
  355. RETURN TRUE
  356. ELSE
  357. RETURN FALSE
  358. END
  359. END Optional;
  360. PROCEDURE OptionalB*( token: Scanner.Token ): BOOLEAN;
  361. BEGIN
  362. (* do not use for Identifier, String or Number, if the result is needed ! *)
  363. IF PeekB(token) THEN
  364. NextSymbol;
  365. RETURN TRUE
  366. ELSE
  367. RETURN FALSE
  368. END
  369. END OptionalB;
  370. (* ignore one ore more symbols of type token *)
  371. PROCEDURE Ignore(token: Scanner.Token);
  372. BEGIN WHILE Optional(token) DO END;
  373. END Ignore;
  374. (** Parsing according to the EBNF **)
  375. (** QualifiedIdentifier = Identifier ['.' Identifier]. **)
  376. PROCEDURE QualifiedIdentifier*( ): SyntaxTree.QualifiedIdentifier;
  377. VAR prefix,suffix: SyntaxTree.Identifier; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; position0,position1: Position;
  378. BEGIN
  379. IF Trace THEN S( "QualifiedIdentifier" ) END;
  380. prefix := Identifier(position0);
  381. IF prefix # SyntaxTree.invalidIdentifier THEN
  382. IF ~Optional( Scanner.Period )THEN
  383. suffix := prefix; prefix := SyntaxTree.invalidIdentifier; (* empty *)
  384. ELSE
  385. suffix := Identifier(position1);
  386. END;
  387. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier( position0, prefix,suffix);
  388. ELSE
  389. qualifiedIdentifier := SyntaxTree.invalidQualifiedIdentifier;
  390. END;
  391. IF Trace THEN E( "QualifiedIdentifier" ) END;
  392. RETURN qualifiedIdentifier
  393. END QualifiedIdentifier;
  394. (** IdentifierDefinition = Identifier [ '*' | '-' ]. **)
  395. PROCEDURE IdentifierDefinition( VAR name: SyntaxTree.Identifier; VAR access: SET; allowedReadOnly: BOOLEAN);
  396. VAR position: Position;
  397. BEGIN
  398. IF Trace THEN S( "IdentifierDefinition" ) END;
  399. name := Identifier(position);
  400. IF Optional( Scanner.Times ) THEN
  401. access := SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal;
  402. ELSIF Optional( Scanner.Minus ) THEN
  403. IF ~allowedReadOnly THEN
  404. Error( symbol.position, Diagnostics.Invalid, "may not be defined read-only" )
  405. ELSE
  406. access := SyntaxTree.ReadOnly + {SyntaxTree.InternalWrite};
  407. END;
  408. ELSE
  409. access := SyntaxTree.Internal;
  410. END;
  411. IF Trace THEN E( "IdentifierDefinition") END;
  412. END IdentifierDefinition;
  413. (** ExpressionList = Expression { ',' Expression }. **)
  414. PROCEDURE ExpressionList( expressionList: SyntaxTree.ExpressionList );
  415. VAR expression: SyntaxTree.Expression;
  416. BEGIN
  417. IF Trace THEN S( "ExpressionList" ) END;
  418. REPEAT
  419. expression := Expression();
  420. expressionList.AddExpression( expression )
  421. UNTIL ~Optional( Scanner.Comma );
  422. IF Trace THEN E( "ExpressionList" ) END;
  423. END ExpressionList;
  424. (** IndexList = '?' [',' ExpressionList ] | ExpressionList [',' '?']. **)
  425. PROCEDURE IndexList(expressionList: SyntaxTree.ExpressionList);
  426. VAR
  427. position: Position;
  428. done: BOOLEAN;
  429. BEGIN
  430. IF Trace THEN S( "IndexList" ) END;
  431. position := symbol.position;
  432. IF Optional(Scanner.Questionmark) THEN
  433. expressionList.AddExpression(SyntaxTree.NewTensorRangeExpression(position));
  434. IF Optional(Scanner.Comma) THEN
  435. ExpressionList(expressionList);
  436. END
  437. ELSE
  438. expressionList.AddExpression(Expression());
  439. done := FALSE;
  440. WHILE ~done DO
  441. IF Optional(Scanner.Comma) THEN
  442. IF Optional(Scanner.Questionmark) THEN
  443. expressionList.AddExpression(SyntaxTree.NewTensorRangeExpression(position));
  444. done := TRUE;
  445. ELSE
  446. expressionList.AddExpression(Expression())
  447. END
  448. ELSE
  449. done := TRUE
  450. END
  451. END
  452. END;
  453. IF Trace THEN E( "IndexList" ) END;
  454. END IndexList;
  455. (** RangeExpression = SimpleExpression | [SimpleExpression] '..' [SimpleExpression] ['by' SimpleExpression] | '*'.
  456. i.e., a RangeExpression can have one of the following forms:
  457. '*'
  458. '..' [delimiter]
  459. '..' 'by' SimpleExpression
  460. '..' SimpleExpression
  461. '..' SimpleExpression 'by' SimpleExpression
  462. SimpleExpression
  463. SimpleExpression '..' [delimiter]
  464. SimpleExpression '..' 'by' SimpleExpression
  465. SimpleExpression '..' SimpleExpression
  466. SimpleExpression '..' SimpleExpression 'by' SimpleExpression
  467. a RangeExpression is always delimited by any of the following tokens: ",", ";", ":", "]", ")", "}", "=", "#", "END".
  468. **)
  469. PROCEDURE RangeExpression(): SyntaxTree.Expression;
  470. VAR
  471. expression, first, last, step: SyntaxTree.Expression;
  472. position: Position;
  473. PROCEDURE HasDelimiter(): BOOLEAN;
  474. BEGIN RETURN
  475. Peek(Scanner.Comma) OR Peek(Scanner.Semicolon) OR Peek(Scanner.Colon) OR
  476. Peek(Scanner.RightBracket) OR Peek(Scanner.RightParenthesis) OR Peek(Scanner.RightBrace) OR
  477. Peek(Scanner.Equal) OR Peek(Scanner.Unequal) OR Peek(Scanner.End)
  478. END HasDelimiter;
  479. BEGIN
  480. IF Trace THEN S( "RangeExpression" ) END;
  481. position := symbol.position;
  482. IF Optional(Scanner.Times) THEN
  483. expression := SyntaxTree.NewRangeExpression(position, NIL, NIL, NIL)
  484. ELSIF Optional(Scanner.Upto) THEN
  485. (* is range expression *)
  486. first := NIL;
  487. IF HasDelimiter() THEN
  488. last := NIL;
  489. step := NIL
  490. ELSIF Optional(Scanner.By) THEN
  491. last := NIL;
  492. step := SimpleExpression()
  493. ELSE
  494. last := SimpleExpression();
  495. IF Optional(Scanner.By) THEN
  496. step := SimpleExpression()
  497. ELSE
  498. step := NIL
  499. END
  500. END;
  501. expression := SyntaxTree.NewRangeExpression(position, first, last, step)
  502. ELSE
  503. expression := SimpleExpression();
  504. IF OptionalB(Scanner.Upto) THEN
  505. (* is range expression *)
  506. first := expression;
  507. IF HasDelimiter() THEN
  508. last := NIL;
  509. step := NIL
  510. ELSIF Optional(Scanner.By) THEN
  511. last := NIL;
  512. step := SimpleExpression()
  513. ELSE
  514. last := SimpleExpression();
  515. IF Optional(Scanner.By) THEN
  516. step := SimpleExpression()
  517. ELSE
  518. step := NIL
  519. END
  520. END;
  521. expression := SyntaxTree.NewRangeExpression(position, first, last, step)
  522. END;
  523. END;
  524. IF Trace THEN E( "RangeExpression" ) END;
  525. RETURN expression
  526. END RangeExpression;
  527. (** Designator = ('self' | 'result' | Identifier)
  528. {'.' Identifier | '[' IndexList ']' | '(' [ExpressionList] ')' | '^'} [Flags].
  529. **)
  530. PROCEDURE Designator( ): SyntaxTree.Designator;
  531. VAR
  532. designator: SyntaxTree.Designator; expressionList: SyntaxTree.ExpressionList;
  533. identifier: SyntaxTree.Identifier; position: Position;
  534. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  535. qualifiedType : SyntaxTree.QualifiedType;
  536. BEGIN
  537. IF Trace THEN S( "Designator" ) END;
  538. position := symbol.position;
  539. IF Optional(Scanner.Self) THEN
  540. designator := SyntaxTree.NewSelfDesignator(position);
  541. ELSIF Optional(Scanner.Result) THEN
  542. designator := SyntaxTree.NewResultDesignator(position);
  543. (* ADDRESS AND SIZE can be type identifiers used for type conversion *)
  544. ELSIF (Token() = Scanner.Address) OR (Token()=Scanner.Size) THEN
  545. identifier := symbol.identifier;
  546. designator := SyntaxTree.NewIdentifierDesignator(position,identifier);
  547. NextSymbol;
  548. ELSIF (Token() = Scanner.New) THEN
  549. identifier := symbol.identifier;
  550. designator := SyntaxTree.NewIdentifierDesignator(position,identifier);
  551. NextSymbol;
  552. IF Token() # Scanner.LeftParenthesis THEN (* NEW Type () *)
  553. qualifiedIdentifier := QualifiedIdentifier();
  554. qualifiedType := SyntaxTree.NewQualifiedType(qualifiedIdentifier.position, currentScope, qualifiedIdentifier);
  555. IF Mandatory( Scanner.LeftParenthesis ) THEN
  556. expressionList := SyntaxTree.NewExpressionList();
  557. IF ~Optional(Scanner.RightParenthesis) THEN
  558. ExpressionList( expressionList );
  559. Check( Scanner.RightParenthesis )
  560. END;
  561. END;
  562. designator := SyntaxTree.NewBuiltinCallDesignator(position,Global.New, NIL, expressionList);
  563. designator(SyntaxTree.BuiltinCallDesignator).SetReturnType(qualifiedType);
  564. (* special case: NEW Type() *)
  565. END;
  566. ELSE
  567. identifier := Identifier(position);
  568. designator := SyntaxTree.NewIdentifierDesignator(position,identifier);
  569. END;
  570. LOOP
  571. position := symbol.position;
  572. IF OptionalB( Scanner.LeftParenthesis ) THEN
  573. expressionList := SyntaxTree.NewExpressionList();
  574. IF ~Optional( Scanner.RightParenthesis ) THEN
  575. ExpressionList( expressionList );
  576. Check( Scanner.RightParenthesis )
  577. END;
  578. designator := SyntaxTree.NewParameterDesignator( position,designator,expressionList);
  579. ELSIF OptionalB( Scanner.Period ) THEN
  580. IF ~Optional(Scanner.Identifier) THEN (* make sure symbol is read *) END;
  581. position := symbol.position;
  582. CASE symbol.identifierString[0] OF
  583. "a".."z", "A" .. "Z":
  584. (*IF Peek(Scanner.Size) (* special rule: support for SYSTEM.SIZE *) THEN*)
  585. identifier := symbol.identifier;
  586. NextSymbol;
  587. ELSE
  588. identifier := Identifier(position);
  589. END;
  590. designator := SyntaxTree.NewSelectorDesignator(position,designator,identifier);
  591. ELSIF OptionalB( Scanner.LeftBracket ) THEN
  592. expressionList := SyntaxTree.NewExpressionList();
  593. IndexList( expressionList );
  594. Check( Scanner.RightBracket );
  595. designator:= SyntaxTree.NewBracketDesignator( position,designator,expressionList );
  596. ELSIF OptionalB( Scanner.Arrow ) THEN
  597. designator:= SyntaxTree.NewArrowDesignator( position,designator );
  598. ELSE EXIT
  599. END;
  600. END;
  601. IF OptionalB(Scanner.LeftBrace) THEN
  602. designator.SetModifiers(Flags());
  603. END;
  604. (*IF OptionalB(Scanner.Escape) THEN END; (* skip breaking signal *)*)
  605. IF Trace THEN E( "Designator" ) END;
  606. RETURN designator
  607. END Designator;
  608. (** Set = '{' [ RangeExpression {',' RangeExpression} ] '}'. **)
  609. PROCEDURE Set( ): SyntaxTree.Expression;
  610. VAR
  611. set: SyntaxTree.Set;
  612. BEGIN
  613. IF Trace THEN S( "Set" ) END;
  614. set := SyntaxTree.NewSet(symbol.position);
  615. Check(Scanner.LeftBrace);
  616. IF ~Optional(Scanner.RightBrace) THEN
  617. REPEAT
  618. set.elements.AddExpression(RangeExpression())
  619. UNTIL ~Optional(Scanner.Comma);
  620. Check(Scanner.RightBrace);
  621. END;
  622. set.End(symbol.position.end);
  623. IF Trace THEN E( "Set" ) END;
  624. RETURN set
  625. END Set;
  626. (* MathArray = '[' Expression {',' Expression} ']'. *)
  627. PROCEDURE MathArray(): SyntaxTree.Expression;
  628. VAR array: SyntaxTree.MathArrayExpression; element: SyntaxTree.Expression;
  629. BEGIN
  630. array := SyntaxTree.NewMathArrayExpression(symbol.position);
  631. IF ~Optional(Scanner.RightBracket) THEN
  632. REPEAT
  633. element := Expression();
  634. array.elements.AddExpression(element);
  635. UNTIL ~Optional(Scanner.Comma);
  636. Check(Scanner.RightBracket);
  637. END;
  638. RETURN array
  639. END MathArray;
  640. (** Factor = Number | Character | String | 'nil' | 'imag' | 'true' | 'false' | Set
  641. | '(' Expression ')' | '~' Factor | Factor '`' | Designator | MathArray.
  642. | 'SIZE' 'OF' Designator | 'ADDRESS' 'OF' Designator | 'ALIAS' OF Expression
  643. **)
  644. PROCEDURE Factor( ): SyntaxTree.Expression;
  645. VAR factor: SyntaxTree.Expression; position: Position; operator: LONGINT;
  646. BEGIN
  647. IF Trace THEN S( "Factor" ) END;
  648. position := symbol.position;
  649. CASE Token() OF
  650. | Scanner.Number:
  651. IF (symbol.numberType = Scanner.Integer) THEN
  652. factor := SyntaxTree.NewIntegerValue( position, symbol.integer);
  653. ELSIF (symbol.numberType = Scanner.Hugeint) THEN
  654. factor := SyntaxTree.NewIntegerValue(position, symbol.hugeint);
  655. ELSIF (symbol.numberType = Scanner.Real) OR (symbol.numberType = Scanner.Longreal) THEN
  656. factor := SyntaxTree.NewRealValue( position, symbol.real);
  657. factor(SyntaxTree.RealValue).SetSubtype(symbol.numberType);
  658. ELSE HALT( 100 )
  659. END;
  660. NextSymbol;
  661. | Scanner.Character:
  662. factor := SyntaxTree.NewCharacterValue(position,symbol.character);
  663. NextSymbol;
  664. | Scanner.String:
  665. factor := SyntaxTree.NewStringValue( position, symbol.string );
  666. NextSymbol;
  667. WHILE (Token() = Scanner.String) OR (Token() = Scanner.Character) DO
  668. IF Token() = Scanner.Character THEN
  669. factor(SyntaxTree.StringValue).AppendChar(symbol.character);
  670. ELSE
  671. factor(SyntaxTree.StringValue).Append(symbol.string);
  672. END;
  673. factor.End(symbol.position.end);
  674. NextSymbol;
  675. END;
  676. | Scanner.Nil:
  677. factor := SyntaxTree.NewNilValue( position );
  678. NextSymbol;
  679. | Scanner.Imag:
  680. factor := SyntaxTree.NewComplexValue(position, 0, 1);
  681. factor(SyntaxTree.ComplexValue).SetSubtype(Scanner.Real);
  682. NextSymbol;
  683. | Scanner.True:
  684. factor := SyntaxTree.NewBooleanValue( position, TRUE );
  685. NextSymbol;
  686. | Scanner.False:
  687. factor := SyntaxTree.NewBooleanValue( position, FALSE );
  688. NextSymbol;
  689. | Scanner.LeftBrace:
  690. factor := Set();
  691. | Scanner.LeftParenthesis:
  692. NextSymbol;
  693. factor := Expression();
  694. Check( Scanner.RightParenthesis );
  695. factor.End( symbol.position.end );
  696. | Scanner.Not:
  697. NextSymbol;
  698. factor := Factor();
  699. factor := SyntaxTree.NewUnaryExpression( position, factor, Scanner.Not );
  700. factor.End( symbol.position.end );
  701. | Scanner.Address, Scanner.Size:
  702. operator := Token();
  703. factor := Designator(); (* ADDRESS AND SIZE can be type identifiers used for type conversion *)
  704. IF Optional(Scanner.Of) THEN
  705. factor := Designator();
  706. factor := SyntaxTree.NewUnaryExpression( position, factor, operator );
  707. END;
  708. factor.End (symbol.position.end)
  709. | Scanner.Alias:
  710. operator := Token();
  711. NextSymbol();
  712. IF Mandatory(Scanner.Of) THEN
  713. factor := Factor();
  714. factor := SyntaxTree.NewUnaryExpression( position, factor, operator );
  715. END;
  716. factor.End (symbol.position.end)
  717. | Scanner.Self, Scanner.Result, Scanner.Identifier, Scanner.New:
  718. factor := Designator();
  719. factor.End( symbol.position.end );
  720. | Scanner.LeftBracket:
  721. NextSymbol;
  722. factor := MathArray();
  723. factor.End(symbol.position.end);
  724. ELSE
  725. Error( position, Basic.ValueStartIncorrectSymbol, "" );
  726. NextSymbol; factor := SyntaxTree.invalidExpression;
  727. END;
  728. (* suffix *)
  729. IF OptionalB(Scanner.Transpose) THEN
  730. IF (factor IS SyntaxTree.UnaryExpression) & (factor(SyntaxTree.UnaryExpression).operator = Scanner.Transpose) THEN
  731. (* transpose operator has higher precedence than not, reevaluate expression: *)
  732. factor := factor(SyntaxTree.UnaryExpression).left;
  733. factor := SyntaxTree.NewUnaryExpression(position,factor,Scanner.Transpose);
  734. factor := SyntaxTree.NewUnaryExpression(position,factor,Scanner.Not);
  735. ELSE
  736. factor := SyntaxTree.NewUnaryExpression(position,factor,Scanner.Transpose);
  737. END;
  738. END;
  739. IF Trace THEN E( "Factor" ) END;
  740. RETURN factor
  741. END Factor;
  742. (** Term = Factor {MulOp Factor}.
  743. MulOp = '*' | '**' | '.*' | '+*' | '/' | '\' | './' | 'div' | 'mod' | '&'.
  744. **)
  745. PROCEDURE Term( ): SyntaxTree.Expression;
  746. VAR term, factor: SyntaxTree.Expression; operator: LONGINT; position: Position;
  747. BEGIN
  748. IF Trace THEN S( "Term" ) END;
  749. position := symbol.position;
  750. term := Factor();
  751. WHILE (TokenB() >= Scanner.Times) & (TokenB() <= Scanner.And) DO
  752. operator := Token();
  753. NextSymbol;
  754. factor := Factor();
  755. term := SyntaxTree.NewBinaryExpression( position, term, factor, operator );
  756. END;
  757. term.End( symbol.position.end );
  758. IF Trace THEN E( "Term" ) END;
  759. RETURN term
  760. END Term;
  761. (** SimpleExpression = ['+'|'-'] Term {AddOp Term}.
  762. AddOp = '+' | '-' | 'or'.
  763. **)
  764. PROCEDURE SimpleExpression( ): SyntaxTree.Expression;
  765. VAR operator: LONGINT; term, expression: SyntaxTree.Expression; position: Position;
  766. BEGIN
  767. IF Trace THEN S( "SimpleExpression" ) END;
  768. position := symbol.position;
  769. IF Peek(Scanner.Plus) OR Peek(Scanner.Minus) THEN (* sign should be part of the factor *)
  770. operator := Token();
  771. NextSymbol;
  772. term := Term();
  773. expression := SyntaxTree.NewUnaryExpression( position, term, operator );
  774. ELSE expression := Term();
  775. END;
  776. WHILE (TokenB() >= Scanner.Or) & (TokenB() <= Scanner.Minus) DO
  777. operator := Token();
  778. NextSymbol;
  779. term := Term();
  780. expression := SyntaxTree.NewBinaryExpression( position, expression, term, operator );
  781. END;
  782. IF Trace THEN E( "SimpleExpression" ) END;
  783. RETURN expression
  784. END SimpleExpression;
  785. (**
  786. Expression = RangeExpression [RelationOp RangeExpression].
  787. RelationOp = '=' | '.=' | '#' | '.#'
  788. | '<' | '.<' | '<=' | '.<=' | '>' | '.>' | '>=' | '.>='
  789. | '??' | '!!' | '<<?' | '>>?'
  790. | 'in' | 'is'
  791. **)
  792. PROCEDURE Expression*( ): SyntaxTree.Expression;
  793. VAR expression, rightExpression: SyntaxTree.Expression; operator: LONGINT; position: Position;
  794. BEGIN
  795. IF Trace THEN S( "Expression" ) END;
  796. position := symbol.position;
  797. expression := RangeExpression();
  798. IF (TokenB() >= Scanner.Equal) & (TokenB() <= Scanner.Is) THEN
  799. operator := Token();
  800. NextSymbol;
  801. rightExpression := RangeExpression();
  802. expression := SyntaxTree.NewBinaryExpression(position, expression, rightExpression, operator );
  803. END;
  804. (*IF OptionalB(Scanner.Escape) THEN END; (* skip breaking escape *)*)
  805. IF Trace THEN E( "Expression" ) END;
  806. RETURN expression
  807. END Expression;
  808. (** Case = RangeExpression {',' RangeExpression} ':' StatementSequence. **)
  809. PROCEDURE Case( caseStatement: SyntaxTree.CaseStatement );
  810. VAR
  811. casePart: SyntaxTree.CasePart;
  812. statements: SyntaxTree.StatementSequence;
  813. element: SyntaxTree.Expression;
  814. BEGIN
  815. IF Trace THEN S( "Case" ) END;
  816. casePart := SyntaxTree.NewCasePart();
  817. CommentCasePart(casePart);
  818. REPEAT
  819. element := RangeExpression();
  820. casePart.elements.AddExpression( element );
  821. UNTIL ~Optional( Scanner.Comma );
  822. Check( Scanner.Colon );
  823. statements := StatementSequence(caseStatement);
  824. casePart.SetStatements( statements );
  825. caseStatement.AddCasePart( casePart );
  826. IF Trace THEN E( "Case" ) END;
  827. END Case;
  828. (** Statement =
  829. [
  830. Designator [':=' Expression |'!' Expression | '?' Expression | '<<' Expresssion | '>>' Expression]
  831. | 'if' Expression 'then' StatementSequence
  832. {'elsif' Expression 'then' StatementSequence}
  833. ['else' StatementSequence]
  834. 'end'
  835. | 'with' Identifier ':' QualifiedIdentifier 'do' StatementSequence
  836. {'|' Identifier ':' QualifiedIdentifier 'do' StatementSequence}
  837. [else StatementSequence]
  838. 'end'
  839. | 'case' Expression 'of' ['|'] Case {'|' Case} ['else' StatementSequence] 'end'
  840. | 'while' Expression 'do' StatementSequence 'end'
  841. | 'repeat' StatementSequence 'until' Expression
  842. | 'for' Identifier ':=' Expression 'to' Expression ['by' Expression] 'do'
  843. StatementSequence 'end'
  844. | 'loop' StatementSequence 'end'
  845. | 'exit'
  846. | 'return' [Expression]
  847. | 'await' Expression
  848. | 'begin' StatementBlock 'end'
  849. | 'code' {any} 'end'
  850. ].
  851. **)
  852. PROCEDURE Statement*( statements: SyntaxTree.StatementSequence; outer: SyntaxTree.Statement): BOOLEAN;
  853. VAR qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; statement: SyntaxTree.Statement;
  854. ifStatement: SyntaxTree.IfStatement; elsifPart: SyntaxTree.IfPart; statementSequence: SyntaxTree.StatementSequence; withStatement: SyntaxTree.WithStatement;
  855. withPart: SyntaxTree.WithPart; caller: SyntaxTree.ProcedureCallStatement;
  856. caseStatement: SyntaxTree.CaseStatement; whileStatement: SyntaxTree.WhileStatement; repeatStatement: SyntaxTree.RepeatStatement; forStatement: SyntaxTree.ForStatement;
  857. identifier: SyntaxTree.Identifier; loopStatement: SyntaxTree.LoopStatement; returnStatement: SyntaxTree.ReturnStatement; awaitStatement: SyntaxTree.AwaitStatement;
  858. qualifiedType: SyntaxTree.QualifiedType; code : SyntaxTree.Code; position: Position; result: BOOLEAN;
  859. commToken: Scanner.Token;
  860. BEGIN
  861. IF Trace THEN S( "Statement" ) END;
  862. CASE Token() OF
  863. | Scanner.Identifier, Scanner.Self, Scanner.Result, Scanner.New:
  864. designator := Designator();
  865. position := symbol.position;
  866. IF OptionalB( Scanner.Becomes ) THEN
  867. expression := Expression();
  868. statement := SyntaxTree.NewAssignment( position, designator, expression,outer );
  869. CommentStatement(statement);
  870. ELSIF PeekB(Scanner.ExclamationMark) OR PeekB(Scanner.Questionmark) OR PeekB(Scanner.LessLess) OR PeekB(Scanner.GreaterGreater) THEN
  871. commToken := Token();
  872. NextSymbol;
  873. expression := Expression();
  874. statement := SyntaxTree.NewCommunicationStatement(position, commToken, designator, expression, outer);
  875. CommentStatement(statement);
  876. ELSE
  877. caller := SyntaxTree.NewProcedureCallStatement(designator.position, designator,outer);
  878. statement := caller;
  879. CommentStatement(statement);
  880. END;
  881. statements.AddStatement( statement );
  882. (*IF OptionalB(Scanner.Escape) THEN END;*)
  883. result := TRUE
  884. | Scanner.If:
  885. NextSymbol;
  886. ifStatement := SyntaxTree.NewIfStatement( symbol.position ,outer);
  887. CommentStatement(ifStatement);
  888. expression := Expression();
  889. ifStatement.ifPart.SetCondition( expression );
  890. Check( Scanner.Then );
  891. statementSequence := StatementSequence(ifStatement);
  892. ifStatement.ifPart.SetStatements( statementSequence );
  893. WHILE Optional( Scanner.Elsif ) DO
  894. elsifPart := SyntaxTree.NewIfPart();
  895. CommentIfPart(elsifPart);
  896. ifStatement.AddElsifPart( elsifPart);
  897. expression := Expression();
  898. elsifPart.SetCondition( expression );
  899. Check( Scanner.Then );
  900. statementSequence := StatementSequence(ifStatement);
  901. elsifPart.SetStatements( statementSequence );
  902. END;
  903. IF Optional( Scanner.Else ) THEN
  904. statementSequence := StatementSequence(ifStatement);
  905. ifStatement.SetElsePart( statementSequence );
  906. END;
  907. Check( Scanner.End ); statements.AddStatement( ifStatement );
  908. result := TRUE
  909. | Scanner.With:
  910. withStatement := SyntaxTree.NewWithStatement( symbol.position ,outer);
  911. CommentStatement(withStatement);
  912. NextSymbol;
  913. REPEAT
  914. identifier := Identifier(position);
  915. IF Optional(Scanner.Period) & Optional(Scanner.Identifier) THEN
  916. Error(position,Diagnostics.Invalid,"forbidden qualified identifier in with statement");
  917. END;
  918. withPart := SyntaxTree.NewWithPart();
  919. CommentWithPart(withPart);
  920. withStatement.AddWithPart(withPart);
  921. designator := SyntaxTree.NewIdentifierDesignator(position,identifier);
  922. withPart.SetVariable( designator );
  923. Check( Scanner.Colon );
  924. qualifiedIdentifier := QualifiedIdentifier();
  925. qualifiedType := SyntaxTree.NewQualifiedType(qualifiedIdentifier.position, currentScope, qualifiedIdentifier);
  926. withPart.SetType(qualifiedType);
  927. Check( Scanner.Do );
  928. statementSequence := StatementSequence(withStatement);
  929. withPart.SetStatements( statementSequence );
  930. UNTIL ~Optional(Scanner.Bar) OR ~CascadedWithSupport;
  931. IF CascadedWithSupport & Optional(Scanner.Else) THEN
  932. statementSequence := StatementSequence(withStatement);
  933. withStatement.SetElsePart(statementSequence);
  934. END;
  935. Check( Scanner.End );
  936. statements.AddStatement( withStatement );
  937. result := TRUE
  938. | Scanner.Case:
  939. caseStatement := SyntaxTree.NewCaseStatement( symbol.position,outer );
  940. CommentStatement(caseStatement);
  941. NextSymbol;
  942. expression := Expression();
  943. Check( Scanner.Of );
  944. caseStatement.SetVariable( expression );
  945. IF Optional(Scanner.Bar) THEN END;
  946. REPEAT
  947. Case(caseStatement)
  948. UNTIL ~Optional(Scanner.Bar);
  949. IF Optional( Scanner.Else ) THEN
  950. statementSequence := StatementSequence(caseStatement);
  951. caseStatement.SetElsePart( statementSequence );
  952. END;
  953. Check( Scanner.End );
  954. statements.AddStatement( caseStatement );
  955. result := TRUE
  956. | Scanner.While:
  957. NextSymbol;
  958. whileStatement := SyntaxTree.NewWhileStatement( symbol.position, outer );
  959. CommentStatement(whileStatement);
  960. expression := Expression();
  961. Check( Scanner.Do );
  962. whileStatement.SetCondition( expression );
  963. statementSequence := StatementSequence(whileStatement);
  964. whileStatement.SetStatements( statementSequence );
  965. Check( Scanner.End );
  966. statements.AddStatement( whileStatement );
  967. result := TRUE
  968. | Scanner.Repeat:
  969. NextSymbol;
  970. repeatStatement := SyntaxTree.NewRepeatStatement( symbol.position, outer );
  971. CommentStatement(repeatStatement);
  972. statementSequence := StatementSequence(repeatStatement);
  973. repeatStatement.SetStatements( statementSequence );
  974. Check( Scanner.Until );
  975. expression := Expression();
  976. repeatStatement.SetCondition( expression );
  977. statements.AddStatement( repeatStatement );
  978. result := TRUE
  979. | Scanner.For:
  980. NextSymbol;
  981. forStatement := SyntaxTree.NewForStatement( symbol.position, outer);
  982. CommentStatement(forStatement);
  983. identifier := Identifier(position);
  984. IF Optional(Scanner.Period) & Optional(Scanner.Identifier) THEN
  985. Error(position,Diagnostics.Invalid,"forbidden non-local counter variable");
  986. END;
  987. designator := SyntaxTree.NewIdentifierDesignator(position,identifier);
  988. forStatement.SetVariable( designator );
  989. Check( Scanner.Becomes );
  990. expression := Expression();
  991. forStatement.SetFrom( expression );
  992. Check( Scanner.To );
  993. expression := Expression();
  994. forStatement.SetTo( expression );
  995. IF Optional( Scanner.By ) THEN
  996. expression := Expression();
  997. forStatement.SetBy( expression );
  998. END;
  999. Check( Scanner.Do );
  1000. statementSequence := StatementSequence(forStatement);
  1001. forStatement.SetStatements( statementSequence );
  1002. Check( Scanner.End );
  1003. statements.AddStatement( forStatement );
  1004. result := TRUE
  1005. | Scanner.Loop:
  1006. NextSymbol;
  1007. loopStatement := SyntaxTree.NewLoopStatement( symbol.position ,outer);
  1008. CommentStatement(loopStatement);
  1009. statementSequence := StatementSequence(loopStatement);
  1010. loopStatement.SetStatements( statementSequence );
  1011. Check( Scanner.End );
  1012. statements.AddStatement( loopStatement );
  1013. result := TRUE;
  1014. | Scanner.Exit:
  1015. NextSymbol;
  1016. statement := SyntaxTree.NewExitStatement( symbol.position, outer);
  1017. CommentStatement(statement);
  1018. statements.AddStatement( statement );
  1019. result := TRUE;
  1020. | Scanner.Return:
  1021. NextSymbol;
  1022. returnStatement := SyntaxTree.NewReturnStatement( symbol.position, outer);
  1023. CommentStatement(returnStatement);
  1024. IF (Token() >= Scanner.Plus) & (Token() <= Scanner.Identifier) THEN
  1025. expression := Expression();
  1026. returnStatement.SetReturnValue( expression );
  1027. END;
  1028. statements.AddStatement( returnStatement );
  1029. result := TRUE;
  1030. | Scanner.Begin:
  1031. NextSymbol; statement := StatementBlock(outer); statements.AddStatement( statement ); Check( Scanner.End );
  1032. result := TRUE;
  1033. | Scanner.Await:
  1034. awaitStatement := SyntaxTree.NewAwaitStatement( symbol.position, outer );
  1035. CommentStatement(awaitStatement);
  1036. NextSymbol;
  1037. expression := Expression();
  1038. awaitStatement.SetCondition( expression );
  1039. statements.AddStatement( awaitStatement );
  1040. result := TRUE
  1041. | Scanner.Code:
  1042. (* assemble *)
  1043. code := Code(outer);
  1044. Check(Scanner.End);
  1045. statements.AddStatement( code );
  1046. result := TRUE
  1047. | Scanner.End: result := FALSE (* end of if, with, case, while, for, loop, or statement sequence *)
  1048. | Scanner.Until: result := FALSE (* end of repeat *)
  1049. | Scanner.Else: result := FALSE (* else of if or case *)
  1050. | Scanner.Elsif: result := FALSE (* elsif of if *)
  1051. | Scanner.Bar: result := FALSE (* next case *)
  1052. | Scanner.Finally: result := FALSE (* end block by finally statement *)
  1053. | Scanner.Semicolon: result := FALSE (* allow the empty statement *)
  1054. (* builtin pseudo procedures are resolved by checker *)
  1055. ELSE
  1056. result := FALSE;
  1057. (*
  1058. IF Lax THEN
  1059. expression := Expression();
  1060. statement := SyntaxTree.NewAssignment( position, NIL, expression,outer );
  1061. statements.AddStatement(statement);
  1062. result := ~error;
  1063. ELSE
  1064. result := FALSE;
  1065. END;
  1066. *)
  1067. END;
  1068. IF Trace THEN E( "Statement" ) END;
  1069. RETURN result
  1070. END Statement;
  1071. (** StatementSequence = Statement {';' Statement}. **)
  1072. PROCEDURE StatementSequence*(outer: SyntaxTree.Statement ): SyntaxTree.StatementSequence;
  1073. VAR statements: SyntaxTree.StatementSequence; b: BOOLEAN;
  1074. BEGIN
  1075. IF Trace THEN S( "StatementSequence" ) END;
  1076. statements := SyntaxTree.NewStatementSequence();
  1077. IF Lax THEN
  1078. WHILE ~Peek(Scanner.Return) & Statement(statements,outer) DO Ignore(Scanner.Semicolon) END;
  1079. IF Peek(Scanner.Return) & Statement(statements,outer) THEN Ignore(Scanner.Semicolon) END; (* return bound to end of statement sequence *)
  1080. ELSE
  1081. REPEAT
  1082. b := Statement( statements,outer )
  1083. UNTIL ~Optional( Scanner.Semicolon );
  1084. END;
  1085. IF Trace THEN E( "StatementSequence" ) END;
  1086. RETURN statements
  1087. END StatementSequence;
  1088. (** StatementBlock = [Flags] StatementSequence. **)
  1089. PROCEDURE StatementBlock(outer: SyntaxTree.Statement): SyntaxTree.StatementBlock;
  1090. VAR block: SyntaxTree.StatementBlock; position: Position;
  1091. BEGIN
  1092. IF Trace THEN S( "StatementBlock" ) END;
  1093. position := symbol.position;
  1094. position.start := position.end;
  1095. block := SyntaxTree.NewStatementBlock( position, outer );
  1096. CommentStatement(block);
  1097. IF Optional( Scanner.LeftBrace ) THEN
  1098. block.SetModifier(Flags());
  1099. END;
  1100. block.SetStatementSequence( StatementSequence(block) );
  1101. IF Trace THEN E( "StatementBlock" ) END;
  1102. RETURN block
  1103. END StatementBlock;
  1104. (** Code = { any \ 'end' \ 'with' } ['with' {('in'|'out') StatementSequence}] . **)
  1105. PROCEDURE Code(outer: SyntaxTree.Statement): SyntaxTree.Code;
  1106. VAR startPos: Position; endPos, i ,len: LONGINT; codeString: Scanner.StringType; code: SyntaxTree.Code;
  1107. end: Scanner.Token; in, out: BOOLEAN; left, right: SyntaxTree.Identifier;
  1108. statements, rules: SyntaxTree.StatementSequence;
  1109. BEGIN
  1110. startPos := symbol.position;
  1111. end := scanner.SkipToEndOfCode(startPos.start, endPos, symbol);
  1112. IF (end = Scanner.End) OR (end = Scanner.With) THEN
  1113. codeString := symbol.string;
  1114. code := SyntaxTree.NewCode(startPos,outer);
  1115. i := 0; len := LEN(codeString^);
  1116. code.SetSourceCode(codeString,len);
  1117. IF (end = Scanner.With) & Mandatory(Scanner.With) THEN
  1118. in := Optional(Scanner.In);
  1119. out := Optional(Scanner.Out);
  1120. WHILE in OR out DO
  1121. statements := StatementSequence(code);
  1122. IF in THEN rules := code.inRules ELSE rules := code.outRules END;
  1123. FOR i := 0 TO statements.Length()-1 DO
  1124. rules.AddStatement(statements.GetStatement(i));
  1125. END;
  1126. in := Optional(Scanner.In);
  1127. out := Optional(Scanner.Out);
  1128. END;
  1129. END;
  1130. END;
  1131. RETURN code;
  1132. END Code;
  1133. (** Body = 'begin' [Flags] StatementSequence ['finally' StatementSequence]
  1134. | 'code' Code. **)
  1135. PROCEDURE Body( scope: SyntaxTree.ProcedureScope ): SyntaxTree.Body;
  1136. VAR body: SyntaxTree.Body; code: SyntaxTree.Code; position: Position; previousScope: SyntaxTree.Scope;
  1137. BEGIN
  1138. previousScope := currentScope;
  1139. currentScope := scope;
  1140. IF Trace THEN S( "Body" ) END;
  1141. IF Peek( Scanner.Code ) THEN
  1142. body := SyntaxTree.NewBody(symbol.position,scope); (* empty body for the time being *)
  1143. (* assemble *)
  1144. code := Code(body);
  1145. body.SetCode(code);
  1146. ELSIF Mandatory( Scanner.Begin ) THEN
  1147. body := SyntaxTree.NewBody(symbol.position,scope);
  1148. IF Optional( Scanner.LeftBrace ) THEN
  1149. body.SetModifier(Flags());
  1150. END;
  1151. position := symbol.position;
  1152. body.SetStatementSequence(StatementSequence(body));
  1153. IF Optional( Scanner.Finally ) THEN
  1154. body.SetFinally(StatementSequence(body));
  1155. END;
  1156. END;
  1157. IF Trace THEN E( "Body" ) END;
  1158. currentScope := previousScope;
  1159. RETURN body
  1160. END Body;
  1161. (** wrapper for a body in records and modules *)
  1162. PROCEDURE BodyProcedure(parentScope: SyntaxTree.Scope): SyntaxTree.Procedure;
  1163. VAR procedureScope: SyntaxTree.ProcedureScope; procedure: SyntaxTree.Procedure;
  1164. BEGIN
  1165. procedureScope := SyntaxTree.NewProcedureScope(parentScope);
  1166. IF parentScope IS SyntaxTree.ModuleScope THEN
  1167. procedure := SyntaxTree.NewProcedure( symbol.position, Global.ModuleBodyName,procedureScope);
  1168. procedure.SetAccess(SyntaxTree.Hidden);
  1169. ELSE
  1170. procedure := SyntaxTree.NewProcedure( symbol.position, Global.RecordBodyName,procedureScope);
  1171. (*! todo: make this a hidden symbol. Problematic when used with paco. *)
  1172. procedure.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1173. END;
  1174. parentScope.AddProcedureDeclaration(procedure);
  1175. procedure.SetType(SyntaxTree.NewProcedureType(SyntaxTree.invalidPosition,parentScope));
  1176. procedure.SetBodyProcedure(TRUE);
  1177. procedureScope.SetBody(Body(procedureScope));
  1178. RETURN procedure
  1179. END BodyProcedure;
  1180. (* ProcedureType = 'procedure' [Flags] [FormalParameters]. *)
  1181. PROCEDURE ProcedureType(position: Position; parentScope: SyntaxTree.Scope): SyntaxTree.ProcedureType;
  1182. VAR procedureType: SyntaxTree.ProcedureType;
  1183. BEGIN
  1184. IF Trace THEN S( "ProcedureType" ) END;
  1185. (* procedure symbol already consumed *)
  1186. procedureType := SyntaxTree.NewProcedureType( position, parentScope);
  1187. IF Optional(Scanner.LeftBrace) THEN
  1188. procedureType.SetModifiers(Flags());
  1189. END;
  1190. IF Optional(Scanner.LeftParenthesis) THEN FormalParameters( procedureType, parentScope) END;
  1191. IF Trace THEN E( "ProcedureType" )
  1192. END;
  1193. RETURN procedureType;
  1194. END ProcedureType;
  1195. (** ObjectType = 'object' | 'object' [Flags] ['(' (QualifiedIdentifier | ArrayType) ')'] DeclarationSequence [Body] 'end' [Identifier] . **)
  1196. PROCEDURE ObjectType(position: Position; name: SyntaxTree.Identifier; parentScope: SyntaxTree.Scope ): SyntaxTree.Type;
  1197. VAR
  1198. objectType: SyntaxTree.RecordType;
  1199. pointerType: SyntaxTree.PointerType;
  1200. recordScope: SyntaxTree.RecordScope;
  1201. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1202. baseType: SyntaxTree.Type;
  1203. identifier: SyntaxTree.Identifier;
  1204. str: Scanner.StringType;
  1205. type: SyntaxTree.Type;
  1206. modifiers: SyntaxTree.Modifier;
  1207. BEGIN
  1208. IF Trace THEN S( "ObjectType" ) END;
  1209. (* symbol object already consumed *)
  1210. (* generic empty OBJECT type *)
  1211. IF Peek(Scanner.Semicolon) OR Peek(Scanner.RightParenthesis) THEN
  1212. Scanner.GetKeyword(scanner.case,Scanner.Object,identifier);
  1213. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(position,SyntaxTree.invalidIdentifier,identifier);
  1214. type := SyntaxTree.NewQualifiedType( position, parentScope, qualifiedIdentifier );
  1215. RETURN type
  1216. END;
  1217. recordScope := SyntaxTree.NewRecordScope(parentScope);
  1218. pointerType := SyntaxTree.NewPointerType(position,parentScope);
  1219. objectType := SyntaxTree.NewRecordType( position,parentScope,recordScope);
  1220. objectType.IsObject(TRUE);
  1221. objectType.SetPointerType(pointerType);
  1222. pointerType.SetPointerBase(objectType);
  1223. IF Optional(Scanner.LeftBrace) THEN
  1224. modifiers := Flags();
  1225. pointerType.SetModifiers(modifiers);
  1226. END;
  1227. IF Optional( Scanner.LeftParenthesis ) THEN
  1228. IF Optional(Scanner.Array) THEN
  1229. baseType := ArrayType(position, parentScope) (* TODO: correct position? *)
  1230. ELSE
  1231. qualifiedIdentifier := QualifiedIdentifier();
  1232. baseType := SyntaxTree.NewQualifiedType(qualifiedIdentifier.position, parentScope, qualifiedIdentifier)
  1233. END;
  1234. objectType.SetBaseType(baseType);
  1235. Check( Scanner.RightParenthesis )
  1236. END;
  1237. (*
  1238. IF Optional( Scanner.Implements ) THEN
  1239. REPEAT
  1240. qualifiedIdentifier := QualifiedIdentifier()
  1241. UNTIL ~Optional( Scanner.Comma );
  1242. END;
  1243. *)
  1244. IF Optional( Scanner.Semicolon ) THEN
  1245. (*Warning(symbol.position,Diagnostics.Invalid,"no semicolon allowed here");*)
  1246. END;
  1247. DeclarationSequence( recordScope);
  1248. IF Peek(Scanner.Begin) OR Peek(Scanner.Code) THEN
  1249. recordScope.SetBodyProcedure(BodyProcedure(recordScope));
  1250. END;
  1251. Check(Scanner.End);
  1252. IF ExpectThisIdentifier( name ) THEN
  1253. (* check name not always, reflect in EBNF? *)
  1254. END;
  1255. IF Trace THEN E( "ObjectType" ) END;
  1256. RETURN pointerType
  1257. END ObjectType;
  1258. (** CellType = 'cell' [Flags] [PortList] [';'] DeclarationSequence [Body] 'end' [Identifier]
  1259. | 'object'. **)
  1260. PROCEDURE CellType(position: Position; name: SyntaxTree.Identifier; parentScope: SyntaxTree.Scope; isCellNet: BOOLEAN): SyntaxTree.Type;
  1261. VAR
  1262. cellType: SyntaxTree.CellType;
  1263. cellScope: SyntaxTree.CellScope;
  1264. modifiers: SyntaxTree.Modifier;
  1265. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1266. qualifiedType: SyntaxTree.Type;
  1267. BEGIN
  1268. IF Trace THEN S( "CellType" ) END;
  1269. (* symbol cell already consumed *)
  1270. cellScope := SyntaxTree.NewCellScope(parentScope);
  1271. cellType := SyntaxTree.NewCellType( position, parentScope,cellScope);
  1272. cellType.IsCellNet(isCellNet);
  1273. cellScope.SetOwnerCell(cellType);
  1274. IF Optional(Scanner.Colon) THEN
  1275. qualifiedIdentifier := QualifiedIdentifier();
  1276. qualifiedType := SyntaxTree.NewQualifiedType(qualifiedIdentifier.position, parentScope, qualifiedIdentifier );
  1277. cellType.SetBaseType( qualifiedType );
  1278. END;
  1279. IF Optional(Scanner.LeftBrace) THEN
  1280. modifiers := Flags();
  1281. cellType.SetModifiers(modifiers);
  1282. END;
  1283. IF Optional( Scanner.LeftParenthesis ) THEN
  1284. PortList(cellType,cellScope);
  1285. END;
  1286. IF Optional( Scanner.Semicolon ) THEN END;
  1287. IF Optional(Scanner.Import) THEN ImportList(cellScope) END;
  1288. DeclarationSequence( cellScope);
  1289. IF Peek(Scanner.Begin) OR Peek(Scanner.Code) THEN
  1290. cellScope.SetBodyProcedure(BodyProcedure(cellScope));
  1291. END;
  1292. Check(Scanner.End);
  1293. IF ExpectThisIdentifier( name ) THEN
  1294. (* check name not always, reflect in EBNF? *)
  1295. END;
  1296. IF Trace THEN E( "CellType" ) END;
  1297. RETURN cellType
  1298. END CellType;
  1299. (** PointerType = 'pointer' [Flags] 'to' Type. **)
  1300. PROCEDURE PointerType( position: Position; parentScope: SyntaxTree.Scope ): SyntaxTree.PointerType;
  1301. VAR pointerType: SyntaxTree.PointerType; base: SyntaxTree.Type; modifiers: SyntaxTree.Modifier;
  1302. BEGIN
  1303. IF Trace THEN S( "PointerType" ) END;
  1304. (* pointer symbol already consumed *)
  1305. pointerType := SyntaxTree.NewPointerType( position ,parentScope);
  1306. IF Optional(Scanner.LeftBrace) THEN
  1307. modifiers := Flags();
  1308. pointerType.SetModifiers(modifiers)
  1309. END;
  1310. Check( Scanner.To );
  1311. base := Type(SyntaxTree.invalidIdentifier, parentScope);
  1312. pointerType.SetPointerBase( base );
  1313. IF base IS SyntaxTree.RecordType THEN
  1314. base(SyntaxTree.RecordType).SetPointerType(pointerType);
  1315. END;
  1316. IF Trace THEN E( "PointerType" ) END;
  1317. RETURN pointerType
  1318. END PointerType;
  1319. (**
  1320. RecordType = 'record' [Flags] ['(' QualifiedIdentifier ')'] [VariableDeclaration {';' VariableDeclaration}] 'end'.
  1321. **)
  1322. PROCEDURE RecordType(position: Position; parentScope:SyntaxTree.Scope ): SyntaxTree.RecordType;
  1323. VAR
  1324. recordType: SyntaxTree.RecordType;
  1325. recordScope: SyntaxTree.RecordScope;
  1326. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; flags: SET; qualifiedType: SyntaxTree.QualifiedType;
  1327. modifier: SyntaxTree.Modifier;
  1328. BEGIN
  1329. IF Trace THEN S( "RecordType" ) END;
  1330. (* record symbol already consumed *)
  1331. flags := {};
  1332. recordScope := SyntaxTree.NewRecordScope(parentScope);
  1333. recordType := SyntaxTree.NewRecordType( position, parentScope, recordScope);
  1334. IF Optional( Scanner.LeftBrace ) THEN
  1335. modifier := Flags();
  1336. recordType.SetModifiers(modifier);
  1337. END;
  1338. IF Optional( Scanner.LeftParenthesis ) THEN
  1339. qualifiedIdentifier := QualifiedIdentifier();
  1340. qualifiedType := SyntaxTree.NewQualifiedType(qualifiedIdentifier.position, parentScope, qualifiedIdentifier );
  1341. recordType.SetBaseType( qualifiedType );
  1342. Check( Scanner.RightParenthesis )
  1343. END;
  1344. IF Lax THEN
  1345. WHILE Peek(Scanner.Identifier) DO VariableDeclaration(recordScope); Ignore(Scanner.Semicolon) END;
  1346. ELSE
  1347. REPEAT
  1348. IF Peek(Scanner.Identifier) THEN VariableDeclaration( recordScope ) END;
  1349. UNTIL ~Optional( Scanner.Semicolon );
  1350. END;
  1351. LOOP
  1352. IF Optional(Scanner.Procedure) THEN ProcedureDeclaration(recordScope); Ignore(Scanner.Semicolon);
  1353. ELSIF Optional(Scanner.Operator) THEN OperatorDeclaration(recordScope); Ignore(Scanner.Semicolon);
  1354. ELSE EXIT
  1355. END;
  1356. END;
  1357. (*WHILE Optional(Scanner.Procedure) DO ProcedureDeclaration(recordScope); Ignore(Scanner.Semicolon) END; *)
  1358. Check( Scanner.End );
  1359. IF Trace THEN E( "RecordType" ) END;
  1360. RETURN recordType
  1361. END RecordType;
  1362. (** ArrayType = 'array' 'of' Type | 'array' Expression {',' Expression} 'of' Type
  1363. | 'array' '[' MathArraySize {',' MathArraySize} ']' ['of' Type].
  1364. MathArraySize = Expression | '*' | '?'.
  1365. **)
  1366. PROCEDURE ArrayType(position: Position; parentScope: SyntaxTree.Scope ): SyntaxTree.Type;
  1367. VAR
  1368. arrayType: SyntaxTree.ArrayType;
  1369. type: SyntaxTree.Type;
  1370. base: SyntaxTree.Type;
  1371. expression: SyntaxTree.Expression;
  1372. PROCEDURE MathArray(): SyntaxTree.Type;
  1373. VAR mathType: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  1374. BEGIN
  1375. IF Optional(Scanner.Questionmark) THEN
  1376. mathType := SyntaxTree.NewMathArrayType(position,parentScope, SyntaxTree.Tensor);
  1377. ELSIF Optional(Scanner.Times) THEN (* open array *)
  1378. mathType := SyntaxTree.NewMathArrayType(position,parentScope, SyntaxTree.Open);
  1379. ELSE (* size given *)
  1380. mathType := SyntaxTree.NewMathArrayType(position,parentScope, SyntaxTree.Static);
  1381. expression := Expression();
  1382. mathType.SetLength(expression);
  1383. END;
  1384. IF Optional(Scanner.Comma) THEN
  1385. base := MathArray()
  1386. ELSIF Mandatory(Scanner.RightBracket) THEN
  1387. IF Optional( Scanner.Of ) THEN
  1388. base := Type(SyntaxTree.invalidIdentifier , parentScope ); (* base type *)
  1389. END;
  1390. END;
  1391. mathType.SetArrayBase(base);
  1392. RETURN mathType;
  1393. END MathArray;
  1394. BEGIN
  1395. IF Trace THEN S( "ArrayType" ) END;
  1396. (* array symbol already consumed *)
  1397. IF Optional(Scanner.LeftBracket) THEN (* math array *)
  1398. type := MathArray();
  1399. ELSIF Optional( Scanner.Of ) THEN (* open array *)
  1400. arrayType := SyntaxTree.NewArrayType(position,parentScope, SyntaxTree.Open);
  1401. type := arrayType;
  1402. base := Type( SyntaxTree.invalidIdentifier ,parentScope);
  1403. arrayType.SetArrayBase( base )
  1404. ELSE (* static array *)
  1405. arrayType := SyntaxTree.NewArrayType(position,parentScope, SyntaxTree.Static);
  1406. type := arrayType;
  1407. expression := SimpleExpression();
  1408. arrayType.SetLength( expression );
  1409. position := symbol.position;
  1410. IF Optional( Scanner.Comma ) THEN
  1411. base := ArrayType( position,parentScope);
  1412. arrayType.SetArrayBase( base )
  1413. ELSIF Mandatory( Scanner.Of ) THEN
  1414. base := Type(SyntaxTree.invalidIdentifier , parentScope ); (* base type *)
  1415. arrayType.SetArrayBase( base );
  1416. END;
  1417. END;
  1418. IF Trace THEN E( "ArrayType" ) END;
  1419. RETURN type
  1420. END ArrayType;
  1421. (** EnumerationType = 'enum' ['('QualifiedIdentifier')'] IdentifierDefinition ['=' Expression]
  1422. {',' IdentifierDefinition ['=' Expression]} 'end'. *)
  1423. PROCEDURE EnumerationType(position: Position; parentScope: SyntaxTree.Scope): SyntaxTree.Type;
  1424. VAR type: SyntaxTree.EnumerationType; scope: SyntaxTree.EnumerationScope; identifier: SyntaxTree.Identifier;
  1425. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; qualifiedType: SyntaxTree.QualifiedType; access: SET;
  1426. constant: SyntaxTree.Constant; expression: SyntaxTree.Expression;
  1427. BEGIN
  1428. (* enum symbol already consumed *)
  1429. scope := SyntaxTree.NewEnumerationScope(parentScope);
  1430. type := SyntaxTree.NewEnumerationType(position,parentScope, scope);
  1431. IF Optional( Scanner.LeftParenthesis ) THEN
  1432. qualifiedIdentifier := QualifiedIdentifier();
  1433. qualifiedType := SyntaxTree.NewQualifiedType(qualifiedIdentifier.position, parentScope, qualifiedIdentifier );
  1434. type.SetEnumerationBase( qualifiedType );
  1435. Check( Scanner.RightParenthesis )
  1436. END;
  1437. REPEAT
  1438. IdentifierDefinition(identifier,access,FALSE);
  1439. position := symbol.position;
  1440. constant := SyntaxTree.NewConstant( position, identifier );
  1441. CommentSymbol(constant);
  1442. constant.SetAccess(access);
  1443. IF Optional(Scanner.Equal) THEN
  1444. expression := Expression();
  1445. constant.SetValue( expression );
  1446. END;
  1447. scope.AddConstant( constant );
  1448. UNTIL ~Optional(Scanner.Comma);
  1449. IF Mandatory(Scanner.End) THEN END;
  1450. RETURN type
  1451. END EnumerationType;
  1452. (** PortType = 'port' ('in'|'out') ['(' Expression ')'] *)
  1453. PROCEDURE PortType(position: Position; parentScope: SyntaxTree.Scope): SyntaxTree.Type;
  1454. VAR type: SyntaxTree.Type; direction: LONGINT; sizeExpression: SyntaxTree.Expression;
  1455. BEGIN
  1456. (* port symbol already consumed *)
  1457. IF Optional(Scanner.In) THEN
  1458. direction := SyntaxTree.InPort
  1459. ELSIF Optional(Scanner.Out) THEN
  1460. direction := SyntaxTree.OutPort
  1461. ELSE
  1462. Error(position,Diagnostics.Invalid,"invalid direction, expected IN or OUT");
  1463. END;
  1464. IF Optional(Scanner.LeftParenthesis) THEN
  1465. sizeExpression := Expression();
  1466. IF Mandatory(Scanner.RightParenthesis )THEN END;
  1467. END;
  1468. type := SyntaxTree.NewPortType(position, direction, sizeExpression, parentScope);
  1469. RETURN type
  1470. END PortType;
  1471. (** Type = ArrayType | RecordType | PointerType | ObjectType | CellType | CellnetType | PortType
  1472. | ProcedureType | EnumerationType | QualifiedIdentifier. *)
  1473. PROCEDURE Type( name: SyntaxTree.Identifier; parentScope: SyntaxTree.Scope ): SyntaxTree.Type;
  1474. VAR type: SyntaxTree.Type; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; position: Position;
  1475. BEGIN
  1476. IF Trace THEN S( "Type" ) END;
  1477. position := symbol.position;
  1478. IF Optional( Scanner.Array ) THEN type := ArrayType( position,parentScope );
  1479. ELSIF Optional( Scanner.Record ) THEN type := RecordType( position,parentScope );
  1480. ELSIF Optional( Scanner.Pointer ) THEN type := PointerType( position,parentScope );
  1481. ELSIF Optional( Scanner.Object ) THEN type := ObjectType( position,name,parentScope );
  1482. ELSIF Optional( Scanner.Cell) THEN type := CellType( position, name, parentScope,FALSE);
  1483. ELSIF Optional( Scanner.CellNet) THEN type := CellType( position, name, parentScope, TRUE);
  1484. ELSIF Optional( Scanner.Port) THEN type := PortType( position, parentScope)
  1485. ELSIF Optional( Scanner.Procedure ) THEN type := ProcedureType( position,parentScope);
  1486. ELSIF Optional( Scanner.Enum ) THEN type := EnumerationType( position,parentScope);
  1487. ELSIF (Token() = Scanner.Address) OR (Token() = Scanner.Size) THEN
  1488. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(position,SyntaxTree.invalidIdentifier, symbol.identifier);
  1489. type := SyntaxTree.NewQualifiedType( qualifiedIdentifier.position, parentScope, qualifiedIdentifier );
  1490. NextSymbol;
  1491. ELSE qualifiedIdentifier := QualifiedIdentifier();
  1492. type := SyntaxTree.NewQualifiedType( qualifiedIdentifier.position, parentScope, qualifiedIdentifier );
  1493. END;
  1494. IF Trace THEN E( "Type" ) END;
  1495. RETURN type
  1496. END Type;
  1497. (** PortDeclaration = Identifier [Flags] {',' Identifier [Flags]}':' Type. **)
  1498. PROCEDURE PortDeclaration(cell: SyntaxTree.CellType; parentScope: SyntaxTree.Scope);
  1499. VAR
  1500. type: SyntaxTree.Type; name: SyntaxTree.Identifier;
  1501. firstParameter, parameter: SyntaxTree.Parameter;
  1502. position: Position; modifiers: SyntaxTree.Modifier;
  1503. BEGIN
  1504. IF Trace THEN S( "PortDeclaration" ) END;
  1505. firstParameter := cell.lastParameter;
  1506. REPEAT
  1507. name := Identifier(position);
  1508. parameter := SyntaxTree.NewParameter(position,cell,name,SyntaxTree.ValueParameter);
  1509. cell.AddParameter(parameter);
  1510. IF Optional(Scanner.LeftBrace) THEN
  1511. modifiers := Flags();
  1512. parameter.SetModifiers(modifiers);
  1513. END;
  1514. UNTIL ~Optional( Scanner.Comma );
  1515. Check( Scanner.Colon );
  1516. type := Type( SyntaxTree.invalidIdentifier, parentScope);
  1517. ASSERT(type # NIL);
  1518. IF firstParameter # NIL THEN parameter := firstParameter.nextParameter ELSE parameter := cell.firstParameter END;
  1519. WHILE parameter # NIL DO
  1520. parameter.SetType( type );
  1521. parameter := parameter.nextParameter;
  1522. END;
  1523. IF Trace THEN E( "PortDeclaration" )
  1524. END;
  1525. END PortDeclaration;
  1526. (** PortList = '(' [PortDeclaration {';' PortDeclaration}] ')'. **)
  1527. PROCEDURE PortList( cell: SyntaxTree.CellType ; parentScope: SyntaxTree.Scope);
  1528. BEGIN
  1529. IF Trace THEN S( "PortList" ) END;
  1530. (* left parenthesis already consumed *)
  1531. IF ~Optional( Scanner.RightParenthesis ) THEN
  1532. IF Lax THEN
  1533. WHILE Peek(Scanner.Identifier) OR Peek(Scanner.Var) OR Peek(Scanner.Const) DO PortDeclaration( cell, parentScope ); Ignore(Scanner.Semicolon) END;
  1534. ELSE
  1535. REPEAT PortDeclaration( cell, parentScope ); UNTIL ~Optional( Scanner.Semicolon );
  1536. END;
  1537. Check( Scanner.RightParenthesis );
  1538. END;
  1539. IF Trace THEN E( "PortList" ) END;
  1540. END PortList;
  1541. (** ParameterDeclaration = ['var'|'const'] Identifier [Flags] ['= Expression] {',' Identifier [Flags] ['= Expression]}':' Type.**)
  1542. PROCEDURE ParameterDeclaration( procedureType: SyntaxTree.ProcedureType ; parentScope: SyntaxTree.Scope);
  1543. VAR
  1544. type: SyntaxTree.Type; name: SyntaxTree.Identifier;
  1545. firstParameter, parameter: SyntaxTree.Parameter; kind: LONGINT; position: Position;
  1546. BEGIN
  1547. IF Trace THEN S( "ParameterDeclaration" ) END;
  1548. IF Optional( Scanner.Var ) THEN (* var parameter *)
  1549. kind := SyntaxTree.VarParameter
  1550. ELSIF Optional( Scanner.Const ) THEN (* const parameter *)
  1551. kind := SyntaxTree.ConstParameter
  1552. ELSIF Token() # Scanner.Identifier THEN
  1553. Error(symbol.position,Scanner.Identifier,"");
  1554. RETURN
  1555. ELSE kind := SyntaxTree.ValueParameter
  1556. END;
  1557. firstParameter := procedureType.lastParameter;
  1558. REPEAT
  1559. name := Identifier(position);
  1560. parameter := SyntaxTree.NewParameter(position,procedureType,name,kind);
  1561. IF Optional(Scanner.LeftBrace) THEN parameter.SetModifiers(Flags()) END;
  1562. procedureType.AddParameter(parameter);
  1563. IF Optional(Scanner.Equal) THEN
  1564. parameter.SetDefaultValue(Expression());
  1565. END
  1566. UNTIL ~Optional( Scanner.Comma );
  1567. Check( Scanner.Colon );
  1568. type := Type( SyntaxTree.invalidIdentifier, parentScope);
  1569. CommentSymbol(parameter);
  1570. ASSERT(type # NIL);
  1571. IF firstParameter # NIL THEN parameter := firstParameter.nextParameter ELSE parameter := procedureType.firstParameter END;
  1572. WHILE parameter # NIL DO
  1573. parameter.SetType( type );
  1574. parameter := parameter.nextParameter;
  1575. END;
  1576. IF Trace THEN E( "ParameterDeclaration" )
  1577. END;
  1578. END ParameterDeclaration;
  1579. (** FormalParameters = '(' [ParameterDeclaration {';' ParameterDeclaration}] ')' [':' [Flags] Type]. **)
  1580. PROCEDURE FormalParameters( procedureType: SyntaxTree.ProcedureType ; parentScope: SyntaxTree.Scope);
  1581. VAR type: SyntaxTree.Type; position: Position;
  1582. BEGIN
  1583. IF Trace THEN S( "FormalParameters" ) END;
  1584. (* left parenthesis already consumed *)
  1585. IF ~Optional( Scanner.RightParenthesis ) THEN
  1586. IF Lax THEN
  1587. WHILE Peek(Scanner.Identifier) OR Peek(Scanner.Const) OR Peek(Scanner.Var) DO
  1588. ParameterDeclaration(procedureType, parentScope); Ignore(Scanner.Semicolon)
  1589. END;
  1590. ELSE
  1591. REPEAT ParameterDeclaration( procedureType, parentScope ); UNTIL ~Optional( Scanner.Semicolon );
  1592. END;
  1593. Check( Scanner.RightParenthesis );
  1594. END;
  1595. IF Optional( Scanner.Colon ) THEN
  1596. position:= symbol.position;
  1597. IF Optional( Scanner.LeftBrace) THEN
  1598. procedureType.SetReturnTypeModifiers(Flags());
  1599. END;
  1600. type := Type(SyntaxTree.invalidIdentifier,parentScope);
  1601. (* formally, any type is permitted as return type. Actually some of them might simply not be usable *)
  1602. procedureType.SetReturnType(type);
  1603. END;
  1604. IF Trace THEN E( "FormalParameters" ) END;
  1605. END FormalParameters;
  1606. (** Flags = '{' [Identifier ['(' Expression ')'|'=' Expression] {',' Identifier ['(' Expression ')' | '=' Expression ] } ] '}'. **)
  1607. PROCEDURE Flags(): SyntaxTree.Modifier;
  1608. VAR identifier: SyntaxTree.Identifier; modifier,list: SyntaxTree.Modifier; position: Position; expression: SyntaxTree.Expression;
  1609. BEGIN
  1610. IF Trace THEN S( "Flags" ) END;
  1611. (* left brace already consumed *)
  1612. list := NIL;
  1613. IF Peek(Scanner.RightBrace) THEN (* empty flags *)
  1614. ELSE
  1615. REPEAT
  1616. position := symbol.position;
  1617. identifier := Identifier(position);
  1618. IF Optional(Scanner.LeftParenthesis) THEN
  1619. expression := Expression();
  1620. Check(Scanner.RightParenthesis)
  1621. ELSIF Optional(Scanner.Equal) THEN
  1622. expression := Expression();
  1623. ELSE
  1624. expression := NIL
  1625. END;
  1626. modifier := SyntaxTree.NewModifier(position,identifier,expression);
  1627. AppendModifier(list,modifier);
  1628. UNTIL ~Optional( Scanner.Comma ) & ~Optional(Scanner.Semicolon);
  1629. END;
  1630. Check(Scanner.RightBrace);
  1631. IF Trace THEN E( "Flags" ) END;
  1632. RETURN list;
  1633. END Flags;
  1634. PROCEDURE SetNextInComment(c: SyntaxTree.Comment; this: ANY);
  1635. BEGIN
  1636. WHILE c # NIL DO
  1637. c.SetItem(this,FALSE);
  1638. c := c.nextComment
  1639. END;
  1640. END SetNextInComment;
  1641. PROCEDURE CommentSymbol(symbol: SyntaxTree.Symbol);
  1642. BEGIN
  1643. IF (recentComment # NIL) (* & (recentComment.nextSymbol = NIL) *) THEN
  1644. symbol.SetComment(recentComment);
  1645. SetNextInComment(recentComment, symbol);
  1646. recentComment := NIL
  1647. END;
  1648. recentLine := scanner.position.line;
  1649. recentCommentItem := symbol;
  1650. END CommentSymbol;
  1651. PROCEDURE CommentStatement(symbol: SyntaxTree.Statement);
  1652. BEGIN
  1653. IF (recentComment # NIL) (* & (recentComment.nextSymbol = NIL) *) THEN
  1654. symbol.SetComment(recentComment);
  1655. SetNextInComment(recentComment, symbol);
  1656. recentComment := NIL
  1657. END;
  1658. recentLine := scanner.position.line;
  1659. recentCommentItem := symbol
  1660. END CommentStatement;
  1661. PROCEDURE CommentCasePart(symbol: SyntaxTree.CasePart);
  1662. BEGIN
  1663. IF (recentComment # NIL) (* & (recentComment.nextSymbol = NIL) *) THEN
  1664. symbol.SetComment(recentComment);
  1665. SetNextInComment(recentComment, symbol);
  1666. recentComment := NIL
  1667. END;
  1668. recentLine := scanner.position.line;
  1669. recentCommentItem := symbol
  1670. END CommentCasePart;
  1671. PROCEDURE CommentIfPart(symbol: SyntaxTree.IfPart);
  1672. BEGIN
  1673. IF (recentComment # NIL) (* & (recentComment.nextSymbol = NIL) *) THEN
  1674. symbol.SetComment(recentComment);
  1675. SetNextInComment(recentComment, symbol);
  1676. recentComment := NIL
  1677. END;
  1678. recentLine := scanner.position.line;
  1679. recentCommentItem := symbol
  1680. END CommentIfPart;
  1681. PROCEDURE CommentWithPart(symbol: SyntaxTree.WithPart);
  1682. BEGIN
  1683. IF (recentComment # NIL) (* & (recentComment.nextSymbol = NIL) *) THEN
  1684. symbol.SetComment(recentComment);
  1685. SetNextInComment(recentComment, symbol);
  1686. recentComment := NIL
  1687. END;
  1688. recentLine := scanner.position.line;
  1689. recentCommentItem := symbol
  1690. END CommentWithPart;
  1691. (** ProcedureDeclaration = 'procedure' ['^'|'&'|'~'|'-'|Flags ['-']] IdentifierDefinition [FormalParameters]';'
  1692. DeclarationSequence [Body] 'end' Identifier.
  1693. Forward declarations ignored.
  1694. **)
  1695. PROCEDURE ProcedureDeclaration( parentScope: SyntaxTree.Scope);
  1696. VAR name: SyntaxTree.Identifier;
  1697. procedure: SyntaxTree.Procedure;
  1698. procedureType: SyntaxTree.ProcedureType;
  1699. procedureScope : SyntaxTree.ProcedureScope;
  1700. access: SET;
  1701. position: Position;
  1702. isConstructor: BOOLEAN;
  1703. isFinalizer: BOOLEAN;
  1704. isInline: BOOLEAN;
  1705. modifiers: SyntaxTree.Modifier;
  1706. forwardDeclaration: BOOLEAN;
  1707. string: Scanner.StringType;
  1708. parameter: SyntaxTree.Parameter;
  1709. qualifiedIdentifier : SyntaxTree.QualifiedIdentifier;
  1710. identifier: SyntaxTree.Identifier;
  1711. kind: LONGINT;
  1712. BEGIN
  1713. IF Trace THEN S( "Procedure" ) END;
  1714. (* symbol procedure has already been consumed *)
  1715. modifiers := NIL;
  1716. isConstructor := FALSE; isFinalizer := FALSE; isInline := FALSE;
  1717. procedureType := SyntaxTree.NewProcedureType(symbol.position, parentScope);
  1718. position := symbol.position;
  1719. IF Optional( Scanner.Arrow) THEN (* ignore forward declarations *)
  1720. forwardDeclaration := TRUE;
  1721. ELSE forwardDeclaration := FALSE;
  1722. END;
  1723. IF Optional( Scanner.And ) THEN (* constructor *)
  1724. isConstructor := TRUE
  1725. ELSIF Optional( Scanner.Not ) THEN (* finalizer *)
  1726. isFinalizer := TRUE
  1727. ELSIF Optional( Scanner.Minus ) THEN (* inline *)
  1728. isInline := TRUE;
  1729. ELSIF Optional( Scanner.LeftBrace) THEN
  1730. modifiers := Flags();
  1731. IF Optional( Scanner.Minus ) THEN (* inline *)
  1732. isInline := TRUE
  1733. END;
  1734. END;
  1735. IF Peek(Scanner.String) OR Peek(Scanner.Character) THEN (* for compatibility with release *)
  1736. Error (position, Diagnostics.Invalid, "Invalid operator declaration: replace 'procedure' by 'operator' keyword!");
  1737. OperatorDeclaration( parentScope );
  1738. RETURN
  1739. END;
  1740. procedureScope := SyntaxTree.NewProcedureScope(parentScope);
  1741. IF Optional(Scanner.LeftParenthesis) THEN (* type bound *)
  1742. IF Optional(Scanner.Var) THEN
  1743. kind := SyntaxTree.VarParameter
  1744. ELSIF Optional(Scanner.Const) THEN
  1745. kind := SyntaxTree.ConstParameter;
  1746. ELSE
  1747. kind := SyntaxTree.ConstParameter;
  1748. END;
  1749. identifier := Identifier(position);
  1750. parameter := SyntaxTree.NewParameter(position, procedureType, identifier, kind);
  1751. Check(Scanner.Colon);
  1752. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(position, SyntaxTree.invalidIdentifier, Identifier(position));
  1753. parameter.SetType(SyntaxTree.NewQualifiedType(position, procedureScope, qualifiedIdentifier));
  1754. Check(Scanner.RightParenthesis);
  1755. procedureType.SetSelfParameter(parameter);
  1756. parameter.SetSelfParameter(TRUE);
  1757. END;
  1758. position:= symbol.position;
  1759. IdentifierDefinition( name, access,TRUE);
  1760. procedure := SyntaxTree.NewProcedure( position, name, procedureScope);
  1761. procedure.SetConstructor(isConstructor);
  1762. procedure.SetFinalizer(isFinalizer);
  1763. procedure.SetInline(isInline);
  1764. CommentSymbol(procedure);
  1765. procedure.SetAccess(access);
  1766. procedureType.SetModifiers(modifiers);
  1767. procedure.SetType(procedureType);
  1768. IF Optional(Scanner.Extern) & MandatoryString(string) THEN procedure.SetExternalName(string); END;
  1769. IF Optional(Scanner.LeftParenthesis) THEN FormalParameters( procedureType, procedureScope) END;
  1770. IF (procedure.externalName = NIL) & ~forwardDeclaration THEN
  1771. IF Lax THEN Ignore(Scanner.Semicolon) ELSE Check( Scanner.Semicolon ) END;
  1772. DeclarationSequence( procedureScope);
  1773. IF Peek(Scanner.Begin) OR Peek(Scanner.Code) THEN
  1774. procedureScope.SetBody(Body(procedureScope));
  1775. END;
  1776. Check(Scanner.End);
  1777. IF ExpectThisIdentifier( name ) THEN END;
  1778. END;
  1779. parentScope.AddProcedureDeclaration( procedure );
  1780. IF Trace THEN E( "Procedure") END;
  1781. END ProcedureDeclaration;
  1782. (** OperatorDeclaration = 'operator' [Flags] ['-'] String ['*'|'-'] FormalParameters ';'
  1783. DeclarationSequence [Body] 'end' String.
  1784. **)
  1785. PROCEDURE OperatorDeclaration(parentScope: SyntaxTree.Scope );
  1786. VAR
  1787. string: Scanner.StringType;
  1788. procedureScope: SyntaxTree.ProcedureScope;
  1789. procedureType: SyntaxTree.ProcedureType;
  1790. operator: SyntaxTree.Operator;
  1791. access: SET;
  1792. i: LONGINT; ch: CHAR; position: Position;
  1793. modifiers: SyntaxTree.Modifier; (* nopov *)
  1794. isInline, forward: BOOLEAN;
  1795. BEGIN
  1796. IF Trace THEN S( "Operator" ) END;
  1797. (* symbol operator already consumed *)
  1798. position := symbol.position;
  1799. forward := Optional(Scanner.Arrow);
  1800. isInline := FALSE;
  1801. IF Optional( Scanner.LeftBrace) THEN
  1802. modifiers := Flags();
  1803. END;
  1804. IF Optional( Scanner.Minus ) THEN (* inline *)
  1805. isInline := TRUE;
  1806. END;
  1807. IF MandatoryString( string ) THEN
  1808. (* copy string to name and check for length. LEN(name)>0, LEN(string)>0 can be presumed *)
  1809. i := 0; WHILE (string^[i] # 0X) DO INC(i) END;
  1810. IF i >= Scanner.MaxIdentifierLength THEN (* string too long to act as operator identifier *)
  1811. Error(symbol.position,Basic.StringTooLong,"");
  1812. END
  1813. END;
  1814. IF Optional( Scanner.Times ) THEN access := SyntaxTree.ReadOnly;
  1815. ELSIF Optional( Scanner.Minus ) THEN access := SyntaxTree.ReadOnly;
  1816. ELSE access := SyntaxTree.Internal;
  1817. END;
  1818. procedureScope := SyntaxTree.NewProcedureScope(parentScope);
  1819. operator := SyntaxTree.NewOperator( symbol.position, SyntaxTree.NewIdentifier(string^), procedureScope);
  1820. CommentSymbol(operator);
  1821. operator.SetAccess(access);
  1822. procedureType := SyntaxTree.NewProcedureType(symbol.position,parentScope);
  1823. IF Mandatory(Scanner.LeftParenthesis) THEN FormalParameters( procedureType, procedureScope ) END;
  1824. procedureType.SetModifiers(modifiers); (* nopov *)
  1825. operator.SetType( procedureType );
  1826. operator.SetInline(isInline);
  1827. IF Lax THEN Ignore(Scanner.Semicolon) ELSE Check( Scanner.Semicolon ) END;
  1828. IF ~forward THEN
  1829. DeclarationSequence( procedureScope );
  1830. IF Peek(Scanner.Begin) OR Peek(Scanner.Code) THEN
  1831. procedureScope.SetBody(Body(procedureScope));
  1832. END;
  1833. IF Mandatory(Scanner.End) & ExpectThisString(string^) THEN END;
  1834. END;
  1835. parentScope.AddProcedureDeclaration(operator);
  1836. IF parentScope IS SyntaxTree.ModuleScope THEN
  1837. parentScope(SyntaxTree.ModuleScope).AddOperator(operator);
  1838. ELSIF parentScope IS SyntaxTree.RecordScope THEN
  1839. parentScope(SyntaxTree.RecordScope).AddOperator(operator);
  1840. ELSE
  1841. Error(position,Diagnostics.Invalid,"Operators only allowed in module or record scope"); (* nopov *)
  1842. END;
  1843. IF Trace THEN EE( "Operator", string^ ) END;
  1844. END OperatorDeclaration;
  1845. (** VariableNameList = IdentifierDefinition [Flags] [':=' Expression | 'extern' String] {',' IdentifierDefinition [Flags] [':=' Expression | 'extern' String] }.**)
  1846. PROCEDURE VariableNameList( scope: SyntaxTree.Scope );
  1847. VAR varname: SyntaxTree.Identifier; position: Position; variable: SyntaxTree.Variable; flags,access: SET; string: Scanner.StringType;
  1848. BEGIN
  1849. IF Trace THEN S( "VariableNameList" ) END;
  1850. REPEAT
  1851. flags := {};
  1852. position := symbol.position;
  1853. IdentifierDefinition( varname, access,TRUE);
  1854. variable := SyntaxTree.NewVariable( position, varname );
  1855. CommentSymbol(variable);
  1856. IF Optional(Scanner.LeftBrace) THEN variable.SetModifiers(Flags()) END;
  1857. IF Optional(Scanner.Becomes) THEN variable.SetInitializer (Expression());
  1858. ELSIF Optional(Scanner.Extern) & MandatoryString(string) THEN variable.SetExternalName(string); END;
  1859. variable.SetAccess(access);
  1860. scope.AddVariable(variable);
  1861. UNTIL ~Optional( Scanner.Comma );
  1862. IF Trace THEN E( "VariableNameList" ) END;
  1863. END VariableNameList;
  1864. (** VariableDeclaration = VariableNameList ':' Type. **)
  1865. PROCEDURE VariableDeclaration(parentScope: SyntaxTree.Scope );
  1866. VAR
  1867. variable, firstVariable: SyntaxTree.Variable; type: SyntaxTree.Type;
  1868. BEGIN
  1869. IF Trace THEN S( "VariableDeclaration" ) END;
  1870. firstVariable := parentScope.lastVariable;
  1871. VariableNameList( parentScope );
  1872. Check( Scanner.Colon );
  1873. type := Type( SyntaxTree.invalidIdentifier, parentScope );
  1874. variable := firstVariable;
  1875. IF firstVariable # NIL THEN variable := firstVariable.nextVariable ELSE variable := parentScope.firstVariable END;
  1876. WHILE variable # NIL DO
  1877. variable.SetType( type );
  1878. variable := variable.nextVariable;
  1879. END;
  1880. IF Trace THEN E( "VariableDeclaration" ) END;
  1881. END VariableDeclaration;
  1882. (** TypeDeclaration = IdentifierDefinition '=' Type.**)
  1883. PROCEDURE TypeDeclaration(parentScope: SyntaxTree.Scope);
  1884. VAR name: SyntaxTree.Identifier; position: Position; type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration; access: SET;
  1885. BEGIN
  1886. IF Trace THEN S( "TypeDeclaration" ) END;
  1887. position := symbol.position;
  1888. IdentifierDefinition( name, access,FALSE);
  1889. typeDeclaration := SyntaxTree.NewTypeDeclaration( position,name);
  1890. CommentSymbol(typeDeclaration);
  1891. Check( Scanner.Equal );
  1892. type := Type( name , parentScope);
  1893. type.SetTypeDeclaration(typeDeclaration);
  1894. typeDeclaration.SetDeclaredType(type);
  1895. (*
  1896. type.SetName(typeDeclaration.name); (* don't do that: overwrites global names ! *)
  1897. *)
  1898. typeDeclaration.SetAccess(access);
  1899. parentScope.AddTypeDeclaration( typeDeclaration );
  1900. IF Trace THEN E( "TypeDeclaration" ) END;
  1901. END TypeDeclaration;
  1902. (** ConstDeclaration = IdentifierDefinition '=' Expression. **)
  1903. PROCEDURE ConstDeclaration(parentScope: SyntaxTree.Scope );
  1904. VAR name: SyntaxTree.Identifier; position: Position; constant: SyntaxTree.Constant; expression: SyntaxTree.Expression; access: SET;
  1905. BEGIN
  1906. IF Trace THEN S( "ConstDeclaration" ) END;
  1907. IdentifierDefinition( name, access, FALSE);
  1908. position := symbol.position;
  1909. constant := SyntaxTree.NewConstant( position, name );
  1910. CommentSymbol(constant);
  1911. constant.SetAccess(access);
  1912. Check( Scanner.Equal );
  1913. expression := Expression();
  1914. constant.SetValue( expression );
  1915. parentScope.AddConstant( constant );
  1916. IF Trace THEN E( "ConstDeclaration" ) END;
  1917. END ConstDeclaration;
  1918. (** DeclarationSequence = { 'const' [ConstDeclaration] {';' [ConstDeclaration]}
  1919. |'type' [TypeDeclaration] {';' [TypeDeclaration]}
  1920. |'var' [VariableDeclaration] {';' [VariableDeclaration]}
  1921. | ProcedureDeclaration
  1922. | OperatorDeclaration
  1923. | ';'
  1924. }
  1925. **)
  1926. PROCEDURE DeclarationSequence( parentScope: SyntaxTree.Scope);
  1927. VAR previousScope: SyntaxTree.Scope;
  1928. BEGIN
  1929. previousScope := currentScope;
  1930. currentScope := parentScope;
  1931. IF Trace THEN S( "DeclarationSequence" ) END;
  1932. IF Lax THEN
  1933. LOOP
  1934. Ignore(Scanner.Semicolon);
  1935. IF Optional(Scanner.Const) THEN
  1936. WHILE Peek(Scanner.Identifier) DO ConstDeclaration(parentScope); Ignore(Scanner.Semicolon) END;
  1937. ELSIF Optional(Scanner.Type) THEN
  1938. WHILE Peek(Scanner.Identifier) DO TypeDeclaration(parentScope); Ignore(Scanner.Semicolon) END;
  1939. ELSIF Optional(Scanner.Var) THEN
  1940. WHILE Peek(Scanner.Identifier) DO VariableDeclaration(parentScope); Ignore(Scanner.Semicolon); END;
  1941. ELSIF Optional(Scanner.Procedure) THEN
  1942. ProcedureDeclaration(parentScope); Ignore(Scanner.Semicolon)
  1943. ELSIF Optional(Scanner.Operator) THEN
  1944. OperatorDeclaration(parentScope); Ignore(Scanner.Semicolon);
  1945. ELSE
  1946. EXIT
  1947. END;
  1948. END;
  1949. ELSE
  1950. LOOP
  1951. IF Optional( Scanner.Const ) THEN
  1952. REPEAT
  1953. IF Peek(Scanner.Identifier) THEN ConstDeclaration( parentScope ) END
  1954. UNTIL ~Optional( Scanner.Semicolon )
  1955. ELSIF Optional( Scanner.Type ) THEN
  1956. REPEAT
  1957. IF Peek(Scanner.Identifier) THEN TypeDeclaration( parentScope) END
  1958. UNTIL ~Optional( Scanner.Semicolon )
  1959. ELSIF Optional( Scanner.Var ) THEN
  1960. REPEAT
  1961. IF Peek(Scanner.Identifier) THEN VariableDeclaration( parentScope ) END
  1962. UNTIL ~Optional( Scanner.Semicolon )
  1963. ELSIF Optional(Scanner.Operator) THEN
  1964. OperatorDeclaration( parentScope);
  1965. ELSIF Optional( Scanner.Procedure ) THEN
  1966. ProcedureDeclaration( parentScope );
  1967. ELSE EXIT
  1968. END;
  1969. Ignore(Scanner.Semicolon)
  1970. END;
  1971. END;
  1972. currentScope := previousScope;
  1973. IF Trace THEN E( "DeclarationSequence" ) END;
  1974. END DeclarationSequence;
  1975. (**
  1976. ImportList = 'import' Import { ',' Import } ';'.
  1977. Import = Identifier [':=' Identifier] ['in' Identifier].
  1978. **)
  1979. PROCEDURE ImportList( scope: SyntaxTree.Scope );
  1980. VAR alias, name, context: SyntaxTree.Identifier; import: SyntaxTree.Import; position, idPosition: Position;
  1981. BEGIN
  1982. IF Trace THEN S( "ImportList" ) END;
  1983. (* import symbol already consumed *)
  1984. REPEAT
  1985. alias := Identifier(idPosition);
  1986. position := symbol.position;
  1987. IF alias # SyntaxTree.invalidIdentifier THEN
  1988. IF Optional( Scanner.Becomes ) THEN name := Identifier(idPosition) ELSE name := alias; END;
  1989. import := SyntaxTree.NewImport( position, alias, name, TRUE );
  1990. CommentSymbol(import);
  1991. IF Optional(Scanner.In) THEN
  1992. position := symbol.position;
  1993. context := Identifier(idPosition);
  1994. IF context # SyntaxTree.invalidIdentifier THEN import.SetContext(context) END;
  1995. END;
  1996. WITH scope: SyntaxTree.ModuleScope DO
  1997. scope.AddImport( import );
  1998. | scope: SyntaxTree.CellScope DO
  1999. scope.AddImport( import );
  2000. END;
  2001. END;
  2002. UNTIL ~Optional( Scanner.Comma );
  2003. IF Lax THEN Ignore(Scanner.Semicolon) ELSE Check( Scanner.Semicolon ) END;
  2004. IF Trace THEN E( "ImportList" ); END;
  2005. END ImportList;
  2006. (** Module = ('module' | 'cellnet' [Flags]) Identifier ['in' Identifier]';'
  2007. [ImportList] DeclarationSequence [Body]
  2008. 'end' Identifier '.'.
  2009. **)
  2010. PROCEDURE Module*(): SyntaxTree.Module;
  2011. VAR moduleName, context: SyntaxTree.Identifier; module: SyntaxTree.Module; position: Position; isCellNet: BOOLEAN;
  2012. scannerDiagnostics: Diagnostics.Diagnostics; modifiers: SyntaxTree.Modifier; c: SyntaxTree.Comment;
  2013. BEGIN
  2014. IF Trace THEN S( "Module" ) END;
  2015. position := symbol.position;
  2016. moduleScope := SyntaxTree.NewModuleScope(); (* needed to feed in comment already before module starts *)
  2017. currentScope := moduleScope;
  2018. isCellNet := Optional(Scanner.CellNet);
  2019. IF isCellNet OR Mandatory( Scanner.Module ) THEN
  2020. (*c := recentComment; recentComment := NIL;*)
  2021. IF isCellNet & Optional(Scanner.LeftBrace) THEN modifiers := Flags() ELSE modifiers := NIL END;
  2022. moduleName := Identifier(position);
  2023. module := SyntaxTree.NewModule( scanner.source^, position, moduleName, moduleScope, scanner.case );
  2024. CommentSymbol(module);
  2025. (*
  2026. module.SetComment(c);
  2027. SetNextInComment(c, module);
  2028. *)
  2029. IF isCellNet THEN module.SetCellNet(TRUE); module.SetModifiers(modifiers); END;
  2030. module.SetType(SyntaxTree.moduleType);
  2031. IF Optional(Scanner.In) THEN
  2032. position := symbol.position;
  2033. context := Identifier(position);
  2034. module.SetContext(context);
  2035. END;
  2036. IF Lax THEN Ignore(Scanner.Semicolon) ELSE Check(Scanner.Semicolon) END;
  2037. IF ~Peek(Scanner.EndOfText) THEN
  2038. module.SetClosingComment(recentComment);
  2039. SetNextInComment(recentComment, module);
  2040. recentComment := NIL;
  2041. END;
  2042. IF Optional(Scanner.Import) THEN ImportList(moduleScope) END;
  2043. DeclarationSequence( moduleScope);
  2044. IF Peek(Scanner.Begin) OR Peek(Scanner.Code) THEN
  2045. moduleScope.SetBodyProcedure(BodyProcedure(moduleScope)); (* insert empty body procedure if necessary *)
  2046. END;
  2047. Check(Scanner.End);
  2048. IF ExpectThisIdentifier( moduleName ) THEN
  2049. IF Token() # Scanner.Period THEN
  2050. Error( symbol.position, Scanner.Period, "" )
  2051. ELSIF ~error & ~scanner.error THEN (* read ahead to read comments and to check for next module *)
  2052. scanner.ResetCase;
  2053. scannerDiagnostics := NIL;
  2054. scanner.ResetErrorDiagnostics(scannerDiagnostics);
  2055. NextSymbol;
  2056. scanner.ResetErrorDiagnostics(scannerDiagnostics);
  2057. END;
  2058. (*
  2059. (* do not use Check for not reading after end of module *)
  2060. IF ~Peek(Scanner.Module) & ~Peek(Scanner.CellNet) THEN
  2061. SetNextInComment(recentComment,module);
  2062. module.SetClosingComment(recentComment);
  2063. recentComment := NIL;
  2064. END;
  2065. *)
  2066. END;
  2067. END;
  2068. IF Trace THEN E( "Module" ) END;
  2069. RETURN module
  2070. END Module;
  2071. (** check if another module declaration is available after recent module parsing -> for parsing and compiling multiple modules within a single file **)
  2072. PROCEDURE NextModule*(): BOOLEAN;
  2073. BEGIN
  2074. RETURN Peek(Scanner.Module) OR Peek(Scanner.CellNet);
  2075. END NextModule;
  2076. END Parser;
  2077. (* utilities *)
  2078. PROCEDURE AppendModifier(VAR list: SyntaxTree.Modifier; modifier: SyntaxTree.Modifier);
  2079. VAR this, next: SyntaxTree.Modifier;
  2080. BEGIN
  2081. IF list = NIL THEN list := modifier
  2082. ELSE
  2083. this := list;
  2084. next := list.nextModifier;
  2085. WHILE next # NIL DO
  2086. this := next;
  2087. next := this.nextModifier;
  2088. END;
  2089. this.SetNext(modifier);
  2090. END;
  2091. END AppendModifier;
  2092. (** parser retrieval **)
  2093. PROCEDURE NewParser*( scanner: Scanner.Scanner; diagnostics: Diagnostics.Diagnostics): Parser;
  2094. VAR parser: Parser;
  2095. BEGIN
  2096. NEW( parser, scanner, diagnostics ); RETURN parser;
  2097. END NewParser;
  2098. END FoxParser.