libcmo21/CodeGen/CommentsFinder.java

52 lines
1.5 KiB
Java
Raw Normal View History

2023-08-20 12:13:40 +08:00
import java.util.List;
2023-08-18 15:55:31 +08:00
import org.antlr.v4.runtime.*;
2023-08-20 12:13:40 +08:00
public class CommentsFinder {
2023-08-18 15:55:31 +08:00
enum CommentsPosition {
Unknown, Precomment, Postcomment
}
2023-08-20 12:13:40 +08:00
public CommentsFinder(BufferedTokenStream tokenStream) {
2023-08-18 15:55:31 +08:00
mTokenStream = tokenStream;
mCommentsPos = CommentsPosition.Unknown;
}
private BufferedTokenStream mTokenStream;
private CommentsPosition mCommentsPos;
public String getComment(Token preToken, Token postToken) {
switch (mCommentsPos) {
case Unknown: {
// if we don't know where is our token,
// we should assume it is from precomment to postcomment
// and check it.
2023-08-20 12:13:40 +08:00
List<Token> precomment = CommonHelper.getPreChannelTokens(mTokenStream, preToken, CKGeneralLexer.COMMENTS);
2023-08-18 15:55:31 +08:00
if (precomment != null) {
mCommentsPos = CommentsPosition.Precomment;
2023-08-20 12:13:40 +08:00
return CommonHelper.cutComments(precomment);
2023-08-18 15:55:31 +08:00
}
2023-08-20 12:13:40 +08:00
List<Token> postcomment = CommonHelper.getPostChannelTokens(mTokenStream, postToken, CKGeneralLexer.COMMENTS);
2023-08-18 15:55:31 +08:00
if (postcomment != null) {
mCommentsPos = CommentsPosition.Postcomment;
2023-08-20 12:13:40 +08:00
return CommonHelper.cutComments(postcomment);
2023-08-18 15:55:31 +08:00
}
// really do not have comment, return empty and keep comment pos
return null;
}
case Precomment: {
2023-08-20 12:13:40 +08:00
return CommonHelper.cutComments(CommonHelper.getPreChannelTokens(mTokenStream, preToken, CKGeneralLexer.COMMENTS));
2023-08-18 15:55:31 +08:00
}
case Postcomment: {
2023-08-20 12:13:40 +08:00
return CommonHelper.cutComments(CommonHelper.getPostChannelTokens(mTokenStream, postToken, CKGeneralLexer.COMMENTS));
2023-08-18 15:55:31 +08:00
}
default:
return null;
}
}
}