Commit 1f0bd967 authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Experimental lexer/parser support for doc comments (issue #442)


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 482ccb63
......@@ -382,13 +382,24 @@ tokens {
}
ONENEWLINE: [ \t\f]* '\r'? '\n' [ \t\f]* -> channel(HIDDEN);
WS: [ \t\r\n\f]+ -> channel(HIDDEN);
LINE_DOCUMENT: '//*' ~[\r\n]* -> channel(HIDDEN);
LINE_COMMENT: '//' ~[\r\n]*
{
detectTasks(getText(), false);
} -> channel(HIDDEN);
BLOCK_DOCUMENT: '/**' .*? '*/'
{
intervalDetector.pushInterval(_tokenStartCharIndex, _tokenStartLine, interval_type.MULTILINE_COMMENT);
intervalDetector.popInterval(_input.index(), _interp.getLine());
detectTasks(getText(), true);
} -> channel(HIDDEN);
BLOCK_COMMENT: '/*' .*? '*/'
{
intervalDetector.pushInterval(_tokenStartCharIndex, _tokenStartLine, interval_type.MULTILINE_COMMENT);
......
......@@ -278,6 +278,60 @@ public void reset() {
}
}
/**
* Gets the documentation comment for the current context, or null if no doc comment found
* The comment should end in the previous line
*
* Both single line and multiline doc comments are supported
*/
public String getDocCommentText() {
BufferedTokenStream stream = (BufferedTokenStream)_input;
Token start = _ctx.getStart();
List<Token> t = null;
if (start != null) {
int pos = start.getTokenIndex();
try {
t = stream.getHiddenTokensToLeft(pos, 1);
} catch (Exception e) {
return null;
}
if (t == null) {
return null;
}
int len = t.size();
if (len < 2) {
return null;
}
Token prev = t.get(len - 1);
if (prev.getType() != ONENEWLINE) {
return null;
}
prev = t.get(len - 2);
if (prev.getType() == BLOCK_DOCUMENT) {
return prev.getText();
}
StringBuilder builder = new StringBuilder();
boolean stop = false;
for (int i = len - 2; !stop && i >= 0; i--) {
prev = t.get(i);
switch (prev.getType()) {
case ONENEWLINE:
case WS:
continue;
case LINE_DOCUMENT:
builder.insert(0, prev.getText() + "\n");
break;
default:
stop = true;
}
}
return builder.length() > 0 ? builder.toString() : null;
}
return null;
}
/**
* Gets the last visible token of the current rule.
*
......@@ -467,6 +521,8 @@ pr_TTCN3Module
ControlPart controlpart = null;
MultipleWithAttributes attributes = null;
List<String> languageSpecifications = null;
String docComment = getDocCommentText();
}:
( m = pr_TTCN3ModuleKeyword { col = $m.start; }
i = pr_TTCN3ModuleId { act_ttcn3_module = new TTCN3Module( $i.identifier, project ); }
......@@ -488,6 +544,9 @@ pr_TTCN3Module
act_ttcn3_module.addControlpart( controlpart );
controlpart.setAttributeParentPath( act_ttcn3_module.getAttributePath() );
}
// if (docComment != null) {
// // act_ttcn3_module.setDocumentationComment(docComment);
// }
};
pr_TTCN3ModuleKeyword returns[String stringValue]:
......@@ -2793,6 +2852,8 @@ pr_FunctionDef returns[Def_Function def_func]
boolean isDeterministic = false;
boolean isControl = false;
Location modifierLocation;
String docComment = getDocCommentText();
}:
( col = pr_FunctionKeyword
fm = pr_FunctionModifier {
......@@ -2833,6 +2894,9 @@ pr_FunctionDef returns[Def_Function def_func]
templateRestriction, exceptions, statementBlock, finallyBlock, isDeterministic, isControl, modifierLocation);
$def_func.setLocation(getLocation( $col.start, $s.stop));
$def_func.setCommentLocation( getLastCommentLocation( $start ) );
if (docComment != null) {
// $def_func.setDocumentationComment(docComment);
}
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment