refactor: rename all general to generic in EnumsAnalyzer
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
*.interp
|
*.interp
|
||||||
*.tokens
|
*.tokens
|
||||||
|
|
||||||
CKGeneralLexer*.java
|
CKGenericLexer*.java
|
||||||
CKEnumsParser*.java
|
CKEnumsParser*.java
|
||||||
CKDefinesParser*.java
|
CKDefinesParser*.java
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
parser grammar CKDefinesParser;
|
parser grammar CKDefinesParser;
|
||||||
options { tokenVocab = CKGeneralLexer; }
|
options { tokenVocab = CKGenericLexer; }
|
||||||
|
|
||||||
prog: definePair+ ;
|
prog: definePair+ ;
|
||||||
|
|
||||||
definePair: CKGENERAL_DEFINE CKGENERAL_ID (CKGENERAL_NUM | CKGENERAL_ID) ;
|
definePair: CKGENERIC_DEFINE CKGENERIC_ID (CKGENERIC_NUM | CKGENERIC_ID) ;
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
parser grammar CKEnumsParser;
|
parser grammar CKEnumsParser;
|
||||||
options { tokenVocab = CKGeneralLexer; }
|
options { tokenVocab = CKGenericLexer; }
|
||||||
|
|
||||||
prog: enumBody* ;
|
prog: enumBody* ;
|
||||||
|
|
||||||
enumBody: CKGENERAL_TYPEDEF? CKGENERAL_ENUM CKGENERAL_ID CKGENERAL_LBRACKET
|
enumBody: CKGENERIC_TYPEDEF? CKGENERIC_ENUM CKGENERIC_ID CKGENERIC_LBRACKET
|
||||||
entryPair+
|
entryPair+
|
||||||
CKGENERAL_RBRACKET CKGENERAL_ID? CKGENERAL_SEMICOLON ;
|
CKGENERIC_RBRACKET CKGENERIC_ID? CKGENERIC_SEMICOLON ;
|
||||||
|
|
||||||
entryPair: CKGENERAL_ID (CKGENERAL_EQUAL entryValue)? CKGENERAL_COMMA? ;
|
entryPair: CKGENERIC_ID (CKGENERIC_EQUAL entryValue)? CKGENERIC_COMMA? ;
|
||||||
|
|
||||||
entryValue: CKGENERAL_NUM (CKGENERAL_LSHIFT CKGENERAL_NUM)? # entryDirectValue
|
entryValue: CKGENERIC_NUM (CKGENERIC_LSHIFT CKGENERIC_NUM)? # entryDirectValue
|
||||||
| CKGENERAL_ID (CKGENERAL_OR CKGENERAL_ID)* # entryRelativeValue
|
| CKGENERIC_ID (CKGENERIC_OR CKGENERIC_ID)* # entryRelativeValue
|
||||||
;
|
;
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
lexer grammar CKGeneralLexer;
|
|
||||||
|
|
||||||
channels { COMMENTS, WHITESPACE }
|
|
||||||
|
|
||||||
// keywords
|
|
||||||
CKGENERAL_TYPEDEF: 'typedef' ;
|
|
||||||
CKGENERAL_DEFINE: '#define' ;
|
|
||||||
CKGENERAL_ENUM: 'enum' ;
|
|
||||||
// symbols
|
|
||||||
CKGENERAL_LBRACKET: '{' ;
|
|
||||||
CKGENERAL_RBRACKET: '}' ;
|
|
||||||
CKGENERAL_EQUAL: '=';
|
|
||||||
CKGENERAL_SEMICOLON: ';' ;
|
|
||||||
CKGENERAL_LSHIFT: '<<' ;
|
|
||||||
CKGENERAL_OR: '|' ;
|
|
||||||
CKGENERAL_COMMA: ',' ;
|
|
||||||
|
|
||||||
// identifider and number
|
|
||||||
CKGENERAL_ID: [_a-zA-Z][_a-zA-Z0-9]* ;
|
|
||||||
CKGENERAL_NUM: (('0'[xX]) | '-')? [0-9a-fA-F]+ [uUlL]* ;
|
|
||||||
|
|
||||||
// comments
|
|
||||||
CKGENERAL_LINE_COMMENT: '//' ~[\r\n]* -> channel(COMMENTS);
|
|
||||||
CKGENERAL_BLOCK_COMMENT: '/*' .*? '*/' -> channel(COMMENTS);
|
|
||||||
// whitespace
|
|
||||||
CKGENERAL_WS: [ \t\r\n]+ -> channel(WHITESPACE);
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
lexer grammar CKGenericLexer;
|
||||||
|
|
||||||
|
channels { COMMENTS, WHITESPACE }
|
||||||
|
|
||||||
|
// keywords
|
||||||
|
CKGENERIC_TYPEDEF: 'typedef' ;
|
||||||
|
CKGENERIC_DEFINE: '#define' ;
|
||||||
|
CKGENERIC_ENUM: 'enum' ;
|
||||||
|
// symbols
|
||||||
|
CKGENERIC_LBRACKET: '{' ;
|
||||||
|
CKGENERIC_RBRACKET: '}' ;
|
||||||
|
CKGENERIC_EQUAL: '=';
|
||||||
|
CKGENERIC_SEMICOLON: ';' ;
|
||||||
|
CKGENERIC_LSHIFT: '<<' ;
|
||||||
|
CKGENERIC_OR: '|' ;
|
||||||
|
CKGENERIC_COMMA: ',' ;
|
||||||
|
|
||||||
|
// identifider and number
|
||||||
|
CKGENERIC_ID: [_a-zA-Z][_a-zA-Z0-9]* ;
|
||||||
|
CKGENERIC_NUM: (('0'[xX]) | '-')? [0-9a-fA-F]+ [uUlL]* ;
|
||||||
|
|
||||||
|
// comments
|
||||||
|
CKGENERIC_LINE_COMMENT: '//' ~[\r\n]* -> channel(COMMENTS);
|
||||||
|
CKGENERIC_BLOCK_COMMENT: '/*' .*? '*/' -> channel(COMMENTS);
|
||||||
|
// whitespace
|
||||||
|
CKGENERIC_WS: [ \t\r\n]+ -> channel(WHITESPACE);
|
||||||
@@ -23,7 +23,7 @@ public class ClassidWalker extends CKDefinesParserBaseListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int getClassidLevel(Token defineHead) {
|
private int getClassidLevel(Token defineHead) {
|
||||||
Token ws = CommonHelper.getPreChannelToken(mTokenStream, defineHead, CKGeneralLexer.WHITESPACE);
|
Token ws = CommonHelper.getPreChannelToken(mTokenStream, defineHead, CKGenericLexer.WHITESPACE);
|
||||||
if (ws == null)
|
if (ws == null)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@@ -87,8 +87,8 @@ public class ClassidWalker extends CKDefinesParserBaseListener {
|
|||||||
@Override
|
@Override
|
||||||
public void exitDefinePair(CKDefinesParser.DefinePairContext ctx) {
|
public void exitDefinePair(CKDefinesParser.DefinePairContext ctx) {
|
||||||
// fill entry info
|
// fill entry info
|
||||||
mCurrentEntry.mEntryName = ctx.CKGENERAL_ID(0).getText();
|
mCurrentEntry.mEntryName = ctx.CKGENERIC_ID(0).getText();
|
||||||
mCurrentEntry.mEntryValue = ctx.CKGENERAL_NUM().getText();
|
mCurrentEntry.mEntryValue = ctx.CKGENERIC_NUM().getText();
|
||||||
|
|
||||||
// fill entry level info
|
// fill entry level info
|
||||||
int this_level = getClassidLevel(ctx.getStart());
|
int this_level = getClassidLevel(ctx.getStart());
|
||||||
|
|||||||
@@ -22,13 +22,13 @@ public class CommentsFinder {
|
|||||||
// if we don't know where is our token,
|
// if we don't know where is our token,
|
||||||
// we should assume it is from precomment to postcomment
|
// we should assume it is from precomment to postcomment
|
||||||
// and check it.
|
// and check it.
|
||||||
List<Token> precomment = CommonHelper.getPreChannelTokens(mTokenStream, preToken, CKGeneralLexer.COMMENTS);
|
List<Token> precomment = CommonHelper.getPreChannelTokens(mTokenStream, preToken, CKGenericLexer.COMMENTS);
|
||||||
if (precomment != null) {
|
if (precomment != null) {
|
||||||
mCommentsPos = CommentsPosition.Precomment;
|
mCommentsPos = CommentsPosition.Precomment;
|
||||||
return CommonHelper.cutComments(precomment);
|
return CommonHelper.cutComments(precomment);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Token> postcomment = CommonHelper.getPostChannelTokens(mTokenStream, postToken, CKGeneralLexer.COMMENTS);
|
List<Token> postcomment = CommonHelper.getPostChannelTokens(mTokenStream, postToken, CKGenericLexer.COMMENTS);
|
||||||
if (postcomment != null) {
|
if (postcomment != null) {
|
||||||
mCommentsPos = CommentsPosition.Postcomment;
|
mCommentsPos = CommentsPosition.Postcomment;
|
||||||
return CommonHelper.cutComments(postcomment);
|
return CommonHelper.cutComments(postcomment);
|
||||||
@@ -38,10 +38,10 @@ public class CommentsFinder {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case Precomment: {
|
case Precomment: {
|
||||||
return CommonHelper.cutComments(CommonHelper.getPreChannelTokens(mTokenStream, preToken, CKGeneralLexer.COMMENTS));
|
return CommonHelper.cutComments(CommonHelper.getPreChannelTokens(mTokenStream, preToken, CKGenericLexer.COMMENTS));
|
||||||
}
|
}
|
||||||
case Postcomment: {
|
case Postcomment: {
|
||||||
return CommonHelper.cutComments(CommonHelper.getPostChannelTokens(mTokenStream, postToken, CKGeneralLexer.COMMENTS));
|
return CommonHelper.cutComments(CommonHelper.getPostChannelTokens(mTokenStream, postToken, CKGenericLexer.COMMENTS));
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public class CommonHelper {
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
switch (comment.getType()) {
|
switch (comment.getType()) {
|
||||||
case CKGeneralLexer.CKGENERAL_LINE_COMMENT: {
|
case CKGenericLexer.CKGENERIC_LINE_COMMENT: {
|
||||||
// For line comment, we start to remove "//" prefix first
|
// For line comment, we start to remove "//" prefix first
|
||||||
String slashRemoved = comment.getText().substring(2);
|
String slashRemoved = comment.getText().substring(2);
|
||||||
// Then remove successive starts
|
// Then remove successive starts
|
||||||
@@ -58,7 +58,7 @@ public class CommonHelper {
|
|||||||
// Okey
|
// Okey
|
||||||
return eolRemoved;
|
return eolRemoved;
|
||||||
}
|
}
|
||||||
case CKGeneralLexer.CKGENERAL_BLOCK_COMMENT: {
|
case CKGenericLexer.CKGENERIC_BLOCK_COMMENT: {
|
||||||
// For block comment, we first cut "/*" head and "*/" tail.
|
// For block comment, we first cut "/*" head and "*/" tail.
|
||||||
String blockComment = comment.getText();
|
String blockComment = comment.getText();
|
||||||
String slashRemoved = blockComment.substring(2, blockComment.length() - 4);
|
String slashRemoved = blockComment.substring(2, blockComment.length() - 4);
|
||||||
@@ -103,7 +103,7 @@ public class CommonHelper {
|
|||||||
// =========== Number Operations ===========
|
// =========== Number Operations ===========
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether Antlr captured CKGENERAL_NUM is a negative number.
|
* Check whether Antlr captured CKGENERIC_NUM is a negative number.
|
||||||
*
|
*
|
||||||
* @param numstr The captured number.
|
* @param numstr The captured number.
|
||||||
* @return true if it is negative number.
|
* @return true if it is negative number.
|
||||||
@@ -113,7 +113,7 @@ public class CommonHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether Altlr captured CKGENERAL_NUM is a hex number.
|
* Check whether Altlr captured CKGENERIC_NUM is a hex number.
|
||||||
*
|
*
|
||||||
* @param numstr The captured number.
|
* @param numstr The captured number.
|
||||||
* @return true if it is hex number.
|
* @return true if it is hex number.
|
||||||
|
|||||||
@@ -42,16 +42,16 @@ public class DefinesWalker extends CKDefinesParserBaseListener {
|
|||||||
@Override
|
@Override
|
||||||
public void exitDefinePair(CKDefinesParser.DefinePairContext ctx) {
|
public void exitDefinePair(CKDefinesParser.DefinePairContext ctx) {
|
||||||
// set values
|
// set values
|
||||||
mCurrentEntry.mEntryName = ctx.CKGENERAL_ID(0).getText();
|
mCurrentEntry.mEntryName = ctx.CKGENERIC_ID(0).getText();
|
||||||
mCurrentEntry.mEntryComment = mCommentsFinder.getComment(ctx.getStart(), ctx.getStop());
|
mCurrentEntry.mEntryComment = mCommentsFinder.getComment(ctx.getStart(), ctx.getStop());
|
||||||
|
|
||||||
if (ctx.CKGENERAL_NUM() == null) {
|
if (ctx.CKGENERIC_NUM() == null) {
|
||||||
// define with id
|
// define with id
|
||||||
mCurrentEntry.mEntryValue = ctx.CKGENERAL_ID(1).getText();
|
mCurrentEntry.mEntryValue = ctx.CKGENERIC_ID(1).getText();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// define with number
|
// define with number
|
||||||
String num = ctx.CKGENERAL_NUM().getText();
|
String num = ctx.CKGENERIC_NUM().getText();
|
||||||
mCurrentEntry.mEntryValue = num;
|
mCurrentEntry.mEntryValue = num;
|
||||||
|
|
||||||
// check whether this enum can be unsigned
|
// check whether this enum can be unsigned
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
|
|||||||
|
|
||||||
private String getEnumComment(Token enumHead) {
|
private String getEnumComment(Token enumHead) {
|
||||||
return CommonHelper
|
return CommonHelper
|
||||||
.cutComments(CommonHelper.getPreChannelTokens(mTokenStream, enumHead, CKGeneralLexer.COMMENTS));
|
.cutComments(CommonHelper.getPreChannelTokens(mTokenStream, enumHead, CKGenericLexer.COMMENTS));
|
||||||
}
|
}
|
||||||
|
|
||||||
private BufferedTokenStream mTokenStream;
|
private BufferedTokenStream mTokenStream;
|
||||||
@@ -56,7 +56,7 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
|
|||||||
// get enum comment
|
// get enum comment
|
||||||
mCurrentEnum.mEnumComment = getEnumComment(ctx.getStart());
|
mCurrentEnum.mEnumComment = getEnumComment(ctx.getStart());
|
||||||
// get the last name (for typedef case)
|
// get the last name (for typedef case)
|
||||||
List<TerminalNode> allNames = ctx.CKGENERAL_ID();
|
List<TerminalNode> allNames = ctx.CKGENERIC_ID();
|
||||||
mCurrentEnum.mEnumName = allNames.get(allNames.size() - 1).getText();
|
mCurrentEnum.mEnumName = allNames.get(allNames.size() - 1).getText();
|
||||||
|
|
||||||
mCurrentProg.mEnums.add(mCurrentEnum);
|
mCurrentProg.mEnums.add(mCurrentEnum);
|
||||||
@@ -73,7 +73,7 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
|
|||||||
// get entry comment
|
// get entry comment
|
||||||
mCurrentEntry.mEntryComment = mCommentsFinder.getComment(ctx.getStart(), ctx.getStop());
|
mCurrentEntry.mEntryComment = mCommentsFinder.getComment(ctx.getStart(), ctx.getStop());
|
||||||
// get entry name
|
// get entry name
|
||||||
mCurrentEntry.mEntryName = ctx.CKGENERAL_ID().getText();
|
mCurrentEntry.mEntryName = ctx.CKGENERIC_ID().getText();
|
||||||
|
|
||||||
mCurrentEnum.mEntries.add(mCurrentEntry);
|
mCurrentEnum.mEntries.add(mCurrentEntry);
|
||||||
mCurrentEntry = null;
|
mCurrentEntry = null;
|
||||||
@@ -82,7 +82,7 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
|
|||||||
@Override
|
@Override
|
||||||
public void exitEntryDirectValue(CKEnumsParser.EntryDirectValueContext ctx) {
|
public void exitEntryDirectValue(CKEnumsParser.EntryDirectValueContext ctx) {
|
||||||
// get all numbers
|
// get all numbers
|
||||||
List<TerminalNode> nums = ctx.CKGENERAL_NUM();
|
List<TerminalNode> nums = ctx.CKGENERIC_NUM();
|
||||||
|
|
||||||
switch (nums.size()) {
|
switch (nums.size()) {
|
||||||
case 1: {
|
case 1: {
|
||||||
@@ -120,7 +120,7 @@ public class EnumsWalker extends CKEnumsParserBaseListener {
|
|||||||
@Override
|
@Override
|
||||||
public void exitEntryRelativeValue(CKEnumsParser.EntryRelativeValueContext ctx) {
|
public void exitEntryRelativeValue(CKEnumsParser.EntryRelativeValueContext ctx) {
|
||||||
// get all identifiers and join them
|
// get all identifiers and join them
|
||||||
mCurrentEntry.mEntryValue = ctx.CKGENERAL_ID().stream().map(value -> value.getText())
|
mCurrentEntry.mEntryValue = ctx.CKGENERIC_ID().stream().map(value -> value.getText())
|
||||||
.collect(Collectors.joining(" | "));
|
.collect(Collectors.joining(" | "));
|
||||||
|
|
||||||
// | operator appears. this enum must have flags feature
|
// | operator appears. this enum must have flags feature
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class MainRunner {
|
|||||||
private static EnumsHelper.BEnumCollection getEnumsCollection(String filename) throws Exception {
|
private static EnumsHelper.BEnumCollection getEnumsCollection(String filename) throws Exception {
|
||||||
String infile = CommonHelper.getInputFilePath(filename);
|
String infile = CommonHelper.getInputFilePath(filename);
|
||||||
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
|
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
|
||||||
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
|
CKGenericLexer lexer = new CKGenericLexer(pair.mAntlrStream);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
CKEnumsParser parser = new CKEnumsParser(tokens);
|
CKEnumsParser parser = new CKEnumsParser(tokens);
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ public class MainRunner {
|
|||||||
private static EnumsHelper.BEnum organiseDefines(String filename, String assignedEnumName) throws Exception {
|
private static EnumsHelper.BEnum organiseDefines(String filename, String assignedEnumName) throws Exception {
|
||||||
String infile = CommonHelper.getInputFilePath(filename);
|
String infile = CommonHelper.getInputFilePath(filename);
|
||||||
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
|
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
|
||||||
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
|
CKGenericLexer lexer = new CKGenericLexer(pair.mAntlrStream);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
CKDefinesParser parser = new CKDefinesParser(tokens);
|
CKDefinesParser parser = new CKDefinesParser(tokens);
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ public class MainRunner {
|
|||||||
private static EnumsHelper.BEnum organiseClassid(String filename) throws Exception {
|
private static EnumsHelper.BEnum organiseClassid(String filename) throws Exception {
|
||||||
String infile = CommonHelper.getInputFilePath(filename);
|
String infile = CommonHelper.getInputFilePath(filename);
|
||||||
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
|
CommonHelper.InputFilePair pair = CommonHelper.openInputFile(infile);
|
||||||
CKGeneralLexer lexer = new CKGeneralLexer(pair.mAntlrStream);
|
CKGenericLexer lexer = new CKGenericLexer(pair.mAntlrStream);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
CKDefinesParser parser = new CKDefinesParser(tokens);
|
CKDefinesParser parser = new CKDefinesParser(tokens);
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ And export them as JSON file which will be utilized by Enums Render.
|
|||||||
Enter `EnumsAnalyzer` directory, and execute following command to generate Antlr lexer and parser:
|
Enter `EnumsAnalyzer` directory, and execute following command to generate Antlr lexer and parser:
|
||||||
|
|
||||||
```
|
```
|
||||||
antlr4 CKGeneralLexer.g4
|
antlr4 CKGenericLexer.g4
|
||||||
antlr4 CKEnumsParser.g4
|
antlr4 CKEnumsParser.g4
|
||||||
antlr4 CKDefinesParser.g4
|
antlr4 CKDefinesParser.g4
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user