2023-08-20 12:13:40 +08:00
|
|
|
|
|
|
|
import org.antlr.v4.runtime.*;
|
|
|
|
import org.antlr.v4.runtime.tree.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The generic walker for collecting defines as a enum.
|
|
|
|
*/
|
|
|
|
public class DefinesWalker extends CKDefinesParserBaseListener {
|
2023-08-20 16:10:22 +08:00
|
|
|
public DefinesWalker(BufferedTokenStream tokenStream) {
|
|
|
|
mCommentsFinder = new CommentsFinder(tokenStream);
|
|
|
|
mResult = null;
|
2023-08-20 12:13:40 +08:00
|
|
|
|
2023-08-20 16:10:22 +08:00
|
|
|
mCurrentEnum = null;
|
|
|
|
mCurrentEntry = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public EnumsHelper.Enum_t getEnum() {
|
|
|
|
return mResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
private CommentsFinder mCommentsFinder;
|
|
|
|
private EnumsHelper.Enum_t mResult;
|
|
|
|
|
|
|
|
private EnumsHelper.Enum_t mCurrentEnum;
|
|
|
|
private EnumsHelper.EnumEntry_t mCurrentEntry;
|
|
|
|
|
2023-08-20 12:13:40 +08:00
|
|
|
@Override
|
|
|
|
public void enterProg(CKDefinesParser.ProgContext ctx) {
|
2023-08-20 16:10:22 +08:00
|
|
|
mCurrentEnum = new EnumsHelper.Enum_t();
|
2023-08-20 12:13:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exitProg(CKDefinesParser.ProgContext ctx) {
|
2023-08-20 16:10:22 +08:00
|
|
|
mResult = mCurrentEnum;
|
|
|
|
mCurrentEnum = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void enterDefinePair(CKDefinesParser.DefinePairContext ctx) {
|
|
|
|
mCurrentEntry = new EnumsHelper.EnumEntry_t();
|
2023-08-20 12:13:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exitDefinePair(CKDefinesParser.DefinePairContext ctx) {
|
2023-08-20 16:10:22 +08:00
|
|
|
// set values
|
|
|
|
mCurrentEntry.mEntryName = ctx.CKGENERAL_ID(0).getText();
|
|
|
|
mCurrentEntry.mEntryComment = mCommentsFinder.getComment(ctx.getStart(), ctx.getStop());
|
|
|
|
|
|
|
|
if (ctx.CKGENERAL_NUM() == null) {
|
|
|
|
// define with id
|
|
|
|
mCurrentEntry.mEntryValue = ctx.CKGENERAL_ID(1).getText();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// define with number
|
|
|
|
String num = ctx.CKGENERAL_NUM().getText();
|
|
|
|
mCurrentEntry.mEntryValue = num;
|
|
|
|
|
|
|
|
// check whether this enum can be unsigned
|
|
|
|
if (CommonHelper.isNegativeNumber(num)) {
|
|
|
|
mCurrentEnum.mCanUnsigned = false;
|
|
|
|
}
|
|
|
|
// if the number is in hex form, this enum MIGHT have flags feature
|
|
|
|
if (CommonHelper.isHexNumber(num)) {
|
|
|
|
mCurrentEnum.mUseFlags = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// add entry
|
|
|
|
mCurrentEnum.mEntries.add(mCurrentEntry);
|
|
|
|
mCurrentEntry = null;
|
2023-08-20 12:13:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|