From 90b4108f4586d850a63f40ce2624527a7a270b97 Mon Sep 17 00:00:00 2001 From: cremet Date: Wed, 30 Jul 2003 08:27:43 +0000 Subject: Added: - collecting of documentation comments. - options for scaladoc - a class to load a documentation module. --- sources/scalac/ast/parser/Parser.java | 54 ++++++++++++++++++++++++++++------ sources/scalac/ast/parser/Scanner.java | 31 ++++++++++++++----- 2 files changed, 68 insertions(+), 17 deletions(-) (limited to 'sources/scalac/ast/parser') diff --git a/sources/scalac/ast/parser/Parser.java b/sources/scalac/ast/parser/Parser.java index d5b3522367..00670ab8fc 100644 --- a/sources/scalac/ast/parser/Parser.java +++ b/sources/scalac/ast/parser/Parser.java @@ -40,6 +40,7 @@ public class Parser implements Tokens { s = new Scanner(unit); make = unit.global.make; pN = new PatternNormalizer( unit ); + mapTreeComment = unit.global.mapTreeComment; } /** this is the general parse method @@ -161,6 +162,38 @@ public class Parser implements Tokens { } } +/////// COMMENT COLLECTION /////////////////////////////////////////////////// + + /** keep the comments associated with a given tree + */ + protected Map mapTreeComment; + + /** stack of comments + */ + protected final Stack commentStack = new Stack(); + + /** positive if we are inside a block + */ + protected int local = 0; + + /** push last encountered comment and reset the buffer + */ + protected void pushComment() { + if (local == 0) { + commentStack.push(s.docBuffer == null ? null : s.docBuffer.toString()); + s.docBuffer = null; + } + } + + /** pop a comment from the stack and associate it with the given tree + */ + protected Tree popComment(Tree tree) { + if (local == 0) + if (!commentStack.empty()) + mapTreeComment.put(tree, (String) commentStack.pop()); + return tree; + } + /////// TREE CONSTRUCTION //////////////////////////////////////////////////// /** Name supply @@ -995,6 +1028,7 @@ public class Parser implements Tokens { * | `{' Block `}' */ Tree blockExpr() { + local++; Tree res; int pos = accept(LBRACE); if (s.token == CASE) { @@ -1008,6 +1042,7 @@ public class Parser implements Tokens { res = block(pos); } accept(RBRACE); + local--; return res; } @@ -1265,6 +1300,7 @@ public class Parser implements Tokens { * | abstract */ int modifiers() { + pushComment(); int mods = 0; while (true) { int mod; @@ -1551,25 +1587,25 @@ public class Parser implements Tokens { case VAL: do { s.nextToken(); - ts.append(patDefOrDcl(mods)); + ts.append(popComment(patDefOrDcl(mods))); } while (s.token == COMMA); return ts.toArray(); case VAR: do { s.nextToken(); - ts.append(varDefOrDcl(mods)); + ts.append(popComment(varDefOrDcl(mods))); } while (s.token == COMMA); return ts.toArray(); case DEF: do { s.nextToken(); - ts.append(funDefOrDcl(mods)); + ts.append(popComment(funDefOrDcl(mods))); } while (s.token == COMMA); return ts.toArray(); case TYPE: do { s.nextToken(); - ts.append(typeDefOrDcl(mods)); + ts.append(popComment(typeDefOrDcl(mods))); } while (s.token == COMMA); return ts.toArray(); default: @@ -1725,11 +1761,11 @@ public class Parser implements Tokens { ValDef[][] params = paramClauseOpt(); TreeList result = new TreeList(); result.append( - make.ClassDef(pos, mods, clazzname, tparams, params, - simpleTypedOpt(), classTemplate())); + popComment(make.ClassDef(pos, mods, clazzname, tparams, params, + simpleTypedOpt(), classTemplate()))); while (s.token == CONSTR) { s.nextToken(); - result.append(constrDef(mods, clazzname, tparams)); + result.append(popComment(constrDef(mods, clazzname, tparams))); } return result.toArray(); } @@ -1737,8 +1773,8 @@ public class Parser implements Tokens { /** ObjectDef ::= Id [`:' SimpleType] ClassTemplate */ Tree objectDef(int mods) { - return make.ModuleDef( - s.pos, mods, ident(), simpleTypedOpt(), classTemplate()); + return popComment(make.ModuleDef( + s.pos, mods, ident(), simpleTypedOpt(), classTemplate())); } /** ClassTemplate ::= [`extends' Constr] {`with' Constr} [TemplateBody] diff --git a/sources/scalac/ast/parser/Scanner.java b/sources/scalac/ast/parser/Scanner.java index bc153c869f..c747fc1bcc 100644 --- a/sources/scalac/ast/parser/Scanner.java +++ b/sources/scalac/ast/parser/Scanner.java @@ -23,6 +23,16 @@ import scalac.util.Name; */ public class Scanner extends TokenData { + /** buffer for the documentation comment + */ + protected StringBuffer docBuffer = null; + + /** add the given character to the documentation buffer + */ + protected void addCharToDoc(byte ch) { + if (docBuffer != null) docBuffer.append((char) ch); + } + /** layout & character constants */ public int tabinc = 8; @@ -342,38 +352,43 @@ public class Scanner extends TokenData { } while ((ch != CR) && (ch != LF) && (ch != SU)); return true; } else if (ch == '*') { + docBuffer = null; int openComments = 1; + nextch(); + if (ch == '*') { + docBuffer = new StringBuffer("/**"); + } while (openComments > 0) { do { do { if (ch == CR) { cline++; ccol = 0; - nextch(); + nextch(); addCharToDoc(ch); if (ch == LF) { ccol = 0; - nextch(); + nextch(); addCharToDoc(ch); } } else if (ch == LF) { cline++; ccol = 0; - nextch(); + nextch(); addCharToDoc(ch); } else if (ch == '\t') { ccol = ((ccol - 1) / tabinc * tabinc) + tabinc; - nextch(); + nextch(); addCharToDoc(ch); } else if (ch == '/') { - nextch(); + nextch(); addCharToDoc(ch); if (ch == '*') { - nextch(); + nextch(); addCharToDoc(ch); openComments++; } } else { - nextch(); + nextch(); addCharToDoc(ch); } } while ((ch != '*') && (ch != SU)); while (ch == '*') { - nextch(); + nextch(); addCharToDoc(ch); } } while (ch != '/' && ch != SU); if (ch == '/') { -- cgit v1.2.3