beginners common error warnings
authorClaus Gittinger <cg@exept.de>
Thu, 18 Jul 1996 10:13:34 +0200
changeset 310 27ed956b591e
parent 309 11b78163a914
child 311 1fcbd4311698
beginners common error warnings
Scanner.st
--- a/Scanner.st	Thu Jul 18 10:04:57 1996 +0200
+++ b/Scanner.st	Thu Jul 18 10:13:34 1996 +0200
@@ -15,10 +15,12 @@
 		tokenName tokenLineNr tokenRadix hereChar peekChar peekChar2
 		requestor exitBlock errorFlag ignoreErrors ignoreWarnings
 		saveComments currentComments warnSTXSpecialComment
-		warnUnderscoreInIdentifier warnOldStyleAssignment outStream
+		warnUnderscoreInIdentifier warnOldStyleAssignment warnCommonMistakes
+		outStream
 		outCol'
 	classVariableNames:'TypeArray ActionArray AllowUnderscoreInIdentifier Warnings
-		WarnSTXSpecials WarnOldStyleAssignment WarnUnderscoreInIdentifier'
+		WarnSTXSpecials WarnOldStyleAssignment WarnUnderscoreInIdentifier
+		WarnCommonMistakes'
 	poolDictionaries:''
 	category:'System-Compiler'
 !
@@ -58,6 +60,7 @@
     WarnSTXSpecials := true.
     WarnUnderscoreInIdentifier := true.
     WarnOldStyleAssignment := true.
+    WarnCommonMistakes := true.
     AllowUnderscoreInIdentifier := false.
 !
 
@@ -142,6 +145,25 @@
     self setupActions
 !
 
+warnCommonMistakes
+    "return true, if common beginners mistakes are to be warned about"
+
+    ^ WarnCommonMistakes
+!
+
+warnCommonMistakes:aBoolean
+    "this allows turning on/off warnings about common beginners mistakes.
+     Those are not really errors in the strict sense, but often lead to
+     run time errors later.
+     Examples are: expr or:expr2, where expr2 is not a block.
+     If you get bored by those warnings, turn them off by adding
+     a line as:
+	Compiler warnCommonMistakes:false
+     in your 'private.rc' file"
+
+    WarnCommonMistakes := aBoolean
+!
+
 warnOldStyleAssignment
     "return true, if underscore-assignment (pre ST-80v4 syntax) are to be warned about"
 
@@ -360,9 +382,9 @@
     "show an errormessage on the Transcript"
 
     ignoreErrors ifFalse:[
-        Smalltalk silentLoading == true ifFalse:[
-            Transcript showCR:(pos printString , ' [line: ' , tokenLineNr printString , '] ' , aMessage)
-        ]
+	Smalltalk silentLoading == true ifFalse:[
+	    Transcript showCR:(pos printString , ' [line: ' , tokenLineNr printString , '] ' , aMessage)
+	]
     ]
 
     "Modified: 18.5.1996 / 15:44:35 / cg"
@@ -388,6 +410,16 @@
     ^ false
 !
 
+warnCommonMistake:msg at:position
+    ignoreWarnings ifFalse:[
+	warnCommonMistakes ifTrue:[
+	    self 
+		warning:msg
+		position:position to:position.
+	]
+    ]
+!
+
 warnOldStyleAssignmentAt:position
     ignoreWarnings ifFalse:[
 	warnOldStyleAssignment ifTrue:[
@@ -531,8 +563,9 @@
     warnSTXSpecialComment := WarnSTXSpecials.
     warnUnderscoreInIdentifier := WarnUnderscoreInIdentifier.
     warnOldStyleAssignment := WarnOldStyleAssignment.
+    warnCommonMistakes := WarnCommonMistakes.
     ActionArray isNil ifTrue:[
-        self class setupActions
+	self class setupActions
     ]
 
     "Modified: 9.5.1996 / 12:48:47 / cg"
@@ -708,69 +741,69 @@
     |nextChar string firstChar|
 
     hereChar == $_ ifTrue:[
-        "/
-        "/ no need to check for AllowUnderscoreInIdentifier here;
-        "/ could not arrive here if it was off
-        "/
-        nextChar := source nextPeek.
-        (nextChar isAlphaNumeric or:[nextChar == $_]) ifFalse:[
-            "oops: a single underscore is an old-style assignement"
-            self warnOldStyleAssignmentAt:tokenPosition.
-            source next.
-            tokenType := $_.
-            ^ tokenType
-        ].
-        string := '_'.
-        self warnUnderscoreAt:tokenPosition.
-        [nextChar == $_] whileTrue:[
-            string := string copyWith:$_.
-            nextChar := source nextPeek.
-        ].
-        string := string , source nextAlphaNumericWord.
+	"/
+	"/ no need to check for AllowUnderscoreInIdentifier here;
+	"/ could not arrive here if it was off
+	"/
+	nextChar := source nextPeek.
+	(nextChar isAlphaNumeric or:[nextChar == $_]) ifFalse:[
+	    "oops: a single underscore is an old-style assignement"
+	    self warnOldStyleAssignmentAt:tokenPosition.
+	    source next.
+	    tokenType := $_.
+	    ^ tokenType
+	].
+	string := '_'.
+	self warnUnderscoreAt:tokenPosition.
+	[nextChar == $_] whileTrue:[
+	    string := string copyWith:$_.
+	    nextChar := source nextPeek.
+	].
+	string := string , source nextAlphaNumericWord.
     ] ifFalse:[
-        string := source nextAlphaNumericWord "self nextId".
+	string := source nextAlphaNumericWord "self nextId".
     ].
     nextChar := source peek.
 
     AllowUnderscoreInIdentifier ifTrue:[
-        nextChar == $_ ifTrue:[
-            self warnUnderscoreAt:(source position).
-        ].
-        [nextChar == $_] whileTrue:[
-            string := string copyWith:nextChar.
-            nextChar := source nextPeek.
-            (nextChar isAlphaNumeric) ifTrue:[
-                string := string , source nextAlphaNumericWord.
-                nextChar := source peek.
-            ]
-        ].
+	nextChar == $_ ifTrue:[
+	    self warnUnderscoreAt:(source position).
+	].
+	[nextChar == $_] whileTrue:[
+	    string := string copyWith:nextChar.
+	    nextChar := source nextPeek.
+	    (nextChar isAlphaNumeric) ifTrue:[
+		string := string , source nextAlphaNumericWord.
+		nextChar := source peek.
+	    ]
+	].
     ].
 
     (nextChar == $:) ifTrue:[
-        source next.
-        (source peek == $=) ifFalse:[
-            tokenName := string copyWith:nextChar.
-            tokenType := #Keyword.
-            ^ self
-        ].
-        peekChar := $:.
-        peekChar2 := $=.
+	source next.
+	(source peek == $=) ifFalse:[
+	    tokenName := string copyWith:nextChar.
+	    tokenType := #Keyword.
+	    ^ self
+	].
+	peekChar := $:.
+	peekChar2 := $=.
     ].
     tokenName := string.
     firstChar := string at:1.
     (firstChar == $s) ifTrue:[
-        (string = 'self') ifTrue:[tokenType := #Self. ^self].
-        (string = 'super') ifTrue:[tokenType := #Super. ^self]
+	(string = 'self') ifTrue:[tokenType := #Self. ^self].
+	(string = 'super') ifTrue:[tokenType := #Super. ^self]
     ].
     (firstChar == $n) ifTrue:[
-        (string = 'nil') ifTrue:[tokenType := #Nil. ^self]
+	(string = 'nil') ifTrue:[tokenType := #Nil. ^self]
     ].
     (firstChar == $t) ifTrue:[
-        (string = 'true') ifTrue:[tokenType := #True. ^self].
-        (string = 'thisContext') ifTrue:[tokenType := #ThisContext. ^self]
+	(string = 'true') ifTrue:[tokenType := #True. ^self].
+	(string = 'thisContext') ifTrue:[tokenType := #ThisContext. ^self]
     ].
     (firstChar == $f) ifTrue:[
-        (string = 'false') ifTrue:[tokenType := #False. ^self]
+	(string = 'false') ifTrue:[tokenType := #False. ^self]
     ].
     tokenType := #Identifier.
     ^ tokenType
@@ -909,30 +942,30 @@
     firstChar := source next.
     secondChar := source peek.
     ((firstChar == $-) and:[secondChar notNil]) ifTrue:[
-        secondChar isDigit ifTrue:[
-            self nextNumber.
-            tokenValue := tokenValue negated.
-            ^ tokenType
-        ]
+	secondChar isDigit ifTrue:[
+	    self nextNumber.
+	    tokenValue := tokenValue negated.
+	    ^ tokenType
+	]
     ].
     string := firstChar asString.
     secondChar notNil ifTrue:[
-        ((TypeArray at:(secondChar asciiValue)) == #special) ifTrue:[
-            (secondChar == $-) ifTrue:[
-                "special- look if minus belongs to number following"
-                p := source position.
-                source next.
-                thirdChar := source peek.
-                source position:p.
-                thirdChar isDigit ifTrue:[
-                    tokenName := string.
-                    tokenType := #BinaryOperator.
-                    ^ tokenType
-                ]
-            ].
-            source next.
-            string := string copyWith:secondChar
-        ].
+	((TypeArray at:(secondChar asciiValue)) == #special) ifTrue:[
+	    (secondChar == $-) ifTrue:[
+		"special- look if minus belongs to number following"
+		p := source position.
+		source next.
+		thirdChar := source peek.
+		source position:p.
+		thirdChar isDigit ifTrue:[
+		    tokenName := string.
+		    tokenType := #BinaryOperator.
+		    ^ tokenType
+		]
+	    ].
+	    source next.
+	    string := string copyWith:secondChar
+	].
     ].
     tokenName := string.
     tokenType := #BinaryOperator.
@@ -1147,6 +1180,6 @@
 !Scanner class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.46 1996-06-17 08:22:39 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.47 1996-07-18 08:13:34 cg Exp $'
 ! !
 Scanner initialize!