Bladeren bron

Added end position for statements, types and expressions.

    position.end             end.end
    v                        v
 FOR i := 0 TO 100 DO ... END; 
^                         ^
position.start            end.start



git-svn-id: https://svn.inf.ethz.ch/svn/lecturers/a2/trunk@7642 8c9fc860-2736-0410-a75d-ab315db34111
felixf 7 jaren geleden
bovenliggende
commit
5045e02088
3 gewijzigde bestanden met toevoegingen van 86 en 38 verwijderingen
  1. 36 24
      source/FoxParser.Mod
  2. 5 0
      source/FoxPrintout.Mod
  3. 45 14
      source/FoxSyntaxTree.Mod

+ 36 - 24
source/FoxParser.Mod

@@ -381,6 +381,7 @@ TYPE
 		BEGIN
 			position := symbol.position;
 			IF MandatoryIdentifier(name) THEN
+				position := symbol.position;
 				identifier := name;
 			ELSE
 				identifier := SyntaxTree.invalidIdentifier;
@@ -678,7 +679,7 @@ TYPE
 				identifier := Identifier(position);
 				designator := SyntaxTree.NewIdentifierDesignator(position,identifier);
 			END;
-
+			designator.End(symbol.position); 
 			LOOP
 				position := symbol.position;
 				IF OptionalB( Scanner.LeftParenthesis ) THEN
@@ -709,11 +710,13 @@ TYPE
 					designator:= SyntaxTree.NewArrowDesignator( position,designator );
 				ELSE EXIT
 				END;
+				designator.End(symbol.position); 
 			END;
 
 			IF OptionalB(Scanner.LeftBrace) THEN
 				designator.SetModifiers(Flags());
 			END;
+			designator.End(symbol.position); 
 			
 			(*IF OptionalB(Scanner.Escape) THEN END; (* skip breaking signal *)*)
 
@@ -736,7 +739,7 @@ TYPE
 				UNTIL ~Optional(Scanner.Comma);
 				Check(Scanner.RightBrace);
 			END;
-			set.End(symbol.position.end);
+			set.End(symbol.position);
 
 			IF Trace THEN E( "Set" ) END;
 			RETURN set
@@ -754,6 +757,7 @@ TYPE
 				UNTIL ~Optional(Scanner.Comma);
 				Check(Scanner.RightBracket);
 			END;
+			array.End(symbol.position);
 			RETURN array
 		END MathArray;
 
@@ -790,7 +794,7 @@ TYPE
 						ELSE
 							factor(SyntaxTree.StringValue).Append(symbol.string);
 						END;
-						factor.End(symbol.position.end);
+						factor.End(symbol.position);
 						NextSymbol;
 					END;
 			| Scanner.Nil:
@@ -812,12 +816,12 @@ TYPE
 					NextSymbol;
 					factor := Expression();
 					Check( Scanner.RightParenthesis );
-					factor.End( symbol.position.end );
+					factor.End( symbol.position);
 			| Scanner.Not:
 					NextSymbol;
 					factor := Factor();
 					factor := SyntaxTree.NewUnaryExpression( position, factor, Scanner.Not );
-					factor.End( symbol.position.end );
+					factor.End( symbol.position);
 			| Scanner.Address, Scanner.Size:
 					operator := Token();
 					factor := Designator(); (* ADDRESS AND SIZE can be type identifiers used for type conversion *)
@@ -825,7 +829,7 @@ TYPE
 						factor := Designator();
 						factor := SyntaxTree.NewUnaryExpression( position, factor, operator );
 					END;
-					factor.End (symbol.position.end)
+					factor.End (symbol.position)
 			| Scanner.Alias:
 					operator := Token();
 					NextSymbol();
@@ -833,14 +837,14 @@ TYPE
 						factor := Factor();
 						factor := SyntaxTree.NewUnaryExpression( position, factor, operator );
 					END;
-					factor.End (symbol.position.end)
+					factor.End (symbol.position)
 			| Scanner.Self, Scanner.Result, Scanner.Identifier, Scanner.New:
 					factor := Designator();
-					factor.End( symbol.position.end );
+					factor.End( symbol.position);
 			| Scanner.LeftBracket:
 					NextSymbol;
 					factor := MathArray();
-					factor.End(symbol.position.end);
+					factor.End(symbol.position);
 			ELSE
 				Error( position, Basic.ValueStartIncorrectSymbol, "" );
 				NextSymbol;  factor := SyntaxTree.invalidExpression;
@@ -875,7 +879,7 @@ TYPE
 				factor := Factor();
 				term := SyntaxTree.NewBinaryExpression( position, term, factor, operator );
 			END;
-			term.End( symbol.position.end );
+			term.End( symbol.position);
 			IF Trace THEN E( "Term" ) END;
 			RETURN term
 		END Term;
@@ -916,13 +920,14 @@ TYPE
 		VAR expression, rightExpression: SyntaxTree.Expression;  operator: LONGINT;  position: Position;
 		BEGIN
 			IF Trace THEN S( "Expression" ) END;
-			position := symbol.position;
 			expression := RangeExpression();
+			position := expression.position;
 			IF (TokenB() >= Scanner.Equal) & (TokenB() <= Scanner.Is) THEN
 				operator := Token();
 				NextSymbol;
 				rightExpression := RangeExpression();
 				expression := SyntaxTree.NewBinaryExpression(position, expression, rightExpression, operator );
+				expression.End(symbol.position); 
 			END;
 			(*IF OptionalB(Scanner.Escape) THEN END; (* skip breaking escape *)*)
 			IF Trace THEN E( "Expression" ) END;
@@ -938,6 +943,7 @@ TYPE
 		BEGIN
 			IF Trace THEN S( "Case" ) END;
 			casePart := SyntaxTree.NewCasePart();
+			casePart.SetPosition(symbol.position);
 			CommentCasePart(casePart);
 			REPEAT
 				element := RangeExpression();
@@ -947,6 +953,7 @@ TYPE
 			statements := StatementSequence(caseStatement);
 			casePart.SetStatements( statements );
 			caseStatement.AddCasePart( casePart );
+			casePart.SetEnd(symbol.position);
 			IF Trace THEN E( "Case" ) END;
 		END Case;
 
@@ -1004,7 +1011,7 @@ TYPE
 						statement := caller;
 						CommentStatement(statement);
 					END;
-					statement.SetEndPosition(prevPosition.end);
+					statement.End(prevPosition);
 					statements.AddStatement( statement );
 					(*IF OptionalB(Scanner.Escape) THEN END;*)
 					result := TRUE
@@ -1017,8 +1024,10 @@ TYPE
 					Check( Scanner.Then );
 					statementSequence := StatementSequence(ifStatement);
 					ifStatement.ifPart.SetStatements( statementSequence );
+					ifStatement.ifPart.SetEnd(symbol.position); 
 					WHILE Optional( Scanner.Elsif ) DO
 						elsifPart := SyntaxTree.NewIfPart();
+						elsifPart.SetPosition(symbol.position);
 						CommentIfPart(elsifPart);
 						ifStatement.AddElsifPart( elsifPart);
 						expression := Expression();
@@ -1026,12 +1035,13 @@ TYPE
 						Check( Scanner.Then );
 						statementSequence := StatementSequence(ifStatement);
 						elsifPart.SetStatements( statementSequence );
+						elsifPart.SetEnd(symbol.position);
 					END;
 					IF Optional( Scanner.Else ) THEN
 						statementSequence := StatementSequence(ifStatement);
 						ifStatement.SetElsePart( statementSequence );
 					END;
-					ifStatement.SetEndPosition(symbol.position.end);
+					ifStatement.End(symbol.position);
 					Check( Scanner.End );  statements.AddStatement( ifStatement );
 					result := TRUE
 			| Scanner.With:
@@ -1044,6 +1054,7 @@ TYPE
 							Error(position,Diagnostics.Invalid,"forbidden qualified identifier in with statement");
 						END;
 						withPart := SyntaxTree.NewWithPart();
+						withPart.SetPosition(symbol.position);
 						CommentWithPart(withPart);
 						withStatement.AddWithPart(withPart);
 						designator := SyntaxTree.NewIdentifierDesignator(position,identifier);
@@ -1055,13 +1066,14 @@ TYPE
 						Check( Scanner.Do );
 						statementSequence := StatementSequence(withStatement);
 						withPart.SetStatements( statementSequence );
+						withPart.SetEnd(symbol.position);
 					UNTIL ~Optional(Scanner.Bar) OR ~CascadedWithSupport;
 					IF CascadedWithSupport & Optional(Scanner.Else) THEN
 						statementSequence := StatementSequence(withStatement);
 						withStatement.SetElsePart(statementSequence);
 					END;
 					Check( Scanner.End );
-					withStatement.SetEndPosition(symbol.position.end);
+					withStatement.End(symbol.position);
 					statements.AddStatement( withStatement );
 					result := TRUE
 			| Scanner.Case:
@@ -1080,7 +1092,7 @@ TYPE
 						caseStatement.SetElsePart( statementSequence );
 					END;
 					Check( Scanner.End );
-					caseStatement.SetEndPosition(symbol.position.end);
+					caseStatement.End(symbol.position);
 					statements.AddStatement( caseStatement );
 					result := TRUE
 			| Scanner.While:
@@ -1093,7 +1105,7 @@ TYPE
 					statementSequence := StatementSequence(whileStatement);
 					whileStatement.SetStatements( statementSequence );
 					Check( Scanner.End );
-					whileStatement.SetEndPosition(symbol.position.end);					
+					whileStatement.End(symbol.position);					
 					statements.AddStatement( whileStatement );
 					result := TRUE
 			| Scanner.Repeat:
@@ -1104,7 +1116,7 @@ TYPE
 					repeatStatement.SetStatements( statementSequence );
 					Check( Scanner.Until );
 					expression := Expression();
-					repeatStatement.SetEndPosition(prevPosition.end);
+					repeatStatement.End(prevPosition);
 					repeatStatement.SetCondition( expression );
 					statements.AddStatement( repeatStatement );
 					result := TRUE
@@ -1132,7 +1144,7 @@ TYPE
 					statementSequence := StatementSequence(forStatement);
 					forStatement.SetStatements( statementSequence );
 					Check( Scanner.End );
-					forStatement.SetEndPosition(symbol.position.end);
+					forStatement.End(symbol.position);
 					statements.AddStatement( forStatement );
 					result := TRUE
 			| Scanner.Loop:
@@ -1142,7 +1154,7 @@ TYPE
 					statementSequence := StatementSequence(loopStatement);
 					loopStatement.SetStatements( statementSequence );
 					Check( Scanner.End );
-					loopStatement.SetEndPosition(symbol.position.end);
+					loopStatement.End(symbol.position);
 					statements.AddStatement( loopStatement );
 					result := TRUE;
 			| Scanner.Exit:
@@ -1159,12 +1171,12 @@ TYPE
 						expression := Expression();
 						returnStatement.SetReturnValue( expression );
 					END;
-					returnStatement.SetEndPosition(symbol.position.end);
+					returnStatement.End(symbol.position);
 					statements.AddStatement( returnStatement );
 					result := TRUE;
 			| Scanner.Begin:
 					NextSymbol;  statement := StatementBlock(outer);  statements.AddStatement( statement );  Check( Scanner.End );
-					statement.SetEndPosition(symbol.position.end);
+					statement.End(symbol.position);
 					result := TRUE;
 			| Scanner.Await:
 					awaitStatement := SyntaxTree.NewAwaitStatement( symbol.position, outer );
@@ -1173,13 +1185,13 @@ TYPE
 					expression := Expression();
 					awaitStatement.SetCondition( expression );
 					statements.AddStatement( awaitStatement );
-					awaitStatement.SetEndPosition(symbol.position.end);
+					awaitStatement.End(symbol.position);
 					result := TRUE
 			| Scanner.Code:
 				(* assemble *)
 				code := Code(outer);
 				Check(Scanner.End);
-				code.SetEndPosition(symbol.position.end);
+				code.End(symbol.position);
 				statements.AddStatement( code );
 				result := TRUE
 			| Scanner.End:  result := FALSE (* end of if, with, case, while, for, loop, or statement sequence *)
@@ -1296,7 +1308,7 @@ TYPE
 					body.SetFinally(StatementSequence(body));
 				END;
 			END;
-			body.SetEndPosition(symbol.position.start);  (* beginning of "end" is end of body *)
+			body.End(symbol.position);  (* beginning of "end" is end of body *)
 			IF Trace THEN E( "Body" ) END;
 			currentScope := previousScope;
 			RETURN body

+ 5 - 0
source/FoxPrintout.Mod

@@ -8,6 +8,7 @@ CONST
 	(* print modes *)
 	Exported*=0; SymbolFile*=1; SourceCode*=2;  All*=3;
 
+	DebugPosition=FALSE;
 TYPE
 
 	Printer*= OBJECT (SyntaxTree.Visitor)
@@ -468,6 +469,7 @@ TYPE
 
 		PROCEDURE Expression*(x: SyntaxTree.Expression);
 		BEGIN
+			IF DebugPosition THEN TRACE(x.position.start, x.position.end, x.end.start, x.end.end) END;
 			IF x = NIL THEN
 				AlertString("nil expression");
 			ELSE
@@ -1508,6 +1510,7 @@ TYPE
 				AlertString("nil statement")
 			ELSE
 				Comments(x.comment, x, FALSE);
+				IF DebugPosition THEN TRACE(x.position.start, x.position.end, x.end.start, x.end.end) END;
 				x.Accept(SELF);
 				Comments(x.comment,x,TRUE);
 			END
@@ -1551,6 +1554,7 @@ TYPE
 
 		PROCEDURE IfPart(x: SyntaxTree.IfPart);
 		BEGIN
+			IF DebugPosition THEN TRACE(x.position.start, x.position.end, x.end.start, x.end.end) END;
 			Comments(x.comment, x, FALSE);
 	 		Keyword("IF " );
 			Expression(x.condition);
@@ -1834,6 +1838,7 @@ TYPE
 		PROCEDURE Body(x: SyntaxTree.Body; implementation: BOOLEAN);
 		VAR
 		BEGIN
+			IF DebugPosition THEN TRACE(x.position.start, x.position.end, x.end.start, x.end.end) END;
 			IF x.code # NIL THEN
 				Indent; Keyword("CODE");
 				IF implementation THEN

+ 45 - 14
source/FoxSyntaxTree.Mod

@@ -418,7 +418,7 @@ TYPE
 			typeDeclaration-: TypeDeclaration; (* link to declaration (if any), needed for printing, debugging and symbol lookup *)
 			scope-: Scope; (* scope where the type has been declared *)
 			resolved-: Type; (* indirection to resolved type to preserve qualified types *)
-			position-: Position;
+			position-,end-: Position;
 			state-: SET;
 			hasPointers-: BOOLEAN;
 
@@ -433,6 +433,7 @@ TYPE
 		PROCEDURE & InitType*( position: Position);
 		BEGIN
 			SELF.position := position; state := Undefined;
+			end := invalidPosition;
 			typeDeclaration := NIL;
 			scope := NIL;
 			resolved := SELF;
@@ -444,6 +445,8 @@ TYPE
 			InitFingerPrint(fingerprint);
 		END InitType;
 
+		
+
 		PROCEDURE SetSize*(sizeInBits: LONGINT);
 		BEGIN SELF.sizeInBits := sizeInBits
 		END SetSize;
@@ -1909,14 +1912,14 @@ TYPE
 		VAR
 			type-: Type; (* the expression's type. Resolved by checker *)
 			assignable-: BOOLEAN;  (* expression can be assigned to (or used as var-parameter): expression := ... *)
-			position-: Position;
+			position-, end-: Position;
 			state-: SET;
 			resolved-: Value;
 			isHidden-: BOOLEAN;
 
 
-		PROCEDURE End*( position: LONGINT );
-		BEGIN SELF.position.end := position;
+		PROCEDURE End*( position: Position);
+		BEGIN SELF.end := position;
 		END End;
 
 		PROCEDURE SetState*(state: LONGINT);
@@ -1924,7 +1927,7 @@ TYPE
 		END SetState;
 
 		PROCEDURE &InitExpression(position: Position);
-		BEGIN	SELF.position := position; state := Undefined; type := NIL; assignable := FALSE; resolved := NIL; isHidden := FALSE;
+		BEGIN	SELF.position := position; end :=  invalidPosition; state := Undefined; type := NIL; assignable := FALSE; resolved := NIL; isHidden := FALSE;
 		END InitExpression;
 
 		PROCEDURE SetHidden*(hidden: BOOLEAN);
@@ -3070,7 +3073,7 @@ TYPE
 		fixed-: BOOLEAN;
 		alignment-: LONGINT;
 
-		position-: Position; state-: SET;
+		position-, end-: Position; state-: SET;
 		fingerprint-: FingerPrint;
 
 		comment-: Comment;
@@ -3079,6 +3082,7 @@ TYPE
 		PROCEDURE & InitSymbol(position: Position; name:Identifier);
 		BEGIN
 			SELF.position := position; state := Undefined;
+			SELF.end := invalidPosition;
 			nextSymbol := NIL;
 			SELF.name := name;
 			externalName := NIL;
@@ -3679,7 +3683,7 @@ TYPE
 
 	Statement*= OBJECT
 	VAR outer-: Statement;
-		position-: Position;
+		position-,end-: Position;
 		isUnreachable-: BOOLEAN;
 
 		comment-: Comment;
@@ -3687,6 +3691,7 @@ TYPE
 		PROCEDURE & InitStatement*(position: Position; outer: Statement);
 		BEGIN
 			SELF.position := position;
+			end := invalidPosition;
 			SELF.outer := outer;
 			isUnreachable := FALSE;
 			comment := NIL;
@@ -3714,10 +3719,10 @@ TYPE
 			HALT(200) (* abstract *)
 		END Clone;
 
-		PROCEDURE SetEndPosition*(pos: LONGINT);
+		PROCEDURE End*(pos: Position);
 		BEGIN
-			position.end := pos;
-		END SetEndPosition;
+			end := pos;
+		END End;
 		
 
 	END Statement;
@@ -3804,9 +3809,30 @@ TYPE
 
 	END CommunicationStatement;
 
+	Part*= OBJECT
+	VAR
+		position-, end-: Position;
+
+		PROCEDURE InitPart;
+		BEGIN
+			position := invalidPosition; end := invalidPosition;
+		END InitPart;
+		
+		PROCEDURE SetPosition*(pos: Position);
+		BEGIN
+			position := pos;
+		END SetPosition;
+
+		PROCEDURE SetEnd*(pos: Position);
+		BEGIN
+			end := pos;
+		END SetEnd;
+		
+	END Part;
+	
 
 	(** << ... condition THEN statements ... >> **)
-	IfPart*= OBJECT
+	IfPart*= OBJECT (Part)
 	VAR
 		condition-: Expression;
 	 	statements-: StatementSequence;
@@ -3815,6 +3841,7 @@ TYPE
 
 	 	PROCEDURE & InitIfPart;
 	 	BEGIN
+	 		InitPart;
 	 		statements := NIL; condition := NIL; comment := NIL;
 	 	END InitIfPart;
 
@@ -3849,7 +3876,9 @@ TYPE
 
 		PROCEDURE & InitIfStatement( position: Position ; outer: Statement);
 		BEGIN
-			InitStatement( position,outer ); ifPart := NewIfPart(); elsePart := NIL;  elsifParts := NIL;
+			InitStatement( position,outer ); ifPart := NewIfPart(); 
+			ifPart.SetPosition(position);
+			elsePart := NIL;  elsifParts := NIL;
 		END InitIfStatement;
 
 		PROCEDURE SetElsePart*( elsePart: StatementSequence );
@@ -3892,7 +3921,7 @@ TYPE
 
 	END IfStatement;
 
-	WithPart*= OBJECT
+	WithPart*= OBJECT (Part)
 	VAR
 		variable-: Designator;
 		type-: Type; (* initially is qualified type *)
@@ -3902,6 +3931,7 @@ TYPE
 
 		PROCEDURE &InitWithPart();
 		BEGIN
+			InitPart();
 			type := NIL; variable := NIL; statements := NIL; comment := NIL;
 		END InitWithPart;
 
@@ -3988,7 +4018,7 @@ TYPE
 	CaseConstant*= POINTER TO RECORD min*,max*: LONGINT; next*: CaseConstant END;
 
 	(** << elements : statements >> **)
-	CasePart* = OBJECT
+	CasePart* = OBJECT (Part)
 		VAR
 			elements-: ExpressionList; (* expression list inserted by the parser *)
 			firstConstant-: CaseConstant; (* expression list resolved to int32s, inserted by checker *)
@@ -3998,6 +4028,7 @@ TYPE
 
 		PROCEDURE & InitCasePart;
 		BEGIN
+			InitPart;
 			elements := NewExpressionList(); firstConstant := NIL;
 		END InitCasePart;