summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiles Sabin <miles@milessabin.com>2009-10-06 14:49:49 +0000
committerMiles Sabin <miles@milessabin.com>2009-10-06 14:49:49 +0000
commitf4dbe6bdc7f54e03da743ef7a239c96683c3aae1 (patch)
tree20716de38fe9ff76cdf8e2b283091532417cb789
parent2d5390fd9925c39f18110c1d74d8dbefd3b16f11 (diff)
downloadscala-f4dbe6bdc7f54e03da743ef7a239c96683c3aae1.tar.gz
scala-f4dbe6bdc7f54e03da743ef7a239c96683c3aae1.tar.bz2
scala-f4dbe6bdc7f54e03da743ef7a239c96683c3aae1.zip
Patch from Mirko Stoker to add comment nodes to...
Patch from Mirko Stoker to add comment nodes to compilation units allowing access to comment contents and positions for all tools which use the Scala AST.
-rw-r--r--src/compiler/scala/tools/nsc/CompilationUnits.scala8
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/Scanners.scala71
2 files changed, 51 insertions, 28 deletions
diff --git a/src/compiler/scala/tools/nsc/CompilationUnits.scala b/src/compiler/scala/tools/nsc/CompilationUnits.scala
index 6b60d12d77..bb25ab27f8 100644
--- a/src/compiler/scala/tools/nsc/CompilationUnits.scala
+++ b/src/compiler/scala/tools/nsc/CompilationUnits.scala
@@ -23,6 +23,14 @@ trait CompilationUnits { self: Global =>
/** the content of the compilation unit in tree form */
var body: Tree = EmptyTree
+ /** representation for a source code comment, includes
+ * '//' or '/*' '*/' in the value and the position
+ */
+ case class Comment(text: String, pos: Position)
+
+ /** all comments found in this compilation unit */
+ val comments = new ListBuffer[Comment]
+
/** Note: depends now contains toplevel classes.
* To get their sourcefiles, you need to dereference with .sourcefile
*/
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
index 01a7f31462..689c0d420e 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
@@ -117,6 +117,8 @@ trait Scanners {
if (docBuffer ne null) docBuffer.append(c)
}
+ protected def foundComment(value: String, start: Int, end: Int) = ()
+
private class TokenData0 extends TokenData
/** we need one token lookahead and one token history
@@ -393,39 +395,48 @@ trait Scanners {
}
private def skipComment(): Boolean = {
- if (ch == '/') {
- do {
- nextChar()
- } while ((ch != CR) && (ch != LF) && (ch != SU))
- true
- } else if (ch == '*') {
- docBuffer = null
- var openComments = 1
- nextChar()
- if (ch == '*' && buildDocs)
- docBuffer = new StringBuilder("/**")
- while (openComments > 0) {
+
+ if (ch == '/' || ch == '*') {
+
+ val comment = new StringBuilder("//")
+ def appendToComment() = comment.append(ch)
+
+ if (ch == '/') {
do {
+ appendToComment()
+ nextChar()
+ } while ((ch != CR) && (ch != LF) && (ch != SU))
+ } else {
+ docBuffer = null
+ var openComments = 1
+ nextChar()
+ appendToComment()
+ if (ch == '*' && buildDocs)
+ docBuffer = new StringBuilder("/**")
+ while (openComments > 0) {
do {
- if (ch == '/') {
- nextChar(); putDocChar(ch)
- if (ch == '*') {
- nextChar(); putDocChar(ch)
- openComments += 1
+ do {
+ if (ch == '/') {
+ nextChar(); putDocChar(ch); appendToComment()
+ if (ch == '*') {
+ nextChar(); putDocChar(ch); appendToComment()
+ openComments += 1
+ }
}
+ if (ch != '*' && ch != SU) {
+ nextChar(); putDocChar(ch); appendToComment()
+ }
+ } while (ch != '*' && ch != SU)
+ while (ch == '*') {
+ nextChar(); putDocChar(ch); appendToComment()
}
- if (ch != '*' && ch != SU) {
- nextChar(); putDocChar(ch)
- }
- } while (ch != '*' && ch != SU)
- while (ch == '*') {
- nextChar(); putDocChar(ch)
- }
- } while (ch != '/' && ch != SU)
- if (ch == '/') nextChar()
- else incompleteInputError("unclosed comment")
- openComments -= 1
+ } while (ch != '/' && ch != SU)
+ if (ch == '/') nextChar()
+ else incompleteInputError("unclosed comment")
+ openComments -= 1
+ }
}
+ foundComment(comment.toString, offset, charOffset - 2)
true
} else {
false
@@ -1040,6 +1051,10 @@ trait Scanners {
}
}
}
+
+ override def foundComment(value: String, start: Int, end: Int) {
+ unit.comments += unit.Comment(value, new RangePosition(unit.source, start, start, end))
+ }
}
class ParensAnalyzer(unit: CompilationUnit, patches: List[BracePatch]) extends UnitScanner(unit, patches) {