Skip to content

Commit

Permalink
#19: Grammar refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kshchepanovskyi committed Jun 5, 2016
1 parent ab93360 commit fc223ac
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ SEMICOLON
ASSIGN
: '='
;
NAME
IDENT
: (ALPHA | UNDERSCORE) (ALPHA | DIGIT | UNDERSCORE)*
;
STRING_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ packageStatement
: PACKAGE packageName SEMICOLON
;
packageName
: name (DOT name)*
: fullIdent
;
importStatement
: IMPORT PUBLIC? STRING_VALUE SEMICOLON
Expand All @@ -32,10 +32,16 @@ optionEntry
: OPTION option SEMICOLON
;
enumBlock
: ENUM name LCURLY (enumConstant | optionEntry)* RCURLY SEMICOLON?
: ENUM enumName LCURLY (enumField | optionEntry)* RCURLY SEMICOLON?
;
enumConstant
: name ASSIGN INTEGER_VALUE fieldOptions? SEMICOLON
enumName
: ident
;
enumField
: enumFieldName ASSIGN INTEGER_VALUE fieldOptions? SEMICOLON
;
enumFieldName
: ident
;
extendBlock
: EXTEND typeReference LCURLY extendBlockEntry* RCURLY SEMICOLON?
Expand All @@ -45,17 +51,23 @@ extendBlockEntry
| groupBlock
;
serviceBlock
: SERVICE name LCURLY (rpcMethod | optionEntry)* RCURLY SEMICOLON?
: SERVICE serviceName LCURLY (rpcMethod | optionEntry)* RCURLY SEMICOLON?
;
serviceName
: ident
;
rpcMethod
: RPC name LPAREN rpcType RPAREN
: RPC rpcName LPAREN rpcType RPAREN
RETURNS LPAREN rpcType RPAREN (LCURLY optionEntry* RCURLY)? SEMICOLON?
;
rpcName
: ident
;
rpcType
: STREAM? typeReference
;
messageBlock
: MESSAGE name LCURLY
: MESSAGE messageName LCURLY
(field
| optionEntry
| messageBlock
Expand All @@ -68,14 +80,20 @@ messageBlock
| reserved)*
RCURLY SEMICOLON?
;
messageName
: ident
;
oneof
: ONEOF name LCURLY (oneofField | oneofGroup)* RCURLY SEMICOLON?
: ONEOF oneofName LCURLY (oneofField | oneofGroup)* RCURLY SEMICOLON?
;
oneofName
: ident
;
oneofField
: typeReference name ASSIGN INTEGER_VALUE fieldOptions? SEMICOLON
: typeReference fieldName ASSIGN INTEGER_VALUE fieldOptions? SEMICOLON
;
oneofGroup
: GROUP name ASSIGN INTEGER_VALUE LCURLY
: GROUP fieldName ASSIGN INTEGER_VALUE LCURLY
(field
| optionEntry
| messageBlock
Expand All @@ -86,7 +104,7 @@ oneofGroup
RCURLY SEMICOLON?
;
map
: MAP LT mapKey COMMA mapValue GT name ASSIGN tag fieldOptions? SEMICOLON
: MAP LT mapKey COMMA mapValue GT fieldName ASSIGN tag fieldOptions? SEMICOLON
;
mapKey
: INT32
Expand All @@ -109,7 +127,7 @@ tag
: INTEGER_VALUE
;
groupBlock
: fieldModifier GROUP name ASSIGN INTEGER_VALUE LCURLY
: fieldModifier GROUP groupName ASSIGN INTEGER_VALUE LCURLY
(field
| optionEntry
| messageBlock
Expand All @@ -119,7 +137,9 @@ groupBlock
| groupBlock)*
RCURLY SEMICOLON?
;

groupName
: ident
;
extensions
: EXTENSIONS ranges SEMICOLON
;
Expand All @@ -133,13 +153,16 @@ reserved
: RESERVED (ranges | fieldNames) SEMICOLON
;
fieldNames
: fieldName (COMMA fieldName)*
: fieldNameString (COMMA fieldNameString)*
;
fieldName
fieldNameString
: STRING_VALUE
;
field
: fieldModifier? typeReference name ASSIGN INTEGER_VALUE fieldOptions? SEMICOLON
: fieldModifier? typeReference fieldName ASSIGN INTEGER_VALUE fieldOptions? SEMICOLON
;
fieldName
: ident
;
fieldModifier
: OPTIONAL
Expand All @@ -162,7 +185,7 @@ typeReference
| BOOL
| STRING
| BYTES
| DOT? name (DOT name)*
| DOT? ident (DOT ident)*
;
fieldOptions
: LSQUARE (option (COMMA option)* )? RSQUARE
Expand All @@ -171,22 +194,22 @@ option
: optionName ASSIGN optionValue
;
optionName
: name (DOT name)*
: ident (DOT ident)*
| LPAREN typeReference RPAREN (DOT optionName)*
;
optionValue
: INTEGER_VALUE
| FLOAT_VALUE
| BOOLEAN_VALUE
| STRING_VALUE
| NAME
| IDENT
| textFormat
;
textFormat
: LCURLY textFormatEntry* RCURLY
;
textFormatOptionName
: name
: ident
| LSQUARE typeReference RSQUARE
;
textFormatEntry
Expand All @@ -198,10 +221,13 @@ textFormatOptionValue
| FLOAT_VALUE
| BOOLEAN_VALUE
| STRING_VALUE
| NAME
| IDENT
;
fullIdent
: ident (DOT ident)*
;
name
: NAME
ident
: IDENT
| PACKAGE
| SYNTAX
| IMPORT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ public void enterEnumBlock(ProtoParser.EnumBlockContext ctx) {
public void exitEnumBlock(ProtoParser.EnumBlockContext ctx) {
Enum e = context.pop(Enum.class);
EnumContainer container = context.peek(EnumContainer.class);
String name = ctx.name().getText();
String name = ctx.enumName().getText();
e.setName(name);
e.setSourceCodeLocation(getSourceCodeLocation(ctx));
container.addEnum(e);
attachComments(ctx, e, false);
}

@Override
public void enterEnumConstant(ProtoParser.EnumConstantContext ctx) {
public void enterEnumField(ProtoParser.EnumFieldContext ctx) {
Enum parent = context.peek(Enum.class);
EnumConstant enumConstant = new EnumConstant(parent);
context.push(enumConstant);
}

@Override
public void exitEnumConstant(ProtoParser.EnumConstantContext ctx) {
public void exitEnumField(ProtoParser.EnumFieldContext ctx) {
EnumConstant enumConstant = context.pop(EnumConstant.class);
Enum e = context.peek(Enum.class);
String name = ctx.name().getText();
String name = ctx.enumFieldName().getText();
int number = Integer.decode(ctx.INTEGER_VALUE().getText());
enumConstant.setName(name);
enumConstant.setValue(number);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void enterMessageBlock(ProtoParser.MessageBlockContext ctx) {
public void exitMessageBlock(ProtoParser.MessageBlockContext ctx) {
Message message = context.pop(Message.class);
MessageContainer container = context.peek(MessageContainer.class);
String name = ctx.name().getText();
String name = ctx.messageName().getText();
message.setName(name);
message.setSourceCodeLocation(getSourceCodeLocation(ctx));
container.addMessage(message);
Expand All @@ -56,7 +56,7 @@ public void exitReserved(ProtoParser.ReservedContext ctx) {
}
ProtoParser.FieldNamesContext fieldNamesContext = ctx.fieldNames();
if (fieldNamesContext != null) {
for (ProtoParser.FieldNameContext fieldNameContext : fieldNamesContext.fieldName()) {
for (ProtoParser.FieldNameStringContext fieldNameContext : fieldNamesContext.fieldNameString()) {
String fieldName = fieldNameContext.getText();
fieldName = Util.removeFirstAndLastChar(fieldName);
message.addReservedFieldName(fieldName);
Expand All @@ -75,7 +75,7 @@ public void enterField(ProtoParser.FieldContext ctx) {
public void exitField(ProtoParser.FieldContext ctx) {
Field field = context.pop(Field.class);
FieldContainer fieldContainer = context.peek(FieldContainer.class);
String name = ctx.name().getText();
String name = ctx.fieldName().getText();
String type = ctx.typeReference().getText();
Integer tag = Integer.decode(ctx.INTEGER_VALUE().getText());
updateModifier(ctx.fieldModifier(), field);
Expand Down Expand Up @@ -123,7 +123,7 @@ public void enterGroupBlock(ProtoParser.GroupBlockContext ctx) {
@Override
public void exitGroupBlock(ProtoParser.GroupBlockContext ctx) {
Group group = context.pop(Group.class);
group.setName(ctx.name().getText());
group.setName(ctx.groupName().getText());
group.setSourceCodeLocation(getSourceCodeLocation(ctx));
GroupContainer groupContainer = context.peek(GroupContainer.class);
FieldContainer fieldContainer = context.peek(FieldContainer.class);
Expand Down Expand Up @@ -151,7 +151,7 @@ public void enterOneof(ProtoParser.OneofContext ctx) {
public void exitOneof(ProtoParser.OneofContext ctx) {
Oneof oneof = context.pop(Oneof.class);
Message message = context.peek(Message.class);
oneof.setName(ctx.name().getText());
oneof.setName(ctx.oneofName().getText());
oneof.setSourceCodeLocation(getSourceCodeLocation(ctx));
message.addOneof(oneof);
attachComments(ctx, oneof, false);
Expand All @@ -171,7 +171,7 @@ public void exitOneofField(ProtoParser.OneofFieldContext ctx) {
Field field = context.pop(Field.class);
Oneof oneOf = context.peek(Oneof.class);
Message message = oneOf.getParent();
String name = ctx.name().getText();
String name = ctx.fieldName().getText();
String type = ctx.typeReference().getText();
Integer tag = Integer.decode(ctx.INTEGER_VALUE().getText());
field.setName(name);
Expand Down Expand Up @@ -210,7 +210,7 @@ public void enterMap(ProtoParser.MapContext ctx) {
public void exitMap(ProtoParser.MapContext ctx) {
Field field = context.pop(Field.class);
Message message = context.peek(Message.class);
String name = ctx.name().getText();
String name = ctx.fieldName().getText();
String keyTypeName = ctx.mapKey().getText();
String valueTypeName = ctx.mapValue().getText();
SourceCodeLocation codeLocation = getSourceCodeLocation(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ private DynamicMessage.Value getOptionValue(ProtoParser.OptionValueContext optio
String text = optionValueContext.STRING_VALUE().getText();
// TODO: unescape
optionValue = DynamicMessage.Value.createString(sourceCodeLocation, Util.removeFirstAndLastChar(text));
} else if (optionValueContext.NAME() != null) {
String text = optionValueContext.NAME().getText();
} else if (optionValueContext.IDENT() != null) {
String text = optionValueContext.IDENT().getText();
optionValue = DynamicMessage.Value.createEnum(sourceCodeLocation, text);
} else if (optionValueContext.FLOAT_VALUE() != null) {
String text = optionValueContext.FLOAT_VALUE().getText();
Expand Down Expand Up @@ -119,7 +119,7 @@ public void exitTextFormat(ProtoParser.TextFormatContext ctx) {
@Override
public void exitTextFormatEntry(ProtoParser.TextFormatEntryContext ctx) {
String optionName;
if (ctx.textFormatOptionName().name() != null) {
if (ctx.textFormatOptionName().ident() != null) {
// standard option key
optionName = ctx.textFormatOptionName().getText();
} else {
Expand All @@ -146,8 +146,8 @@ private DynamicMessage.Value getTextFormatOptionValue(ProtoParser.TextFormatEntr
String text = ctx.textFormatOptionValue().STRING_VALUE().getText();
// TODO: unescape
optionValue = DynamicMessage.Value.createString(sourceCodeLocation, Util.removeFirstAndLastChar(text));
} else if (ctx.textFormatOptionValue().NAME() != null) {
String text = ctx.textFormatOptionValue().NAME().getText();
} else if (ctx.textFormatOptionValue().IDENT() != null) {
String text = ctx.textFormatOptionValue().IDENT().getText();
optionValue = DynamicMessage.Value.createEnum(sourceCodeLocation, text);
} else if (ctx.textFormatOptionValue().FLOAT_VALUE() != null) {
String text = ctx.textFormatOptionValue().FLOAT_VALUE().getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void enterServiceBlock(ProtoParser.ServiceBlockContext ctx) {
public void exitServiceBlock(ProtoParser.ServiceBlockContext ctx) {
Service service = context.pop(Service.class);
Proto proto = context.peek(Proto.class);
String name = ctx.name().getText();
String name = ctx.serviceName().getText();
service.setName(name);
service.setSourceCodeLocation(getSourceCodeLocation(ctx));
proto.addService(service);
Expand All @@ -45,7 +45,7 @@ public void enterRpcMethod(ProtoParser.RpcMethodContext ctx) {
public void exitRpcMethod(ProtoParser.RpcMethodContext ctx) {
ServiceMethod method = context.pop(ServiceMethod.class);
Service service = context.peek(Service.class);
String name = ctx.name().getText();
String name = ctx.rpcName().getText();
String arg = ctx.rpcType(0).typeReference().getText();
boolean argStream = ctx.rpcType(0).STREAM() != null;
String ret = ctx.rpcType(1).typeReference().getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ public void messageNamedMessage() {
String input = "message message{}";
ProtoParser parser = createParser(input);
ProtoParser.MessageBlockContext context = parser.messageBlock();
Assert.assertEquals("message", context.name().getText());
Assert.assertEquals("message", context.messageName().getText());
}

@Test
public void enumNamedEnum() {
String input = "enum enum{}";
ProtoParser parser = createParser(input);
ProtoParser.EnumBlockContext context = parser.enumBlock();
Assert.assertEquals("enum", context.name().getText());
Assert.assertEquals("enum", context.enumName().getText());
}

@Test
public void enumConstantNamedEnum() {
String input = "enum = 1;";
ProtoParser parser = createParser(input);
ProtoParser.EnumConstantContext context = parser.enumConstant();
Assert.assertEquals("enum", context.name().getText());
ProtoParser.EnumFieldContext context = parser.enumField();
Assert.assertEquals("enum", context.enumFieldName().getText());
}

@Test
public void fieldNamedMessage() {
String input = "optional int32 message = 1;";
ProtoParser parser = createParser(input);
ProtoParser.FieldContext context = parser.field();
Assert.assertEquals("message", context.name().getText());
Assert.assertEquals("message", context.fieldName().getText());
}

private ProtoParser createParser(String input) {
Expand Down

0 comments on commit fc223ac

Please sign in to comment.